Skip to content

Commit

Permalink
Update timeouts and clean up constants (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
erademacher committed Nov 16, 2022
1 parent 2320087 commit d57f5f9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
16 changes: 11 additions & 5 deletions internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"regexp"
"sort"
"time"

"github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand All @@ -33,6 +34,11 @@ import (

type clusterResourceType struct{}

const (
clusterCreateTimeout = time.Hour
clusterUpdateTimeout = time.Hour * 2
)

func (r clusterResourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Cluster Resource",
Expand Down Expand Up @@ -278,7 +284,7 @@ func (r clusterResource) Create(ctx context.Context, req tfsdk.CreateResourceReq
return
}

err = resource.RetryContext(ctx, CREATE_TIMEOUT,
err = resource.RetryContext(ctx, clusterCreateTimeout,
waitForClusterCreatedFunc(ctx, clusterObj.Id, r.provider.service, clusterObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -415,7 +421,7 @@ func (r clusterResource) Update(ctx context.Context, req tfsdk.UpdateResourceReq
return
}

err = resource.RetryContext(ctx, CREATE_TIMEOUT,
err = resource.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterCreatedFunc(ctx, clusterObj.Id, r.provider.service, clusterObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -554,18 +560,18 @@ func loadClusterToTerraformState(clusterObj *client.Cluster, state *CockroachClu
func waitForClusterCreatedFunc(ctx context.Context, id string, cl client.Service, cluster *client.Cluster) resource.RetryFunc {
return func() *resource.RetryError {
apiCluster, httpResp, err := cl.GetCluster(ctx, id)
*cluster = *apiCluster
if err != nil {
if httpResp.StatusCode < http.StatusInternalServerError {
return resource.NonRetryableError(fmt.Errorf("error getting cluster: %s", formatAPIErrorMessage(err)))
} else {
return resource.RetryableError(fmt.Errorf("encountered a server error while reading cluster status - trying again"))
}
}
if string(cluster.State) == CLUSTERSTATETYPE_CREATED {
*cluster = *apiCluster
if cluster.State == client.CLUSTERSTATETYPE_CREATED {
return nil
}
if string(cluster.State) == CLUSTERSTATETYPE_CREATION_FAILED {
if cluster.State == client.CLUSTERSTATETYPE_CREATION_FAILED {
return resource.NonRetryableError(fmt.Errorf("cluster creation failed"))
}
return resource.RetryableError(fmt.Errorf("cluster is not ready yet"))
Expand Down
10 changes: 3 additions & 7 deletions internal/provider/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ package provider

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-framework/types"
)

const (
CockroachAPIKey string = "COCKROACH_API_KEY"
APIServerURLKey string = "COCKROACH_SERVER"
UserAgent string = "terraform-provider-cockroach"
CLUSTERSTATETYPE_CREATED string = "CREATED"
CLUSTERSTATETYPE_CREATION_FAILED string = "CREATION_FAILED"
CREATE_TIMEOUT = 60 * time.Minute
CockroachAPIKey string = "COCKROACH_API_KEY"
APIServerURLKey string = "COCKROACH_SERVER"
UserAgent string = "terraform-provider-cockroach"
)

type Region struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/private_endpoint_connection_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type privateEndpointConnectionResourceType struct{}
const (
// clusterID:endpointID
privateEndpointConnectionIDFmt = "%s:%s"
endpointConnectionCreateTimeout = time.Minute * 5
endpointConnectionCreateTimeout = time.Minute * 10
)

var privateEndpointConnectionIDRegex = regexp.MustCompile(fmt.Sprintf("^(%s):(.*)$", uuidRegex))
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/private_endpoint_services_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand All @@ -31,6 +32,8 @@ import (

type privateEndpointServicesResourceType struct{}

const endpointServicesCreateTimeout = time.Hour

func (n privateEndpointServicesResourceType) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "PrivateEndpointServices contains services that allow for VPC communication, either via PrivateLink (AWS) or Peering (GCP)",
Expand Down Expand Up @@ -155,7 +158,7 @@ func (n privateEndpointServicesResource) Create(ctx context.Context, req tfsdk.C
return
}
var services client.PrivateEndpointServices
err = resource.RetryContext(ctx, CREATE_TIMEOUT,
err = resource.RetryContext(ctx, endpointServicesCreateTimeout,
waitForEndpointServicesCreatedFunc(ctx, cluster.Id, n.provider.service, &services))
if err != nil {
resp.Diagnostics.AddError(
Expand Down

0 comments on commit d57f5f9

Please sign in to comment.