fix: change identity handling for user & database (#50)

## Description

<!-- **Please link some issue here describing what you are trying to achieve.**

In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->

relates to #1234

## Checklist

- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)

Reviewed-on: #50
Reviewed-by: Marcel_Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
Co-committed-by: Andre Harms <andre.harms@stackit.cloud>
This commit is contained in:
Andre_Harms 2026-02-11 15:39:20 +00:00 committed by Marcel_Henselin
parent 86fc98461c
commit 131e1700bb
Signed by: tf-provider.git.onstackit.cloud
GPG key ID: 6D7E8A1ED8955A9C
14 changed files with 278 additions and 241 deletions

View file

@ -23,11 +23,6 @@ func DatabasesDataSourceSchema(ctx context.Context) schema.Schema {
"databases": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"created": schema.StringAttribute{
Computed: true,
Description: "The data when the database was created in RFC3339 format.",
MarkdownDescription: "The data when the database was created in RFC3339 format.",
},
"id": schema.Int64Attribute{
Computed: true,
Description: "The id of the database.",
@ -169,24 +164,6 @@ func (t DatabasesType) ValueFromObject(ctx context.Context, in basetypes.ObjectV
attributes := in.Attributes()
createdAttribute, ok := attributes["created"]
if !ok {
diags.AddError(
"Attribute Missing",
`created is missing from object`)
return nil, diags
}
createdVal, ok := createdAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`created expected to be basetypes.StringValue, was: %T`, createdAttribute))
}
idAttribute, ok := attributes["id"]
if !ok {
@ -246,11 +223,10 @@ func (t DatabasesType) ValueFromObject(ctx context.Context, in basetypes.ObjectV
}
return DatabasesValue{
Created: createdVal,
Id: idVal,
Name: nameVal,
Owner: ownerVal,
state: attr.ValueStateKnown,
Id: idVal,
Name: nameVal,
Owner: ownerVal,
state: attr.ValueStateKnown,
}, diags
}
@ -317,24 +293,6 @@ func NewDatabasesValue(attributeTypes map[string]attr.Type, attributes map[strin
return NewDatabasesValueUnknown(), diags
}
createdAttribute, ok := attributes["created"]
if !ok {
diags.AddError(
"Attribute Missing",
`created is missing from object`)
return NewDatabasesValueUnknown(), diags
}
createdVal, ok := createdAttribute.(basetypes.StringValue)
if !ok {
diags.AddError(
"Attribute Wrong Type",
fmt.Sprintf(`created expected to be basetypes.StringValue, was: %T`, createdAttribute))
}
idAttribute, ok := attributes["id"]
if !ok {
@ -394,11 +352,10 @@ func NewDatabasesValue(attributeTypes map[string]attr.Type, attributes map[strin
}
return DatabasesValue{
Created: createdVal,
Id: idVal,
Name: nameVal,
Owner: ownerVal,
state: attr.ValueStateKnown,
Id: idVal,
Name: nameVal,
Owner: ownerVal,
state: attr.ValueStateKnown,
}, diags
}
@ -470,20 +427,18 @@ func (t DatabasesType) ValueType(ctx context.Context) attr.Value {
var _ basetypes.ObjectValuable = DatabasesValue{}
type DatabasesValue struct {
Created basetypes.StringValue `tfsdk:"created"`
Id basetypes.Int64Value `tfsdk:"id"`
Name basetypes.StringValue `tfsdk:"name"`
Owner basetypes.StringValue `tfsdk:"owner"`
state attr.ValueState
Id basetypes.Int64Value `tfsdk:"id"`
Name basetypes.StringValue `tfsdk:"name"`
Owner basetypes.StringValue `tfsdk:"owner"`
state attr.ValueState
}
func (v DatabasesValue) ToTerraformValue(ctx context.Context) (tftypes.Value, error) {
attrTypes := make(map[string]tftypes.Type, 4)
attrTypes := make(map[string]tftypes.Type, 3)
var val tftypes.Value
var err error
attrTypes["created"] = basetypes.StringType{}.TerraformType(ctx)
attrTypes["id"] = basetypes.Int64Type{}.TerraformType(ctx)
attrTypes["name"] = basetypes.StringType{}.TerraformType(ctx)
attrTypes["owner"] = basetypes.StringType{}.TerraformType(ctx)
@ -492,15 +447,7 @@ func (v DatabasesValue) ToTerraformValue(ctx context.Context) (tftypes.Value, er
switch v.state {
case attr.ValueStateKnown:
vals := make(map[string]tftypes.Value, 4)
val, err = v.Created.ToTerraformValue(ctx)
if err != nil {
return tftypes.NewValue(objectType, tftypes.UnknownValue), err
}
vals["created"] = val
vals := make(map[string]tftypes.Value, 3)
val, err = v.Id.ToTerraformValue(ctx)
@ -556,10 +503,9 @@ func (v DatabasesValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValu
var diags diag.Diagnostics
attributeTypes := map[string]attr.Type{
"created": basetypes.StringType{},
"id": basetypes.Int64Type{},
"name": basetypes.StringType{},
"owner": basetypes.StringType{},
"id": basetypes.Int64Type{},
"name": basetypes.StringType{},
"owner": basetypes.StringType{},
}
if v.IsNull() {
@ -573,10 +519,9 @@ func (v DatabasesValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValu
objVal, diags := types.ObjectValue(
attributeTypes,
map[string]attr.Value{
"created": v.Created,
"id": v.Id,
"name": v.Name,
"owner": v.Owner,
"id": v.Id,
"name": v.Name,
"owner": v.Owner,
})
return objVal, diags
@ -597,10 +542,6 @@ func (v DatabasesValue) Equal(o attr.Value) bool {
return true
}
if !v.Created.Equal(other.Created) {
return false
}
if !v.Id.Equal(other.Id) {
return false
}
@ -626,10 +567,9 @@ func (v DatabasesValue) Type(ctx context.Context) attr.Type {
func (v DatabasesValue) AttributeTypes(ctx context.Context) map[string]attr.Type {
return map[string]attr.Type{
"created": basetypes.StringType{},
"id": basetypes.Int64Type{},
"name": basetypes.StringType{},
"owner": basetypes.StringType{},
"id": basetypes.Int64Type{},
"name": basetypes.StringType{},
"owner": basetypes.StringType{},
}
}

View file

@ -178,18 +178,11 @@ func (r *databaseResource) Create(
return
}
// Read identity data
var identityData DatabaseResourceIdentityModel
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
if resp.Diagnostics.HasError() {
return
}
ctx = core.InitProviderContext(ctx)
projectId := identityData.ProjectID.ValueString()
region := identityData.ProjectID.ValueString()
instanceId := identityData.InstanceID.ValueString()
projectId := model.ProjectId.ValueString()
region := model.InstanceId.ValueString()
instanceId := model.InstanceId.ValueString()
ctx = tflog.SetField(ctx, "project_id", projectId)
ctx = tflog.SetField(ctx, "instance_id", instanceId)
@ -218,8 +211,6 @@ func (r *databaseResource) Create(
return
}
ctx = core.LogResponse(ctx)
if databaseResp == nil || databaseResp.Id == nil {
core.LogAndAddError(
ctx,
@ -231,6 +222,19 @@ func (r *databaseResource) Create(
}
databaseId := *databaseResp.Id
ctx = tflog.SetField(ctx, "database_id", databaseId)
ctx = core.LogResponse(ctx)
// Save identity into Terraform state
identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
DatabaseID: types.Int64Value(databaseId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
database, err := getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
if err != nil {
@ -255,18 +259,6 @@ func (r *databaseResource) Create(
return
}
// Set data returned by API in identity
identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
DatabaseID: types.Int64Value(databaseId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
// Set state to fully populated data
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
if resp.Diagnostics.HasError() {
@ -337,6 +329,18 @@ func (r *databaseResource) Read(
return
}
// Save identity into Terraform state
identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
DatabaseID: types.Int64Value(databaseId),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
// Set refreshed state
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
@ -415,7 +419,7 @@ func (r *databaseResource) Update(
}
// Update existing database
res, err := r.client.UpdateDatabasePartiallyRequest(
err := r.client.UpdateDatabasePartiallyRequest(
ctx,
projectId,
region,
@ -430,19 +434,45 @@ func (r *databaseResource) Update(
ctx = core.LogResponse(ctx)
// Map response body to schema
err = mapResourceFields(res.Database, &model)
databaseResp, err := getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId64)
if err != nil {
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if (ok && oapiErr.StatusCode == http.StatusNotFound) || errors.Is(err, errDatabaseNotFound) {
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading database", fmt.Sprintf("Calling API: %v", err))
return
}
ctx = core.LogResponse(ctx)
// Map response body to schema
err = mapResourceFields(databaseResp, &model)
if err != nil {
core.LogAndAddError(
ctx,
&resp.Diagnostics,
"Error updating database",
"Error reading database",
fmt.Sprintf("Processing API payload: %v", err),
)
return
}
// Save identity into Terraform state
identity := DatabaseResourceIdentityModel{
ProjectID: types.StringValue(projectId),
Region: types.StringValue(region),
InstanceID: types.StringValue(instanceId),
DatabaseID: types.Int64Value(databaseId64),
}
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
if resp.Diagnostics.HasError() {
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}