Skip to content

Commit

Permalink
[CC-30497] add support for setting cidr_range on clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
fantapop committed Nov 14, 2024
1 parent e5dc4e9 commit ab4f78b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Setting and fetching of `cidr_range` is now available for GCP Advanced tier
clusters.

- Management of cluster backup settings is now supported using the
`backup_config` attribute on the `cockroach_cluster` resource. For more
information, refer to
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Read-Only:

Read-Only:

- `cidr_range` (String) The IPv4 range in CIDR format that is in use by the cluster. It is only set on GCP clusters and is otherwise empty.
- `disk_iops` (Number) Number of disk I/O operations per second that are permitted on each node in the cluster. Zero indicates the cloud provider-specific default.
- `machine_type` (String) Machine type identifier within the given cloud provider, ex. m6.xlarge, n2-standard-4.
- `memory_gib` (Number) Memory per node in GiB.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Optional:

Optional:

- `cidr_range` (String) The IPv4 range in CIDR format that will be used by the cluster. This is supported only on GCP, and must have a subnet mask no larger than /19. Defaults to "172.28.0.0/14". This cannot be changed after cluster creation.
- `disk_iops` (Number) Number of disk I/O operations per second that are permitted on each node in the cluster. Zero indicates the cloud provider-specific default.
- `machine_type` (String) Machine type identifier within the given cloud provider, e.g., m6.xlarge, n2-standard-4.
- `num_virtual_cpus` (Number) Number of virtual CPUs per node in the cluster.
Expand Down
1 change: 1 addition & 0 deletions examples/workflows/cockroach_advanced_cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ resource "cockroach_cluster" "example" {
dedicated = {
storage_gib = var.storage_gib
num_virtual_cpus = var.num_virtual_cpus
cidr_range = "172.28.0.0/14"
}
regions = [
for r in var.cloud_provider_regions : {
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func (d *clusterDataSource) Schema(
Computed: true,
Description: "Indicates whether private IP addresses are assigned to nodes. Required for CMEK and other advanced networking features.",
},
"cidr_range": schema.StringAttribute{
Computed: true,
Description: "The IPv4 range in CIDR format that is in use by the cluster. It is only set on GCP clusters and is otherwise empty.",
},
},
},
"regions": schema.ListNestedAttribute{
Expand Down
18 changes: 18 additions & 0 deletions internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ func (r *clusterResource) Schema(
boolplanmodifier.UseStateForUnknown(),
},
},
"cidr_range": schema.StringAttribute{
Optional: true,
Computed: true,
Description: "The IPv4 range in CIDR format that will be used by the cluster. This is supported only on GCP, and must have a subnet mask no larger than /19. Defaults to \"172.28.0.0/14\". This cannot be changed after cluster creation.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
"regions": schema.ListNestedAttribute{
Expand Down Expand Up @@ -472,6 +480,9 @@ func (r *clusterResource) Create(
visibilityPrivate := client.NETWORKVISIBILITYTYPE_PRIVATE
dedicated.NetworkVisibility = &visibilityPrivate
}
if cfg.CidrRange.ValueString() != "" {
dedicated.CidrRange = ptr(cfg.CidrRange.ValueString())
}
}
clusterSpec.SetDedicated(dedicated)
}
Expand Down Expand Up @@ -700,6 +711,12 @@ func (r *clusterResource) ModifyPlan(
"visibility isn't allowed. Please explicitly destroy this cluster before changing "+
"network visibility.")
}
if dedicated := plan.DedicatedConfig; dedicated != nil && dedicated.CidrRange != state.DedicatedConfig.CidrRange {
resp.Diagnostics.AddError("Cannot update cidr range",
"To prevent accidental deletion of data, changing a cluster's cidr range "+
"isn't allowed. Please explicitly destroy this cluster before changing "+
"cidr range.")
}
}

if req.Plan.Raw.IsNull() {
Expand Down Expand Up @@ -1238,6 +1255,7 @@ func loadClusterToTerraformState(
MemoryGib: types.Float64Value(float64(clusterObj.Config.Dedicated.MemoryGib)),
DiskIops: types.Int64Value(int64(clusterObj.Config.Dedicated.DiskIops)),
PrivateNetworkVisibility: types.BoolValue(clusterObj.GetNetworkVisibility() == client.NETWORKVISIBILITYTYPE_PRIVATE),
CidrRange: types.StringValue(clusterObj.CidrRange),
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/provider/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ func TestIntegrationDedicatedClusterResource(t *testing.T) {
NodeCount: 1,
},
},
CidrRange: "172.28.0.0/16",
}

upgradingCluster := initialCluster
Expand Down Expand Up @@ -1768,10 +1769,12 @@ func testDedicatedClusterResource(
resource.TestCheckResourceAttrSet(resourceName, "cloud_provider"),
resource.TestCheckResourceAttrSet(resourceName, "cockroach_version"),
resource.TestCheckResourceAttr(resourceName, "plan", "ADVANCED"),
resource.TestCheckResourceAttr(resourceName, "dedicated.cidr_range", "172.28.0.0/16"),
resource.TestCheckResourceAttr(dataSourceName, "name", clusterName),
resource.TestCheckResourceAttrSet(dataSourceName, "cloud_provider"),
resource.TestCheckResourceAttrSet(dataSourceName, "cockroach_version"),
resource.TestCheckResourceAttr(dataSourceName, "plan", "ADVANCED"),
resource.TestCheckResourceAttr(dataSourceName, "dedicated.cidr_range", "172.28.0.0/16"),
),
},
{
Expand Down Expand Up @@ -1880,6 +1883,7 @@ resource "cockroach_cluster" "test" {
dedicated = {
storage_gib = 15
num_virtual_cpus = %d
cidr_range = "172.28.0.0/16"
}
regions = [{
name: "us-central1"
Expand Down
1 change: 1 addition & 0 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type DedicatedClusterConfig struct {
MemoryGib types.Float64 `tfsdk:"memory_gib"`
DiskIops types.Int64 `tfsdk:"disk_iops"`
PrivateNetworkVisibility types.Bool `tfsdk:"private_network_visibility"`
CidrRange types.String `tfsdk:"cidr_range"`
}

type ServerlessClusterConfig struct {
Expand Down

0 comments on commit ab4f78b

Please sign in to comment.