From 187e09757fafb4d0a37fd9a648bb94a491155f57 Mon Sep 17 00:00:00 2001 From: Evan Rademacher <44209290+erademacher@users.noreply.github.com> Date: Thu, 10 Nov 2022 11:36:36 -0800 Subject: [PATCH] Fix inconsistent result error when using cockroach_version (#58) --- .../cockroach_dedicated_cluster/main.tf | 5 +++-- internal/provider/cluster_resource.go | 22 ++++++++++++++++++- internal/provider/cluster_resource_test.go | 13 ++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/examples/workflows/cockroach_dedicated_cluster/main.tf b/examples/workflows/cockroach_dedicated_cluster/main.tf index 8460bfd7..e812a892 100644 --- a/examples/workflows/cockroach_dedicated_cluster/main.tf +++ b/examples/workflows/cockroach_dedicated_cluster/main.tf @@ -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 diff --git a/internal/provider/cluster_resource.go b/internal/provider/cluster_resource.go index 9e5b73b0..0c04eb1a 100644 --- a/internal/provider/cluster_resource.go +++ b/internal/provider/cluster_resource.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "net/http" + "regexp" "sort" "github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client" @@ -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 @@ -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 diff --git a/internal/provider/cluster_resource_test.go b/internal/provider/cluster_resource_test.go index 78642ca1..4572fac8 100644 --- a/internal/provider/cluster_resource_test.go +++ b/internal/provider/cluster_resource_test.go @@ -138,11 +138,11 @@ 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, @@ -150,7 +150,7 @@ func TestIntegrationDedicatedClusterResource(t *testing.T) { }, Regions: []client.Region{ { - Name: "ap-south-1", + Name: "us-central1", NodeCount: 1, }, }, @@ -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 }] }