diff --git a/stackit/internal/services/postgresflexalpha/instance/functions.go b/stackit/internal/services/postgresflexalpha/instance/functions.go index ead4e6f7..fedae080 100644 --- a/stackit/internal/services/postgresflexalpha/instance/functions.go +++ b/stackit/internal/services/postgresflexalpha/instance/functions.go @@ -55,13 +55,13 @@ func mapGetInstanceResponseToModel( } m.FlavorId = types.StringValue(resp.GetFlavorId()) - if m.Id.IsNull() || m.Id.IsUnknown() { - m.Id = utils.BuildInternalTerraformId( - m.ProjectId.ValueString(), - m.Region.ValueString(), - m.InstanceId.ValueString(), - ) - } + //if m.Id.IsNull() || m.Id.IsUnknown() { + m.Id = utils.BuildInternalTerraformId( + m.ProjectId.ValueString(), + m.Region.ValueString(), + m.InstanceId.ValueString(), + ) + //} m.InstanceId = types.StringValue(resp.Id) m.IsDeletable = types.BoolValue(resp.GetIsDeletable()) diff --git a/stackit/internal/services/postgresflexalpha/instance/resource.go b/stackit/internal/services/postgresflexalpha/instance/resource.go index 26eb9930..b6a6bfa7 100644 --- a/stackit/internal/services/postgresflexalpha/instance/resource.go +++ b/stackit/internal/services/postgresflexalpha/instance/resource.go @@ -341,7 +341,7 @@ func (r *instanceResource) Read( } } - if !model.Id.IsUnknown() && !model.Id.IsNull() { + if model.Id.IsUnknown() || model.Id.IsNull() { model.Id = utils.BuildInternalTerraformId(projectID, region, instanceID) } diff --git a/stackit/internal/services/sqlserverflexbeta/instance/functions.go b/stackit/internal/services/sqlserverflexbeta/instance/functions.go index 18ad8dc0..2444efb1 100644 --- a/stackit/internal/services/sqlserverflexbeta/instance/functions.go +++ b/stackit/internal/services/sqlserverflexbeta/instance/functions.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/types" + "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3beta1api" @@ -27,7 +28,7 @@ func mapResponseToModel( m.Edition = types.StringValue(string(resp.GetEdition())) m.Encryption = handleEncryption(ctx, m, resp) m.FlavorId = types.StringValue(resp.GetFlavorId()) - m.Id = types.StringValue(resp.GetId()) + m.Id = utils.BuildInternalTerraformId(m.ProjectId.ValueString(), m.Region.ValueString(), resp.GetId()) m.InstanceId = types.StringValue(resp.GetId()) m.IsDeletable = types.BoolValue(resp.GetIsDeletable()) m.Name = types.StringValue(resp.GetName()) diff --git a/stackit/internal/services/sqlserverflexbeta/instance/resource.go b/stackit/internal/services/sqlserverflexbeta/instance/resource.go index a824aabf..2aae314e 100644 --- a/stackit/internal/services/sqlserverflexbeta/instance/resource.go +++ b/stackit/internal/services/sqlserverflexbeta/instance/resource.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" - "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/stackit-sdk-go/core/config" @@ -32,7 +31,6 @@ var ( _ resource.ResourceWithConfigure = &instanceResource{} _ resource.ResourceWithImportState = &instanceResource{} _ resource.ResourceWithModifyPlan = &instanceResource{} - _ resource.ResourceWithIdentity = &instanceResource{} ) func NewInstanceResource() resource.Resource { @@ -47,12 +45,6 @@ type instanceResource struct { // resourceModel describes the resource data model. type resourceModel = sqlserverflexbetaResGen.InstanceModel -type InstanceResourceIdentityModel struct { - ProjectID types.String `tfsdk:"project_id"` - Region types.String `tfsdk:"region"` - InstanceID types.String `tfsdk:"instance_id"` -} - func (r *instanceResource) Metadata( _ context.Context, req resource.MetadataRequest, @@ -81,26 +73,6 @@ func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp.Schema = s } -func (r *instanceResource) IdentitySchema( - _ context.Context, - _ resource.IdentitySchemaRequest, - resp *resource.IdentitySchemaResponse, -) { - resp.IdentitySchema = identityschema.Schema{ - Attributes: map[string]identityschema.Attribute{ - "project_id": identityschema.StringAttribute{ - RequiredForImport: true, // must be set during import by the practitioner - }, - "region": identityschema.StringAttribute{ - RequiredForImport: true, // can be defaulted by the provider configuration - }, - "instance_id": identityschema.StringAttribute{ - RequiredForImport: true, // can be defaulted by the provider configuration - }, - }, - } -} - // Configure adds the provider configured client to the resource. func (r *instanceResource) Configure( ctx context.Context, @@ -190,9 +162,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = core.InitProviderContext(ctx) - projectId := data.ProjectId.ValueString() + projectID := data.ProjectId.ValueString() region := data.Region.ValueString() - ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "project_id", projectID) ctx = tflog.SetField(ctx, "region", region) // Generate API request body from model @@ -210,7 +182,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques // Create new Instance createResp, err := r.client.DefaultAPI.CreateInstanceRequest( ctx, - projectId, + projectID, region, ).CreateInstanceRequestPayload(*payload).Execute() if err != nil { @@ -220,24 +192,25 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques ctx = core.LogResponse(ctx) - instanceId := createResp.Id - data.InstanceId = types.StringValue(instanceId) + instanceID := createResp.Id + data.InstanceId = types.StringValue(instanceID) + data.Id = utils.BuildInternalTerraformId(projectID, region, instanceID) - identity := InstanceResourceIdentityModel{ - ProjectID: types.StringValue(projectId), - Region: types.StringValue(region), - InstanceID: types.StringValue(instanceId), - } - resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...) - if resp.Diagnostics.HasError() { - return - } + // Set data returned by API in id + resp.Diagnostics.Append( + resp.State.SetAttribute( + ctx, + path.Root("id"), + utils.BuildInternalTerraformId(projectID, region, instanceID), + )..., + ) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceID)...) waitResp, err := wait.CreateInstanceWaitHandler( ctx, r.client.DefaultAPI, - projectId, - instanceId, + projectID, + instanceID, region, ).SetSleepBeforeWait( 10 * time.Second, @@ -326,17 +299,6 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r return } - // Save identity into Terraform state - identity := InstanceResourceIdentityModel{ - ProjectID: types.StringValue(projectId), - Region: types.StringValue(region), - InstanceID: types.StringValue(instanceId), - } - resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...) - if resp.Diagnostics.HasError() { - return - } - // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) if resp.Diagnostics.HasError() { @@ -417,16 +379,6 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques return } - identity := InstanceResourceIdentityModel{ - ProjectID: types.StringValue(projectId), - Region: types.StringValue(region), - InstanceID: types.StringValue(instanceId), - } - resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...) - if resp.Diagnostics.HasError() { - return - } - // Save updated data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) if resp.Diagnostics.HasError() { @@ -445,21 +397,14 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques return } - // Read identity data - var identityData InstanceResourceIdentityModel - resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...) - if resp.Diagnostics.HasError() { - return - } - ctx = core.InitProviderContext(ctx) - projectId := identityData.ProjectID.ValueString() - region := identityData.Region.ValueString() + projectId := data.ProjectId.ValueString() + region := data.Region.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "region", region) - instanceId := identityData.InstanceID.ValueString() + instanceId := data.InstanceId.ValueString() ctx = tflog.SetField(ctx, "instance_id", instanceId) // Delete existing instance @@ -506,41 +451,24 @@ func (r *instanceResource) ImportState( ) { ctx = core.InitProviderContext(ctx) - if req.ID != "" { - idParts := strings.Split(req.ID, core.Separator) + idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { - core.LogAndAddError( - ctx, &resp.Diagnostics, - "Error importing instance", - fmt.Sprintf( - "Expected import identifier with format [project_id],[region],[instance_id] Got: %q", - req.ID, - ), - ) - return - } - - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) + if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + core.LogAndAddError( + ctx, &resp.Diagnostics, + "Error importing instance", + fmt.Sprintf( + "Expected import identifier with format [project_id],[region],[instance_id] Got: %q", + req.ID, + ), + ) return } - // If no ID is provided, attempt to read identity attributes from the import configuration - var identityData InstanceResourceIdentityModel - resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...) - if resp.Diagnostics.HasError() { - return - } - - projectId := identityData.ProjectID.ValueString() - region := identityData.Region.ValueString() - instanceId := identityData.InstanceID.ValueString() - - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), utils.BuildInternalTerraformId(idParts...).ValueString())...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) tflog.Info(ctx, "Sqlserverflexbeta instance state imported") }