Skip to content

Commit

Permalink
add upgrade_type to cluster resource
Browse files Browse the repository at this point in the history
upgrade_type is now added for BASIC and STANDARD clusters.  It can be
used to dictate how the cluster handles major version upgrades.  Valid
values for upgrade_type are AUTOMATIC and MANUAL.  BASIC clusters
support only AUTOMATIC upgrades.
  • Loading branch information
fantapop committed Sep 18, 2024
1 parent fa25ec6 commit 4b47f49
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `upgrade_type` was added as an attribute for serverless clusters.
- New `STANDARD` clusters with provisioned serverless capacity.
- Ability to upgrade from `BASIC` plan to `STANDARD` plan.

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 @@ -79,6 +79,7 @@ Read-Only:

- `routing_id` (String) Cluster identifier in a connection string.
- `spend_limit` (Number, Deprecated) Spend limit in US cents.
- `upgrade_type` (String) Dictates the behavior of cockroach major version upgrades.
- `usage_limits` (Attributes) (see [below for nested schema](#nestedatt--serverless--usage_limits))

<a id="nestedatt--serverless--usage_limits"></a>
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ resource "cockroach_cluster" "standard" {
usage_limits = {
provisioned_virtual_cpus = 2
}
upgrade_type = "AUTOMATIC"
}
regions = [
{
Expand Down Expand Up @@ -132,6 +133,9 @@ Read-Only:
Optional:

- `spend_limit` (Number, Deprecated) Spend limit in US cents.
- `upgrade_type` (String) Dictates the behavior of cockroach major version upgrades. If plan type is 'BASIC', this attribute must be left empty or set to 'AUTOMATIC'. Allowed values are:
* MANUAL
* AUTOMATIC
- `usage_limits` (Attributes) (see [below for nested schema](#nestedatt--serverless--usage_limits))

Read-Only:
Expand Down
1 change: 1 addition & 0 deletions examples/resources/cockroach_cluster/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ resource "cockroach_cluster" "standard" {
usage_limits = {
provisioned_virtual_cpus = 2
}
upgrade_type = "AUTOMATIC"
}
regions = [
{
Expand Down
7 changes: 7 additions & 0 deletions examples/workflows/cockroach_standard_cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ variable "cloud_provider_regions" {
default = ["us-central1"]
}

variable "upgrade_type" {
type = string
nullable = false
default = "AUTOMATIC"
}

terraform {
required_providers {
cockroach = {
Expand All @@ -56,6 +62,7 @@ resource "cockroach_cluster" "example" {
usage_limits = {
provisioned_virtual_cpus = var.provisioned_virtual_cpus
}
upgrade_type = var.upgrade_type
}
regions = [for r in var.cloud_provider_regions : { name = r }]
}
Expand Down
1 change: 1 addition & 0 deletions internal/provider/allowlist_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func TestIntegrationAllowlistEntryResource(t *testing.T) {
Config: client.ClusterConfig{
Serverless: &client.ServerlessClusterConfig{
RoutingId: "routing-id",
UpgradeType: client.UPGRADETYPETYPE_AUTOMATIC,
},
},
Regions: []client.Region{
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 @@ -89,6 +89,10 @@ func (d *clusterDataSource) Schema(
Computed: true,
Description: "Cluster identifier in a connection string.",
},
"upgrade_type": schema.StringAttribute{
Computed: true,
Description: "Dictates the behavior of cockroach major version upgrades.",
},
},
},
"dedicated": schema.SingleNestedAttribute{
Expand Down
54 changes: 53 additions & 1 deletion internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ type clusterResource struct {
provider *provider
}

var AllowedUpgradeTypeTypeEnumValueStrings = func() []string {
var strings []string
for i := range client.AllowedUpgradeTypeTypeEnumValues {
strings = append(strings, string(client.AllowedUpgradeTypeTypeEnumValues[i]))
}
return strings
}()

var regionSchema = schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Expand Down Expand Up @@ -179,6 +187,16 @@ func (r *clusterResource) Schema(
},
Description: "Cluster identifier in a connection string.",
},
"upgrade_type": schema.StringAttribute{
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{stringvalidator.OneOf(AllowedUpgradeTypeTypeEnumValueStrings...)},
MarkdownDescription: "Dictates the behavior of cockroach major version upgrades. If plan type is 'BASIC', this attribute must be left empty or set to 'AUTOMATIC'. Allowed values are: " +
formatEnumMarkdownList(AllowedUpgradeTypeTypeEnumValueStrings),
},
},
},
"dedicated": schema.SingleNestedAttribute{
Expand Down Expand Up @@ -363,6 +381,16 @@ func (r *clusterResource) Create(
serverless.UsageLimits.ProvisionedVirtualCpus = usageLimits.ProvisionedVirtualCpus.ValueInt64Pointer()
}

upgradeTypeString := plan.ServerlessConfig.UpgradeType.ValueString()
if upgradeTypeString != "" {
upgradeType, err := client.NewUpgradeTypeTypeFromValue(upgradeTypeString)
if err != nil {
resp.Diagnostics.AddError("Invalid Upgrade Type", err.Error())
} else {
serverless.SetUpgradeType(*upgradeType)
}
}

clusterSpec.SetServerless(*serverless)
} else if plan.DedicatedConfig != nil {
dedicated := client.DedicatedClusterCreateSpecification{}
Expand Down Expand Up @@ -758,6 +786,30 @@ func (r *clusterResource) Update(
serverless.UsageLimits.StorageMibLimit = usageLimits.StorageMibLimit.ValueInt64Pointer()
serverless.UsageLimits.ProvisionedVirtualCpus = usageLimits.ProvisionedVirtualCpus.ValueInt64Pointer()
}

planUpgradeType := plan.ServerlessConfig.UpgradeType.ValueString()
stateUpgradeType := state.ServerlessConfig.UpgradeType.ValueString()

// Only add the upgrade_type if it changed.
if planUpgradeType != "" && planUpgradeType != stateUpgradeType {

upgradeTypePtr, err := client.NewUpgradeTypeTypeFromValue(plan.ServerlessConfig.UpgradeType.ValueString())
if err != nil {
resp.Diagnostics.AddError("Invalid value for upgrade_type", err.Error())
return
}
upgradeType := *upgradeTypePtr

// Check this combination explicitly to provide a better message
// than the ccapi is currently returning for this case.
if plan.Plan.ValueString() == string(client.PLANTYPE_BASIC) && upgradeType == client.UPGRADETYPETYPE_MANUAL {
resp.Diagnostics.AddError("Invalid value for upgrade_type", "plan type BASIC does not allow upgrade_type MANUAL")
return
}

serverless.SetUpgradeType(upgradeType)
}

clusterReq.SetServerless(*serverless)
} else if cfg := plan.DedicatedConfig; cfg != nil {
dedicated := client.NewDedicatedClusterUpdateSpecification()
Expand Down Expand Up @@ -823,7 +875,6 @@ func (r *clusterResource) Update(
}

traceAPICall("UpdateCluster")

clusterObj, _, err := r.provider.service.UpdateCluster(ctx, state.ID.ValueString(), clusterReq)
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -982,6 +1033,7 @@ func loadClusterToTerraformState(
if clusterObj.Config.Serverless != nil {
serverlessConfig := &ServerlessClusterConfig{
RoutingId: types.StringValue(clusterObj.Config.Serverless.RoutingId),
UpgradeType: types.StringValue(string(clusterObj.Config.Serverless.UpgradeType)),
}

if plan != nil && plan.ServerlessConfig != nil && IsKnown(plan.ServerlessConfig.SpendLimit) {
Expand Down
Loading

0 comments on commit 4b47f49

Please sign in to comment.