Skip to content

Commit

Permalink
Add disk service.
Browse files Browse the repository at this point in the history
  • Loading branch information
beornf committed Sep 4, 2024
1 parent 43fa057 commit a0a2081
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Client struct {
App AppService
Attachment AttachmentService
Container ContainerService
Disk DiskService
Domain DomainService
Env EnvService
Log LogService
Expand Down Expand Up @@ -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}
Expand Down
60 changes: 60 additions & 0 deletions disk.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
23 changes: 13 additions & 10 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a0a2081

Please sign in to comment.