Skip to content

Commit

Permalink
Rework private_vxc resource
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
alkar committed Oct 10, 2019
1 parent 9595dc5 commit ebe4c40
Showing 1 changed file with 29 additions and 52 deletions.
81 changes: 29 additions & 52 deletions megaport/resource_megaport_private_vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -124,15 +102,14 @@ 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 {
return nil
}

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 {
Expand Down

0 comments on commit ebe4c40

Please sign in to comment.