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

Support partner interconnect GetBgpAuthKey and RegenerateServiceKey operations #785

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like root.RegenerateServiceKey will always be nil? RegenerateServiceKey is just an empty struct and regenerateServiceKeyRoot uses the stuct tag json:"-"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. it will always be nil. To keep the consistency compare other methods hence i added the responseModel, Response and error. Let me know if you think it needs any improvement

}
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)
}
Loading