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

Commit

Permalink
Merge branch 'csp_connection'
Browse files Browse the repository at this point in the history
  • Loading branch information
alkar committed Feb 5, 2020
2 parents 4df927f + 03b778e commit a2c106c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 34 deletions.
79 changes: 63 additions & 16 deletions megaport/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package api

import (
"encoding/json"
"fmt"
)

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

Expand Down Expand Up @@ -280,9 +282,12 @@ func (v *ProductAssociatedVxc) Type() string {
if v.AEnd.OwnerUid == v.BEnd.OwnerUid {
return VxcTypePrivate
}
if v.Resources.AwsVirtualInterface.ConnectType == "AWS" {
if c, ok := v.Resources.CspConnection.(*ProductAssociatedVxcResourcesCspConnectionAws); ok && c.ConnectType == vxcConnectTypeAws {
return VxcTypeAws
}
if c, ok := v.Resources.CspConnection.(*ProductAssociatedVxcResourcesCspConnectionGcp); ok && c.ConnectType == vxcConnectTypeGoogle {
return VxcTypeGcp
}
return VxcTypePartner
}

Expand All @@ -305,25 +310,51 @@ type ProductAssociatedVxcApproval struct {
}

type ProductAssociatedVxcResources struct {
AwsVirtualInterface ProductAssociatedVxcResourcesAwsVirtualInterface `json:"csp_connection"`
CspConnection interface{} `json:"csp_connection"`
}

type ProductAssociatedVxcResourcesAwsVirtualInterface struct {
type productAssociatedVxcResources ProductAssociatedVxcResources

func (pr *ProductAssociatedVxcResources) UnmarshalJSON(b []byte) (err error) {
r := productAssociatedVxcResources{}
v := struct {
CspConnection struct{ ConnectType string } `json:"csp_connection"`
}{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch t := v.CspConnection.ConnectType; t {
case vxcConnectTypeAws:
r.CspConnection = &ProductAssociatedVxcResourcesCspConnectionAws{}
case vxcConnectTypeGoogle:
r.CspConnection = &ProductAssociatedVxcResourcesCspConnectionGcp{}
case "":
default:
return fmt.Errorf("Cannot unmarshal resources: csp_connection has unknown connect type %q", t)
}
if err := json.Unmarshal(b, &r); err != nil {
return err
}
*pr = ProductAssociatedVxcResources(r)
return nil
}

type ProductAssociatedVxcResourcesCspConnectionAws struct {
Account string
AmazonAsn uint64 `json:"-"`
AmazonAddress string `json:"amazon_address"`
AmazonIpAddress string
AmazonAddress string `json:"Amazon_address"`
// Amazon_Asn uint64 `json:"Amazon_asn"`
// Amazon_Asn uint64 `json:"amazon_asn"`
Asn uint64 `json:"-"`
AuthKey string
// Auth_key string `json:"Auth_key"`
ConnectType string
CustomerAddress string `json:"customer_address"`
CustomerIpAddress string
// Customer_address string `json:"Customer_address"`
Id uint64 `json:"-"`
Name string
OwnerAccount string
PeerAsn uint64 `json:"-"`
Id uint64 `json:"-"`
Name string
OwnerAccount string
PeerAsn uint64 `json:"-"`
// Prefixes // null?
ResourceName string `json:"Resource_name"`
ResourceType string `json:"Resource_type"`
Expand All @@ -332,23 +363,23 @@ type ProductAssociatedVxcResourcesAwsVirtualInterface struct {
Vlan uint64 `json:"-"`
}

type productAssociatedVxcResourcesAwsVirtualInterfaceFloats struct {
type productAssociatedVxcResourcesCspConnectionAwsFloats struct {
AmazonAsn float64 `json:"amazonAsn"`
Asn float64 `json:"asn"`
Id float64 `json:"id"`
PeerAsn float64 `json:"peerAsn"`
Vlan float64 `json:"vlan"`
}

type productAssociatedVxcResourcesAwsVirtualInterface ProductAssociatedVxcResourcesAwsVirtualInterface
type productAssociatedVxcResourcesCspConnectionAws ProductAssociatedVxcResourcesCspConnectionAws

func (pr *ProductAssociatedVxcResourcesAwsVirtualInterface) UnmarshalJSON(b []byte) (err error) {
v := productAssociatedVxcResourcesAwsVirtualInterface{}
func (pr *ProductAssociatedVxcResourcesCspConnectionAws) UnmarshalJSON(b []byte) (err error) {
v := productAssociatedVxcResourcesCspConnectionAws{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*pr = ProductAssociatedVxcResourcesAwsVirtualInterface(v)
vf := productAssociatedVxcResourcesAwsVirtualInterfaceFloats{}
*pr = ProductAssociatedVxcResourcesCspConnectionAws(v)
vf := productAssociatedVxcResourcesCspConnectionAwsFloats{}
if err := json.Unmarshal(b, &vf); err != nil {
return err
}
Expand All @@ -360,6 +391,22 @@ func (pr *ProductAssociatedVxcResourcesAwsVirtualInterface) UnmarshalJSON(b []by
return nil
}

type ProductAssociatedVxcResourcesCspConnectionGcp struct {
Bandwidth uint64
Bandwidths []uint64
ConnectType string
CspName string `json:"csp_name"`
Megaports []ProductAssociatedVxcResourcesCspConnectionGcpMegaports
PairingKey string
ResourceName string `json:"resource_name"`
ResourceType string `json:"resource_type"`
}

type ProductAssociatedVxcResourcesCspConnectionGcpMegaports struct {
Port uint64
Vxc uint64
}

type MegaportCharges struct {
Currency string
DailyRate float64
Expand Down
25 changes: 25 additions & 0 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"strings"
)

const (
vxcConnectTypeAws = "AWS"
vxcConnectTypeGoogle = "GOOGLE"
)

type networkDesignInput interface {
toPayload() ([]byte, error)
productType() string
Expand Down Expand Up @@ -234,6 +239,26 @@ type vxcCreatePayloadPartnerConfigAws struct {
Type *string `json:"type,omitempty"`
}

type PartnerConfigGcp struct {
PairingKey *string
}

func (v *PartnerConfigGcp) connectType() string {
return "GOOGLE"
}

func (v *PartnerConfigGcp) toPayload() interface{} {
return &vxcCreatePayloadPartnerConfigGcp{
ConnectType: String(v.connectType()),
PairingKey: String(v.PairingKey),
}
}

type vxcCreatePayloadPartnerConfigGcp struct {
ConnectType *string `json:"connectType,omitempty"`
PairingKey *string `json:"pairingKey,omitempty"`
}

type CloudVxcCreateInput struct {
InvoiceReference *string
Name *string
Expand Down
2 changes: 1 addition & 1 deletion megaport/data_source_megaport_partner_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func dataSourceMegaportPartnerPort() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"AWS"}, false),
ValidateFunc: validation.StringInSlice([]string{"AWS", "GOOGLE"}, false),
},
"location_id": {
Type: schema.TypeInt,
Expand Down
36 changes: 19 additions & 17 deletions megaport/resource_megaport_aws_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ func resourceMegaportVxcAwsEndElem() *schema.Resource {
}
}

func flattenVxcEndAws(configProductUid string, v api.ProductAssociatedVxcEnd, r api.ProductAssociatedVxcResources) []interface{} {
func flattenVxcEndAws(configProductUid string, v *api.ProductAssociatedVxc) []interface{} {
cc := v.Resources.CspConnection.(*api.ProductAssociatedVxcResourcesCspConnectionAws)
return []interface{}{map[string]interface{}{
"product_uid": configProductUid,
"connected_product_uid": v.ProductUid,
"aws_connection_name": r.AwsVirtualInterface.Name,
"aws_account_id": r.AwsVirtualInterface.OwnerAccount,
"aws_ip_address": r.AwsVirtualInterface.AmazonIpAddress,
"bgp_auth_key": r.AwsVirtualInterface.AuthKey,
"customer_asn": int(r.AwsVirtualInterface.Asn),
"customer_ip_address": r.AwsVirtualInterface.CustomerIpAddress,
"type": strings.ToLower(r.AwsVirtualInterface.Type),
"connected_product_uid": v.BEnd.ProductUid,
"aws_connection_name": cc.Name,
"aws_account_id": cc.OwnerAccount,
"aws_ip_address": cc.AmazonIpAddress,
"bgp_auth_key": cc.AuthKey,
"customer_asn": int(cc.Asn),
"customer_ip_address": cc.CustomerIpAddress,
"type": strings.ToLower(cc.Type),
}}
}

Expand Down Expand Up @@ -157,7 +158,7 @@ func resourceMegaportAwsVxcRead(d *schema.ResourceData, m interface{}) error {
return err
}
puid := d.Get("b_end").([]interface{})[0].(map[string]interface{})["product_uid"].(string)
if err := d.Set("b_end", flattenVxcEndAws(puid, p.BEnd, p.Resources)); err != nil {
if err := d.Set("b_end", flattenVxcEndAws(puid, p)); err != nil {
return err
}
if err := d.Set("invoice_reference", p.CostCentre); err != nil {
Expand Down Expand Up @@ -263,25 +264,26 @@ func waitUntilAwsVxcIsUpdated(client *api.Client, input *api.CloudVxcUpdateInput
return nil, "", nil
}
pc := input.PartnerConfig.(*api.PartnerConfigAws)
if !compareNillableStrings(pc.AmazonIPAddress, v.Resources.AwsVirtualInterface.AmazonIpAddress) {
cc := v.Resources.CspConnection.(*api.ProductAssociatedVxcResourcesCspConnectionAws)
if !compareNillableStrings(pc.AmazonIPAddress, cc.AmazonIpAddress) {
return nil, "", nil
}
if !compareNillableStrings(pc.AwsAccountId, v.Resources.AwsVirtualInterface.OwnerAccount) {
if !compareNillableStrings(pc.AwsAccountId, cc.OwnerAccount) {
return nil, "", nil
}
if !compareNillableStrings(pc.AwsConnectionName, v.Resources.AwsVirtualInterface.Name) {
if !compareNillableStrings(pc.AwsConnectionName, cc.Name) {
return nil, "", nil
}
if !compareNillableStrings(pc.BGPAuthKey, v.Resources.AwsVirtualInterface.AuthKey) {
if !compareNillableStrings(pc.BGPAuthKey, cc.AuthKey) {
return nil, "", nil
}
if !compareNillableUints(pc.CustomerASN, v.Resources.AwsVirtualInterface.Asn) {
if !compareNillableUints(pc.CustomerASN, cc.Asn) {
return nil, "", nil
}
if !compareNillableStrings(pc.CustomerIPAddress, v.Resources.AwsVirtualInterface.CustomerIpAddress) {
if !compareNillableStrings(pc.CustomerIPAddress, cc.CustomerIpAddress) {
return nil, "", nil
}
if !compareNillableStrings(pc.Type, strings.ToLower(v.Resources.AwsVirtualInterface.Type)) {
if !compareNillableStrings(pc.Type, strings.ToLower(cc.Type)) {
return nil, "", nil
}
return v, v.ProvisioningStatus, nil
Expand Down

0 comments on commit a2c106c

Please sign in to comment.