Skip to content

Commit

Permalink
fixup! [CC-30640] allow manual version control for standard clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
fantapop committed Dec 5, 2024
1 parent e95da01 commit 990f5b0
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 7 deletions.
32 changes: 25 additions & 7 deletions internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,27 @@ func (r *clusterResource) ConfigValidators(_ context.Context) []resource.ConfigV
}
}

func derivePlanType(cluster *CockroachCluster) (client.PlanType, error) {
var planType client.PlanType
if IsKnown(cluster.Plan) {
planType = client.PlanType(cluster.Plan.ValueString())
} else if cluster.DedicatedConfig != nil {
planType = client.PLANTYPE_ADVANCED
} else if cluster.ServerlessConfig != nil {
if cluster.ServerlessConfig.UsageLimits != nil && IsKnown(cluster.ServerlessConfig.UsageLimits.ProvisionedVirtualCpus) {
planType = client.PLANTYPE_STANDARD
} else {
planType = client.PLANTYPE_BASIC
}
} else {
return "", fmt.Errorf("could not derive plan type, plan must contain either a ServerlessConfig or a DedicatedConfig")
}
if !planType.IsValid() {
return "", fmt.Errorf("invalid plan type %q", cluster.Plan.ValueString())
}
return planType, nil
}

func (r *clusterResource) Create(
ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse,
) {
Expand Down Expand Up @@ -439,13 +460,10 @@ func (r *clusterResource) Create(
}

if IsKnown(plan.CockroachVersion) {
var planType client.PlanType
if clusterSpec.Plan != nil {
planType = *clusterSpec.Plan
} else if serverless.UsageLimits != nil && serverless.UsageLimits.ProvisionedVirtualCpus != nil {
planType = client.PLANTYPE_STANDARD
} else {
planType = client.PLANTYPE_BASIC
planType, err := derivePlanType(&plan)
if err != nil {
resp.Diagnostics.AddError("Invalid Configuration", err.Error())
return
}

if planType == client.PLANTYPE_BASIC {
Expand Down
89 changes: 89 additions & 0 deletions internal/provider/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2287,3 +2287,92 @@ func TestIsDowngrade(t *testing.T) {
require.NoError(t, err)
require.False(t, upgrade)
}
func TestDerivePlanType(t *testing.T) {
tests := []struct {
name string
cluster CockroachCluster
expected client.PlanType
err error
}{
{
name: "Explicit Plan: Advanced",
cluster: CockroachCluster{
Plan: types.StringValue("ADVANCED"),
},
expected: client.PLANTYPE_ADVANCED,
err: nil,
},
{
name: "Explicit Plan: Standard",
cluster: CockroachCluster{
Plan: types.StringValue("STANDARD"),
},
expected: client.PLANTYPE_STANDARD,
err: nil,
},
{
name: "Explicit Plan: BASIC",
cluster: CockroachCluster{
Plan: types.StringValue("BASIC"),
},
expected: client.PLANTYPE_BASIC,
err: nil,
},
{
name: "Explicit Plan: Invalid",
cluster: CockroachCluster{
Plan: types.StringValue("asdf"),
},
expected: "",
err: fmt.Errorf(`invalid plan type "asdf"`),
},
{
name: "Dedicated Config",
cluster: CockroachCluster{
DedicatedConfig: &DedicatedClusterConfig{},
},
expected: client.PLANTYPE_ADVANCED,
err: nil,
},
{
name: "Serverless Config with Provisioned Resources",
cluster: CockroachCluster{
ServerlessConfig: &ServerlessClusterConfig{
UsageLimits: &UsageLimits{
ProvisionedVirtualCpus: types.Int64Value(4),
},
},
},
expected: client.PLANTYPE_STANDARD,
err: nil,
},
{
name: "Serverless Config without Provisioned Resources",
cluster: CockroachCluster{
ServerlessConfig: &ServerlessClusterConfig{
UsageLimits: &UsageLimits{},
},
},
expected: client.PLANTYPE_BASIC,
err: nil,
},
{
name: "Underivable Plan",
cluster: CockroachCluster{},
expected: "",
err: fmt.Errorf("could not derive plan type, plan must contain either a ServerlessConfig or a DedicatedConfig"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
planType, err := derivePlanType(&tt.cluster)
if tt.err != nil {
require.Equal(t, tt.err, err)
} else {
require.Equal(t, tt.expected, planType)
}
})
}
}

0 comments on commit 990f5b0

Please sign in to comment.