Skip to content

Commit

Permalink
added get and list interconnect attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
guptado committed Jan 3, 2025
1 parent 423d5a3 commit 9d228aa
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 3 deletions.
76 changes: 76 additions & 0 deletions commands/displayers/partner_interconnect_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package displayers

import (
"io"
"strings"

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

type PartnerInterconnectAttachment struct {
PartnerInterconnectAttachments do.PartnerInterconnectAttachments
}

var _ Displayable = &PartnerInterconnectAttachment{}

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

func (v *PartnerInterconnectAttachment) Cols() []string {
return []string{
"ID",
"Name",
"State",
"ConnectionBandwidthInMbps",
"Region",
"NaaSProvider",
"VPCIDs",
"CreatedAt",
"BGPLocalASN",
"BGPLocalRouterIP",
"BGPPeerASN",
"BGPPeerRouterIP",
}
}

func (v *PartnerInterconnectAttachment) ColMap() map[string]string {
return map[string]string{
"ID": "ID",
"Name": "Name",
"State": "State",
"ConnectionBandwidthInMbps": "Connection Bandwidth (MBPS)",
"Region": "Region",
"NaaSProvider": "NaaS Provider",
"VPCIDs": "VPCIDs",
"Created": "Created At",
"BGPLocalASN": "BGP Local ASN",
"BGPLocalRouterIP": "BGP Local Router IP",
"BGPPeerASN": "BGP Peer ASN",
"BGPPeerRouterIP": "BGP Peer Router IP",
}
}

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

for _, ia := range v.PartnerInterconnectAttachments {
o := map[string]any{
"ID": ia.ID,
"Name": ia.Name,
"State": ia.State,
"ConnectionBandwidthInMbps": ia.ConnectionBandwidthInMbps,
"Region": ia.Region,
"NaaSProvider": ia.NaaSProvider,
"VPCIDs": strings.Join(ia.VPCIDs, ","),
"Created": ia.CreatedAt,
"BGPLocalASN": ia.BGP.LocalASN,
"BGPLocalRouterIP": ia.BGP.LocalRouterIP,
"BGPPeerASN": ia.BGP.PeerASN,
"BGPPeerRouterIP": ia.BGP.PeerRouterIP,
}
out = append(out, o)
}

return out
}
4 changes: 3 additions & 1 deletion commands/doit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"strings"
"time"

"github.com/digitalocean/doctl"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/digitalocean/doctl"
)

const (
Expand Down Expand Up @@ -184,6 +185,7 @@ func addCommands() {
DoitCmd.AddCommand(Version())
DoitCmd.AddCommand(Registry())
DoitCmd.AddCommand(VPCs())
DoitCmd.AddCommand(Partner())
DoitCmd.AddCommand(OneClicks())
DoitCmd.AddCommand(Monitoring())
DoitCmd.AddCommand(Serverless())
Expand Down
93 changes: 93 additions & 0 deletions commands/partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package commands

import (
"github.com/spf13/cobra"

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

// Partner creates the partner commands
func Partner() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "partner",
Short: "Display commands that manage Partner products",
Long: `The commands under ` + "`" + `doctl partner` + "`" + ` are for managing Partner products`,
GroupID: manageResourcesGroup,
},
}

cmd.AddCommand(InterconnectAttachments())

return cmd
}

// InterconnectAttachments creates the interconnect attachment command
func InterconnectAttachments() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "interconnect-attachment",
Short: "Display commands that manage Partner Interconnect Attachments",
Long: `The commands under ` + "`" + `doctl partner interconnect-attachment` + "`" + ` are for managing your Partner Interconnect Attachments.
With the Partner Interconnect Attachments commands, you can get or list, create, update, or delete Partner Interconnect Attachments, and manage their configuration details.`,
},
}

interconnectAttachmentDetails := `
- The Partner Interconnect Attachment ID
- The Partner Interconnect Attachment Name
- The Partner Interconnect Attachment State
- The Partner Interconnect Attachment Connection Bandwidth in Mbps
- The Partner Interconnect Attachment Region
- The Partner Interconnect Attachment NaaS Provider
- The Partner Interconnect Attachment VPC network IDs
- The Partner Interconnect Attachment creation date, in ISO8601 combined date and time format
- The Partner Interconnect Attachment BGP Local ASN
- The Partner Interconnect Attachment BGP Local Router IP
- The Partner Interconnect Attachment BGP Peer ASN
- The Partner Interconnect Attachment BGP Peer Router IP
`

cmdPartnerIAGet := CmdBuilder(cmd, RunPartnerInterconnectAttachmentGet, "get <interconnect-attachment-id>",
"Retrieves a Partner Interconnect Attachment", "Retrieves information about a Partner Interconnect Attachment, including:"+interconnectAttachmentDetails, Writer,
aliasOpt("g"), displayerType(&displayers.PartnerInterconnectAttachment{}))
cmdPartnerIAGet.Example = `The following example retrieves information about a Partner Interconnect Attachment with the ID ` + "`" + `f81d4fae-7dec-11d0-a765-00a0c91e6bf6` + "`" + `: doctl partner interconnect-attachment get f81d4fae-7dec-11d0-a765-00a0c91e6bf6`

cmdPartnerIAList := CmdBuilder(cmd, RunPartnerInterconnectAttachmentList, "list", "List Partner Interconnect Attachments", "Retrieves a list of the Partner Interconnect Attachments on your account, including the following information for each:"+interconnectAttachmentDetails, Writer,
aliasOpt("ls"), displayerType(&displayers.PartnerInterconnectAttachment{}))
cmdPartnerIAList.Example = `The following example lists the Partner Interconnect Attachments on your account : doctl partner interconnect-attachment list --format Name,VPCIDs`

return cmd
}

// RunPartnerInterconnectAttachmentGet retrieves an existing Partner Interconnect Attachment by its identifier.
func RunPartnerInterconnectAttachmentGet(c *CmdConfig) error {
err := ensureOneArg(c)
if err != nil {
return err
}
iaID := c.Args[0]

interconnectAttachment, err := c.VPCs().GetPartnerInterconnectAttachment(iaID)
if err != nil {
return err
}

item := &displayers.PartnerInterconnectAttachment{
PartnerInterconnectAttachments: do.PartnerInterconnectAttachments{*interconnectAttachment},
}
return c.Display(item)
}

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

list, err := c.VPCs().ListPartnerInterconnectAttachments()
if err != nil {
return err
}

item := &displayers.PartnerInterconnectAttachment{PartnerInterconnectAttachments: list}
return c.Display(item)
}
57 changes: 57 additions & 0 deletions commands/partner_interconnect_attachments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package commands

import (
"testing"

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

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

var (
testIA = do.PartnerInterconnectAttachment{
PartnerInterconnectAttachment: &godo.PartnerInterconnectAttachment{
Name: "ia-name",
VPCIDs: []string{"f81d4fae-7dec-11d0-a765-00a0c91e6bf6", "3f900b61-30d7-40d8-9711-8c5d6264b268"},
},
}

testIAList = do.PartnerInterconnectAttachments{
testIA,
}
)

func TestInterconnectAttachmentsCommand(t *testing.T) {
cmd := InterconnectAttachments()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "get", "list")
}

func TestInterconnectAttachmentsGet(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
iaID := "e819b321-a9a1-4078-b437-8e6b8bf13530"
tm.vpcs.EXPECT().GetPartnerInterconnectAttachment(iaID).Return(&testIA, nil)

config.Args = append(config.Args, iaID)

err := RunPartnerInterconnectAttachmentGet(config)
assert.NoError(t, err)
})
}

func TestInterconnectAttachmentsGetNoID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
err := RunPartnerInterconnectAttachmentGet(config)
assert.Error(t, err)
})
}

func TestInterconnectAttachmentsList(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.vpcs.EXPECT().ListPartnerInterconnectAttachments().Return(testIAList, nil)

err := RunPartnerInterconnectAttachmentList(config)
assert.NoError(t, err)
})
}
30 changes: 30 additions & 0 deletions do/mocks/VPCsService.go

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

47 changes: 47 additions & 0 deletions do/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type VPCPeering struct {
// VPCPeerings is a slice of VPCPeering
type VPCPeerings []VPCPeering

// PartnerInterconnectAttachment wraps a godo PartnerInterconnectAttachment
type PartnerInterconnectAttachment struct {
*godo.PartnerInterconnectAttachment
}

// PartnerInterconnectAttachments is a slice of PartnerInterconnectAttachment
type PartnerInterconnectAttachments []PartnerInterconnectAttachment

// VPCsService is the godo VPCsService interface.
type VPCsService interface {
Get(vpcUUID string) (*VPC, error)
Expand All @@ -49,6 +57,8 @@ type VPCsService interface {
UpdateVPCPeering(peeringID string, req *godo.VPCPeeringUpdateRequest) (*VPCPeering, error)
DeleteVPCPeering(peeringID string) error
ListVPCPeeringsByVPCID(vpcID string) (VPCPeerings, error)
GetPartnerInterconnectAttachment(iaID string) (*PartnerInterconnectAttachment, error)
ListPartnerInterconnectAttachments() (PartnerInterconnectAttachments, error)
}

var _ VPCsService = &vpcsService{}
Expand Down Expand Up @@ -221,3 +231,40 @@ func (v *vpcsService) ListVPCPeeringsByVPCID(vpcID string) (VPCPeerings, error)

return list, nil
}

func (v *vpcsService) GetPartnerInterconnectAttachment(iaID string) (*PartnerInterconnectAttachment, error) {
partnerIA, _, err := v.client.PartnerInterconnectAttachments.Get(context.TODO(), iaID)
if err != nil {
return nil, err
}
return &PartnerInterconnectAttachment{PartnerInterconnectAttachment: partnerIA}, nil
}

func (v *vpcsService) ListPartnerInterconnectAttachments() (PartnerInterconnectAttachments, error) {
f := func(opt *godo.ListOptions) ([]any, *godo.Response, error) {
list, resp, err := v.client.PartnerInterconnectAttachments.List(context.TODO(), 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([]PartnerInterconnectAttachment, len(si))
for i := range si {
a := si[i].(*godo.PartnerInterconnectAttachment)
list[i] = PartnerInterconnectAttachment{PartnerInterconnectAttachment: a}
}

return list, nil
}
4 changes: 2 additions & 2 deletions vendor/github.com/digitalocean/godo/util/droplet.go

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

0 comments on commit 9d228aa

Please sign in to comment.