Skip to content

Commit

Permalink
Revisit waitUntilAwsVxcIsDeleted
Browse files Browse the repository at this point in the history
Renamed to waitUntilVxcIsConfigured since we can accomodate all VXC
types in the same check. Additionally, api.ProductAssociatedVxc now
exports the Type() method.

Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Jan 28, 2020
1 parent 07b63b2 commit 98460c9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
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
49 changes: 49 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 @@ -125,3 +128,49 @@ func waitUntilVxcIsConfigured(client *api.Client, productUid string, timeout tim
_, 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
}
43 changes: 1 addition & 42 deletions megaport/resource_megaport_aws_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,53 +227,12 @@ func resourceMegaportAwsVxcDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] VXC (%s) not found, deleting from state anyway", d.Id())
return nil
}
if err := waitUntilAwsVxcIsDeleted(cfg.Client, d.Id(), 5*time.Minute); err != nil {
if err := waitUntilVxcIsDeleted(cfg.Client, d.Id(), 5*time.Minute); err != nil {
return err
}
return nil
}

func waitUntilAwsVxcIsDeleted(client *api.Client, productUid string, timeout time.Duration) error {
var (
portUid string
vlanId uint64
)
if v, err := client.GetVxc(productUid); err != nil { // TODO we can probably use this for any kind of VXC
log.Printf("[ERROR] Could not retrieve VXC while waiting for deletion to finish: %v", err)
return err
} else {
portUid = v.AEnd.ProductUid
vlanId = v.AEnd.Vlan
}
scc := &resource.StateChangeConf{
Target: []string{api.ProductStatusDecommissioned},
Refresh: func() (interface{}, string, error) {
v, err := client.GetVxc(productUid) // TODO we can probably use this for any kind of VXC
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
}
ok, err := client.GetPortVlanIdAvailable(portUid, vlanId)
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
}

func waitUntilAwsVxcIsUpdated(client *api.Client, input *api.CloudVxcUpdateInput, timeout time.Duration) error {
scc := &resource.StateChangeConf{
Target: []string{api.ProductStatusConfigured, api.ProductStatusLive},
Expand Down

0 comments on commit 98460c9

Please sign in to comment.