Skip to content

Commit

Permalink
Merge branch 'extend-tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
alkar committed Jan 13, 2020
2 parents 5c8e605 + 7ed0486 commit 5df4e0e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 26 deletions.
10 changes: 5 additions & 5 deletions examples/megaport_aws_vxc_basic/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ data "megaport_partner_port" "aws" {
location_id = data.megaport_location.aws.id
}

data "megaport_location" "port" {
data "megaport_location" "foo" {
name_regex = "Telehouse North"
}

resource "megaport_port" "port" {
resource "megaport_port" "foo" {
name = "terraform_acctest_{{ .uid }}"
location_id = data.megaport_location.port.id
location_id = data.megaport_location.foo.id
speed = 1000
term = 1
}

resource "megaport_aws_vxc" "test" {
resource "megaport_aws_vxc" "foo" {
name = "terraform_acctest_{{ .uid }}"
rate_limit = 100
invoice_reference = "terraform_acctest_ref_{{ .uid }}"

a_end {
product_uid = megaport_port.port.id
product_uid = megaport_port.foo.id
}

b_end {
Expand Down
6 changes: 3 additions & 3 deletions examples/megaport_port_basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
data "megaport_location" "port" {
data "megaport_location" "foo" {
name_regex = "{{ .location }}"
}

resource "megaport_port" "test" {
resource "megaport_port" "foo" {
name = "terraform_acctest_{{ .uid }}"
location_id = data.megaport_location.port.id
location_id = data.megaport_location.foo.id
speed = 1000
term = 1
}
Expand Down
13 changes: 13 additions & 0 deletions examples/megaport_port_basic_forcenew/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "megaport_location" "foo" {
name_regex = "{{ .location }}"
}

resource "megaport_port" "foo" {
name = "terraform_acctest_{{ .uid }}"
location_id = data.megaport_location.foo.id
speed = 10000
term = 12
invoice_reference = "{{ .uid }}"
marketplace_visibility = "public"
}

13 changes: 13 additions & 0 deletions examples/megaport_port_basic_update/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "megaport_location" "foo" {
name_regex = "{{ .location }}"
}

resource "megaport_port" "foo" {
name = "terraform_acctest_{{ .uid }}"
location_id = data.megaport_location.foo.id
speed = 1000
term = 1
invoice_reference = "{{ .uid }}"
marketplace_visibility = "public"
}

5 changes: 4 additions & 1 deletion megaport/api/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (
// mcr2: virtual = false, type = MCR2

type portCreatePayload struct {
CreateDate *uint64 `json:"createDate,omitempty"` // TODO: need to fill in? :o
CreateDate *uint64 `json:"createDate,omitempty"` // TODO: need to fill in? :o
CostCentre *string `json:"costCentre"`
LagPortCount *uint64 `json:"lagPortCount,omitempty"` // TODO: Required: the number of ports in this LAG order (https://dev.megaport.com/#standard-api-orders-validate-lag-order)
LocationId *uint64 `json:"locationId"`
LocationUid *string `json:"locationUid,omitempty"` // TODO: null in example, is it a string? https://dev.megaport.com/#standard-api-orders-validate-port-order
Expand All @@ -44,6 +45,7 @@ type PortCreateInput struct {
Name *string
Speed *uint64
Term *uint64
InvoiceReference *string
}

func (v *PortCreateInput) productType() string {
Expand All @@ -53,6 +55,7 @@ func (v *PortCreateInput) productType() string {
func (v *PortCreateInput) toPayload() ([]byte, error) {
payload := []*portCreatePayload{{
LocationId: v.LocationId,
CostCentre: v.InvoiceReference,
PortSpeed: v.Speed,
ProductName: v.Name,
ProductType: String(ProductTypePort), // TODO
Expand Down
1 change: 1 addition & 0 deletions megaport/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type Product struct {
ContractStartDate uint64
ContractEndDate uint64
ContractTermMonths uint64
CostCentre string
CreateDate uint64
CreatedBy string
// LagId // TODO: haven't seen a value other than null
Expand Down
4 changes: 2 additions & 2 deletions megaport/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func testAccGetConfig(name string, values map[string]interface{}) (string, error
return cfg.String(), nil
}

func testAccLogConfig(cfg string) {
func testAccLogConfig(step int, cfg string) {
l := strings.Split(cfg, "\n")
for i := range l {
l[i] = " " + l[i]
}
fmt.Printf("+++ CONFIG:\n%s\n", strings.Join(l, "\n"))
fmt.Printf("+++ CONFIG (step %d):\n%s\n", step, strings.Join(l, "\n"))
}
6 changes: 3 additions & 3 deletions megaport/resource_megaport_aws_vxc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccMegaportAwsVxc_basic(t *testing.T) {
if err != nil {
t.Fatal(err)
}
testAccLogConfig(cfg)
testAccLogConfig(0, cfg)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -33,8 +33,8 @@ func TestAccMegaportAwsVxc_basic(t *testing.T) {
{
Config: cfg,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("megaport_port.port", &vxcBefore),
testAccCheckResourceExists("megaport_aws_vxc.test", &vxcBefore),
testAccCheckResourceExists("megaport_port.foo", &vxcBefore),
testAccCheckResourceExists("megaport_aws_vxc.foo", &vxcBefore),
),
},
},
Expand Down
16 changes: 10 additions & 6 deletions megaport/resource_megaport_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func resourceMegaportPort() *schema.Resource {
"speed": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"term": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"invoice_reference": {
Type: schema.TypeString,
Expand All @@ -48,7 +50,6 @@ func resourceMegaportPort() *schema.Resource {
Set: schema.HashResource(resourceMegaportPrivateVxc()),
},
"marketplace_visibility": resourceAttributePrivatePublic(),
// TODO: LAG ports
},
}
}
Expand All @@ -73,6 +74,9 @@ func resourceMegaportPortRead(d *schema.ResourceData, m interface{}) error {
if err := d.Set("term", p.ContractTermMonths); err != nil {
return err
}
if err := d.Set("invoice_reference", p.CostCentre); err != nil {
return err
}
if err := d.Set("associated_vxcs", schema.NewSet(schema.HashResource(resourceMegaportPrivateVxc()), flattenVxcList(p.AssociatedVxcs))); err != nil {
return err
}
Expand All @@ -84,7 +88,6 @@ func resourceMegaportPortRead(d *schema.ResourceData, m interface{}) error {
return err
}
}
//d.Set("invoice_reference", p.) // TODO: is this even exported?
return nil
}

Expand All @@ -96,6 +99,7 @@ func resourceMegaportPortCreate(d *schema.ResourceData, m interface{}) error {
Name: api.String(d.Get("name")),
Speed: api.Uint64FromInt(d.Get("speed")),
Term: api.Uint64FromInt(d.Get("term")),
InvoiceReference: api.String(d.Get("invoice_reference")),
})
if err != nil {
return err
Expand All @@ -107,10 +111,10 @@ func resourceMegaportPortCreate(d *schema.ResourceData, m interface{}) error {
func resourceMegaportPortUpdate(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
if err := cfg.Client.UpdatePort(&api.PortUpdateInput{
InvoiceReference: api.String(d.Get("invoice_reference")),
Name: api.String(d.Get("name")),
ProductUid: api.String(d.Id()),
//RateLimit: api.Uint64FromInt(d.Get("rate_limit")),
InvoiceReference: api.String(d.Get("invoice_reference")),
Name: api.String(d.Get("name")),
ProductUid: api.String(d.Id()),
MarketplaceVisibility: api.Bool(d.Get("marketplace_visibility") == "public"),
}); err != nil {
return err
}
Expand Down
63 changes: 57 additions & 6 deletions megaport/resource_megaport_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@ import (
)

func TestAccMegaportPort_basic(t *testing.T) {
var portBefore api.Product
var port, portUpdated, portNew api.Product
rName := "t" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

cfg, err := testAccGetConfig("megaport_port_basic", map[string]interface{}{
configValues := map[string]interface{}{
"uid": rName,
"location": "Telehouse North",
})
}

cfg, err := testAccGetConfig("megaport_port_basic", configValues)
if err != nil {
t.Fatal(err)
}
testAccLogConfig(cfg)
testAccLogConfig(0, cfg)
cfgUpdate, err := testAccGetConfig("megaport_port_basic_update", configValues)
if err != nil {
t.Fatal(err)
}
testAccLogConfig(1, cfgUpdate)
cfgForceNew, err := testAccGetConfig("megaport_port_basic_forcenew", configValues)
if err != nil {
t.Fatal(err)
}
testAccLogConfig(2, cfgForceNew)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -29,9 +40,49 @@ func TestAccMegaportPort_basic(t *testing.T) {
{
Config: cfg,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("megaport_port.test", &portBefore),
testAccCheckResourceExists("megaport_port.foo", &port),
resource.TestCheckResourceAttr("megaport_port.foo", "name", "terraform_acctest_"+rName),
resource.TestCheckResourceAttr("megaport_port.foo", "speed", "1000"),
resource.TestCheckResourceAttr("megaport_port.foo", "term", "1"),
resource.TestCheckResourceAttrPair("megaport_port.foo", "location_id", "data.megaport_location.foo", "id"),
resource.TestCheckResourceAttr("megaport_port.foo", "invoice_reference", ""),
resource.TestCheckNoResourceAttr("megaport_port.foo", "associated_vxcs"),
resource.TestCheckResourceAttr("megaport_port.foo", "marketplace_visibility", "private"),
),
},
{
Config: cfgUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("megaport_port.foo", &portUpdated),
resource.TestCheckResourceAttr("megaport_port.foo", "name", "terraform_acctest_"+rName),
resource.TestCheckResourceAttr("megaport_port.foo", "speed", "1000"),
resource.TestCheckResourceAttr("megaport_port.foo", "term", "1"),
resource.TestCheckResourceAttrPair("megaport_port.foo", "location_id", "data.megaport_location.foo", "id"),
resource.TestCheckResourceAttr("megaport_port.foo", "invoice_reference", rName),
resource.TestCheckNoResourceAttr("megaport_port.foo", "associated_vxcs"),
resource.TestCheckResourceAttr("megaport_port.foo", "marketplace_visibility", "public"),
),
},
{
Config: cfgForceNew,
Check: resource.ComposeTestCheckFunc(
testAccCheckResourceExists("megaport_port.foo", &portNew),
resource.TestCheckResourceAttr("megaport_port.foo", "name", "terraform_acctest_"+rName),
resource.TestCheckResourceAttr("megaport_port.foo", "speed", "10000"),
resource.TestCheckResourceAttr("megaport_port.foo", "term", "12"),
resource.TestCheckResourceAttrPair("megaport_port.foo", "location_id", "data.megaport_location.foo", "id"),
resource.TestCheckResourceAttr("megaport_port.foo", "invoice_reference", rName),
resource.TestCheckNoResourceAttr("megaport_port.foo", "associated_vxcs"),
resource.TestCheckResourceAttr("megaport_port.foo", "marketplace_visibility", "public"),
),
},
},
})

if port.ProductUid != portUpdated.ProductUid {
t.Errorf("TestAccMegaportPort_basic: expected the port to be updated but the resource ids differ")
}
if port.ProductUid == portNew.ProductUid {
t.Errorf("TestAccMegaportPort_basic: expected the port to be recreated but the resource ids are identical")
}
}

0 comments on commit 5df4e0e

Please sign in to comment.