From 4899cb128a7a3701e289b14b0324e7956889cbd5 Mon Sep 17 00:00:00 2001 From: Dimitrios Karagiannis Date: Thu, 13 Feb 2020 13:20:26 +0000 Subject: [PATCH] megaport_aws_vxc, megaport_gcp_vxc: fix imports When importing a resource, we have no access to the config and cannot properly setup the state with regard to the `product_uid` and `connected_product_uid` attributes. This change leaves the `product_uid` attribute empty if the value is unknown (which will happen when importing any of these two resources). Additionally, it introduces a DiffSuppressFunc which will force terraform to ignore changes to the attribute if its value in the state is empty. The result is that we have a clean plan after import, working around the load balancing behaviour for cloud partner ports. However, it also means that to modify the `product_uid` value of the B-End of a cloud VXC, the resource will have to be tainted. Signed-off-by: Dimitrios Karagiannis --- megaport/resource_megaport_aws_vxc.go | 8 +++++++- megaport/resource_megaport_gcp_vxc.go | 8 +++++++- website/docs/r/aws_vxc.html.md | 15 +++++++++++++++ website/docs/r/gcp_vxc.html.md | 15 +++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/megaport/resource_megaport_aws_vxc.go b/megaport/resource_megaport_aws_vxc.go index 6a9462f..1a6256f 100644 --- a/megaport/resource_megaport_aws_vxc.go +++ b/megaport/resource_megaport_aws_vxc.go @@ -58,6 +58,9 @@ func resourceMegaportVxcAwsEndElem() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return old == "" + }, }, "connected_product_uid": { Type: schema.TypeString, @@ -157,7 +160,10 @@ func resourceMegaportAwsVxcRead(d *schema.ResourceData, m interface{}) error { if err := d.Set("a_end", flattenVxcEnd(p.AEnd)); err != nil { return err } - puid := d.Get("b_end").([]interface{})[0].(map[string]interface{})["product_uid"].(string) + puid := "" + if v := d.Get("b_end").([]interface{}); len(v) > 0 { + puid = v[0].(map[string]interface{})["product_uid"].(string) + } if err := d.Set("b_end", flattenVxcEndAws(puid, p)); err != nil { return err } diff --git a/megaport/resource_megaport_gcp_vxc.go b/megaport/resource_megaport_gcp_vxc.go index 3ff9e26..6fff01e 100644 --- a/megaport/resource_megaport_gcp_vxc.go +++ b/megaport/resource_megaport_gcp_vxc.go @@ -56,6 +56,9 @@ func resourceMegaportVxcGcpEndElem() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return old == "" + }, }, "connected_product_uid": { Type: schema.TypeString, @@ -100,7 +103,10 @@ func resourceMegaportGcpVxcRead(d *schema.ResourceData, m interface{}) error { if err := d.Set("a_end", flattenVxcEnd(p.AEnd)); err != nil { return err } - puid := d.Get("b_end").([]interface{})[0].(map[string]interface{})["product_uid"].(string) + puid := "" + if v := d.Get("b_end").([]interface{}); len(v) > 0 { + puid = v[0].(map[string]interface{})["product_uid"].(string) + } if err := d.Set("b_end", flattenVxcEndGcp(puid, p)); err != nil { return err } diff --git a/website/docs/r/aws_vxc.html.md b/website/docs/r/aws_vxc.html.md index 55f7662..364f7eb 100644 --- a/website/docs/r/aws_vxc.html.md +++ b/website/docs/r/aws_vxc.html.md @@ -112,3 +112,18 @@ plan when the aforementioned situations occur. In addition to all arguments above, the following attributes are exported: * `id` - The unique product id of the port. + +## Import + +The AWS VXC can be imported using its product uid, like any other resource, +e.g.: + +``` +$ terraform import megaport_aws_vxc.foobar 1f33ea1d-ecc2-4fc3-a3a4-1e4774b04d76 +``` + +!> **Warning:** When a AWS VXC is imported, any changes to the B End +`product_uid` attribute are ignored. To force an update, you will need to +`taint` the resource. After re-creating the resource, it will start to behave as +expected and will compute the full diff. This is to work around the load +balancing behaviour mentioned in the Note above. diff --git a/website/docs/r/gcp_vxc.html.md b/website/docs/r/gcp_vxc.html.md index d4c9df9..c658451 100644 --- a/website/docs/r/gcp_vxc.html.md +++ b/website/docs/r/gcp_vxc.html.md @@ -97,3 +97,18 @@ plan when the aforementioned situations occur. In addition to all arguments above, the following attributes are exported: * `id` - The unique product id of the port. + +## Import + +The GCP VXC can be imported using its product uid, like any other resource, +e.g.: + +``` +$ terraform import megaport_gcp_vxc.foobar 1f33ea1d-ecc2-4fc3-a3a4-1e4774b04d76 +``` + +!> **Warning:** When a GCP VXC is imported, any changes to the B End +`product_uid` attribute are ignored. To force an update, you will need to +`taint` the resource. After re-creating the resource, it will start to behave as +expected and will compute the full diff. This is to work around the load +balancing behaviour mentioned in the Note above.