From 547fc7dc554ea94a8999575f3538c0865b22d3f0 Mon Sep 17 00:00:00 2001 From: Michael Fomenko Date: Fri, 26 Jul 2024 17:57:44 +0200 Subject: [PATCH] Added update method support for "cloudconnexa_connector" resource (#19) * update connector implemented * fixed updated fields list in resource connector * update connector implemented * fixed updated fields list in resource connector * init * Add small changes to allow provider to be initialized * Add description field to resourceConnectorCreate function * Remove duplicate description field * update network uses new update connector client method * updated client lib * fixed resourceNetworkUpdate * fixed resourceNetworkUpdate * fixed resourceNetworkUpdate --------- Co-authored-by: oleksandr.lytvyn Co-authored-by: Olexandr Lytvyn --- cloudconnexa/resource_connector.go | 24 +++++++++++++++++++----- cloudconnexa/resource_network.go | 17 ++++++++--------- go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/cloudconnexa/resource_connector.go b/cloudconnexa/resource_connector.go index c55925b..ef78191 100644 --- a/cloudconnexa/resource_connector.go +++ b/cloudconnexa/resource_connector.go @@ -16,6 +16,7 @@ func resourceConnector() *schema.Resource { CreateContext: resourceConnectorCreate, ReadContext: resourceConnectorRead, DeleteContext: resourceConnectorDelete, + UpdateContext: resourceConnectorUpdate, Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -23,21 +24,18 @@ func resourceConnector() *schema.Resource { "name": { Type: schema.TypeString, Required: true, - ForceNew: true, Description: "The connector display name.", }, "description": { Type: schema.TypeString, Optional: true, - ForceNew: true, Default: "Managed by Terraform", ValidateFunc: validation.StringLenBetween(1, 120), - Description: "The display description for this resource. Defaults to `Managed by Terraform`.", + Description: "The description for the UI. Defaults to `Managed by Terraform`.", }, "vpn_region_id": { Type: schema.TypeString, Required: true, - ForceNew: true, Description: "The id of the region where the connector will be deployed.", }, "network_item_type": { @@ -72,14 +70,30 @@ func resourceConnector() *schema.Resource { } } +func resourceConnectorUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*cloudconnexa.Client) + var diags diag.Diagnostics + connector := cloudconnexa.Connector{ + Id: d.Id(), + Name: d.Get("name").(string), + Description: d.Get("description").(string), + VpnRegionId: d.Get("vpn_region_id").(string), + } + _, err := c.Connectors.Update(connector) + if err != nil { + return append(diags, diag.FromErr(err)...) + } + return diags +} + func resourceConnectorCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { c := m.(*cloudconnexa.Client) var diags diag.Diagnostics name := d.Get("name").(string) + description := d.Get("description").(string) networkItemId := d.Get("network_item_id").(string) networkItemType := d.Get("network_item_type").(string) vpnRegionId := d.Get("vpn_region_id").(string) - description := d.Get("description").(string) connector := cloudconnexa.Connector{ Name: name, NetworkItemId: networkItemId, diff --git a/cloudconnexa/resource_network.go b/cloudconnexa/resource_network.go index f69d0e7..bfd3b02 100644 --- a/cloudconnexa/resource_network.go +++ b/cloudconnexa/resource_network.go @@ -283,6 +283,7 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interf newConnector := cloudconnexa.Connector{ Name: newSlice[0].(map[string]interface{})["name"].(string), VpnRegionId: newSlice[0].(map[string]interface{})["vpn_region_id"].(string), + Description: newSlice[0].(map[string]interface{})["description"].(string), NetworkItemType: "NETWORK", } _, err := c.Connectors.Create(newConnector, d.Id()) @@ -292,23 +293,21 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interf } else { oldMap := oldSlice[0].(map[string]interface{}) newMap := newSlice[0].(map[string]interface{}) - if oldMap["name"].(string) != newMap["name"].(string) || oldMap["vpn_region_id"].(string) != newMap["vpn_region_id"].(string) { + if oldMap["name"].(string) != newMap["name"].(string) || + oldMap["description"].(string) != newMap["description"].(string) || + oldMap["vpn_region_id"].(string) != newMap["vpn_region_id"].(string) { + newConnector := cloudconnexa.Connector{ + Id: oldMap["id"].(string), Name: newMap["name"].(string), VpnRegionId: newMap["vpn_region_id"].(string), + Description: newMap["description"].(string), NetworkItemType: "NETWORK", } - _, err := c.Connectors.Create(newConnector, d.Id()) + _, err := c.Connectors.Update(newConnector) if err != nil { return append(diags, diag.FromErr(err)...) } - if len(oldMap["id"].(string)) > 0 { - // This can sometimes happen when importing the resource - err = c.Connectors.Delete(oldMap["id"].(string), d.Id(), oldMap["network_item_type"].(string)) - if err != nil { - return append(diags, diag.FromErr(err)...) - } - } } } } diff --git a/go.mod b/go.mod index 3b33588..fd32a41 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/gruntwork-io/terratest v0.46.1 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 - github.com/openvpn/cloudconnexa-go-client/v2 v2.0.8 + github.com/openvpn/cloudconnexa-go-client/v2 v2.0.9 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index 546fc20..8294df8 100644 --- a/go.sum +++ b/go.sum @@ -485,8 +485,8 @@ github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zx github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= -github.com/openvpn/cloudconnexa-go-client/v2 v2.0.8 h1:67NXu2WqNnE05fhrq1HXbQKFMidhnd7ts6SFeuZSkLo= -github.com/openvpn/cloudconnexa-go-client/v2 v2.0.8/go.mod h1:udq5IDkgXvMO6mQUEFsLHzEyGGAduhO0jJvlb9f4JkE= +github.com/openvpn/cloudconnexa-go-client/v2 v2.0.9 h1:1clb6wRbCjJXUM41Jrbe8/0LBYGNMnCA77QIGAAiUpQ= +github.com/openvpn/cloudconnexa-go-client/v2 v2.0.9/go.mod h1:udq5IDkgXvMO6mQUEFsLHzEyGGAduhO0jJvlb9f4JkE= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=