From a0a20814d542f157614fe3caf495905c7d871d29 Mon Sep 17 00:00:00 2001 From: Beorn Facchini Date: Wed, 4 Sep 2024 17:05:01 +1000 Subject: [PATCH] Add disk service. --- client.go | 2 ++ disk.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ model.go | 9 +++++++++ plan.go | 23 +++++++++++---------- 4 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 disk.go diff --git a/client.go b/client.go index bc33e95..f59c44d 100644 --- a/client.go +++ b/client.go @@ -14,6 +14,7 @@ type Client struct { App AppService Attachment AttachmentService Container ContainerService + Disk DiskService Domain DomainService Env EnvService Log LogService @@ -56,6 +57,7 @@ func NewClient(httpClient *http.Client) *Client { c.App = &AppClient{client: c} c.Attachment = &AttachmentClient{client: c} c.Container = &ContainerClient{client: c} + c.Disk = &DiskClient{client: c} c.Domain = &DomainClient{client: c} c.Env = &EnvClient{client: c} c.Log = &LogClient{client: c} diff --git a/disk.go b/disk.go new file mode 100644 index 0000000..8beb5f2 --- /dev/null +++ b/disk.go @@ -0,0 +1,60 @@ +package lade + +import "fmt" + +var _ DiskService = new(DiskClient) + +type DiskClient struct { + client *Client +} + +type DiskCreateOpts struct { + Name string `json:"name"` + PlanID string `json:"plan_id"` + Path string `json:"path"` +} + +type DiskUpdateOpts struct { + PlanID string `json:"plan_id"` +} + +type DiskService interface { + Create(appID string, opts *DiskCreateOpts) (*Disk, error) + Delete(disk *Disk) error + Get(appID, diskID string) (*Disk, error) + Head(appID, diskID string) error + List(appID string) ([]*Disk, error) + Update(appID, diskID string, opts *DiskUpdateOpts) (*Disk, error) +} + +func (d *DiskClient) Create(appID string, opts *DiskCreateOpts) (disk *Disk, err error) { + disk = new(Disk) + err = d.client.doCreate("apps/"+appID+"/disks", opts, disk) + return +} + +func (d *DiskClient) Delete(disk *Disk) error { + path := fmt.Sprintf("apps/%d/disks/%d", disk.AppID, disk.ID) + return d.client.doDelete(path, nil) +} + +func (d *DiskClient) Get(appID, diskID string) (disk *Disk, err error) { + disk = new(Disk) + err = d.client.doByID("apps/"+appID+"/disks", diskID, nil, disk) + return +} + +func (d *DiskClient) Head(appID, diskID string) error { + return d.client.doByID("apps/"+appID+"/disks", diskID, nil, nil) +} + +func (d *DiskClient) List(appID string) (disks []*Disk, err error) { + err = d.client.doList("apps/"+appID+"/disks", nil, &disks) + return +} + +func (d *DiskClient) Update(appID, diskID string, opts *DiskUpdateOpts) (disk *Disk, err error) { + disk = new(Disk) + err = d.client.doUpdate("apps/"+appID+"/disks/"+diskID, opts, disk) + return +} diff --git a/model.go b/model.go index 87b6743..588390a 100644 --- a/model.go +++ b/model.go @@ -63,6 +63,15 @@ type Container struct { CreatedAt time.Time `json:"created_at"` } +type Disk struct { + ID int `json:"id"` + AppID int `json:"app_id"` + PlanID string `json:"plan_id"` + Name string `json:"name"` + Path string `json:"path"` + CreatedAt time.Time `json:"created_at"` +} + type Domain struct { ID int `json:"id"` AppID int `json:"app_id"` diff --git a/plan.go b/plan.go index 10b2131..fdd8ff4 100644 --- a/plan.go +++ b/plan.go @@ -7,27 +7,30 @@ type PlanClient struct { } type PlanOpts struct { - ID string `qstring:"id,omitempty"` + ID string `qstring:"id,omitempty"` + Type string `qstring:"type,omitempty"` } type PlanService interface { - Default() (*Plan, error) - List() ([]*Plan, error) - User(id string) ([]*Plan, error) + Default(ptype string) (*Plan, error) + List(ptype string) ([]*Plan, error) + User(id, ptype string) ([]*Plan, error) } -func (p *PlanClient) Default() (plan *Plan, err error) { - err = p.client.doGet("plans/default", nil, &plan) +func (p *PlanClient) Default(ptype string) (plan *Plan, err error) { + opts := &PlanOpts{Type: ptype} + err = p.client.doGet("plans/default", opts, &plan) return } -func (p *PlanClient) List() (plans []*Plan, err error) { - err = p.client.doList("plans", nil, &plans) +func (p *PlanClient) List(ptype string) (plans []*Plan, err error) { + opts := &PlanOpts{Type: ptype} + err = p.client.doList("plans", opts, &plans) return } -func (p *PlanClient) User(id string) (plans []*Plan, err error) { - opts := &PlanOpts{ID: id} +func (p *PlanClient) User(id, ptype string) (plans []*Plan, err error) { + opts := &PlanOpts{ID: id, Type: ptype} err = p.client.doList("plans/user", opts, &plans) return }