Skip to content

Commit

Permalink
Update CloudEnvironment to support updating (#72)
Browse files Browse the repository at this point in the history
closes: #71
  • Loading branch information
mitch-hamm authored Nov 19, 2024
1 parent bee665c commit afc852b
Showing 1 changed file with 83 additions and 3 deletions.
86 changes: 83 additions & 3 deletions cloud/resource_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ func resourceCloudEnvironment() *schema.Resource {
// This is create event, so we don't need to check the diff.
return nil
}

old, new := diff.GetChange("default_gateway")
oldGateway := convertGateway(old)
newGateway := convertGateway(new)

if oldGateway.Access != newGateway.Access {
return fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
"The cloud environment does not support updating the gateway access, please recreate it")
}

if diff.HasChanges("organization") ||
diff.HasChanges("cloud_connection_name") ||
diff.HasChanges("region") ||
diff.HasChanges("network_id") ||
diff.HasChanges("network_cidr") {
return fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
"The cloud environment does not support updates, please recreate it")
"The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr. Please recreate it")
}
return nil
},
Expand Down Expand Up @@ -293,8 +303,78 @@ func resourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData, m
}

func resourceCloudEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
"The cloud environment does not support updates, please recreate it"))
namespace := d.Get("organization").(string)
waitForCompletion := d.Get("wait_for_completion").(bool)
name := strings.Split(d.Id(), "/")[1]

clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_UPDATE_CLOUD_ENVIRONMENT: %w", err))
}

old, new := d.GetChange("default_gateway")
oldGateway := convertGateway(old)
newGateway := convertGateway(new)

if oldGateway.Access != newGateway.Access {
return diag.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
"The cloud environment does not support updating the gateway access, please recreate it")
}

if d.HasChanges("organization") ||
d.HasChanges("cloud_connection_name") ||
d.HasChanges("region") ||
d.HasChanges("network_id") ||
d.HasChanges("network_cidr") {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: " +
"The cloud environment does not support updates on the attributes: organization, cloud_connection_name, region, network_id, network_cidr. Please recreate it"))
}

cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
}

cloudEnvironment.Spec.DefaultGateway = convertGateway(d.Get("default_gateway"))

if _, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Update(ctx, cloudEnvironment, metav1.UpdateOptions{
FieldManager: "terraform-update",
}); err != nil {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_CLOUD_ENVIRONMENT: %w", err))
}

ready := false

if waitForCompletion {
err = retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), retryUntilCloudEnvironmentIsProvisioned(ctx, clientSet, namespace, cloudEnvironment.GetObjectMeta().GetName()))
if err != nil {
return diag.FromErr(err)
}
} else {
for _, condition := range cloudEnvironment.Status.Conditions {
if condition.Type == "Ready" && condition.Status == "True" {
ready = true
}
}
}

if ready {
_ = d.Set("organization", namespace)
return resourceCloudEnvironmentRead(ctx, d, meta)
}

err = retry.RetryContext(ctx, 3*time.Minute, func() *retry.RetryError {
dia := resourceCloudEnvironmentRead(ctx, d, meta)
if dia.HasError() {
return retry.NonRetryableError(fmt.Errorf("ERROR_RETRY_READ_CLOUD_ENVIRONMENT: %s", dia[0].Summary))
}
return nil
})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_RETRY_READ_CLOUD_ENVIRONMENT: %w", err))
}

return nil
}

func resourceCloudEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down

0 comments on commit afc852b

Please sign in to comment.