Skip to content

Commit

Permalink
Merge branch 'main' into feature/cloudconnexa_user_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sahaqaa authored Jul 26, 2024
2 parents 9b7d21f + 547fc7d commit 76b8d50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
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

0 comments on commit 76b8d50

Please sign in to comment.