Skip to content
Permalink
Browse files
vpc: modify vpc update cmd to enable users to only pass arguments tha…
…t they intend to update (#903)

Co-authored-by: Andrew Louis <[email protected]>
  • Loading branch information
andrewlouis93 and Andrew Louis committed Oct 30, 2020
1 parent bf13cc7 commit bc0c15a22af95555ddbf7450e333601a21b2736a
Showing with 74 additions and 24 deletions.
  1. +20 −12 commands/vpcs.go
  2. +23 −11 commands/vpcs_test.go
  3. +20 −0 do/mocks/VPCsService.go
  4. +10 −0 do/vpcs.go
  5. +1 −1 integration/vpc_update_test.go
@@ -63,7 +63,7 @@ With the vpcs command, you can list, create, or delete VPCs, and manage their co
cmdRecordUpdate := CmdBuilder(cmd, RunVPCUpdate, "update <id>",
"Update a VPC's configuration", `Use this command to update the configuration of a specified VPC.`, Writer, aliasOpt("u"))
AddStringFlag(cmdRecordUpdate, doctl.ArgVPCName, "", "",
"The VPC's name", requiredOpt())
"The VPC's name")
AddStringFlag(cmdRecordUpdate, doctl.ArgVPCDescription, "", "",
"The VPC's description")
AddBoolFlag(cmdRecordUpdate, doctl.ArgVPCDefault, "", false,
@@ -154,30 +154,38 @@ func RunVPCUpdate(c *CmdConfig) error {
}
vpcUUID := c.Args[0]

r := new(godo.VPCUpdateRequest)
name, err := c.Doit.GetString(c.NS, doctl.ArgVPCName)
if err != nil {
return err
options := make([]godo.VPCSetField, 0)

if c.Doit.IsSet(doctl.ArgVPCName) {
name, err := c.Doit.GetString(c.NS, doctl.ArgVPCName)
if err != nil {
return err
}

options = append(options, godo.VPCSetName(name))
}
r.Name = name

desc, err := c.Doit.GetString(c.NS, doctl.ArgVPCDescription)
if err != nil {
return err
if c.Doit.IsSet(doctl.ArgVPCDescription) {
name, err := c.Doit.GetString(c.NS, doctl.ArgVPCDescription)
if err != nil {
return err
}

options = append(options, godo.VPCSetDescription(name))
}
r.Description = desc

def, err := c.Doit.GetBoolPtr(c.NS, doctl.ArgVPCDefault)
if err != nil {
return err
}

if def != nil {
r.Default = boolPtr(true)
options = append(options, godo.VPCSetDefault())
}

vpcs := c.VPCs()
vpc, err := vpcs.Update(vpcUUID, r)

vpc, err := vpcs.PartialUpdate(vpcUUID, options...)
if err != nil {
return err
}
@@ -83,7 +83,7 @@ func TestVPCUpdate(t *testing.T) {
desc string
setup func(*CmdConfig)
expectedVPCId string
expectedRequest *godo.VPCUpdateRequest
expectedRequest []godo.VPCSetField
}{
{
desc: "update vpc name",
@@ -93,8 +93,8 @@ func TestVPCUpdate(t *testing.T) {

},
expectedVPCId: "vpc-uuid",
expectedRequest: &godo.VPCUpdateRequest{
Name: "update-vpc-name-test",
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
},
},

@@ -107,9 +107,9 @@ func TestVPCUpdate(t *testing.T) {

},
expectedVPCId: "vpc-uuid",
expectedRequest: &godo.VPCUpdateRequest{
Name: "update-vpc-name-test",
Description: "i am a new desc",
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
godo.VPCSetDescription("i am a new desc"),
},
},

@@ -122,10 +122,22 @@ func TestVPCUpdate(t *testing.T) {
in.Doit.Set(in.NS, doctl.ArgVPCDefault, true)
},
expectedVPCId: "vpc-uuid",
expectedRequest: &godo.VPCUpdateRequest{
Name: "update-vpc-name-test",
Description: "i am a new desc",
Default: boolPtr(true),
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
godo.VPCSetDescription("i am a new desc"),
godo.VPCSetDefault(),
},
},

{
desc: "update only default",
setup: func(in *CmdConfig) {
in.Args = append(in.Args, "vpc-uuid")
in.Doit.Set(in.NS, doctl.ArgVPCDefault, true)
},
expectedVPCId: "vpc-uuid",
expectedRequest: []godo.VPCSetField{
godo.VPCSetDefault(),
},
},
}
@@ -136,7 +148,7 @@ func TestVPCUpdate(t *testing.T) {
tt.setup(config)
}

tm.vpcs.EXPECT().Update(tt.expectedVPCId, tt.expectedRequest).Return(&testVPC, nil)
tm.vpcs.EXPECT().PartialUpdate(tt.expectedVPCId, tt.expectedRequest).Return(&testVPC, nil)
err := RunVPCUpdate(config)

assert.NoError(t, err)

Some generated files are not rendered by default. Learn more.

@@ -33,6 +33,7 @@ type VPCsService interface {
List() (VPCs, error)
Create(vpcr *godo.VPCCreateRequest) (*VPC, error)
Update(vpcUUID string, vpcr *godo.VPCUpdateRequest) (*VPC, error)
PartialUpdate(vpcUUID string, options ...godo.VPCSetField) (*VPC, error)
Delete(vpcUUID string) error
}

@@ -105,6 +106,15 @@ func (v *vpcsService) Update(vpcUUID string, vpcr *godo.VPCUpdateRequest) (*VPC,
return &VPC{VPC: vpc}, nil
}

func (v *vpcsService) PartialUpdate(vpcUUID string, options ...godo.VPCSetField) (*VPC, error) {
vpc, _, err := v.client.VPCs.Set(context.TODO(), vpcUUID, options...)
if err != nil {
return nil, err
}

return &VPC{VPC: vpc}, nil
}

func (v *vpcsService) Delete(vpcUUID string) error {
_, err := v.client.VPCs.Delete(context.TODO(), vpcUUID)
return err
@@ -35,7 +35,7 @@ var _ = suite("vpcs/update", func(t *testing.T, when spec.G, it spec.S) {
return
}

if req.Method != http.MethodPut {
if req.Method != http.MethodPatch {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

0 comments on commit bc0c15a

Please sign in to comment.