Skip to content

Commit

Permalink
add block storage attach & detach
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesClonk committed Nov 26, 2016
1 parent ae5bdc3 commit 664403f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
2 changes: 2 additions & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (c *CLI) RegisterCommands() {
cmd.Command("create", "create new block storage", blockStorageCreate)
cmd.Command("resize", "resize existing block storage", blockStorageResize)
cmd.Command("label", "rename existing block storage", blockStorageLabel)
cmd.Command("attach", "attach block storage to virtual machine", blockStorageAttach)
cmd.Command("detach", "detach block storage from virtual machine", blockStorageDetach)
cmd.Command("delete", "remove block storage", blockStorageDelete)
cmd.Command("list", "list all block storage", blockStorageList)
})
Expand Down
24 changes: 24 additions & 0 deletions cmd/commands_block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ func blockStorageLabel(cmd *cli.Cmd) {
}
}

func blockStorageAttach(cmd *cli.Cmd) {
cmd.Spec = "SUBID ATTACH_TO_SUBID"

id := cmd.StringArg("SUBID", "", "SUBID of block storage to attach (see <storage list>)")
serverId := cmd.StringArg("ATTACH_TO_SUBID", "", "SUBID of virtual machine to attach to (see <servers>)")

cmd.Action = func() {
if err := GetClient().AttachBlockStorage(*id, *serverId); err != nil {
log.Fatal(err)
}
fmt.Println("Block storage attached")
}
}

func blockStorageDetach(cmd *cli.Cmd) {
id := cmd.StringArg("SUBID", "", "SUBID of block storage to detach (see <storage list>)")
cmd.Action = func() {
if err := GetClient().DetachBlockStorage(*id); err != nil {
log.Fatal(err)
}
fmt.Println("Block storage detached")
}
}

func blockStorageDelete(cmd *cli.Cmd) {
id := cmd.StringArg("SUBID", "", "SUBID of block storage to delete (see <storage list>)")
cmd.Action = func() {
Expand Down
9 changes: 6 additions & 3 deletions cmd/commands_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ func regionList(cmd *cli.Cmd) {
return
}

lengths := []int{8, 48, 24, 8, 8}
tabsPrint(Columns{"DCID", "NAME", "CONTINENT", "COUNTRY", "STATE"}, lengths)
lengths := []int{8, 48, 24, 8, 8, 8, 8}
tabsPrint(Columns{"DCID", "NAME", "CONTINENT", "COUNTRY", "STATE", "STORAGE", "CODE"}, lengths)
for _, region := range regions {
tabsPrint(Columns{region.ID, region.Name, region.Continent, region.Country, region.State}, lengths)
tabsPrint(Columns{
region.ID, region.Name, region.Continent,
region.Country, region.State, region.BlockStorage, region.Code,
}, lengths)
}
tabsFlush()
}
Expand Down
49 changes: 39 additions & 10 deletions lib/block_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ func (b *BlockStorage) UnmarshalJSON(data []byte) (err error) {
}

value := fmt.Sprintf("%v", fields["SUBID"])
if len(value) == 0 || value == "<nil>" {
value = "0"
}
id, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
if len(value) == 0 || value == "<nil>" || value == "0" {
b.ID = ""
} else {
id, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
b.ID = strconv.FormatFloat(id, 'f', -1, 64)
}
b.ID = strconv.FormatFloat(id, 'f', -1, 64)

value = fmt.Sprintf("%v", fields["DCID"])
if len(value) == 0 || value == "<nil>" {
Expand All @@ -63,10 +64,15 @@ func (b *BlockStorage) UnmarshalJSON(data []byte) (err error) {
b.SizeGB = int(size)

value = fmt.Sprintf("%v", fields["attached_to_SUBID"])
if value == "<nil>" {
value = ""
if len(value) == 0 || value == "<nil>" || value == "0" {
b.AttachedTo = ""
} else {
attached, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
}
b.AttachedTo = strconv.FormatFloat(attached, 'f', -1, 64)
}
b.AttachedTo = value

b.Name = fmt.Sprintf("%v", fields["label"])
b.Created = fmt.Sprintf("%v", fields["date_created"])
Expand Down Expand Up @@ -139,6 +145,29 @@ func (c *Client) LabelBlockStorage(id, name string) error {
return nil
}

func (c *Client) AttachBlockStorage(id, serverId string) error {
values := url.Values{
"SUBID": {id},
"attach_to_SUBID": {serverId},
}

if err := c.post(`block/attach`, values, nil); err != nil {
return err
}
return nil
}

func (c *Client) DetachBlockStorage(id string) error {
values := url.Values{
"SUBID": {id},
}

if err := c.post(`block/detach`, values, nil); err != nil {
return err
}
return nil
}

func (c *Client) DeleteBlockStorage(id string) error {
values := url.Values{
"SUBID": {id},
Expand Down
14 changes: 8 additions & 6 deletions lib/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package lib

// Region on Vultr
type Region struct {
ID int `json:"DCID,string"`
Name string `json:"name"`
Country string `json:"country"`
Continent string `json:"continent"`
State string `json:"state"`
Ddos bool `json:"ddos_protection"`
ID int `json:"DCID,string"`
Name string `json:"name"`
Country string `json:"country"`
Continent string `json:"continent"`
State string `json:"state"`
Ddos bool `json:"ddos_protection"`
BlockStorage bool `json:"block_storage"`
Code string `json:"regioncode"`
}

func (c *Client) GetRegions() ([]Region, error) {
Expand Down
11 changes: 8 additions & 3 deletions lib/regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func Test_Regions_GetRegions_NoRegions(t *testing.T) {

func Test_Regions_GetRegions_OK(t *testing.T) {
server, client := getTestServerAndClient(http.StatusOK, `{
"5":{"DCID":"5","name":"Los Angeles","country":"US","continent":"North America","state":"CA","ddos_protection":true},
"9":{"DCID":"9","name":"Frankfurt","country":"DE","continent":"Europe","state":""},
"19":{"DCID":"19","name":"Australia","country":"AU","continent":"Australia","state":"","ddos_protection":false}}`)
"5":{"DCID":"5","name":"Los Angeles","country":"US","continent":"North America","state":"CA","ddos_protection":true,"regioncode":"LAX"},
"9":{"DCID":"9","name":"Frankfurt","country":"DE","continent":"Europe","state":"","block_storage":false},
"19":{"DCID":"19","name":"Australia","country":"AU","continent":"Australia","state":"","ddos_protection":false,"block_storage":true}}`)
defer server.Close()

regions, err := client.GetRegions()
Expand All @@ -50,16 +50,21 @@ func Test_Regions_GetRegions_OK(t *testing.T) {
assert.Equal(t, "US", region.Country)
assert.Equal(t, "CA", region.State)
assert.Equal(t, true, region.Ddos)
assert.Equal(t, false, region.BlockStorage)
assert.Equal(t, "LAX", region.Code)
case 9:
assert.Equal(t, "Frankfurt", region.Name)
assert.Equal(t, "DE", region.Country)
assert.Equal(t, "Europe", region.Continent)
assert.Equal(t, false, region.Ddos)
assert.Equal(t, false, region.BlockStorage)
assert.Equal(t, "", region.Code)
case 19:
assert.Equal(t, "AU", region.Country)
assert.Equal(t, "", region.State)
assert.Equal(t, "Australia", region.Continent)
assert.Equal(t, false, region.Ddos)
assert.Equal(t, true, region.BlockStorage)
default:
t.Error("Unknown DCID")
}
Expand Down

0 comments on commit 664403f

Please sign in to comment.