Skip to content

Commit

Permalink
Support partner interconnect GetBgpAuthKey and RegenerateServiceKey o…
Browse files Browse the repository at this point in the history
…perations (#785)

* support partner interconnect GetBgpAuthKey and RegenerateServiceKey

* add state in service key

---------

Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
guptado and andrewsomething authored Feb 18, 2025
1 parent 7aa2955 commit e712507
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
52 changes: 52 additions & 0 deletions partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type PartnerInterconnectAttachmentsService interface {
GetServiceKey(context.Context, string) (*ServiceKey, *Response, error)
SetRoutes(context.Context, string, *PartnerInterconnectAttachmentSetRoutesRequest) (*PartnerInterconnectAttachment, *Response, error)
ListRoutes(context.Context, string, *ListOptions) ([]*RemoteRoute, *Response, error)
GetBGPAuthKey(ctx context.Context, iaID string) (*BgpAuthKey, *Response, error)
RegenerateServiceKey(ctx context.Context, iaID string) (*RegenerateServiceKey, *Response, error)
}

var _ PartnerInterconnectAttachmentsService = &PartnerInterconnectAttachmentsServiceOp{}
Expand Down Expand Up @@ -105,6 +107,7 @@ type BGP struct {
// ServiceKey represents the service key of a Partner Interconnect Attachment.
type ServiceKey struct {
ServiceKey string `json:"service_key,omitempty"`
State string `json:"state,omitempty"`
}

// RemoteRoute represents a route for a Partner Interconnect Attachment.
Expand Down Expand Up @@ -157,6 +160,21 @@ type remoteRoutesRoot struct {
Meta *Meta `json:"meta"`
}

type BgpAuthKey struct {
Value string `json:"value"`
}

type bgpAuthKeyRoot struct {
BgpAuthKey *BgpAuthKey `json:"bgp_auth_key"`
}

type RegenerateServiceKey struct {
}

type regenerateServiceKeyRoot struct {
RegenerateServiceKey *RegenerateServiceKey `json:"-"`
}

// List returns a list of all Partner Interconnect Attachments, with optional pagination.
func (s *PartnerInterconnectAttachmentsServiceOp) List(ctx context.Context, opt *ListOptions) ([]*PartnerInterconnectAttachment, *Response, error) {
path, err := addOptions(partnerInterconnectAttachmentsBasePath, opt)
Expand Down Expand Up @@ -308,3 +326,37 @@ func (s *PartnerInterconnectAttachmentsServiceOp) SetRoutes(ctx context.Context,

return root.PartnerInterconnectAttachment, resp, nil
}

// GetBGPAuthKey returns Partner Interconnect Attachment bgp auth key
func (s *PartnerInterconnectAttachmentsServiceOp) GetBGPAuthKey(ctx context.Context, iaID string) (*BgpAuthKey, *Response, error) {
path := fmt.Sprintf("%s/%s/bgp_auth_key", partnerInterconnectAttachmentsBasePath, iaID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(bgpAuthKeyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.BgpAuthKey, resp, nil
}

// RegenerateServiceKey regenerates the service key of a Partner Interconnect Attachment.
func (s *PartnerInterconnectAttachmentsServiceOp) RegenerateServiceKey(ctx context.Context, iaID string) (*RegenerateServiceKey, *Response, error) {
path := fmt.Sprintf("%s/%s/service_key", partnerInterconnectAttachmentsBasePath, iaID)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, nil)
if err != nil {
return nil, nil, err
}

root := new(regenerateServiceKeyRoot)
resp, err := s.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root.RegenerateServiceKey, resp, nil
}
49 changes: 49 additions & 0 deletions partner_interconnect_attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,52 @@ func TestPartnerInterconnectAttachment_Set(t *testing.T) {
require.Equal(t, tt.expectedUpdatedInterconnect, got)
}
}

func TestPartnerInterconnectAttachment_GetBGPAuthKey(t *testing.T) {
setup()
defer teardown()

svc := client.PartnerInterconnectAttachments
path := "/v2/partner_interconnect/attachments"
want := &BgpAuthKey{
Value: "bgp-auth-secret",
}
id := "880b7f98-f062-404d-b33c-458d545696f6"
jsonBlob := `
{
"bgp_auth_key": {
"value": "bgp-auth-secret"
}
}
`

mux.HandleFunc(path+"/"+id+"/bgp_auth_key", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.Write([]byte(jsonBlob))
})

got, _, err := svc.GetBGPAuthKey(ctx, id)
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestPartnerInterconnectAttachment_RegenerateServiceKey(t *testing.T) {
setup()
defer teardown()

svc := client.PartnerInterconnectAttachments
path := "/v2/partner_interconnect/attachments"
id := "880b7f98-f062-404d-b33c-458d545696f6"
jsonBlob := `{}`

mux.HandleFunc(path+"/"+id+"/service_key", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
w.Write([]byte(jsonBlob))
})

got, _, err := svc.RegenerateServiceKey(ctx, id)
require.NoError(t, err)

expectedResponse := regenerateServiceKeyRoot{}
require.Equal(t, expectedResponse.RegenerateServiceKey, got)
}

0 comments on commit e712507

Please sign in to comment.