Skip to content

Commit

Permalink
Commands to empty storage pools and heads
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Manavopoulos committed Mar 27, 2017
1 parent 49babef commit c29094c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/bytemark/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"github.com/BytemarkHosting/bytemark-client/util/log"
"github.com/urfave/cli"
)

func init() {
adminCommands = append(adminCommands, cli.Command{
Name: "empty",
Action: cli.ShowSubcommandHelp,
Subcommands: []cli.Command{
{
Name: "storage_pool",
Usage: "empty a storage pool",
UsageText: "bytemark --admin empty storage_pool <storage_pool>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "storage_pool",
Usage: "the ID or label of the storage pool to be emptied",
},
},
Action: With(OptionalArgs("storage_pool"), RequiredFlags("storage_pool"), AuthProvider, func(c *Context) error {
if err := global.Client.EmptyStoragePool(c.String("storage_pool")); err != nil {
return err
}

log.Output("Storage pool updated")

return nil
}),
},
{
Name: "head",
Usage: "empty a head",
UsageText: "bytemark --admin empty head <head>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "head",
Usage: "the ID or label of the head to be emptied",
},
},
Action: With(OptionalArgs("head"), RequiredFlags("head"), AuthProvider, func(c *Context) error {
if err := global.Client.EmptyHead(c.String("head")); err != nil {
return err
}

log.Output("Head updated")

return nil
}),
},
},
})
}
67 changes: 67 additions & 0 deletions cmd/bytemark/empty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"fmt"
"github.com/cheekybits/is"
"testing"
)

func TestEmptyStoragePool(t *testing.T) {
is := is.New(t)
_, c := baseTestAuthSetup(t, true)

c.When("EmptyStoragePool", "pool1").Return(nil).Times(1)

err := global.App.Run([]string{"bytemark", "empty", "storage_pool", "pool1"})

is.Nil(err)

if ok, err := c.Verify(); !ok {
t.Fatal(err)
}
}

func TestEmptyStoragePoolError(t *testing.T) {
is := is.New(t)
_, c := baseTestAuthSetup(t, true)

c.When("EmptyStoragePool", "pool1").Return(fmt.Errorf("Could not empty storage pool")).Times(1)

err := global.App.Run([]string{"bytemark", "empty", "storage_pool", "pool1"})

is.NotNil(err)

if ok, err := c.Verify(); !ok {
t.Fatal(err)
}
}

func TestEmptyHead(t *testing.T) {
is := is.New(t)
_, c := baseTestAuthSetup(t, true)

c.When("EmptyHead", "pool1").Return(nil).Times(1)

err := global.App.Run([]string{"bytemark", "empty", "head", "pool1"})

is.Nil(err)

if ok, err := c.Verify(); !ok {
t.Fatal(err)
}
}

func TestEmptyHeadError(t *testing.T) {
is := is.New(t)
_, c := baseTestAuthSetup(t, true)

c.When("EmptyHead", "pool1").Return(fmt.Errorf("Could not empty storage pool")).Times(1)

err := global.App.Run([]string{"bytemark", "empty", "head", "pool1"})

is.NotNil(err)

if ok, err := c.Verify(); !ok {
t.Fatal(err)
}
}
20 changes: 20 additions & 0 deletions lib/admin_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,23 @@ func (c *bytemarkClient) CancelVMMigration(id int) (err error) {
_, _, err = r.Run(nil, nil)
return
}

func (c *bytemarkClient) EmptyStoragePool(idOrLabel string) (err error) {
r, err := c.BuildRequest("POST", BrainEndpoint, "/admin/storage_pools/%s/empty", idOrLabel)
if err != nil {
return
}

_, _, err = r.Run(nil, nil)
return
}

func (c *bytemarkClient) EmptyHead(idOrLabel string) (err error) {
r, err := c.BuildRequest("POST", BrainEndpoint, "/admin/heads/%s/empty", idOrLabel)
if err != nil {
return
}

_, _, err = r.Run(nil, nil)
return
}
12 changes: 12 additions & 0 deletions lib/admin_calls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,15 @@ func TestPostCancelVMMigration(t *testing.T) {
return client.CancelVMMigration(1235)
})
}

func TestPostEmptyStoragePool(t *testing.T) {
simplePostTest(t, "/admin/storage_pools/pool1/empty", ``, func(client Client) error {
return client.EmptyStoragePool("pool1")
})
}

func TestPostEmptyHead(t *testing.T) {
simplePostTest(t, "/admin/heads/head1/empty", ``, func(client Client) error {
return client.EmptyHead("head1")
})
}
2 changes: 2 additions & 0 deletions lib/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,6 @@ type Client interface {
CreateIPRange(ipRange string, vlanNum int) error
CancelDiscMigration(id int) error
CancelVMMigration(id int) error
EmptyStoragePool(idOrLabel string) error
EmptyHead(idOrLabel string) error
}
8 changes: 8 additions & 0 deletions mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,11 @@ func (c *Client) CancelVMMigration(id int) error {
r := c.Called(id)
return r.Error(0)
}
func (c *Client) EmptyStoragePool(idOrLabel string) error {
r := c.Called(idOrLabel)
return r.Error(0)
}
func (c *Client) EmptyHead(idOrLabel string) error {
r := c.Called(idOrLabel)
return r.Error(0)
}

0 comments on commit c29094c

Please sign in to comment.