From 5a5a6d69ee9426b8d9e34ed4ea3d9040edc594ba Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Thu, 2 May 2024 23:20:07 +0700 Subject: [PATCH] chore: Update to Go v1.21.9 for compatibility with terraform-plugin-docs v0.19.2 (#41) * chore: Update to Go v1.21.9 for compatibility with terraform-plugin-docs v0.19.2 * docs: Update resource name * refactor: Use crypto/rand instead of deprecated math/rand --- .tool-versions | 2 +- docs/resources/webhook_registration.md | 8 +++---- .../resource.tf | 4 +--- go.mod | 4 +++- internal/provider/transformation/resource.go | 23 +++++++++++++------ .../provider/webhookregistration/resource.go | 2 +- 6 files changed, 25 insertions(+), 18 deletions(-) rename examples/resources/{webhook_registration => hookdeck_webhook_registration}/resource.tf (90%) diff --git a/.tool-versions b/.tool-versions index 1799f52..0e6ce87 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -golang 1.19 +golang 1.21.9 diff --git a/docs/resources/webhook_registration.md b/docs/resources/webhook_registration.md index 3d12439..2449596 100644 --- a/docs/resources/webhook_registration.md +++ b/docs/resources/webhook_registration.md @@ -1,21 +1,19 @@ --- # generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "webhook_registration Resource - terraform-provider-hookdeck" +page_title: "hookdeck_webhook_registration Resource - terraform-provider-hookdeck" subcategory: "" description: |- Webhook Resource --- -# webhook_registration (Resource) +# hookdeck_webhook_registration (Resource) Webhook Resource ## Example Usage ```terraform -resource "webhook_registration" "webhook_stripe" { - provider = hookdeck - +resource "hookdeck_webhook_registration" "webhook_stripe" { register = { request = { method = "POST" diff --git a/examples/resources/webhook_registration/resource.tf b/examples/resources/hookdeck_webhook_registration/resource.tf similarity index 90% rename from examples/resources/webhook_registration/resource.tf rename to examples/resources/hookdeck_webhook_registration/resource.tf index 1a9becb..e29db0e 100644 --- a/examples/resources/webhook_registration/resource.tf +++ b/examples/resources/hookdeck_webhook_registration/resource.tf @@ -1,6 +1,4 @@ -resource "webhook_registration" "webhook_stripe" { - provider = hookdeck - +resource "hookdeck_webhook_registration" "webhook_stripe" { register = { request = { method = "POST" diff --git a/go.mod b/go.mod index 8b3822a..59c0e1c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module terraform-provider-hookdeck -go 1.19 +go 1.21 + +toolchain go1.21.9 require ( github.com/hashicorp/terraform-plugin-docs v0.19.2 diff --git a/internal/provider/transformation/resource.go b/internal/provider/transformation/resource.go index c3c9726..0c80883 100644 --- a/internal/provider/transformation/resource.go +++ b/internal/provider/transformation/resource.go @@ -2,9 +2,8 @@ package transformation import ( "context" + "crypto/rand" "fmt" - "math/rand" - "time" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -137,11 +136,12 @@ func (r *transformationResource) Delete(ctx context.Context, req resource.Delete // for now, we'll update the transformation to a random ID in this template `deleted-${transformation_name}-${random}` // so users can still create a new transformation with the old name length := 10 // length of random key - rand.Seed(time.Now().UnixNano()) - b := make([]byte, length+2) - rand.Read(b) - randomizedName := "deleted-" + data.Name.ValueString() + "-" + fmt.Sprintf("%x", b)[2:length+2] - _, err := r.client.Transformation.Update(context.Background(), data.ID.ValueString(), &hookdeck.TransformationUpdateRequest{ + randomizedString, err := generateRandomBytes(length) + if err != nil { + resp.Diagnostics.AddError("Error deleting source", err.Error()) + } + randomizedName := "deleted-" + data.Name.ValueString() + "-" + fmt.Sprintf("%x", randomizedString)[2:length+2] + _, err = r.client.Transformation.Update(context.Background(), data.ID.ValueString(), &hookdeck.TransformationUpdateRequest{ Name: hookdeck.OptionalOrNull(&randomizedName), }) if err != nil { @@ -153,3 +153,12 @@ func (r *transformationResource) ImportState(ctx context.Context, req resource.I // Retrieve import ID and save to id attribute resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } + +func generateRandomBytes(length int) ([]byte, error) { + str := make([]byte, length) + _, err := rand.Read(str) + if err != nil { + return nil, err + } + return str, nil +} diff --git a/internal/provider/webhookregistration/resource.go b/internal/provider/webhookregistration/resource.go index ca6efd3..c7dd375 100644 --- a/internal/provider/webhookregistration/resource.go +++ b/internal/provider/webhookregistration/resource.go @@ -25,7 +25,7 @@ type webhookRegistrationResource struct { // Metadata returns the resource type name. func (r *webhookRegistrationResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { - resp.TypeName = "webhook_registration" + resp.TypeName = req.ProviderTypeName + "_webhook_registration" } // Configure adds the provider configured client to the resource.