-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ac0398
commit 518aa4f
Showing
10 changed files
with
342 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.