Skip to content

Commit

Permalink
added client for blockstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaputra07 committed Nov 8, 2024
1 parent 6ac0398 commit 518aa4f
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 111 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Progress:

- [x] Locations
- [x] Object storage
- [ ] Block storage
- [x] Block storage
- [ ] Floating IP
- [ ] Load balancer
- [ ] Managed services
- [ ] Virtual machine
- [ ] Virtual Private Cloud (VPC)


> Disclaimer: This library is created solely based on information that are available on IDCloudHost API documentation website https://api.idcloudhost.com. I/We (the authors) ONLY maintains/fixing-bugs/responsible for issues that are in the scope of this library, any platform issues should be addressed directly to the IDCloudHost official support.
> Disclaimer: This library is created solely based on information that are available on IDCloudHost API documentation website https://api.idcloudhost.com. The author(s) ONLY maintains/fixing-bugs/responsible for issues that are in the scope of this library, any platform issues should be addressed directly to the IDCloudHost official support.
97 changes: 97 additions & 0 deletions blockstorage/blockstorage.go
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
package blockstorage

import (
"context"
"fmt"
"net/url"
"strconv"

"github.com/ekaputra07/idcloudhost-go/http"
"github.com/gorilla/schema"
)

func NewClient() *Client {
return &Client{
H: http.DefaultClient,
}
}

// ListDisks https://api.idcloudhost.com/#list-disks
func (c *Client) LisDisks(ctx context.Context) *http.ClientResponse {
rc := http.RequestConfig{
Method: "GET",
Path: "/v1/storage/disks",
}
return c.H.FormRequest(ctx, rc)
}

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

rc := http.RequestConfig{
Method: "POST",
Path: "/v1/storage/disks",
Data: d,
}
return c.H.FormRequest(ctx, rc)
}

// GetDisk https://api.idcloudhost.com/#get-disk
func (c *Client) GetDisk(ctx context.Context, diskId string) *http.ClientResponse {
rc := http.RequestConfig{
Method: "GET",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskId),
}
return c.H.FormRequest(ctx, rc)
}

// DeleteDisk https://api.idcloudhost.com/#delete-disk
func (c *Client) DeleteDisk(ctx context.Context, diskId string) *http.ClientResponse {
rc := http.RequestConfig{
Method: "DELETE",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskId),
}
return c.H.FormRequest(ctx, rc)
}

// AttachDiskToVM https://api.idcloudhost.com/#attach-disk
func (c *Client) AttachDiskToVM(ctx context.Context, diskId, vmId string) *http.ClientResponse {
d := url.Values{
"uuid": []string{vmId},
"storage_uuid": []string{diskId},
}
rc := http.RequestConfig{
Method: "POST",
Path: "/v1/user-resource/vm/storage/attach",
Data: d,
}
return c.H.FormRequest(ctx, rc)
}

// DetachDiskFromVM https://api.idcloudhost.com/#detach-disk
func (c *Client) DetachDiskFromVM(ctx context.Context, diskId, vmId string) *http.ClientResponse {
d := url.Values{
"uuid": []string{vmId},
"storage_uuid": []string{diskId},
}
rc := http.RequestConfig{
Method: "POST",
Path: "/v1/user-resource/vm/storage/detach",
Data: d,
}
return c.H.FormRequest(ctx, rc)
}

// UpdateDiskBillingAccount https://api.idcloudhost.com/#modify-disk-info
func (c *Client) UpdateDiskBillingAccount(ctx context.Context, diskId string, billingAccountId int) *http.ClientResponse {
rc := http.RequestConfig{
Method: "PATCH",
Path: fmt.Sprintf("/v1/storage/disks/%s", diskId),
Data: url.Values{"billing_account_id": []string{strconv.Itoa(billingAccountId)}},
}
return c.H.FormRequest(ctx, rc)
}
114 changes: 114 additions & 0 deletions blockstorage/blockstorage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package blockstorage

import (
"context"
"net/http"
"strconv"
"testing"

h "github.com/ekaputra07/idcloudhost-go/http"
"github.com/stretchr/testify/assert"
)

func TestListDisks(t *testing.T) {
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
assert.Equal(t, "/v1/storage/disks", r.RequestURI)
})
defer s.Close()

bs := Client{H: c}
bs.LisDisks(context.Background())
}

func TestCreateDisk(t *testing.T) {
config := CreateDiskConfig{
SizeGb: 10,
BillingAccountId: 123,
SourceImageType: ImageTypeOSBase,
SourceImage: "ubuntu_20.04",
}
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/v1/storage/disks", r.RequestURI)

_ = 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, string(ImageTypeOSBase), r.Form.Get("source_image_type"))
assert.Equal(t, config.SourceImage, r.Form.Get("source_image"))
})
defer s.Close()

bs := Client{H: c}
bs.CreateDisk(context.Background(), config)
}

func TestGetDisk(t *testing.T) {
id := "testId"
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
assert.Equal(t, "/v1/storage/disks/testId", r.RequestURI)
})
defer s.Close()

bs := Client{H: c}
bs.GetDisk(context.Background(), id)
}

func TestDeleteDisk(t *testing.T) {
id := "testId"
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "DELETE", r.Method)
assert.Equal(t, "/v1/storage/disks/testId", r.RequestURI)
})
defer s.Close()

bs := Client{H: c}
bs.DeleteDisk(context.Background(), id)
}

func TestAttachDiskToVM(t *testing.T) {
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/v1/user-resource/vm/storage/attach", r.RequestURI)

_ = r.ParseForm()
assert.Equal(t, "vmId", r.Form.Get("uuid"))
assert.Equal(t, "diskId", r.Form.Get("storage_uuid"))
})
defer s.Close()

bs := Client{H: c}
bs.AttachDiskToVM(context.Background(), "diskId", "vmId")
}

func TestDetachDiskFromVM(t *testing.T) {
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/v1/user-resource/vm/storage/detach", r.RequestURI)

_ = r.ParseForm()
assert.Equal(t, "vmId", r.Form.Get("uuid"))
assert.Equal(t, "diskId", r.Form.Get("storage_uuid"))
})
defer s.Close()

bs := Client{H: c}
bs.DetachDiskFromVM(context.Background(), "diskId", "vmId")
}

func TestUpdateBucketBillingAccount(t *testing.T) {
c, s := h.MockClientServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "PATCH", r.Method)
assert.Equal(t, "/v1/storage/disks/testId", r.RequestURI)

_ = r.ParseForm()
assert.Equal(t, "123", r.Form.Get("billing_account_id"))
})
defer s.Close()

bs := Client{H: c}
bs.UpdateDiskBillingAccount(context.Background(), "testId", 123)
}
25 changes: 25 additions & 0 deletions blockstorage/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package blockstorage

import (
"github.com/ekaputra07/idcloudhost-go/http"
)

type Client struct {
H *http.Client
}

type SourceImageType string

const (
ImageTypeOSBase SourceImageType = "OS_BASE"
ImageTypeDisk SourceImageType = "DISK"
ImageTypeSnapshot SourceImageType = "SNAPSHOT"
ImageTypeEmpty SourceImageType = "EMPTY"
)

type CreateDiskConfig struct {
SizeGb int `schema:"size_gb"`
BillingAccountId int `schema:"billing_account_id"`
SourceImageType SourceImageType `schema:"source_image_type,default:EMPTY"`
SourceImage string `schema:"source_image"`
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/ekaputra07/idcloudhost-go

go 1.20

require github.com/stretchr/testify v1.9.0
require (
github.com/gorilla/schema v1.4.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E=
github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
20 changes: 10 additions & 10 deletions location/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"github.com/ekaputra07/idcloudhost-go/http"
)

type LocationClient struct {
func NewClient() *Client {
return &Client{
H: http.DefaultClient,
}
}

type Client struct {
H *http.Client
}

func (c *LocationClient) ListLocations(ctx context.Context) *http.ClientResponse {
cfg := http.RequestConfig{
func (c *Client) ListLocations(ctx context.Context) *http.ClientResponse {
rc := http.RequestConfig{
Method: "GET",
Path: "/v1/config/locations",
}
return c.H.FormRequest(ctx, cfg)
}

func NewClient() *LocationClient {
return &LocationClient{
H: http.DefaultClient,
}
return c.H.FormRequest(ctx, rc)
}
2 changes: 1 addition & 1 deletion location/location_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func TestListLocations(t *testing.T) {
})
defer s.Close()

lc := LocationClient{H: c}
lc := Client{H: c}
lc.ListLocations(context.Background())
}
Loading

0 comments on commit 518aa4f

Please sign in to comment.