diff --git a/partner_interconnect_attachments.go b/partner_interconnect_attachments.go index 87a0e9e..df21c92 100644 --- a/partner_interconnect_attachments.go +++ b/partner_interconnect_attachments.go @@ -46,6 +46,37 @@ type PartnerInterconnectAttachmentCreateRequest struct { BGP BGP `json:"bgp,omitempty"` } +type partnerInterconnectAttachmentRequestBody struct { + // Name is the name of the Partner Interconnect Attachment + Name string `json:"name,omitempty"` + // ConnectionBandwidthInMbps is the bandwidth of the connection in Mbps + ConnectionBandwidthInMbps int `json:"connection_bandwidth_in_mbps,omitempty"` + // Region is the region where the Partner Interconnect Attachment is created + Region string `json:"region,omitempty"` + // NaaSProvider is the name of the Network as a Service provider + NaaSProvider string `json:"naas_provider,omitempty"` + // VPCIDs is the IDs of the VPCs to which the Partner Interconnect Attachment is connected + VPCIDs []string `json:"vpc_ids,omitempty"` + // BGP is the BGP configuration of the Partner Interconnect Attachment + BGP *BGP `json:"bgp,omitempty"` +} + +func (req *PartnerInterconnectAttachmentCreateRequest) buildReq() *partnerInterconnectAttachmentRequestBody { + request := &partnerInterconnectAttachmentRequestBody{ + Name: req.Name, + ConnectionBandwidthInMbps: req.ConnectionBandwidthInMbps, + Region: req.Region, + NaaSProvider: req.NaaSProvider, + VPCIDs: req.VPCIDs, + } + + if req.BGP != (BGP{}) { + request.BGP = &req.BGP + } + + return request +} + // PartnerInterconnectAttachmentUpdateRequest represents a request to update a Partner Interconnect Attachment. type PartnerInterconnectAttachmentUpdateRequest struct { // Name is the name of the Partner Interconnect Attachment @@ -154,7 +185,8 @@ func (s *PartnerInterconnectAttachmentsServiceOp) List(ctx context.Context, opt // Create creates a new Partner Interconnect Attachment. func (s *PartnerInterconnectAttachmentsServiceOp) Create(ctx context.Context, create *PartnerInterconnectAttachmentCreateRequest) (*PartnerInterconnectAttachment, *Response, error) { path := partnerInterconnectAttachmentsBasePath - req, err := s.client.NewRequest(ctx, http.MethodPost, path, create) + + req, err := s.client.NewRequest(ctx, http.MethodPost, path, create.buildReq()) if err != nil { return nil, nil, err } diff --git a/partner_interconnect_attachments_test.go b/partner_interconnect_attachments_test.go index 2d74e3c..9ca04d6 100644 --- a/partner_interconnect_attachments_test.go +++ b/partner_interconnect_attachments_test.go @@ -2,6 +2,7 @@ package godo import ( "encoding/json" + "io" "net/http" "testing" "time" @@ -27,6 +28,17 @@ var vInterconnectTestObj = &PartnerInterconnectAttachment{ CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC), } +var vInterconnectNoBGPTestObj = &PartnerInterconnectAttachment{ + ID: "880b7f98-f062-404d-b33c-458d545696f6", + Name: "my-new-partner-interconnect", + State: "ACTIVE", + ConnectionBandwidthInMbps: 50, + Region: "NYC", + NaaSProvider: "MEGAPORT", + VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"}, + CreatedAt: time.Date(2024, 12, 26, 21, 48, 40, 995304079, time.UTC), +} + var vInterconnectTestJSON = ` { "id":"880b7f98-f062-404d-b33c-458d545696f6", @@ -46,6 +58,22 @@ var vInterconnectTestJSON = ` } ` +var vInterconnectNoBGPTestJSON = ` + { + "id":"880b7f98-f062-404d-b33c-458d545696f6", + "name":"my-new-partner-interconnect", + "state":"ACTIVE", + "connection_bandwidth_in_mbps":50, + "region":"NYC", + "naas_provider":"MEGAPORT", + "vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"], + "created_at":"2024-12-26T21:48:40.995304079Z" + } +` + +const expectedCreateBodyNoBGP = `{"name":"my-new-partner-interconnect","connection_bandwidth_in_mbps":50,"region":"NYC","naas_provider":"MEGAPORT","vpc_ids":["f5a0c5e4-7537-47de-bb8d-46c766f89ffb"]} +` + func TestPartnerInterconnectAttachments_List(t *testing.T) { setup() defer teardown() @@ -134,6 +162,52 @@ func TestPartnerInterconnectAttachments_Create(t *testing.T) { require.Equal(t, want, got) } +func TestPartnerInterconnectAttachments_CreateNoBGP(t *testing.T) { + setup() + defer teardown() + + svc := client.PartnerInterconnectAttachments + path := "/v2/partner_interconnect/attachments" + want := vInterconnectNoBGPTestObj + req := &PartnerInterconnectAttachmentCreateRequest{ + Name: "my-new-partner-interconnect", + ConnectionBandwidthInMbps: 50, + Region: "NYC", + NaaSProvider: "MEGAPORT", + VPCIDs: []string{"f5a0c5e4-7537-47de-bb8d-46c766f89ffb"}, + } + jsonBlob := ` +{ + "partner_interconnect_attachment": +` + vInterconnectNoBGPTestJSON + ` +} +` + + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + if err != nil { + t.Fatal(err) + } + defer r.Body.Close() + + require.Equal(t, expectedCreateBodyNoBGP, string(body)) + + c := new(PartnerInterconnectAttachmentCreateRequest) + err = json.Unmarshal(body, c) + if err != nil { + t.Fatal(err) + } + + testMethod(t, r, http.MethodPost) + require.Equal(t, c, req) + w.Write([]byte(jsonBlob)) + }) + + got, _, err := svc.Create(ctx, req) + require.NoError(t, err) + require.Equal(t, want, got) +} + func TestPartnerInterconnectAttachments_Get(t *testing.T) { setup() defer teardown()