feat: refactor Terraform ID handling and add user mapping functions
This commit is contained in:
parent
91913c3446
commit
546eafcb2f
4 changed files with 816 additions and 55 deletions
|
|
@ -27,7 +27,7 @@ type DataSourceModel struct {
|
|||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
||||
TerraformID types.String `tfsdk:"tf_id"`
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
|
|
@ -99,7 +99,7 @@ func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequ
|
|||
Optional: true,
|
||||
Computed: true,
|
||||
}
|
||||
s.Attributes["tf_id"] = schema.StringAttribute{
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
||||
"`database_id`\\\".\",",
|
||||
Optional: true,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
|
|
@ -24,7 +25,6 @@ import (
|
|||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
|
|
@ -34,12 +34,14 @@ var (
|
|||
_ resource.ResourceWithImportState = &databaseResource{}
|
||||
_ resource.ResourceWithModifyPlan = &databaseResource{}
|
||||
_ resource.ResourceWithIdentity = &databaseResource{}
|
||||
|
||||
errDatabaseNotFound = errors.New("database not found")
|
||||
)
|
||||
|
||||
// ResourceModel describes the resource data model.
|
||||
type ResourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
TerraformID types.String `tfsdk:"tf_id"`
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
||||
}
|
||||
|
||||
|
|
@ -126,7 +128,7 @@ var modifiersFileByte []byte
|
|||
// Schema defines the schema for the resource.
|
||||
func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
s := postgresflexalpha2.DatabaseResourceSchema(ctx)
|
||||
s.Attributes["tf_id"] = schema.StringAttribute{
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`,`database_id`\\\".\",",
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
|
|
@ -172,7 +174,6 @@ func (r *databaseResource) IdentitySchema(
|
|||
RequiredForImport: true,
|
||||
},
|
||||
"database_id": identityschema.Int64Attribute{
|
||||
// database id
|
||||
RequiredForImport: true,
|
||||
},
|
||||
},
|
||||
|
|
@ -508,64 +509,42 @@ func (r *databaseResource) ImportState(
|
|||
return
|
||||
}
|
||||
|
||||
databaseId, err := strconv.ParseInt(idParts[3], 10, 64)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error importing database",
|
||||
fmt.Sprintf("Invalid database_id format: %q. It must be a valid integer.", idParts[3]),
|
||||
)
|
||||
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])...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_id"), idParts[3])...)
|
||||
core.LogAndAddWarning(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Postgresflex database imported with empty password",
|
||||
"The database password is not imported as it is only available upon creation of a new database. The password field will be empty.",
|
||||
)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_id"), databaseId)...)
|
||||
|
||||
//TODO: Investigate if this logic is still required.
|
||||
//core.LogAndAddWarning(
|
||||
// ctx,
|
||||
// &resp.Diagnostics,
|
||||
// "Postgresflex database imported with empty password",
|
||||
// "The database password is not imported as it is only available upon creation of a new database. The password field will be empty.",
|
||||
//)
|
||||
|
||||
var identityData DatabaseResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
identityData.ProjectID = types.StringValue(idParts[0])
|
||||
identityData.Region = types.StringValue(idParts[1])
|
||||
identityData.InstanceID = types.StringValue(idParts[2])
|
||||
identityData.DatabaseID = types.Int64Value(databaseId)
|
||||
|
||||
resp.Diagnostics.Append(req.Identity.Set(ctx, &identityData)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("tf_id"),
|
||||
utils.BuildInternalTerraformId(
|
||||
identityData.ProjectID.ValueString(),
|
||||
identityData.Region.ValueString(),
|
||||
identityData.InstanceID.ValueString(),
|
||||
identityData.DatabaseID.String(),
|
||||
),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("project_id"),
|
||||
identityData.ProjectID.ValueString(),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx, path.Root("region"), identityData.Region.ValueString(),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("instance_id"),
|
||||
identityData.InstanceID.ValueString(),
|
||||
)...,
|
||||
)
|
||||
resp.Diagnostics.Append(
|
||||
resp.State.SetAttribute(
|
||||
ctx,
|
||||
path.Root("database_id"),
|
||||
identityData.DatabaseID.ValueInt64(),
|
||||
)...,
|
||||
)
|
||||
|
||||
tflog.Info(ctx, "Postgres Flex instance state imported")
|
||||
|
||||
}
|
||||
|
||||
var errDatabaseNotFound = errors.New("database not found")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue