Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added update method support for "cloudconnexa_connector" resource #19

Merged
merged 15 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions cloudconnexa/resource_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@ func resourceConnector() *schema.Resource {
CreateContext: resourceConnectorCreate,
ReadContext: resourceConnectorRead,
DeleteContext: resourceConnectorDelete,
UpdateContext: resourceConnectorUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"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": {
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions cloudconnexa/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)...)
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down