feat: enhance database resource model with additional attributes and refactor data source schema
This commit is contained in:
parent
c4891b770d
commit
1284bcc8a3
6 changed files with 66 additions and 80 deletions
|
|
@ -5,18 +5,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||||
|
"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/datasources_gen"
|
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
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/validate"
|
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||||
|
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||||
|
|
@ -25,10 +22,6 @@ import (
|
||||||
// DataSourceModel maps the data source schema data.
|
// DataSourceModel maps the data source schema data.
|
||||||
type DataSourceModel struct {
|
type DataSourceModel struct {
|
||||||
postgresflexalpha2.DatabaseModel
|
postgresflexalpha2.DatabaseModel
|
||||||
ProjectId types.String `tfsdk:"project_id"`
|
|
||||||
InstanceId types.String `tfsdk:"instance_id"`
|
|
||||||
Region types.String `tfsdk:"region"`
|
|
||||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
|
||||||
TerraformID types.String `tfsdk:"id"`
|
TerraformID types.String `tfsdk:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,34 +73,7 @@ func (r *databaseDataSource) Configure(
|
||||||
// Schema defines the schema for the data source.
|
// Schema defines the schema for the data source.
|
||||||
func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||||
|
|
||||||
s := postgresflexalpha2.DatabaseResourceSchema(ctx)
|
s := postgresflexalpha2.DatabaseDataSourceSchema(ctx)
|
||||||
s.Attributes["project_id"] = schema.StringAttribute{
|
|
||||||
Description: "STACKIT project ID to which the instance is associated.",
|
|
||||||
MarkdownDescription: "STACKIT project ID to which the instance is associated.",
|
|
||||||
Required: true,
|
|
||||||
Validators: []validator.String{
|
|
||||||
validate.UUID(),
|
|
||||||
validate.NoSeparator(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
s.Attributes["instance_id"] = schema.StringAttribute{
|
|
||||||
Description: "ID of the PostgresFlex instance.",
|
|
||||||
MarkdownDescription: "ID of the PostgresFlex instance.",
|
|
||||||
Required: true,
|
|
||||||
Validators: []validator.String{
|
|
||||||
validate.UUID(),
|
|
||||||
validate.NoSeparator(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
s.Attributes["region"] = schema.StringAttribute{
|
|
||||||
Description: "Region of the PostgresFlex instance.",
|
|
||||||
MarkdownDescription: "Region of the PostgresFlex instance.",
|
|
||||||
Optional: true,
|
|
||||||
}
|
|
||||||
s.Attributes["database_id"] = schema.Int64Attribute{
|
|
||||||
Description: "The ID of the database.",
|
|
||||||
Required: true,
|
|
||||||
}
|
|
||||||
s.Attributes["id"] = schema.StringAttribute{
|
s.Attributes["id"] = schema.StringAttribute{
|
||||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
||||||
"`database_id`\\\".\",",
|
"`database_id`\\\".\",",
|
||||||
|
|
@ -179,7 +145,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
||||||
projectId, region, instanceId string,
|
projectId, region, instanceId string,
|
||||||
diags *diag.Diagnostics,
|
diags *diag.Diagnostics,
|
||||||
) (*postgresflexalpha.ListDatabase, error) {
|
) (*postgresflexalpha.ListDatabase, error) {
|
||||||
isIdSet := !model.DatabaseID.IsNull() && !model.DatabaseID.IsUnknown()
|
isIdSet := !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown()
|
||||||
isNameSet := !model.Name.IsNull() && !model.Name.IsUnknown()
|
isNameSet := !model.Name.IsNull() && !model.Name.IsUnknown()
|
||||||
|
|
||||||
if (isIdSet && isNameSet) || (!isIdSet && !isNameSet) {
|
if (isIdSet && isNameSet) || (!isIdSet && !isNameSet) {
|
||||||
|
|
@ -191,7 +157,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
||||||
}
|
}
|
||||||
|
|
||||||
if isIdSet {
|
if isIdSet {
|
||||||
databaseId := model.DatabaseID.ValueInt64()
|
databaseId := model.DatabaseId.ValueInt64()
|
||||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||||
return getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
|
return getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@ package postgresflexalpha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||||
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// mapFields maps fields from a ListDatabase API response to a ResourceModel for the data source.
|
// mapFields maps fields from a ListDatabase API response to a ResourceModel for the data source.
|
||||||
|
|
@ -24,8 +26,8 @@ func mapFields(
|
||||||
}
|
}
|
||||||
|
|
||||||
var databaseId int64
|
var databaseId int64
|
||||||
if model.Id.ValueInt64() != 0 {
|
if model.DatabaseId.ValueInt64() != 0 {
|
||||||
databaseId = model.Id.ValueInt64()
|
databaseId = model.DatabaseId.ValueInt64()
|
||||||
} else if source.Id != nil {
|
} else if source.Id != nil {
|
||||||
databaseId = *source.Id
|
databaseId = *source.Id
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -33,10 +35,18 @@ func mapFields(
|
||||||
}
|
}
|
||||||
|
|
||||||
model.Id = types.Int64Value(databaseId)
|
model.Id = types.Int64Value(databaseId)
|
||||||
model.DatabaseID = types.Int64Value(databaseId)
|
model.DatabaseId = types.Int64Value(databaseId)
|
||||||
model.Name = types.StringValue(source.GetName())
|
model.Name = types.StringValue(source.GetName())
|
||||||
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||||
model.Region = types.StringValue(region)
|
model.Region = types.StringValue(region)
|
||||||
|
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
||||||
|
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
||||||
|
model.TerraformID = utils.BuildInternalTerraformId(
|
||||||
|
model.ProjectId.ValueString(),
|
||||||
|
region,
|
||||||
|
model.InstanceId.ValueString(),
|
||||||
|
strconv.FormatInt(databaseId, 10),
|
||||||
|
)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +73,7 @@ func mapResourceFields(source *postgresflexalpha.ListDatabase, model *ResourceMo
|
||||||
}
|
}
|
||||||
|
|
||||||
model.Id = types.Int64Value(databaseId)
|
model.Id = types.Int64Value(databaseId)
|
||||||
model.DatabaseID = types.Int64Value(databaseId)
|
model.DatabaseId = types.Int64Value(databaseId)
|
||||||
model.Name = types.StringValue(source.GetName())
|
model.Name = types.StringValue(source.GetName())
|
||||||
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||||
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||||
resource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMapFields(t *testing.T) {
|
func TestMapFields(t *testing.T) {
|
||||||
|
|
@ -41,12 +40,15 @@ func TestMapFields(t *testing.T) {
|
||||||
expected: expected{
|
expected: expected{
|
||||||
model: &DataSourceModel{
|
model: &DataSourceModel{
|
||||||
DatabaseModel: datasource.DatabaseModel{
|
DatabaseModel: datasource.DatabaseModel{
|
||||||
Id: types.Int64Value(1),
|
Id: types.Int64Value(1),
|
||||||
Name: types.StringValue("my-db"),
|
Name: types.StringValue("my-db"),
|
||||||
Owner: types.StringValue("my-owner"),
|
Owner: types.StringValue("my-owner"),
|
||||||
|
Region: types.StringValue("eu01"),
|
||||||
|
DatabaseId: types.Int64Value(1),
|
||||||
|
InstanceId: types.StringValue("my-instance"),
|
||||||
|
ProjectId: types.StringValue("my-project"),
|
||||||
},
|
},
|
||||||
Region: types.StringValue("eu01"),
|
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
||||||
DatabaseID: types.Int64Value(1),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -59,7 +61,9 @@ func TestMapFields(t *testing.T) {
|
||||||
},
|
},
|
||||||
model: &DataSourceModel{
|
model: &DataSourceModel{
|
||||||
DatabaseModel: datasource.DatabaseModel{
|
DatabaseModel: datasource.DatabaseModel{
|
||||||
Id: types.Int64Value(1),
|
Id: types.Int64Value(1),
|
||||||
|
ProjectId: types.StringValue("my-project"),
|
||||||
|
InstanceId: types.StringValue("my-instance"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
region: "eu01",
|
region: "eu01",
|
||||||
|
|
@ -69,10 +73,12 @@ func TestMapFields(t *testing.T) {
|
||||||
DatabaseModel: datasource.DatabaseModel{
|
DatabaseModel: datasource.DatabaseModel{
|
||||||
Id: types.Int64Value(1),
|
Id: types.Int64Value(1),
|
||||||
Name: types.StringValue("my-db"),
|
Name: types.StringValue("my-db"),
|
||||||
Owner: types.StringNull(),
|
Owner: types.StringNull(), DatabaseId: types.Int64Value(1),
|
||||||
|
Region: types.StringValue("eu01"),
|
||||||
|
InstanceId: types.StringValue("my-instance"),
|
||||||
|
ProjectId: types.StringValue("my-project"),
|
||||||
},
|
},
|
||||||
DatabaseID: types.Int64Value(1),
|
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
||||||
Region: types.StringValue("eu01"),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -146,12 +152,10 @@ func TestMapResourceFields(t *testing.T) {
|
||||||
},
|
},
|
||||||
expected: expected{
|
expected: expected{
|
||||||
model: &ResourceModel{
|
model: &ResourceModel{
|
||||||
DatabaseModel: resource.DatabaseModel{
|
Id: types.Int64Value(1),
|
||||||
Id: types.Int64Value(1),
|
Name: types.StringValue("my-db"),
|
||||||
Name: types.StringValue("my-db"),
|
Owner: types.StringValue("my-owner"),
|
||||||
Owner: types.StringValue("my-owner"),
|
DatabaseId: types.Int64Value(1),
|
||||||
},
|
|
||||||
DatabaseID: types.Int64Value(1),
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -200,10 +204,8 @@ func TestToCreatePayload(t *testing.T) {
|
||||||
name: "should convert model to payload",
|
name: "should convert model to payload",
|
||||||
given: given{
|
given: given{
|
||||||
model: &ResourceModel{
|
model: &ResourceModel{
|
||||||
DatabaseModel: resource.DatabaseModel{
|
Name: types.StringValue("my-db"),
|
||||||
Name: types.StringValue("my-db"),
|
Owner: types.StringValue("my-owner"),
|
||||||
Owner: types.StringValue("my-owner"),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expected: expected{
|
expected: expected{
|
||||||
|
|
|
||||||
|
|
@ -42,14 +42,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ResourceModel describes the resource data model.
|
// ResourceModel describes the resource data model.
|
||||||
type ResourceModel struct {
|
type ResourceModel = postgresflexalpha2.DatabaseModel
|
||||||
postgresflexalpha2.DatabaseModel
|
|
||||||
ProjectID types.String `tfsdk:"project_id"`
|
|
||||||
Region types.String `tfsdk:"region"`
|
|
||||||
InstanceID types.String `tfsdk:"instance_id"`
|
|
||||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
|
||||||
TerraformID types.String `tfsdk:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DatabaseResourceIdentityModel describes the resource's identity attributes.
|
// DatabaseResourceIdentityModel describes the resource's identity attributes.
|
||||||
type DatabaseResourceIdentityModel struct {
|
type DatabaseResourceIdentityModel struct {
|
||||||
|
|
@ -289,9 +282,21 @@ func (r *databaseResource) Create(
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write identity attributes to state
|
||||||
|
identityData.ProjectID = types.StringValue(projectId)
|
||||||
|
identityData.Region = types.StringValue(region)
|
||||||
|
identityData.InstanceID = types.StringValue(instanceId)
|
||||||
|
identityData.DatabaseID = types.Int64Value(databaseId)
|
||||||
|
|
||||||
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, &identityData)...)
|
||||||
|
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Set state to fully populated data
|
// Set state to fully populated data
|
||||||
diags = resp.State.Set(ctx, model)
|
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
|
||||||
resp.Diagnostics.Append(diags...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -589,8 +594,8 @@ func (r *databaseResource) extractIdentityData(
|
||||||
model ResourceModel,
|
model ResourceModel,
|
||||||
identity DatabaseResourceIdentityModel,
|
identity DatabaseResourceIdentityModel,
|
||||||
) (projectId, region, instanceId string, databaseId int64, err error) {
|
) (projectId, region, instanceId string, databaseId int64, err error) {
|
||||||
if !model.DatabaseID.IsNull() && !model.DatabaseID.IsUnknown() {
|
if !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown() {
|
||||||
databaseId = model.DatabaseID.ValueInt64()
|
databaseId = model.DatabaseId.ValueInt64()
|
||||||
} else {
|
} else {
|
||||||
if identity.DatabaseID.IsNull() || identity.DatabaseID.IsUnknown() {
|
if identity.DatabaseID.IsNull() || identity.DatabaseID.IsUnknown() {
|
||||||
return "", "", "", 0, fmt.Errorf("database_id not found in config")
|
return "", "", "", 0, fmt.Errorf("database_id not found in config")
|
||||||
|
|
@ -598,8 +603,8 @@ func (r *databaseResource) extractIdentityData(
|
||||||
databaseId = identity.DatabaseID.ValueInt64()
|
databaseId = identity.DatabaseID.ValueInt64()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !model.ProjectID.IsNull() && !model.ProjectID.IsUnknown() {
|
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
|
||||||
projectId = model.ProjectID.ValueString()
|
projectId = model.ProjectId.ValueString()
|
||||||
} else {
|
} else {
|
||||||
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
|
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
|
||||||
return "", "", "", 0, fmt.Errorf("project_id not found in config")
|
return "", "", "", 0, fmt.Errorf("project_id not found in config")
|
||||||
|
|
@ -616,8 +621,8 @@ func (r *databaseResource) extractIdentityData(
|
||||||
region = r.providerData.GetRegionWithOverride(identity.Region)
|
region = r.providerData.GetRegionWithOverride(identity.Region)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !model.InstanceID.IsNull() && !model.InstanceID.IsUnknown() {
|
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
|
||||||
instanceId = model.InstanceID.ValueString()
|
instanceId = model.InstanceId.ValueString()
|
||||||
} else {
|
} else {
|
||||||
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
|
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
|
||||||
return "", "", "", 0, fmt.Errorf("instance_id not found in config")
|
return "", "", "", 0, fmt.Errorf("instance_id not found in config")
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||||
|
"github.com/hashicorp/terraform-plugin-go/tftypes"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue