From ebe4c40367d80adb4cb4afe16b5dc7470e2a346d Mon Sep 17 00:00:00 2001 From: Dimitrios Karagiannis Date: Thu, 10 Oct 2019 14:48:34 +0100 Subject: [PATCH] Rework private_vxc resource Change sub-resources to TypeList. TypeSet is not suitable for this application as the checks are based on the hash and from the looks of it there is no way to have some attributes of the subresource cause updates and some to force a new resource. tl;dr - if the Set element's key (hash): - does not change, then terraform considers no changes are to be made to the the element - does change, then terraform will replace the element (which will have a new key) and if one if its attributes is set to `ForceNew`, the entire resource is going to be recreated: even if the value of the `ForceNew` attribute has not changed, its path in the state will have changed. Signed-off-by: Dimitrios Karagiannis --- megaport/resource_megaport_private_vxc.go | 81 ++++++++--------------- 1 file changed, 29 insertions(+), 52 deletions(-) diff --git a/megaport/resource_megaport_private_vxc.go b/megaport/resource_megaport_private_vxc.go index c6b24cf..6456451 100644 --- a/megaport/resource_megaport_private_vxc.go +++ b/megaport/resource_megaport_private_vxc.go @@ -7,6 +7,23 @@ import ( "github.com/utilitywarehouse/terraform-provider-megaport/megaport/api" ) +var ( + vxcEndResourceElem = &schema.Resource{ + Schema: map[string]*schema.Schema{ + "product_uid": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "vlan": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + }, + } +) + func resourceMegaportPrivateVxc() *schema.Resource { return &schema.Resource{ Create: resourceMegaportPrivateVxcCreate, @@ -28,49 +45,16 @@ func resourceMegaportPrivateVxc() *schema.Resource { Required: true, }, "a_end": { - Type: schema.TypeSet, + Type: schema.TypeList, 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, - Computed: true, - }, - "untag": { // TODO: what's the key for the API request? - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - }, - }, + Elem: vxcEndResourceElem, }, "b_end": { - Type: schema.TypeSet, + Type: schema.TypeList, 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, - Computed: true, - }, - }, - }, + Elem: vxcEndResourceElem, }, "invoice_reference": { Type: schema.TypeString, @@ -88,30 +72,24 @@ func resourceMegaportPrivateVxcRead(d *schema.ResourceData, m interface{}) error d.SetId("") return nil } - log.Printf("%#v", p) d.Set("name", p.ProductName) - d.Set("limit", p.RateLimit) - d.Set("a_end", schema.NewSet(schema.HashResource(vxcAEndResource), []interface{}{map[string]interface{}{ + d.Set("rate_limit", p.RateLimit) + d.Set("a_end", []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{}{ + }}) + d.Set("b_end", []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 } func resourceMegaportPrivateVxcCreate(d *schema.ResourceData, m interface{}) error { cfg := m.(*Config) - var a, b map[string]interface{} - if t := d.Get("a_end").(*schema.Set).List(); t != nil && len(t) == 1 { - a = t[0].(map[string]interface{}) - } - if t := d.Get("b_end").(*schema.Set).List(); t != nil && len(t) == 1 { - b = t[0].(map[string]interface{}) - } + a := d.Get("a_end").([]interface{})[0].(map[string]interface{}) + b := d.Get("b_end").([]interface{})[0].(map[string]interface{}) uid, err := cfg.Client.Vxc.Create( a["product_uid"].(string), b["product_uid"].(string), @@ -124,7 +102,7 @@ func resourceMegaportPrivateVxcCreate(d *schema.ResourceData, m interface{}) err return err } d.SetId(uid) - return nil + return resourceMegaportPrivateVxcRead(d, m) } func resourceMegaportPrivateVxcUpdate(d *schema.ResourceData, m interface{}) error { @@ -132,7 +110,6 @@ func resourceMegaportPrivateVxcUpdate(d *schema.ResourceData, m interface{}) err } func resourceMegaportPrivateVxcDelete(d *schema.ResourceData, m interface{}) error { - log.Printf("!!! DELETE") cfg := m.(*Config) err := cfg.Client.Vxc.Delete(d.Id()) if err != nil && err != api.ErrNotFound {