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

Make CIDR immutable for network resource #240

Merged
merged 3 commits into from
Jul 9, 2024
Merged

Make CIDR immutable for network resource #240

merged 3 commits into from
Jul 9, 2024

Conversation

uzaxirr
Copy link
Member

@uzaxirr uzaxirr commented Jun 23, 2024

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)
}

@uzaxirr uzaxirr self-assigned this Jun 23, 2024
@uzaxirr uzaxirr merged commit 11d10f8 into master Jul 9, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] changing CIDR range on civo_network resource does nothing
2 participants