From e71250739a04e1ea87a59535e5d8c6044a06e22a Mon Sep 17 00:00:00 2001 From: guptado <141348137+guptado@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:16:00 +0530 Subject: [PATCH] Support partner interconnect GetBgpAuthKey and RegenerateServiceKey operations (#785) * support partner interconnect GetBgpAuthKey and RegenerateServiceKey * add state in service key --------- Co-authored-by: Andrew Starr-Bochicchio --- partner_interconnect_attachments.go | 52 ++++++++++++++++++++++++ partner_interconnect_attachments_test.go | 49 ++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/partner_interconnect_attachments.go b/partner_interconnect_attachments.go index 15af985..e2707dc 100644 --- a/partner_interconnect_attachments.go +++ b/partner_interconnect_attachments.go @@ -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{} @@ -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. @@ -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) @@ -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 +} diff --git a/partner_interconnect_attachments_test.go b/partner_interconnect_attachments_test.go index 06ad15e..0db54d4 100644 --- a/partner_interconnect_attachments_test.go +++ b/partner_interconnect_attachments_test.go @@ -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) +}