From 712aca870e6686b21b6790edfa83203317ff01da Mon Sep 17 00:00:00 2001 From: Thulasiraj Komminar <39799163+thulasirajkomminar@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:59:40 +0200 Subject: [PATCH] bug fix: fixed pointer issue in token resource --- docs/resources/database.md | 2 +- internal/provider/database_resource.go | 9 +++++++-- internal/provider/token_resource.go | 11 +++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/resources/database.md b/docs/resources/database.md index c4bc9a0..ac1f410 100644 --- a/docs/resources/database.md +++ b/docs/resources/database.md @@ -17,7 +17,7 @@ Creates and manages a database. ### Required -- `name` (String) The name of the cluster database. The Length should be between `[ 1 .. 64 ]` characters. **Note:** After a database is deleted, you cannot [reuse](https://docs.influxdata.com/influxdb/cloud-dedicated/admin/databases/delete/#cannot-reuse-database-names) the same name for a new database. +- `name` (String) The name of the cluster database. The Length should be between `[ 1 .. 64 ]` characters. **Note:** Database names can't be updated. After a database is deleted, you cannot [reuse](https://docs.influxdata.com/influxdb/cloud-dedicated/admin/databases/delete/#cannot-reuse-database-names) the same name for a new database. ### Optional diff --git a/internal/provider/database_resource.go b/internal/provider/database_resource.go index 8fb090c..17ee67c 100644 --- a/internal/provider/database_resource.go +++ b/internal/provider/database_resource.go @@ -9,6 +9,8 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/komminarlabs/influxdb3" @@ -55,7 +57,10 @@ func (r *DatabaseResource) Schema(ctx context.Context, req resource.SchemaReques }, "name": schema.StringAttribute{ Required: true, - Description: "The name of the cluster database. The Length should be between `[ 1 .. 64 ]` characters. **Note:** After a database is deleted, you cannot [reuse](https://docs.influxdata.com/influxdb/cloud-dedicated/admin/databases/delete/#cannot-reuse-database-names) the same name for a new database.", + Description: "The name of the cluster database. The Length should be between `[ 1 .. 64 ]` characters. **Note:** Database names can't be updated. After a database is deleted, you cannot [reuse](https://docs.influxdata.com/influxdb/cloud-dedicated/admin/databases/delete/#cannot-reuse-database-names) the same name for a new database.", + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, Validators: []validator.String{ stringvalidator.LengthBetween(1, 64), }, @@ -257,7 +262,7 @@ func (r *DatabaseResource) Delete(ctx context.Context, req resource.DeleteReques return } - if deleteDatabasesResponse.StatusCode() != 200 { + if deleteDatabasesResponse.StatusCode() != 204 { resp.Diagnostics.AddError( "Error deleting database", fmt.Sprintf("Status: %s", deleteDatabasesResponse.Status()), diff --git a/internal/provider/token_resource.go b/internal/provider/token_resource.go index e7f3773..47785d8 100644 --- a/internal/provider/token_resource.go +++ b/internal/provider/token_resource.go @@ -3,7 +3,6 @@ package provider import ( "context" "fmt" - "unsafe" "github.com/google/uuid" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" @@ -119,9 +118,11 @@ func (r *TokenResource) Create(ctx context.Context, req resource.CreateRequest, // Generate API request body from plan var permissionsRequest []influxdb3.DatabaseTokenPermission for _, permission := range plan.Permissions { + resource := influxdb3.DatabaseTokenPermissionResource{} + resource.FromClusterDatabaseName(permission.Resource.ValueString()) permission := influxdb3.DatabaseTokenPermission{ Action: permission.Action.ValueStringPointer(), - Resource: (*influxdb3.DatabaseTokenPermissionResource)((unsafe.Pointer(&permission.Resource))), + Resource: &resource, } permissionsRequest = append(permissionsRequest, permission) } @@ -243,9 +244,11 @@ func (r *TokenResource) Update(ctx context.Context, req resource.UpdateRequest, // Generate API request body from plan var permissionsRequest []influxdb3.DatabaseTokenPermission for _, permission := range plan.Permissions { + resource := influxdb3.DatabaseTokenPermissionResource{} + resource.FromClusterDatabaseName(permission.Resource.ValueString()) permission := influxdb3.DatabaseTokenPermission{ Action: permission.Action.ValueStringPointer(), - Resource: (*influxdb3.DatabaseTokenPermissionResource)((unsafe.Pointer(&permission.Resource))), + Resource: &resource, } permissionsRequest = append(permissionsRequest, permission) } @@ -319,7 +322,7 @@ func (r *TokenResource) Delete(ctx context.Context, req resource.DeleteRequest, return } - if deleteTokenResponse.StatusCode() != 200 { + if deleteTokenResponse.StatusCode() != 204 { resp.Diagnostics.AddError( "Error deleting token", fmt.Sprintf("Status: %s", deleteTokenResponse.Status()),