Skip to content

Commit

Permalink
address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
guptado committed May 21, 2024
1 parent 4d49b02 commit 21edaaa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
6 changes: 2 additions & 4 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,8 @@ const (

// ArgVPCPeeringName is a name of the VPC Peering.
ArgVPCPeeringName = "name"
// ArgVPCPeeringFirstVPCID is the first vpc id of the peering
ArgVPCPeeringFirstVPCID = "first-vpc-id"
// ArgVPCPeeringSecondVPCID is the second vpc id of the peering
ArgVPCPeeringSecondVPCID = "second-vpc-id"
// ArgVPCPeeringVPCIDs is the first vpc id of the peering
ArgVPCPeeringVPCIDs = "vpc-ids"
// ArgVPCPeeringVPCID is id of the VPC.
ArgVPCPeeringVPCID = "vpc-id"

Expand Down
26 changes: 12 additions & 14 deletions commands/displayers/vpc_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package displayers
import (
"github.com/digitalocean/doctl/do"
"io"
"strings"
)

type VPCPeering struct {
Expand All @@ -19,21 +20,19 @@ func (v *VPCPeering) Cols() []string {
return []string{
"ID",
"Name",
"FirstVpcID",
"SecondVpcID",
"VPCIDs",
"Status",
"Created",
}
}

func (v *VPCPeering) ColMap() map[string]string {
return map[string]string{
"ID": "ID",
"Name": "Name",
"FirstVpcID": "First Vpc ID",
"SecondVpcID": "Second Vpc ID",
"Status": "Status",
"Created": "Created At",
"ID": "ID",
"Name": "Name",
"VPCIDs": "VPCIDs",
"Status": "Status",
"Created": "Created At",
}
}

Expand All @@ -42,12 +41,11 @@ func (v *VPCPeering) KV() []map[string]any {

for _, v := range v.VPCPeerings {
o := map[string]any{
"ID": v.ID,
"Name": v.Name,
"FirstVpcID": v.VPCIDs[0],
"SecondVpcID": v.VPCIDs[1],
"Status": v.Status,
"Created": v.CreatedAt,
"ID": v.ID,
"Name": v.Name,
"VPCIDs": strings.Join(v.VPCIDs, ","),
"Status": v.Status,
"Created": v.CreatedAt,
}
out = append(out, o)
}
Expand Down
35 changes: 15 additions & 20 deletions commands/vpc_peerings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"errors"
"fmt"
"strings"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/commands/displayers"
Expand All @@ -27,8 +28,7 @@ With the VPC Peerings commands, you can get, list, create, update, or delete VPC
peeringDetails := `
- The VPC Peering ID
- The VPC Peering Name
- The First VPC network's ID
- The Second VPC network's ID
- The Peered VPC network IDs
- The VPC Peering Status
- The VPC Peering creation date, in ISO8601 combined date and time format
`
Expand All @@ -38,22 +38,20 @@ With the VPC Peerings commands, you can get, list, create, update, or delete VPC
cmdPeeringGet.Example = `The following example retrieves information about a VPC Peering with the ID ` + "`" + `f81d4fae-7dec-11d0-a765-00a0c91e6bf6` + "`" + `: doctl vpc-peerings get f81d4fae-7dec-11d0-a765-00a0c91e6bf6`

cmdPeeringList := CmdBuilder(cmd, RunVPCPeeringList, "list", "List VPC Peerings", "Retrieves a list of the VPC Peerings on your account, including the following informations for each:"+peeringDetails, Writer,
aliasOpt("ls"), displayerType(&displayers.VPC{}))
aliasOpt("ls"), displayerType(&displayers.VPCPeering{}))
AddStringFlag(cmdPeeringList, doctl.ArgVPCPeeringVPCID, "", "",
"VPC ID")
cmdPeeringList.Example = `The following example lists the VPC Peerings on your account : doctl vpc-peerings list --format Name,FirstVpcID,SecondVpcID`
cmdPeeringList.Example = `The following example lists the VPC Peerings on your account : doctl vpc-peerings list --format Name,VPCIDs`

cmdPeeringCreate := CmdBuilder(cmd, RunVPCPeeringCreate, "create",
"Create a new VPC Peering", "Use this command to create a new VPC Peering on your account.", Writer, aliasOpt("c"))
AddStringFlag(cmdPeeringCreate, doctl.ArgVPCPeeringName, "", "",
"The VPC Peering's name", requiredOpt())
AddStringFlag(cmdPeeringCreate, doctl.ArgVPCPeeringFirstVPCID, "", "",
"First VPC ID")
AddStringFlag(cmdPeeringCreate, doctl.ArgVPCPeeringSecondVPCID, "", "",
"Second VPC ID")
AddStringFlag(cmdPeeringCreate, doctl.ArgVPCPeeringVPCIDs, "", "",
"Peering VPC IDs")
cmdPeeringCreate.Example = `The following example creates a VPC Peering named ` +
"`" + `example-peering` + "`" +
` : doctl vpc-peerings create --name example-peering --first-vpc-id f81d4fae-7dec-11d0-a765-00a0c91e6bf6 --second-vpc-id 3f900b61-30d7-40d8-9711-8c5d6264b268`
` : doctl vpc-peerings create --name example-peering --vpc-ids f81d4fae-7dec-11d0-a765-00a0c91e6bf6,3f900b61-30d7-40d8-9711-8c5d6264b268`

cmdPeeringUpdate := CmdBuilder(cmd, RunVPCPeeringUpdate, "update <id>",
"Update a VPC Peering's name", `Use this command to update the name of a VPC Peering`, Writer, aliasOpt("u"))
Expand Down Expand Up @@ -99,26 +97,23 @@ func RunVPCPeeringCreate(c *CmdConfig) error {
}
r.Name = name

firstVpcID, err := c.Doit.GetString(c.NS, doctl.ArgVPCPeeringFirstVPCID)
vpcIDs, err := c.Doit.GetString(c.NS, doctl.ArgVPCPeeringVPCIDs)
if err != nil {
return err
}

if firstVpcID == "" {
return errors.New("first VPC ID is empty")
}
for _, v := range strings.Split(vpcIDs, ",") {
if v == "" {
return errors.New("VPC ID is empty")
}

secondVPCID, err := c.Doit.GetString(c.NS, doctl.ArgVPCPeeringSecondVPCID)
if err != nil {
return err
r.VPCIDs = append(r.VPCIDs, strings.TrimSpace(v))
}

if secondVPCID == "" {
return errors.New("second VPC ID is empty")
if len(r.VPCIDs) != 2 {
return errors.New("VPC IDs length should be 2")
}

r.VPCIDs = []string{firstVpcID, secondVPCID}

peering, err := c.VPCs().CreateVPCPeering(r)
if err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions commands/vpc_peerings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func TestVPCPeeringCreate(t *testing.T) {
tm.vpcs.EXPECT().CreateVPCPeering(&r).Return(&testPeering, nil)

config.Doit.Set(config.NS, doctl.ArgVPCPeeringName, "peering-name")
config.Doit.Set(config.NS, doctl.ArgVPCPeeringFirstVPCID, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
config.Doit.Set(config.NS, doctl.ArgVPCPeeringSecondVPCID, "3f900b61-30d7-40d8-9711-8c5d6264b268")
config.Doit.Set(config.NS, doctl.ArgVPCPeeringVPCIDs, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6, 3f900b61-30d7-40d8-9711-8c5d6264b268")

err := RunVPCPeeringCreate(config)
assert.NoError(t, err)
Expand All @@ -88,15 +87,15 @@ func TestVPCPeeringCreate(t *testing.T) {
config.Doit.Set(config.NS, doctl.ArgVPCPeeringName, "peering-name")

err := RunVPCPeeringCreate(config)
assert.EqualError(t, err, "first VPC ID is empty")
assert.EqualError(t, err, "VPC ID is empty")
})

withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
config.Doit.Set(config.NS, doctl.ArgVPCPeeringName, "peering-name")
config.Doit.Set(config.NS, doctl.ArgVPCPeeringFirstVPCID, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
config.Doit.Set(config.NS, doctl.ArgVPCPeeringVPCIDs, "f81d4fae-7dec-11d0-a765-00a0c91e6bf6")

err := RunVPCPeeringCreate(config)
assert.EqualError(t, err, "second VPC ID is empty")
assert.EqualError(t, err, "VPC IDs length should be 2")
})
}

Expand Down

0 comments on commit 21edaaa

Please sign in to comment.