diff --git a/megaport/api/types.go b/megaport/api/types.go index 4e1949b..dc74dd6 100644 --- a/megaport/api/types.go +++ b/megaport/api/types.go @@ -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 { @@ -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 } @@ -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 } diff --git a/megaport/api/vxc.go b/megaport/api/vxc.go index 5b9bc6c..ef00da1 100644 --- a/megaport/api/vxc.go +++ b/megaport/api/vxc.go @@ -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 +} diff --git a/megaport/resource_megaport_port.go b/megaport/resource_megaport_port.go index e3686cd..52392c1 100644 --- a/megaport/resource_megaport_port.go +++ b/megaport/resource_megaport_port.go @@ -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, diff --git a/megaport/resource_megaport_private_vxc.go b/megaport/resource_megaport_private_vxc.go index 32215a4..68c751b 100644 --- a/megaport/resource_megaport_private_vxc.go +++ b/megaport/resource_megaport_private_vxc.go @@ -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 { @@ -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, @@ -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, @@ -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, @@ -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 } @@ -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)), )