Skip to content

Commit

Permalink
WIP: more upgrade stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
fantapop committed Mar 13, 2024
1 parent 274c637 commit 3eafceb
Show file tree
Hide file tree
Showing 14 changed files with 627 additions and 124 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0
github.com/hashicorp/terraform-plugin-go v0.22.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0
github.com/hashicorp/terraform-plugin-testing v1.7.0
github.com/stretchr/testify v1.7.2
)
Expand Down Expand Up @@ -46,7 +47,6 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.20.0 // indirect
github.com/hashicorp/terraform-json v0.21.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.33.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down Expand Up @@ -80,5 +80,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.61.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gav
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -289,8 +290,9 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
22 changes: 12 additions & 10 deletions internal/provider/client_ca_cert_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
sdk_resource "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

const (
Expand Down Expand Up @@ -115,7 +115,7 @@ func (r *clientCACertResource) Create(ctx context.Context, req resource.CreateRe
}

// poll until update completes
err = sdk_resource.RetryContext(ctx, clientCACertEndpointTimeout,
err = retry.RetryContext(ctx, clientCACertEndpointTimeout,
waitForClientCACertReady(ctx, plan.ID.ValueString(), r.provider.service, certInfo))
if err != nil {
resp.Diagnostics.AddError("Client CA Cert update timed out and may have failed", formatAPIErrorMessage(err))
Expand Down Expand Up @@ -194,7 +194,7 @@ func (r *clientCACertResource) Update(ctx context.Context, req resource.UpdateRe
}

// poll until update completes
err = sdk_resource.RetryContext(ctx, clientCACertEndpointTimeout,
err = retry.RetryContext(ctx, clientCACertEndpointTimeout,
waitForClientCACertReady(ctx, plan.ID.ValueString(), r.provider.service, certInfo))
if err != nil {
resp.Diagnostics.AddError("Client CA Cert update timed out and may have failed", formatAPIErrorMessage(err))
Expand Down Expand Up @@ -227,7 +227,7 @@ func (r *clientCACertResource) Delete(ctx context.Context, req resource.DeleteRe
}

// poll until update completes
err = sdk_resource.RetryContext(ctx, clientCACertEndpointTimeout,
err = retry.RetryContext(ctx, clientCACertEndpointTimeout,
waitForClientCACertReady(ctx, state.ID.ValueString(), r.provider.service, certInfo))
if err != nil {
resp.Diagnostics.AddError("Client CA Cert update timed out and may have failed", formatAPIErrorMessage(err))
Expand All @@ -242,23 +242,25 @@ func (r *clientCACertResource) ImportState(ctx context.Context, req resource.Imp
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func waitForClientCACertReady(ctx context.Context, clusterId string, cl client.Service, certInfo *client.ClientCACertInfo) sdk_resource.RetryFunc {
return func() *sdk_resource.RetryError {
func waitForClientCACertReady(
ctx context.Context, clusterId string, cl client.Service, certInfo *client.ClientCACertInfo,
) retry.RetryFunc {
return func() *retry.RetryError {
res, httpResp, err := cl.GetClientCACert(ctx, clusterId)
if err != nil {
if httpResp != nil && httpResp.StatusCode < http.StatusInternalServerError {
return sdk_resource.NonRetryableError(fmt.Errorf("error getting client ca cert: %s", formatAPIErrorMessage(err)))
return retry.NonRetryableError(fmt.Errorf("error getting client ca cert: %s", formatAPIErrorMessage(err)))
} else {
return sdk_resource.RetryableError(fmt.Errorf("encountered a server error while reading client ca cert status - trying again"))
return retry.RetryableError(fmt.Errorf("encountered a server error while reading client ca cert status - trying again"))
}
}

*certInfo = *res
switch status := certInfo.GetStatus(); status {
case client.CLIENTCACERTSTATUS_PENDING:
return sdk_resource.RetryableError(fmt.Errorf("client ca cert update is still pending"))
return retry.RetryableError(fmt.Errorf("client ca cert update is still pending"))
case client.CLIENTCACERTSTATUS_FAILED:
return sdk_resource.NonRetryableError(fmt.Errorf("client ca cert update failed"))
return retry.NonRetryableError(fmt.Errorf("client ca cert update failed"))
default:
return nil
}
Expand Down
24 changes: 12 additions & 12 deletions internal/provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
sdk_resource "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

const (
Expand Down Expand Up @@ -414,7 +414,7 @@ func (r *clusterResource) Create(
return
}

err = sdk_resource.RetryContext(ctx, clusterCreateTimeout,
err = retry.RetryContext(ctx, clusterCreateTimeout,
waitForClusterReadyFunc(ctx, clusterObj.Id, r.provider.service, clusterObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -636,7 +636,7 @@ func (r *clusterResource) Update(
return
}

err = sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, clusterObj.Id, r.provider.service, clusterObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -750,7 +750,7 @@ func (r *clusterResource) Update(
return
}

err = sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, clusterObj.Id, r.provider.service, clusterObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -955,27 +955,27 @@ func getManagedRegions(apiRegions *[]client.Region, plan []Region) []Region {

func waitForClusterReadyFunc(
ctx context.Context, id string, cl client.Service, cluster *client.Cluster,
) sdk_resource.RetryFunc {
return func() *sdk_resource.RetryError {
) retry.RetryFunc {
return func() *retry.RetryError {
apiCluster, httpResp, err := cl.GetCluster(ctx, id)
if err != nil {
if httpResp != nil && httpResp.StatusCode < http.StatusInternalServerError {
return sdk_resource.NonRetryableError(fmt.Errorf("error getting cluster: %s", formatAPIErrorMessage(err)))
return retry.NonRetryableError(fmt.Errorf("error getting cluster: %s", formatAPIErrorMessage(err)))
} else {
return sdk_resource.RetryableError(fmt.Errorf("encountered a server error while reading cluster status - trying again"))
return retry.RetryableError(fmt.Errorf("encountered a server error while reading cluster status - trying again"))
}
}
*cluster = *apiCluster
if cluster.State == client.CLUSTERSTATETYPE_CREATED {
return nil
}
if cluster.State == client.CLUSTERSTATETYPE_CREATION_FAILED {
return sdk_resource.NonRetryableError(fmt.Errorf("cluster creation failed"))
return retry.NonRetryableError(fmt.Errorf("cluster creation failed"))
}
if cluster.State == client.CLUSTERSTATETYPE_DELETED {
return sdk_resource.NonRetryableError(fmt.Errorf("cluster was deleted"))
return retry.NonRetryableError(fmt.Errorf("cluster was deleted"))
}
return sdk_resource.RetryableError(fmt.Errorf("cluster is not ready yet"))
return retry.RetryableError(fmt.Errorf("cluster is not ready yet"))
}
}

Expand All @@ -991,7 +991,7 @@ func waitForClusterLock(
diags.AddError("Couldn't retrieve cluster info", formatAPIErrorMessage(err))
return
}
err = sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, clusterObj.Id, s, clusterObj))
if err != nil {
diags.AddError("Cluster is not ready", err.Error())
Expand Down
20 changes: 10 additions & 10 deletions internal/provider/cmek_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
sdk_resource "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)

var cmekAttributes = map[string]schema.Attribute{
Expand Down Expand Up @@ -176,7 +176,7 @@ func (r *cmekResource) Create(
)
return
}
err = sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForCMEKReadyFunc(ctx, plan.ID.ValueString(), r.provider.service, cmekObj))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -307,7 +307,7 @@ func (r *cmekResource) Update(
return
}

err = sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, plan.ID.ValueString(), r.provider.service, cluster))
if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -332,7 +332,7 @@ func (r *cmekResource) Update(
}

var clusterInfo client.CMEKClusterInfo
err := sdk_resource.RetryContext(ctx, clusterUpdateTimeout,
err := retry.RetryContext(ctx, clusterUpdateTimeout,
waitForCMEKReadyFunc(ctx, plan.ID.ValueString(), r.provider.service, &clusterInfo))
if err != nil {
resp.Diagnostics.AddError(
Expand Down Expand Up @@ -437,14 +437,14 @@ func cmekRegionToClientSpec(region CMEKRegion) client.CMEKRegionSpecification {

func waitForCMEKReadyFunc(
ctx context.Context, clusterID string, cl client.Service, cmek *client.CMEKClusterInfo,
) sdk_resource.RetryFunc {
return func() *sdk_resource.RetryError {
) retry.RetryFunc {
return func() *retry.RetryError {
apiCMEK, httpResp, err := cl.GetCMEKClusterInfo(ctx, clusterID)
if err != nil {
if httpResp != nil && httpResp.StatusCode < http.StatusInternalServerError {
return sdk_resource.NonRetryableError(fmt.Errorf("error getting cmek: %s", formatAPIErrorMessage(err)))
return retry.NonRetryableError(fmt.Errorf("error getting cmek: %s", formatAPIErrorMessage(err)))
} else {
return sdk_resource.RetryableError(fmt.Errorf("encountered a server error while reading cmek status - trying again"))
return retry.RetryableError(fmt.Errorf("encountered a server error while reading cmek status - trying again"))
}
}
*cmek = *apiCMEK
Expand All @@ -458,9 +458,9 @@ func waitForCMEKReadyFunc(
client.CMEKSTATUS_ENABLE_FAILED,
client.CMEKSTATUS_REVOKE_FAILED,
client.CMEKSTATUS_ROTATE_FAILED:
return sdk_resource.NonRetryableError(fmt.Errorf("cmek update failed"))
return retry.NonRetryableError(fmt.Errorf("cmek update failed"))
default:
return sdk_resource.RetryableError(fmt.Errorf("cmek is not ready yet"))
return retry.RetryableError(fmt.Errorf("cmek is not ready yet"))
}
}
return nil
Expand Down
Loading

0 comments on commit 3eafceb

Please sign in to comment.