Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
Merge branch 'private_vxc'
Browse files Browse the repository at this point in the history
  • Loading branch information
alkar committed Jan 29, 2020
2 parents 3faaaea + f41d9a0 commit 4c42b2e
Show file tree
Hide file tree
Showing 14 changed files with 581 additions and 222 deletions.
34 changes: 34 additions & 0 deletions examples/megaport_private_vxc_basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
data "megaport_location" "foo" {
name_regex = "{{ .locationA }}"
}

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

data "megaport_location" "bar" {
name_regex = "{{ .locationB }}"
}

resource "megaport_port" "bar" {
name = "terraform_acctest_b_{{ .uid }}"
location_id = data.megaport_location.bar.id
speed = 1000
term = 1
}

resource "megaport_private_vxc" "foobar" {
name = "terraform_acctest_{{ .uid }}"
rate_limit = 100

a_end {
product_uid = megaport_port.foo.id
}

b_end {
product_uid = megaport_port.bar.id
}
}
37 changes: 37 additions & 0 deletions examples/megaport_private_vxc_full/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
data "megaport_location" "foo" {
name_regex = "{{ .locationA }}"
}

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

data "megaport_location" "bar" {
name_regex = "{{ .locationB }}"
}

resource "megaport_port" "bar" {
name = "terraform_acctest_b_{{ .uid }}"
location_id = data.megaport_location.bar.id
speed = 1000
term = 1
}

resource "megaport_private_vxc" "foobar" {
name = "terraform_acctest_{{ .uid }}"
rate_limit = 200
invoice_reference = "{{ .uid }}"

a_end {
product_uid = megaport_port.foo.id
vlan = {{ .vlanA }}
}

b_end {
product_uid = megaport_port.bar.id
vlan = {{ .vlanB }}
}
}
89 changes: 89 additions & 0 deletions megaport/acctest_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package megaport

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)

var (
testAccConfigTemplates = &template.Template{}
)

func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
r := make(map[string]interface{}, len(a))
for k, v := range a {
r[k] = v
}
for k, v := range b {
r[k] = v
}
return r
}

func testAccNewConfig(name string) (*template.Template, error) {
config := ""
if err := filepath.Walk(filepath.Join("../examples/", name), func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
r, err := filepath.Match("*.tf", f.Name())
if err != nil {
return err
}
if r {
c, err := ioutil.ReadFile(path)
if err != nil {
return err
}
config = config + string(c)
}
return nil
}); err != nil {
return nil, err
}
t, err := testAccConfigTemplates.New(name).Parse(config)
if err != nil {
return nil, err
}
return t, nil
}

type testAccConfig struct {
Config string
Name string
Step int
}

func (c testAccConfig) log() {
l := strings.Split(c.Config, "\n")
for i := range l {
l[i] = " " + l[i]
}
fmt.Printf("+++ CONFIG %q (step %d):\n%s\n", c.Name, c.Step, strings.Join(l, "\n"))
}

func newTestAccConfig(name string, values map[string]interface{}, step int) (*testAccConfig, error) {
var (
t *template.Template
err error
cfg = &strings.Builder{}
)
t = testAccConfigTemplates.Lookup(name)
if t == nil {
t, err = testAccNewConfig(name)
if err != nil {
return nil, err
}
}
if err := t.Execute(cfg, values); err != nil {
return nil, err
}
return &testAccConfig{Config: cfg.String(), Name: name, Step: step}, nil
}
2 changes: 1 addition & 1 deletion megaport/api/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
ProductTypePort = "MEGAPORT"
ProductTypeMCR1 = "MEGAPORT"
ProductTypeMCR2 = "MCR2"
ProductTypeVXC = "VXC"
ProductTypeVxc = "VXC"
)

// port: virtual = false, type = MEGAPORT
Expand Down
16 changes: 16 additions & 0 deletions megaport/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"encoding/json"
)

const (
VxcTypePrivate = "private"
VxcTypeAws = "aws"
VxcTypePartner = "partner"
)

// Some of the following types differ from examples seen in the documentation at
// https://dev.megaport.com. In some cases, the API responds with a different
// set of fields and/or field types and so the structs in this file match that,
Expand Down Expand Up @@ -270,6 +276,16 @@ type ProductAssociatedVxc struct {
VxcApproval ProductAssociatedVxcApproval
}

func (v *ProductAssociatedVxc) Type() string {
if v.AEnd.OwnerUid == v.BEnd.OwnerUid {
return VxcTypePrivate
}
if v.Resources.AwsVirtualInterface.ConnectType == "AWS" {
return VxcTypeAws
}
return VxcTypePartner
}

type ProductAssociatedVxcEnd struct {
LocationId uint64
Location string
Expand Down
30 changes: 9 additions & 21 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type PrivateVxcCreateInput struct {
}

func (v *PrivateVxcCreateInput) productType() string {
return ProductTypeVXC
return ProductTypeVxc
}

func (v *PrivateVxcCreateInput) toPayload() ([]byte, error) {
Expand Down Expand Up @@ -151,7 +151,7 @@ type PrivateVxcUpdateInput struct {
}

func (v *PrivateVxcUpdateInput) productType() string {
return ProductTypeVXC
return ProductTypeVxc
}

func (v *PrivateVxcUpdateInput) toPayload() ([]byte, error) {
Expand All @@ -174,18 +174,18 @@ func (c *Client) CreatePrivateVxc(v *PrivateVxcCreateInput) (*string, error) {
return &uid, nil
}

func (c *Client) GetPrivateVxc(uid string) (*ProductAssociatedVxc, error) { // TODO: rename struct
func (c *Client) GetVxc(uid string) (*ProductAssociatedVxc, error) { // TODO: rename struct
d := &ProductAssociatedVxc{}
err := c.get(uid, d)
return d, err
}

func (c *Client) UpdatePrivateVxc(v *PrivateVxcUpdateInput) error {
return c.update(*v.ProductUid, v)
func (c *Client) DeleteVxc(uid string) error {
return c.delete(uid)
}

func (c *Client) DeletePrivateVxc(uid string) error {
return c.delete(uid)
func (c *Client) UpdatePrivateVxc(v *PrivateVxcUpdateInput) error {
return c.update(*v.ProductUid, v)
}

type PartnerConfig interface {
Expand Down Expand Up @@ -266,7 +266,7 @@ func (v *CloudVxcCreateInput) toPayload() ([]byte, error) {
}

func (v *CloudVxcCreateInput) productType() string {
return ProductTypeVXC
return ProductTypeVxc
}

type CloudVxcUpdateInput struct {
Expand All @@ -279,7 +279,7 @@ type CloudVxcUpdateInput struct {
}

func (v *CloudVxcUpdateInput) productType() string {
return ProductTypeVXC
return ProductTypeVxc
}

func (v *CloudVxcUpdateInput) toPayload() ([]byte, error) {
Expand All @@ -302,18 +302,6 @@ func (c *Client) CreateCloudVxc(v *CloudVxcCreateInput) (*string, error) {
return &uid, nil
}

func (c *Client) GetCloudVxc(uid string) (*ProductAssociatedVxc, error) { // TODO: rename struct
d := &ProductAssociatedVxc{}
if err := c.get(uid, d); err != nil {
return nil, err
}
return d, nil
}

func (c *Client) UpdateCloudVxc(v *CloudVxcUpdateInput) error {
return c.update(*v.ProductUid, v)
}

func (c *Client) DeleteCloudVxc(uid string) error {
return c.delete(uid)
}
73 changes: 73 additions & 0 deletions megaport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package megaport

import (
"fmt"
"log"
"net"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)
Expand Down Expand Up @@ -101,3 +104,73 @@ func compareNillableStrings(a *string, b string) bool {
func compareNillableUints(a *uint64, b uint64) bool {
return a == nil || *a == b
}

func waitUntilVxcIsConfigured(client *api.Client, productUid string, timeout time.Duration) error {
scc := &resource.StateChangeConf{
Pending: []string{api.ProductStatusDeployable},
Target: []string{api.ProductStatusConfigured, api.ProductStatusLive},
Refresh: func() (interface{}, string, error) {
v, err := client.GetVxc(productUid)
if err != nil {
log.Printf("[ERROR] Could not retrieve VXC while waiting for setup to finish: %v", err)
return nil, "", err
}
if v == nil {
return nil, "", nil
}
return v, v.ProvisioningStatus, nil
},
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 5 * time.Second,
}
log.Printf("[INFO] Waiting for VXC (%s) to be configured", productUid)
_, err := scc.WaitForState()
return err
}

func waitUntilVxcIsDeleted(client *api.Client, productUid string, timeout time.Duration) error {
initial, err := client.GetVxc(productUid)
if err != nil {
log.Printf("[ERROR] Could not retrieve VXC while waiting for deletion to finish: %v", err)
return err
}
scc := &resource.StateChangeConf{
Target: []string{api.ProductStatusDecommissioned},
Refresh: func() (interface{}, string, error) {
v, err := client.GetVxc(productUid)
if err != nil {
log.Printf("[ERROR] Could not retrieve VXC while waiting for deletion to finish: %v", err)
return nil, "", err
}
if v == nil {
return nil, "", nil
}
if initial.AEnd.Vlan > 0 {
ok, err := client.GetPortVlanIdAvailable(initial.AEnd.ProductUid, initial.AEnd.Vlan)
if err != nil {
return v, "", err
}
if !ok {
return v, "", nil
}
}
if initial.BEnd.Vlan > 0 && initial.Type() == api.VxcTypePrivate {
ok, err := client.GetPortVlanIdAvailable(initial.BEnd.ProductUid, initial.BEnd.Vlan)
if err != nil {
return v, "", err
}
if !ok {
return v, "", nil
}
}
return v, v.ProvisioningStatus, nil
},
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 5 * time.Second,
}
log.Printf("[INFO] Waiting for VXC (%s) to be deleted", productUid)
_, err = scc.WaitForState()
return err
}
Loading

0 comments on commit 4c42b2e

Please sign in to comment.