fix: add psql datasource

This commit is contained in:
Marcel_Henselin 2025-12-22 11:17:38 +01:00
parent d01ae71b75
commit b08b32ef1d
6 changed files with 109 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import (
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-log/tflog"
postgresflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/postgresflexalpha"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
@ -68,6 +69,14 @@ func CreateInstanceWaitHandler(
case InstanceStateProgressing:
return false, nil, nil
case InstanceStateSuccess:
if s.Network.InstanceAddress == nil {
tflog.Info(ctx, "Waiting for instance_address")
return false, nil, nil
}
if s.Network.RouterAddress == nil {
tflog.Info(ctx, "Waiting for router_address")
return false, nil, nil
}
instanceCreated = true
instanceGetResponse = s
case InstanceStateFailed:

View file

@ -10,6 +10,7 @@ import (
"strings"
"time"
"github.com/hashicorp/terraform-plugin-log/tflog"
sqlserverflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/sqlserverflexalpha"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/wait"
@ -40,8 +41,14 @@ func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface
}
switch strings.ToLower(string(*s.Status)) {
case strings.ToLower(InstanceStateSuccess):
// if no instance address - return false
// if no router address - return false
if s.Network.InstanceAddress == nil {
tflog.Info(ctx, "Waiting for instance_address")
return false, nil, nil
}
if s.Network.RouterAddress == nil {
tflog.Info(ctx, "Waiting for router_address")
return false, nil, nil
}
return true, s, nil
case strings.ToLower(InstanceStateUnknown), strings.ToLower(InstanceStateFailed):
return true, s, fmt.Errorf("create failed for instance with id %s", instanceId)

View file

@ -26,3 +26,13 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "ptlsdbsrv" {
}
version = 14
}
data "stackitprivatepreview_postgresflexalpha_instance" "datapsql" {
project_id = var.project_id
instance_id = "e0c028e0-a201-4b75-8ee5-50a0ad17b0d7"
region = "eu01"
}
output "sample_psqlinstance" {
value = data.stackitprivatepreview_postgresflexalpha_instance.datapsql
}

View file

@ -1,4 +1,4 @@
package postgresflexa
package postgresflexalpha
import (
"context"
@ -145,6 +145,51 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Optional: true,
Description: descriptions["region"],
},
"encryption": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"key_id": schema.StringAttribute{
Description: descriptions["key_id"],
Computed: true,
},
"key_version": schema.StringAttribute{
Description: descriptions["key_version"],
Computed: true,
},
"keyring_id": schema.StringAttribute{
Description: descriptions["keyring_id"],
Computed: true,
},
"service_account": schema.StringAttribute{
Description: descriptions["service_account"],
Computed: true,
},
},
Description: descriptions["encryption"],
},
"network": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"access_scope": schema.StringAttribute{
Description: descriptions["access_scope"],
Computed: true,
},
"acl": schema.ListAttribute{
Description: descriptions["acl"],
ElementType: types.StringType,
Computed: true,
},
"instance_address": schema.StringAttribute{
Description: descriptions["instance_address"],
Computed: true,
},
"router_address": schema.StringAttribute{
Description: descriptions["router_address"],
Computed: true,
},
},
Description: descriptions["network"],
},
},
}
}
@ -207,7 +252,16 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
}
}
err = mapFields(ctx, instanceResp, &model, flavor, storage, region)
var network = &networkModel{}
if !(model.Network.IsNull() || model.Network.IsUnknown()) {
diags = model.Network.As(ctx, network, basetypes.ObjectAsOptions{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
err = mapFields(ctx, instanceResp, &model, flavor, storage, network, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
return

View file

@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
//postgresflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/postgresflexalpha"
postgresflex "github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/postgresflexalpha"
"github.com/mhenselin/terraform-provider-stackitprivatepreview/pkg/postgresflexalpha/wait"
@ -341,11 +342,7 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest,
},
},
},
//Blocks: nil,
//CustomType: nil,
Description: descriptions["encryption"],
//MarkdownDescription: "",
//DeprecationMessage: "",
//Validators: nil,
PlanModifiers: []planmodifier.Object{},
},
@ -357,17 +354,37 @@ func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest,
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
validate.NoSeparator(),
},
},
"acl": schema.ListAttribute{
Description: descriptions["acl"],
ElementType: types.StringType,
Required: true,
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
},
"instance_address": schema.StringAttribute{
Description: descriptions["instance_address"],
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"router_address": schema.StringAttribute{
Description: descriptions["router_address"],
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
//Blocks: nil,
//CustomType: nil,
Description: descriptions["network"],
//MarkdownDescription: "",
//DeprecationMessage: "",
//Validators: nil,
PlanModifiers: []planmodifier.Object{},
},

View file

@ -486,8 +486,7 @@ func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest,
// DataSources defines the data sources implemented in the provider.
func (p *Provider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
// TODO @mhenselin
// postgresFlexAlphaInstance.NewInstanceDataSource(),
postgresFlexAlphaInstance.NewInstanceDataSource,
postgresFlexAlphaUser.NewUserDataSource,
sqlServerFlexAlphaInstance.NewInstanceDataSource,
sqlserverFlexAlphaUser.NewUserDataSource,