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

[VPC-3918] List PIA remote routes #1658

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions commands/displayers/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,38 @@ func (v *PartnerInterconnectAttachment) KV() []map[string]any {

return out
}

type PartnerInterconnectAttachmentRoute struct {
PartnerInterconnectAttachmentRoutes do.PartnerInterconnectAttachmentRoutes
}

var _ Displayable = &PartnerInterconnectAttachmentRoute{}

func (v *PartnerInterconnectAttachmentRoute) JSON(out io.Writer) error {
return writeJSON(v.PartnerInterconnectAttachmentRoutes, out)
}

func (v *PartnerInterconnectAttachmentRoute) Cols() []string {
return []string{
"Cidr",
}
}

func (v *PartnerInterconnectAttachmentRoute) ColMap() map[string]string {
return map[string]string{
"Cidr": "Cidr",
}
}

func (v *PartnerInterconnectAttachmentRoute) KV() []map[string]any {
out := make([]map[string]any, 0, len(v.PartnerInterconnectAttachmentRoutes))

for _, ia := range v.PartnerInterconnectAttachmentRoutes {
o := map[string]any{
"Cidr": ia.Cidr,
}
out = append(out, o)
}

return out
}
38 changes: 36 additions & 2 deletions commands/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ With the Partner Interconnect Attachments commands, you can get, list, create, u
`: doctl network --type "partner" interconnect-attachment update f81d4fae-7dec-11d0-a765-00a0c91e6bf6 --name "new-name" --
vpc-ids "270a76ed-1bb7-4c5d-a6a5-e863de086940"`

interconnectAttachmentRouteDetails := `
- The Partner Interconnect Attachment ID
- The Partner Interconnect Attachment Cidr`

cmdPartnerIARouteList := CmdBuilder(cmd, RunPartnerInterconnectAttachmentRouteList, "list-routes", "List Network Interconnect Attachment Routes", "Retrieves a list of the Network Interconnect Attachment Routes on your account, including the following information for each:"+interconnectAttachmentRouteDetails, Writer,
aliasOpt("ls-routes"), displayerType(&displayers.PartnerInterconnectAttachment{}))
AddStringFlag(cmdPartnerIARouteList, doctl.ArgInterconnectAttachmentType, "", "partner", "Specify interconnect attachment type (e.g., partner)")
//AddStringFlag(cmdPartnerIARouteList, doctl.ArgInterconnectAttachmentType, "", "partner", "Specify interconnect attachment type (e.g., partner)")
cmdPartnerIARouteList.Example = `The following example lists the Network Interconnect Attachments on your account :` +
` doctl network --type "partner" interconnect-attachment list-routes --format ID,Cidr `

return cmd
}

Expand Down Expand Up @@ -260,7 +271,7 @@ func RunPartnerInterconnectAttachmentUpdate(c *CmdConfig) error {
if err != nil {
return err
}
peeringID := c.Args[0]
iaID := c.Args[0]

r := new(godo.PartnerInterconnectAttachmentUpdateRequest)
name, err := c.Doit.GetString(c.NS, doctl.ArgPartnerInterconnectAttachmentName)
Expand All @@ -275,7 +286,7 @@ func RunPartnerInterconnectAttachmentUpdate(c *CmdConfig) error {
}
r.VPCIDs = strings.Split(vpcIDs, ",")

interconnectAttachment, err := c.PartnerInterconnectAttachments().UpdatePartnerInterconnectAttachment(peeringID, r)
interconnectAttachment, err := c.PartnerInterconnectAttachments().UpdatePartnerInterconnectAttachment(iaID, r)
if err != nil {
return err
}
Expand Down Expand Up @@ -336,6 +347,29 @@ func RunPartnerInterconnectAttachmentDelete(c *CmdConfig) error {
return nil
}

// RunPartnerInterconnectAttachmentRouteList lists Partner Interconnect Attachment routes
func RunPartnerInterconnectAttachmentRouteList(c *CmdConfig) error {

if err := ensurePartnerAttachmentType(c); err != nil {
return err
}

err := ensureOneArg(c)
if err != nil {
return err
}
iaID := c.Args[0]

pias := c.PartnerInterconnectAttachments()
routeList, err := pias.ListPartnerInterconnectAttachmentRoutes(iaID)
if err != nil {
return err
}

item := &displayers.PartnerInterconnectAttachmentRoute{PartnerInterconnectAttachmentRoutes: routeList}
return c.Display(item)
}

func waitForPIA(pias do.PartnerInterconnectAttachmentsService, iaID string, wantStatus string, terminateOnNotFound bool) error {
const maxAttempts = 360
const errStatus = "ERROR"
Expand Down
31 changes: 28 additions & 3 deletions commands/partner_interconnect_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"testing"
"time"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"

"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
)

var (
Expand All @@ -28,13 +29,24 @@ var (
testPartnerIAList = do.PartnerInterconnectAttachments{
testPartnerAttachment,
}

testPartnerAttachmentRoute = do.PartnerInterconnectAttachmentRoute{
RemoteRoute: &godo.RemoteRoute{
ID: "test-route-id",
Cidr: "10.10.0.0/24",
},
}

testPartnerIARouteList = do.PartnerInterconnectAttachmentRoutes{
testPartnerAttachmentRoute,
}
)

func TestPartnerInterconnectAttachmentsCommand(t *testing.T) {
cmd := PartnerInterconnectAttachments()
assert.NotNil(t, cmd)

assertCommandNames(t, cmd, "create", "get", "list", "delete", "update")
assertCommandNames(t, cmd, "create", "get", "list", "delete", "update", "list-routes")
}

func TestPartnerInterconnectAttachmentCreate(t *testing.T) {
Expand Down Expand Up @@ -150,3 +162,16 @@ func TestInterconnectAttachmentsUpdateNoID(t *testing.T) {
assert.Error(t, err)
})
}

func TestInterconnectAttachmentRoutesList(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
config.Doit.Set(config.NS, doctl.ArgInterconnectAttachmentType, "partner")

iaID := "ia-uuid1"
config.Args = append(config.Args, iaID)
tm.partnerInterconnectAttachment.EXPECT().ListPartnerInterconnectAttachmentRoutes(iaID).Return(testPartnerIARouteList, nil)

err := RunPartnerInterconnectAttachmentRouteList(config)
assert.NoError(t, err)
})
}
15 changes: 15 additions & 0 deletions do/mocks/PartnerInterconnectAttachmentsService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions do/partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type PartnerInterconnectAttachment struct {
// PartnerInterconnectAttachments is a slice of PartnerInterconnectAttachment.
type PartnerInterconnectAttachments []PartnerInterconnectAttachment

// PartnerInterconnectAttachmentRoute wraps a godo RemoteRoute.
type PartnerInterconnectAttachmentRoute struct {
*godo.RemoteRoute
}

// PartnerInterconnectAttachmentRoutes is a slice of PartnerInterconnectAttachmentRoute.
type PartnerInterconnectAttachmentRoutes []PartnerInterconnectAttachmentRoute

// PartnerInterconnectAttachmentsService is an interface for interacting with
// DigitalOcean's partner interconnect attachments api.
type PartnerInterconnectAttachmentsService interface {
Expand All @@ -35,6 +43,7 @@ type PartnerInterconnectAttachmentsService interface {
ListPartnerInterconnectAttachments() (PartnerInterconnectAttachments, error)
DeletePartnerInterconnectAttachment(iaID string) error
UpdatePartnerInterconnectAttachment(iaID string, req *godo.PartnerInterconnectAttachmentUpdateRequest) (*PartnerInterconnectAttachment, error)
ListPartnerInterconnectAttachmentRoutes(iaID string) (PartnerInterconnectAttachmentRoutes, error)
}

var _ PartnerInterconnectAttachmentsService = &partnerInterconnectAttachmentsService{}
Expand Down Expand Up @@ -111,3 +120,35 @@ func (p *partnerInterconnectAttachmentsService) UpdatePartnerInterconnectAttachm

return &PartnerInterconnectAttachment{PartnerInterconnectAttachment: partnerIA}, nil
}

// ListRoutes(context.Context, string, *ListOptions) ([]*RemoteRoute, *Response, error)

// ListPartnerInterconnectAttachmentRoutes lists all partner interconnect attachment routes.
func (p *partnerInterconnectAttachmentsService) ListPartnerInterconnectAttachmentRoutes(iaID string) (PartnerInterconnectAttachmentRoutes, error) {
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
list, resp, err := p.client.PartnerInterconnectAttachments.ListRoutes(context.TODO(), iaID, opt)
if err != nil {
return nil, nil, err
}

si := make([]any, len(list))
for i := range list {
si[i] = list[i]
}

return si, resp, err
}

si, err := PaginateResp(f)
if err != nil {
return nil, err
}

list := make([]PartnerInterconnectAttachmentRoute, len(si))
for i := range si {
a := si[i].(*godo.RemoteRoute)
list[i] = PartnerInterconnectAttachmentRoute{RemoteRoute: a}
}

return list, nil
}
Loading