Skip to content

Commit

Permalink
Fix inconsistent result error when using cockroach_version (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
erademacher committed Nov 10, 2022
1 parent bddfc6f commit 187e097
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 3 additions & 2 deletions examples/workflows/cockroach_dedicated_cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ provider "cockroach" {
}

resource "cockroach_cluster" "example" {
name = var.cluster_name
cloud_provider = var.cloud_provider
name = var.cluster_name
cloud_provider = var.cloud_provider
cockroach_version = "v22.0"
dedicated = {
storage_gib = var.storage_gib
machine_type = var.machine_type
Expand Down
22 changes: 21 additions & 1 deletion internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"sort"

"github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client"
Expand Down Expand Up @@ -470,6 +471,25 @@ func (r clusterResource) ImportState(ctx context.Context, req tfsdk.ImportResour
tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
}

// versionRE is the regexp that is used to verify that a version string is
// of the form "vMAJOR.MINOR.PATCH[-PRERELEASE][+METADATA]". This
// conforms to https://semver.org/spec/v2.0.0.html
var versionRE = regexp.MustCompile(
`^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-.]+)?(\+[0-9A-Za-z-.]+|)?$`,
// ^major ^minor ^patch ^preRelease ^metadata
)

func simplifyClusterVersion(version string) string {
parts := versionRE.FindStringSubmatch(version)
if parts == nil {
return version
}
if parts[4] != "" {
return "preview"
}
return fmt.Sprintf("v%s.%s", parts[1], parts[2])
}

// Since the API response will always sort regions by name, we need to
// resort the list, so it matches up with the plan. If the response and
// plan regions don't match up, the sort won't work right, but we can
Expand Down Expand Up @@ -503,7 +523,7 @@ func loadClusterToTerraformState(clusterObj *client.Cluster, state *CockroachClu
state.ID = types.String{Value: clusterObj.Id}
state.Name = types.String{Value: clusterObj.Name}
state.CloudProvider = types.String{Value: string(clusterObj.CloudProvider)}
state.CockroachVersion = types.String{Value: clusterObj.CockroachVersion}
state.CockroachVersion = types.String{Value: simplifyClusterVersion(clusterObj.CockroachVersion)}
state.Plan = types.String{Value: string(clusterObj.Plan)}
if clusterObj.AccountId == nil {
state.AccountId.Null = true
Expand Down
13 changes: 7 additions & 6 deletions internal/provider/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@ func TestIntegrationDedicatedClusterResource(t *testing.T) {
Name: clusterName,
CockroachVersion: "v22.1.0",
Plan: "DEDICATED",
CloudProvider: "AWS",
CloudProvider: "GCP",
State: "CREATED",
Config: client.ClusterConfig{
Dedicated: &client.DedicatedHardwareConfig{
MachineType: "m5.large",
MachineType: "n1-standard-2",
NumVirtualCpus: 2,
StorageGib: 15,
MemoryGib: 8,
},
},
Regions: []client.Region{
{
Name: "ap-south-1",
Name: "us-central1",
NodeCount: 1,
},
},
Expand Down Expand Up @@ -235,13 +235,14 @@ func getTestDedicatedClusterResourceConfig(name string) string {
return fmt.Sprintf(`
resource "cockroach_cluster" "dedicated" {
name = "%s"
cloud_provider = "AWS"
cloud_provider = "GCP"
cockroach_version = "v22.1"
dedicated = {
storage_gib = 15
machine_type = "m5.large"
machine_type = "n1-standard-2"
}
regions = [{
name: "ap-south-1"
name: "us-central1"
node_count: 1
}]
}
Expand Down

0 comments on commit 187e097

Please sign in to comment.