Skip to content

Commit

Permalink
Address differences in private and partner VXCs
Browse files Browse the repository at this point in the history
Additionally, add unit tests for the `toPayload()` method to ensure they
produce the proper payload for the megaport API.

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Oct 16, 2019
1 parent 6b4baab commit 04b66a6
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 9 deletions.
25 changes: 16 additions & 9 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,23 @@ type vxcCreatePayload struct {
}

type vxcCreatePayloadAssociatedVxc struct {
ProductName string `json:"productName"`
RateLimit uint64 `json:"rateLimit"`
CostCentre string `json:"costCentre"`
AEnd interface{} `json:"aEnd"`
BEnd interface{} `json:"bEnd"`
ProductName string `json:"productName"`
RateLimit uint64 `json:"rateLimit"`
CostCentre string `json:"costCentre,omitempty"`
AEnd *vxcCreatePayloadVxcEndA `json:"aEnd,omitempty"`
BEnd interface{} `json:"bEnd"`
}

type vxcCreatePayloadPrivateVxcEnd struct {
type vxcCreatePayloadVxcEndA struct {
Vlan uint64 `json:"vlan"`
}

type vxcCreatePayloadPartnerVxcEnd struct {
type vxcCreatePayloadVxcEndBPrivate struct {
ProductUid string `json:"productUid"`
Vlan uint64 `json:"vlan,omitempty"`
}

type vxcCreatePayloadVxcEndBPartner struct {
ProductUid string `json:"productUid"`
}
type PrivateVxcCreateInput struct {
Expand All @@ -122,10 +127,12 @@ func (v *PrivateVxcCreateInput) toPayload() ([]byte, error) {
ProductName: v.Name,
RateLimit: v.RateLimit,
CostCentre: v.InvoiceReference,
AEnd: &vxcCreatePayloadPrivateVxcEnd{Vlan: v.VlanA},
BEnd: &vxcCreatePayloadPrivateVxcEnd{ProductUid: v.ProductUidB, Vlan: v.VlanB},
BEnd: &vxcCreatePayloadVxcEndBPrivate{ProductUid: v.ProductUidB, Vlan: v.VlanB},
}},
}}
if v.VlanA != 0 {
payload[0].AssociatedVxcs[0].AEnd = &vxcCreatePayloadVxcEndA{Vlan: v.VlanA}
}
return json.Marshal(payload)
}

Expand Down
133 changes: 133 additions & 0 deletions megaport/api/vxc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package api

import (
"bytes"
"strconv"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)

func TestPrivateVxcCreateInput_toPayload(t *testing.T) {
name := acctest.RandString(10)
ref := acctest.RandString(10)
rate := []uint64{100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}[acctest.RandIntRange(0, 10)]
rateString := strconv.FormatUint(rate, 10)
vlanA := uint64(acctest.RandIntRange(1, 4094))
vlanAString := strconv.FormatUint(vlanA, 10)
vlanB := uint64(acctest.RandIntRange(1, 4094))
vlanBString := strconv.FormatUint(vlanB, 10)
uuidA := uuid.New().String()
uuidB := uuid.New().String()
testCases := []struct {
i PrivateVxcCreateInput
o []byte
}{
{
PrivateVxcCreateInput{ // 0
InvoiceReference: ref,
Name: name,
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: rate,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 1
InvoiceReference: "",
Name: name,
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: rate,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 2
InvoiceReference: ref,
Name: "",
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: rate,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"","rateLimit":` + rateString + `,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 3
InvoiceReference: ref,
Name: name,
ProductUidA: "",
ProductUidB: uuidB,
RateLimit: rate,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 4
InvoiceReference: ref,
Name: name,
ProductUidA: uuidA,
ProductUidB: "",
RateLimit: rate,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 5
InvoiceReference: ref,
Name: name,
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: 0,
VlanA: vlanA,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":0,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 6
InvoiceReference: ref,
Name: name,
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: rate,
VlanA: 0,
VlanB: vlanB,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"costCentre":"` + ref + `","bEnd":{"productUid":"` + uuidB + `","vlan":` + vlanBString + `}}]}]`),
},
{
PrivateVxcCreateInput{ // 7
InvoiceReference: ref,
Name: name,
ProductUidA: uuidA,
ProductUidB: uuidB,
RateLimit: rate,
VlanA: vlanA,
VlanB: 0,
},
[]byte(`[{"productUid":"` + uuidA + `","associatedVxcs":[{"productName":"` + name + `","rateLimit":` + rateString + `,"costCentre":"` + ref + `","aEnd":{"vlan":` + vlanAString + `},"bEnd":{"productUid":"` + uuidB + `"}}]}]`),
},
}
for i, tc := range testCases {
p, err := tc.i.toPayload()
if err != nil {
t.Errorf("PrivateVxcCreateInput.toPayload (#%d): %w", i, err)
}
if !bytes.Equal(tc.o, p) {
t.Errorf("PrivateVxcCreateInput.toPayload (#%d):\n\tgot `%s`\n\texpected `%s`", i, p, tc.o)
}
}
}

0 comments on commit 04b66a6

Please sign in to comment.