feat: update SQL Server Flex client configuration
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 5s
CI Workflow / CI (pull_request) Failing after 9m28s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 16m18s
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 5s
CI Workflow / CI (pull_request) Failing after 9m28s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 16m18s
This commit is contained in:
parent
4383300103
commit
694a3cfef0
22 changed files with 3342 additions and 1740 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
|
|
@ -28,7 +29,7 @@ func NewDatabaseDataSource() datasource.DataSource {
|
|||
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexbetaGen.DatabaseModel
|
||||
TfId types.String `tfsdk:"id"`
|
||||
TerraformId types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type databaseDataSource struct {
|
||||
|
|
@ -97,8 +98,6 @@ func (d *databaseDataSource) Configure(
|
|||
|
||||
func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var data dataSourceModel
|
||||
readErr := "Read DB error"
|
||||
|
||||
// Read Terraform configuration data into the model
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
|
||||
|
|
@ -108,6 +107,7 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
// Extract identifiers from the plan
|
||||
projectId := data.ProjectId.ValueString()
|
||||
region := d.providerData.GetRegionWithOverride(data.Region)
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
|
|
@ -120,44 +120,55 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
|
||||
databaseResp, err := d.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading database",
|
||||
fmt.Sprintf("database with %q does not exist in project %q.", databaseName, projectId),
|
||||
map[int]string{
|
||||
http.StatusForbidden: fmt.Sprintf("Project with %q not found or forbidden access", projectId),
|
||||
},
|
||||
)
|
||||
handleReadError(ctx, &resp.Diagnostics, err, projectId, instanceId)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
dbId, ok := databaseResp.GetIdOk()
|
||||
if !ok {
|
||||
// Map response body to schema and populate Computed attribute values
|
||||
err = mapFields(databaseResp, &data, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
readErr,
|
||||
"Database creation waiting: returned id is nil",
|
||||
"Error reading database",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
data.Id = types.Int64Value(dbId)
|
||||
data.TfId = utils.BuildInternalTerraformId(projectId, region, instanceId, databaseName)
|
||||
data.Owner = types.StringValue(databaseResp.GetOwner())
|
||||
data.CollationName = types.StringValue(databaseResp.GetCollationName())
|
||||
data.CompatibilityLevel = types.Int64Value(databaseResp.GetCompatibilityLevel())
|
||||
data.DatabaseName = types.StringValue(databaseResp.GetName())
|
||||
// data.InstanceId = types.StringValue(databaseResp.GetInstanceId())
|
||||
// data.Name = types.Sometype(apiResponse.GetName())
|
||||
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
|
||||
// data.Region = types.Sometype(apiResponse.GetRegion())
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "SQL Server Flex beta database read")
|
||||
|
||||
}
|
||||
|
||||
// handleReadError centralizes API error handling for the Read operation.
|
||||
func handleReadError(ctx context.Context, diags *diag.Diagnostics, err error, projectId, instanceId string) {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
diags,
|
||||
err,
|
||||
"Reading database",
|
||||
fmt.Sprintf(
|
||||
"Could not retrieve database for instance %q in project %q.",
|
||||
instanceId,
|
||||
projectId,
|
||||
),
|
||||
map[int]string{
|
||||
http.StatusBadRequest: fmt.Sprintf(
|
||||
"Invalid request parameters for project %q and instance %q.",
|
||||
projectId,
|
||||
instanceId,
|
||||
),
|
||||
http.StatusNotFound: fmt.Sprintf(
|
||||
"Database, instance %q, or project %q not found.",
|
||||
instanceId,
|
||||
projectId,
|
||||
),
|
||||
http.StatusForbidden: fmt.Sprintf("Forbidden access to project %q.", projectId),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue