Skip to content

Commit

Permalink
Implement VXC read
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitrios Karagiannis <[email protected]>
  • Loading branch information
alkar committed Oct 9, 2019
1 parent 239452b commit 0930404
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 28 deletions.
54 changes: 44 additions & 10 deletions megaport/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type ProductResources struct { // TODO: verify these are the only valid fields
// CrossConnect ProductResourcesCrossConnect `json:"cross_connect"` // TODO: only referenced in https://dev.megaport.com/#general-get-product-list
Interface ProductResourcesInterface
VirtualRouter ProductResourcesVirtualRouter `json:"virtual_router"`
VLL ProductResourcesVLL
}

type ProductResourcesInterface struct {
Expand All @@ -150,15 +151,15 @@ type ProductResourcesInterface struct {

type productResourcesInterface ProductResourcesInterface

func (pri *ProductResourcesInterface) UnmarshalJSON(b []byte) (err error) {
func (pr *ProductResourcesInterface) UnmarshalJSON(b []byte) (err error) {
v := productResourcesInterface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*pri = ProductResourcesInterface(v)
pri.Id = uint64(pri._id)
pri.PortSpeed = uint64(pri._portSpeed)
pri.Up = uint64(pri._up)
*pr = ProductResourcesInterface(v)
pr.Id = uint64(pr._id)
pr.PortSpeed = uint64(pr._portSpeed)
pr.Up = uint64(pr._up)
return nil
}

Expand All @@ -176,15 +177,48 @@ type ProductResourcesVirtualRouter struct {

type productResourcesVirtualRouter ProductResourcesVirtualRouter

func (pri *ProductResourcesVirtualRouter) UnmarshalJSON(b []byte) (err error) {
func (pr *productResourcesVirtualRouter) UnmarshalJSON(b []byte) (err error) {
v := productResourcesVirtualRouter{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*pri = ProductResourcesVirtualRouter(v)
pri.Id = uint64(pri._id)
pri.McrASN = uint64(pri._mcrASN)
pri.Speed = uint64(pri._speed)
*pr = productResourcesVirtualRouter(v)
pr.Id = uint64(pr._id)
pr.McrASN = uint64(pr._mcrASN)
pr.Speed = uint64(pr._speed)
return nil
}

type ProductResourcesVLL struct {
AVLan uint64 `json:"-"`
_aVLan float64 `json:"a_vlan"`
BVLan uint64 `json:"-"`
_bVLan float64 `json:"b_vlan"`
Description string
Id uint64 `json:"-"`
_id float64 `json:"id"`
Name string
RateLimit uint64 `json:"-"`
_rateLimit float64 `json:"rate_limit_mbps"`
ResourceName string `json:"resource_name"`
ResourceType string `json:"resource_type"`
Up uint64 `json:"-"`
_up float64 `json:"up"`
}

type productResourcesVLL ProductResourcesVLL

func (pr *productResourcesVLL) UnmarshalJSON(b []byte) (err error) {
v := productResourcesVLL{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
*pr = productResourcesVLL(v)
pr.AVLan = uint64(pr._aVLan)
pr.BVLan = uint64(pr._bVLan)
pr.Id = uint64(pr._id)
pr.RateLimit = uint64(pr._rateLimit)
pr.Up = uint64(pr._up)
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions megaport/api/vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ func (p *VxcService) Create(productAUid, productBUid, name string, vlanA, vlanB,
}
return d[0]["vxcJTechnicalServiceUid"].(string), nil
}

func (p *VxcService) Get(uid string) (*ProductAssociatedVxc, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/product/%s", p.c.BaseURL, uid), nil)
if err != nil {
return nil, err
}
data := &ProductAssociatedVxc{}
if err := p.c.do(req, &data); err != nil {
return nil, err
}
return data, nil
}
4 changes: 4 additions & 0 deletions megaport/resource_megaport_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ var (

vxcAEndResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"product_uid": {
Type: schema.TypeString,
Required: true,
},
"vlan": {
Type: schema.TypeInt,
Optional: true,
Expand Down
59 changes: 41 additions & 18 deletions megaport/resource_megaport_private_vxc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package megaport

import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"github.com/utilitywarehouse/terraform-provider-megaport/megaport/api"
)

func resourceMegaportPrivateVxc() *schema.Resource {
Expand All @@ -16,10 +19,9 @@ func resourceMegaportPrivateVxc() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"product_uid": {
"uid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Expand All @@ -30,13 +32,17 @@ func resourceMegaportPrivateVxc() *schema.Resource {
Required: true,
},
"a_end": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
MaxItems: 1,
ConfigMode: schema.SchemaConfigModeAttr,
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
//ConfigMode: schema.SchemaConfigModeAttr,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"product_uid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": {
Type: schema.TypeInt,
Optional: true,
Expand All @@ -51,15 +57,16 @@ func resourceMegaportPrivateVxc() *schema.Resource {
},
},
"b_end": {
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
ConfigMode: schema.SchemaConfigModeAttr,
Type: schema.TypeSet,
Required: true,
MaxItems: 1,
//ConfigMode: schema.SchemaConfigModeAttr,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"product_uid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": {
Type: schema.TypeInt,
Expand All @@ -78,6 +85,26 @@ func resourceMegaportPrivateVxc() *schema.Resource {
}

func resourceMegaportPrivateVxcRead(d *schema.ResourceData, m interface{}) error {
cfg := m.(*Config)
p, err := cfg.Client.Vxc.Get(d.Id())
if err != nil {
log.Printf("resourceMegaportPrivateVxcRead: %v", err)
d.SetId("")
return nil
}
log.Printf("%#v", p)
d.Set("uid", p.ProductUid)
d.Set("name", p.ProductName)
d.Set("limit", p.RateLimit)
d.Set("a_end", schema.NewSet(schema.HashResource(vxcAEndResource), []interface{}{map[string]interface{}{
"product_uid": p.AEnd.ProductUid,
"vlan": int(p.AEnd.Vlan),
}}))
d.Set("b_end", schema.NewSet(schema.HashResource(vxcBEndResource), []interface{}{map[string]interface{}{
"product_uid": p.BEnd.ProductUid,
"vlan": int(p.BEnd.Vlan),
}}))
//d.Set("invoice_reference", p.) // TODO: is this even exported?
return nil
}

Expand All @@ -90,15 +117,11 @@ func resourceMegaportPrivateVxcCreate(d *schema.ResourceData, m interface{}) err
if t := d.Get("b_end").(*schema.Set).List(); t != nil && len(t) == 1 {
b = t[0].(map[string]interface{})
}
var vlanA uint64
if a != nil {
vlanA = uint64(a["vlan"].(int))
}
uid, err := cfg.Client.Vxc.Create(
d.Get("product_uid").(string),
a["product_uid"].(string),
b["product_uid"].(string),
d.Get("name").(string),
vlanA,
uint64(a["vlan"].(int)),
uint64(b["vlan"].(int)),
uint64(d.Get("rate_limit").(int)),
)
Expand Down

0 comments on commit 0930404

Please sign in to comment.