Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consolidate logic, droplet create --tagName(s) #754

Merged
merged 1 commit into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
consolidate logic, droplet create --tagName(s)
Per @andrewsomething

"A bit of context: For Droplet --tag-name first checks that the tag
exists since, when first introduced, that was a prerequisite. If the
user had not already created that tag, the Droplet create request
would fail. These days, we will automatically create the tag for the
user if they attempt to create a Droplet using a tag that did not
previously exist. So that check is now redundant and just adds an
unnecessary API call."

In fact, it adds three unnecessary API calls!

We should look at pulling --tagName from the docs at a later time.
  • Loading branch information
Hilary Holz committed Mar 3, 2020
commit af770fd60726b044c4d6b92f8b37e73f726daa20
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