Skip to content

Commit

Permalink
update bgp to be a pointer (#777)
Browse files Browse the repository at this point in the history
* update bgp to be a pointer

* address feedback

* Update partner_interconnect_attachments.go

Co-authored-by: Ben Tranter <ben@bentranter.io>

* added test function

---------

Co-authored-by: Ben Tranter <ben@bentranter.io>
  • Loading branch information
apinonformoso and bentranter authored Jan 28, 2025
1 parent 48f8914 commit 975870d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
34 changes: 33 additions & 1 deletion partner_interconnect_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
74 changes: 74 additions & 0 deletions partner_interconnect_attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package godo

import (
"encoding/json"
"io"
"net/http"
"testing"
"time"
Expand All @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 975870d

Please sign in to comment.