civo / terraform-provider-civo

Terraform Civo provider
https://www.civo.com
Mozilla Public License 2.0
65 stars 51 forks source link

Make CIDR immutable for network resource #240

Open uzaxirr opened 1 week ago

uzaxirr commented 1 week ago

Fixes: #233

Since CIDR is immutable via API, applying the same over here. The CustomizeDiff function allows us to perform custom validation during the diff phase and can be used to detect changes to the cidr_v4 field and return an error if a change is detected.

This would throw an error in the planning phase itself.

Screenshot 2024-06-23 at 11 23 32 AM

The below change will throw error error when terraform apply is done hence this approach is not preferable.

func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
    apiClient := m.(*civogo.Client)

    // overwrite the region if is defined in the datasource
    if region, ok := d.GetOk("region"); ok {
        apiClient.Region = region.(string)
    }

    if d.HasChange("cidr_v4") {
        return diag.Errorf("the 'cidr_v4' field is immutable")
    }

    if d.HasChange("label") {
        log.Printf("[INFO] updating the network %s", d.Id())
        _, err := apiClient.RenameNetwork(d.Get("label").(string), d.Id())
        if err != nil {
            return diag.Errorf("[ERR] An error occurred while rename the network %s", d.Id())
        }
        return resourceNetworkRead(ctx, d, m)
    }
    return resourceNetworkRead(ctx, d, m)
}