Skip to content

Commit

Permalink
Merge pull request #754 from hilary/cleanup_tag
Browse files Browse the repository at this point in the history
consolidate logic, droplet create --tagName(s)
  • Loading branch information
hilary committed Mar 5, 2020
2 parents 168528a + af770fd commit cd06cef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
30 changes: 3 additions & 27 deletions commands/droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func RunDropletCreate(c *CmdConfig) error {
if err != nil {
return err
}
if len(tagName) > 0 {
tagNames = append(tagNames, tagName)
}

sshKeys := extractSSHKeys(keys)

Expand Down Expand Up @@ -247,7 +250,6 @@ func RunDropletCreate(c *CmdConfig) error {
}

ds := c.Droplets()
ts := c.Tags()

var wg sync.WaitGroup
var createdList do.Droplets
Expand All @@ -272,38 +274,12 @@ func RunDropletCreate(c *CmdConfig) error {
go func() {
defer wg.Done()

if tagName != "" {
tag, err := ts.Get(tagName)
if err != nil {
errs <- err
return
}
if tag == nil {
errs <- fmt.Errorf("Specified tag does not exist.")
return
}
}

d, err := ds.Create(dcr, wait)
if err != nil {
errs <- err
return
}

if tagName != "" {
trr := &godo.TagResourcesRequest{
Resources: []godo.Resource{
{ID: strconv.Itoa(d.ID), Type: godo.DropletResourceType},
},
}

err := ts.TagResources(tagName, trr)
if err != nil {
errs <- err
}

}

createdList = append(createdList, *d)
}()
}
Expand Down
20 changes: 11 additions & 9 deletions commands/droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ func TestDropletCreate(t *testing.T) {

func TestDropletCreateWithTag(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
dcr := &godo.DropletCreateRequest{Name: "droplet", Region: "dev0", Size: "1gb", Image: godo.DropletCreateImage{ID: 0, Slug: "image"}, SSHKeys: []godo.DropletCreateSSHKey{}, Backups: false, IPv6: false, PrivateNetworking: false, UserData: "#cloud-config"}
dcr := &godo.DropletCreateRequest{
Name: "droplet",
Region: "dev0",
Size: "1gb",
Image: godo.DropletCreateImage{ID: 0, Slug: "image"},
SSHKeys: []godo.DropletCreateSSHKey{},
Backups: false,
IPv6: false,
PrivateNetworking: false,
UserData: "#cloud-config",
Tags: []string{"my-tag"}}
tm.droplets.EXPECT().Create(dcr, false).Return(&testDroplet, nil)
tm.tags.EXPECT().Get("my-tag").Return(&testTag, nil)

trr := &godo.TagResourcesRequest{
Resources: []godo.Resource{
{ID: "1", Type: godo.DropletResourceType},
},
}
tm.tags.EXPECT().TagResources("my-tag", trr).Return(nil)

config.Args = append(config.Args, "droplet")

Expand Down

0 comments on commit cd06cef

Please sign in to comment.