Skip to content

Commit

Permalink
updated return values for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaputra07 committed Nov 28, 2024
1 parent 5e13553 commit 448465d
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 57 deletions.
53 changes: 37 additions & 16 deletions blockstorage/blockstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockstorage

import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
Expand All @@ -18,50 +19,70 @@ func NewClient(client *api.API) *Client {
}

// ListDisks https://api.warren.io/#list-disks
func (c *Client) LisDisks(ctx context.Context) *api.ClientResponse {
func (c *Client) LisDisks(ctx context.Context) (*[]Disk, error) {
rc := api.RequestConfig{
Method: "GET",
Path: "/v1/storage/disks",
}
return c.API.FormRequest(ctx, rc)
resp := c.API.FormRequest(ctx, rc)
if resp.Error != nil {
return nil, resp.Error
}
var disks []Disk
if err := json.Unmarshal(resp.Body, &disks); err != nil {
return nil, err
}
return &disks, nil
}

// CreateDisk https://api.warren.io/#create-disk
func (c *Client) CreateDisk(ctx context.Context, cfg CreateDiskConfig) *api.ClientResponse {
func (c *Client) CreateDisk(ctx context.Context, disk *Disk) error {
enc := schema.NewEncoder()
d := url.Values{}
if err := enc.Encode(cfg, d); err != nil {
return &api.ClientResponse{Error: err}
if err := enc.Encode(disk, d); err != nil {
return err
}

rc := api.RequestConfig{
Method: "POST",
Path: "/v1/storage/disks",
Data: d,
}
return c.API.FormRequest(ctx, rc)
resp := c.API.FormRequest(ctx, rc)
if resp.Error != nil {
return resp.Error
}
return json.Unmarshal(resp.Body, disk)
}

// GetDisk https://api.warren.io/#get-disk
func (c *Client) GetDisk(ctx context.Context, diskID uuid.UUID) *api.ClientResponse {
func (c *Client) GetDisk(ctx context.Context, diskID uuid.UUID) (*Disk, error) {
rc := api.RequestConfig{
Method: "GET",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskID),
}
return c.API.FormRequest(ctx, rc)
resp := c.API.FormRequest(ctx, rc)
if resp.Error != nil {
return nil, resp.Error
}
var disk Disk
if err := json.Unmarshal(resp.Body, &disk); err != nil {
return nil, err
}
return &disk, nil
}

// DeleteDisk https://api.warren.io/#delete-disk
func (c *Client) DeleteDisk(ctx context.Context, diskID uuid.UUID) *api.ClientResponse {
func (c *Client) DeleteDisk(ctx context.Context, diskID uuid.UUID) error {
rc := api.RequestConfig{
Method: "DELETE",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskID),
}
return c.API.FormRequest(ctx, rc)
return c.API.FormRequest(ctx, rc).Error
}

// AttachDiskToVM https://api.warren.io/#attach-disk
func (c *Client) AttachDiskToVM(ctx context.Context, diskID, vmID uuid.UUID) *api.ClientResponse {
func (c *Client) AttachDiskToVM(ctx context.Context, diskID, vmID uuid.UUID) error {
d := url.Values{
"uuid": []string{vmID.String()},
"storage_uuid": []string{diskID.String()},
Expand All @@ -71,11 +92,11 @@ func (c *Client) AttachDiskToVM(ctx context.Context, diskID, vmID uuid.UUID) *ap
Path: "/v1/user-resource/vm/storage/attach",
Data: d,
}
return c.API.FormRequest(ctx, rc)
return c.API.FormRequest(ctx, rc).Error
}

// DetachDiskFromVM https://api.warren.io/#detach-disk
func (c *Client) DetachDiskFromVM(ctx context.Context, diskID, vmID uuid.UUID) *api.ClientResponse {
func (c *Client) DetachDiskFromVM(ctx context.Context, diskID, vmID uuid.UUID) error {
d := url.Values{
"uuid": []string{vmID.String()},
"storage_uuid": []string{diskID.String()},
Expand All @@ -85,15 +106,15 @@ func (c *Client) DetachDiskFromVM(ctx context.Context, diskID, vmID uuid.UUID) *
Path: "/v1/user-resource/vm/storage/detach",
Data: d,
}
return c.API.FormRequest(ctx, rc)
return c.API.FormRequest(ctx, rc).Error
}

// UpdateDiskBillingAccount https://api.warren.io/#modify-disk-info
func (c *Client) UpdateDiskBillingAccount(ctx context.Context, diskID uuid.UUID, billingAccountID int) *api.ClientResponse {
func (c *Client) UpdateDiskBillingAccount(ctx context.Context, diskID uuid.UUID, billingAccountID int) error {
rc := api.RequestConfig{
Method: "PATCH",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskID),
Data: url.Values{"billing_account_id": []string{strconv.Itoa(billingAccountID)}},
}
return c.API.FormRequest(ctx, rc)
return c.API.FormRequest(ctx, rc).Error
}
10 changes: 5 additions & 5 deletions blockstorage/blockstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestListDisks(t *testing.T) {
}

func TestCreateDisk(t *testing.T) {
config := CreateDiskConfig{
disk := Disk{
SizeGB: 10,
BillingAccountID: 123,
SourceImageType: ImageTypeOSBase,
Expand All @@ -36,15 +36,15 @@ func TestCreateDisk(t *testing.T) {

_ = r.ParseForm()

assert.Equal(t, strconv.Itoa(config.SizeGB), r.Form.Get("size_gb"))
assert.Equal(t, strconv.Itoa(config.BillingAccountID), r.Form.Get("billing_account_id"))
assert.Equal(t, strconv.Itoa(disk.SizeGB), r.Form.Get("size_gb"))
assert.Equal(t, strconv.Itoa(disk.BillingAccountID), r.Form.Get("billing_account_id"))
assert.Equal(t, string(ImageTypeOSBase), r.Form.Get("source_image_type"))
assert.Equal(t, config.SourceImage, r.Form.Get("source_image"))
assert.Equal(t, disk.SourceImage, r.Form.Get("source_image"))
})
defer s.Close()

bs := Client{API: a}
bs.CreateDisk(context.Background(), config)
bs.CreateDisk(context.Background(), &disk)
}

func TestGetDisk(t *testing.T) {
Expand Down
24 changes: 20 additions & 4 deletions blockstorage/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package blockstorage

import "github.com/ekaputra07/warren-go/api"
import (
"github.com/ekaputra07/warren-go/api"
"github.com/google/uuid"
)

type Client struct {
API *api.API
Expand All @@ -15,9 +18,22 @@ const (
ImageTypeEmpty SourceImageType = "EMPTY"
)

type CreateDiskConfig struct {
SizeGB int `schema:"size_gb"`
type Snapshot struct {
UUID uuid.UUID `schema:"uuid"`
SizeGB int `schema:"sizeGb"`
CreatedAt string `schema:"created_at"`
DiskUUID uuid.UUID `schema:"disk_uuid"`
}

type Disk struct {
UUID uuid.UUID `schema:"uuid"`
Status string `schema:"status"`
Snapshots []Snapshot `schema:"snapshots"`
UserID int `schema:"user_id"`
BillingAccountID int `schema:"billing_account_id"`
SourceImageType SourceImageType `schema:"source_image_type,default:EMPTY"`
SizeGB int `schema:"size_gb"`
SourceImageType SourceImageType `schema:"source_image_type"`
SourceImage string `schema:"source_image"`
CreatedAt string `schema:"created_at"`
UpdatedAt string `schema:"updated_at"`
}
26 changes: 24 additions & 2 deletions location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ package location

import (
"context"
"encoding/json"

"github.com/ekaputra07/warren-go/api"
)

// Location represents data center location
type Location struct {
DisplayName string `json:"display_name"`
IsDefault bool `json:"is_default"`
IsPreferred bool `json:"is_preferred"`
Description string `json:"description"`
OrderNr int `json:"order_nr"`
Slug string `json:"slug"`
CountryCode string `json:"country_code"`
}

func NewClient(client *api.API) *Client {
return &Client{
API: client,
Expand All @@ -16,10 +28,20 @@ type Client struct {
API *api.API
}

func (c *Client) ListLocations(ctx context.Context) *api.ClientResponse {
// ListLocations https://api.warren.io/#list-locations
func (c *Client) ListLocations(ctx context.Context) (*[]Location, error) {
rc := api.RequestConfig{
Method: "GET",
Path: "/v1/config/locations",
}
return c.API.FormRequest(ctx, rc)
resp := c.API.FormRequest(ctx, rc)
if resp.Error != nil {
return nil, resp.Error
}

var locations []Location
if err := json.Unmarshal(resp.Body, &locations); err != nil {
return nil, err
}
return &locations, nil
}
Loading

0 comments on commit 448465d

Please sign in to comment.