## 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: #58
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
640 lines
19 KiB
Go
640 lines
19 KiB
Go
package sqlserverflexbeta
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
|
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
|
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
|
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
|
wait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/sqlserverflexbeta"
|
|
|
|
"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"
|
|
|
|
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/database/resources_gen"
|
|
)
|
|
|
|
var (
|
|
_ resource.Resource = &databaseResource{}
|
|
_ resource.ResourceWithConfigure = &databaseResource{}
|
|
_ resource.ResourceWithImportState = &databaseResource{}
|
|
_ resource.ResourceWithModifyPlan = &databaseResource{}
|
|
_ resource.ResourceWithIdentity = &databaseResource{}
|
|
|
|
// Define errors
|
|
errDatabaseNotFound = errors.New("database not found")
|
|
|
|
// Error message constants
|
|
extractErrorSummary = "extracting failed"
|
|
extractErrorMessage = "Extracting identity data: %v"
|
|
)
|
|
|
|
func NewDatabaseResource() resource.Resource {
|
|
return &databaseResource{}
|
|
}
|
|
|
|
// resourceModel describes the resource data model.
|
|
type resourceModel = sqlserverflexbetaResGen.DatabaseModel
|
|
|
|
type databaseResource struct {
|
|
client *sqlserverflexbeta.APIClient
|
|
providerData core.ProviderData
|
|
}
|
|
|
|
type DatabaseResourceIdentityModel struct {
|
|
ProjectID types.String `tfsdk:"project_id"`
|
|
Region types.String `tfsdk:"region"`
|
|
InstanceID types.String `tfsdk:"instance_id"`
|
|
DatabaseName types.String `tfsdk:"database_name"`
|
|
}
|
|
|
|
func (r *databaseResource) Metadata(
|
|
_ context.Context,
|
|
req resource.MetadataRequest,
|
|
resp *resource.MetadataResponse,
|
|
) {
|
|
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_database"
|
|
}
|
|
|
|
//go:embed planModifiers.yaml
|
|
var modifiersFileByte []byte
|
|
|
|
func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
|
|
s := sqlserverflexbetaResGen.DatabaseResourceSchema(ctx)
|
|
|
|
fields, err := utils.ReadModifiersConfig(modifiersFileByte)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("error during read modifiers config file", err.Error())
|
|
return
|
|
}
|
|
|
|
err = utils.AddPlanModifiersToResourceSchema(fields, &s)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("error adding plan modifiers", err.Error())
|
|
return
|
|
}
|
|
resp.Schema = s
|
|
}
|
|
|
|
func (r *databaseResource) IdentitySchema(
|
|
_ context.Context,
|
|
_ resource.IdentitySchemaRequest,
|
|
resp *resource.IdentitySchemaResponse,
|
|
) {
|
|
resp.IdentitySchema = identityschema.Schema{
|
|
Attributes: map[string]identityschema.Attribute{
|
|
"project_id": identityschema.StringAttribute{
|
|
RequiredForImport: true, // must be set during import by the practitioner
|
|
},
|
|
"region": identityschema.StringAttribute{
|
|
RequiredForImport: true, // can be defaulted by the provider configuration
|
|
},
|
|
"instance_id": identityschema.StringAttribute{
|
|
RequiredForImport: true, // can be defaulted by the provider configuration
|
|
},
|
|
"database_name": identityschema.StringAttribute{
|
|
RequiredForImport: true, // can be defaulted by the provider configuration
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Configure adds the provider configured client to the resource.
|
|
func (r *databaseResource) Configure(
|
|
ctx context.Context,
|
|
req resource.ConfigureRequest,
|
|
resp *resource.ConfigureResponse,
|
|
) {
|
|
var ok bool
|
|
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
apiClientConfigOptions := []config.ConfigurationOption{
|
|
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
utils.UserAgentConfigOption(r.providerData.Version),
|
|
}
|
|
if r.providerData.SQLServerFlexCustomEndpoint != "" {
|
|
apiClientConfigOptions = append(
|
|
apiClientConfigOptions,
|
|
config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint),
|
|
)
|
|
} else {
|
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion()))
|
|
}
|
|
apiClient, err := sqlserverflexbeta.NewAPIClient(apiClientConfigOptions...)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError(
|
|
"Error configuring API client",
|
|
fmt.Sprintf(
|
|
"Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration",
|
|
err,
|
|
),
|
|
)
|
|
return
|
|
}
|
|
r.client = apiClient
|
|
tflog.Info(ctx, "sqlserverflexbeta.Database client configured")
|
|
}
|
|
|
|
func (r *databaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var data resourceModel
|
|
createErr := "DB create error"
|
|
|
|
// Read Terraform plan data into the model
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
|
|
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
ctx = core.InitProviderContext(ctx)
|
|
|
|
projectId := data.ProjectId.ValueString()
|
|
region := data.Region.ValueString()
|
|
instanceId := data.InstanceId.ValueString()
|
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
|
ctx = tflog.SetField(ctx, "region", region)
|
|
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
|
|
|
databaseName := data.Name.ValueString()
|
|
ctx = tflog.SetField(ctx, "database_name", databaseName)
|
|
|
|
payLoad := sqlserverflexbeta.CreateDatabaseRequestPayload{}
|
|
if !data.Collation.IsNull() && !data.Collation.IsUnknown() {
|
|
payLoad.Collation = data.Collation.ValueStringPointer()
|
|
}
|
|
|
|
if !data.Compatibility.IsNull() && !data.Compatibility.IsUnknown() {
|
|
payLoad.Compatibility = data.Compatibility.ValueInt64Pointer()
|
|
}
|
|
|
|
payLoad.Name = data.Name.ValueStringPointer()
|
|
payLoad.Owner = data.Owner.ValueStringPointer()
|
|
|
|
_, err := wait.WaitForUserWaitHandler(
|
|
ctx,
|
|
r.client,
|
|
projectId,
|
|
instanceId,
|
|
region,
|
|
data.Owner.ValueString(),
|
|
).
|
|
SetSleepBeforeWait(10 * time.Second).
|
|
WaitWithContext(ctx)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
fmt.Sprintf("Calling API: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
createResp, err := r.client.CreateDatabaseRequest(ctx, projectId, region, instanceId).
|
|
CreateDatabaseRequestPayload(payLoad).
|
|
Execute()
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
fmt.Sprintf("Calling API: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
if createResp == nil || createResp.Id == nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
"Error creating database",
|
|
"API didn't return database Id. A database might have been created",
|
|
)
|
|
return
|
|
}
|
|
|
|
databaseId := *createResp.Id
|
|
|
|
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
|
|
|
ctx = core.LogResponse(ctx)
|
|
|
|
// Set data returned by API in identity
|
|
identity := DatabaseResourceIdentityModel{
|
|
ProjectID: types.StringValue(projectId),
|
|
Region: types.StringValue(region),
|
|
InstanceID: types.StringValue(instanceId),
|
|
DatabaseName: types.StringValue(databaseName),
|
|
}
|
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// TODO: is this necessary to wait for the database-> API say 200 ?
|
|
waitResp, err := wait.CreateDatabaseWaitHandler(
|
|
ctx,
|
|
r.client,
|
|
projectId,
|
|
instanceId,
|
|
region,
|
|
databaseName,
|
|
).SetSleepBeforeWait(
|
|
30 * time.Second,
|
|
).SetTimeout(
|
|
15 * time.Minute,
|
|
).WaitWithContext(ctx)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
fmt.Sprintf("Database creation waiting: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
if waitResp.Id == nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
"Database creation waiting: returned id is nil",
|
|
)
|
|
return
|
|
}
|
|
|
|
if *waitResp.Id != databaseId {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
"Database creation waiting: returned id is different",
|
|
)
|
|
return
|
|
}
|
|
|
|
if *waitResp.Owner != data.Owner.ValueString() {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
"Database creation waiting: returned owner is different",
|
|
)
|
|
return
|
|
}
|
|
|
|
if *waitResp.Name != data.Name.ValueString() {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
createErr,
|
|
"Database creation waiting: returned name is different",
|
|
)
|
|
return
|
|
}
|
|
|
|
database, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
"Error creating database",
|
|
fmt.Sprintf("Getting database details after creation: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
// Map response body to schema
|
|
err = mapResourceFields(database, &data, region)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
"Error creating database",
|
|
fmt.Sprintf("Processing API payload: %v", err),
|
|
)
|
|
return
|
|
}
|
|
|
|
// Set state to fully populated data
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Save data into Terraform state
|
|
|
|
tflog.Info(ctx, "sqlserverflexbeta.Database created")
|
|
}
|
|
|
|
func (r *databaseResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var model resourceModel
|
|
diags := req.State.Get(ctx, &model)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
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, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
|
|
if errExt != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
extractErrorSummary,
|
|
fmt.Sprintf(extractErrorMessage, errExt),
|
|
)
|
|
}
|
|
|
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
|
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
|
ctx = tflog.SetField(ctx, "region", region)
|
|
ctx = tflog.SetField(ctx, "database_name", databaseName)
|
|
|
|
databaseResp, err := r.client.GetDatabaseRequest(ctx, projectId, region, instanceId, databaseName).Execute()
|
|
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, region)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
"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),
|
|
DatabaseName: types.StringValue(databaseName),
|
|
}
|
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
// Set refreshed state
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
tflog.Info(ctx, "sqlserverflexbeta.Database read")
|
|
}
|
|
|
|
func (r *databaseResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
// TODO: Check update api endpoint - not available at the moment, so return an error for now
|
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating database", "Database can't be updated")
|
|
}
|
|
|
|
func (r *databaseResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
// nolint:gocritic // function signature required by Terraform
|
|
var model resourceModel
|
|
diags := req.State.Get(ctx, &model)
|
|
resp.Diagnostics.Append(diags...)
|
|
if resp.Diagnostics.HasError() {
|
|
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, region, instanceId, databaseName, errExt := r.extractIdentityData(model, identityData)
|
|
if errExt != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
extractErrorSummary,
|
|
fmt.Sprintf(extractErrorMessage, errExt),
|
|
)
|
|
}
|
|
|
|
ctx = tflog.SetField(ctx, "project_id", projectId)
|
|
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
|
ctx = tflog.SetField(ctx, "region", region)
|
|
ctx = tflog.SetField(ctx, "database_name", databaseName)
|
|
|
|
// Delete existing record set
|
|
err := r.client.DeleteDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseName)
|
|
if err != nil {
|
|
core.LogAndAddError(
|
|
ctx,
|
|
&resp.Diagnostics,
|
|
"Error deleting database",
|
|
fmt.Sprintf(
|
|
"Calling API: %v\nname: %s, region: %s, instanceId: %s", err, databaseName, region, instanceId))
|
|
return
|
|
}
|
|
|
|
ctx = core.LogResponse(ctx)
|
|
resp.State.RemoveResource(ctx)
|
|
|
|
tflog.Info(ctx, "sqlserverflexbeta.Database deleted")
|
|
}
|
|
|
|
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
|
// Use the modifier to set the effective region in the current plan.
|
|
func (r *databaseResource) ModifyPlan(
|
|
ctx context.Context,
|
|
req resource.ModifyPlanRequest,
|
|
resp *resource.ModifyPlanResponse,
|
|
) { // nolint:gocritic // function signature required by Terraform
|
|
|
|
// skip initial empty configuration to avoid follow-up errors
|
|
if req.Config.Raw.IsNull() {
|
|
return
|
|
}
|
|
|
|
var configModel resourceModel
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
var planModel resourceModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
var identityModel DatabaseResourceIdentityModel
|
|
identityModel.ProjectID = planModel.ProjectId
|
|
identityModel.Region = planModel.Region
|
|
|
|
if !planModel.InstanceId.IsNull() && !planModel.InstanceId.IsUnknown() {
|
|
identityModel.InstanceID = planModel.InstanceId
|
|
}
|
|
|
|
if !planModel.Name.IsNull() && !planModel.Name.IsUnknown() {
|
|
identityModel.DatabaseName = planModel.Name
|
|
}
|
|
|
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, identityModel)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
}
|
|
|
|
// ImportState imports a resource into the Terraform state on success.
|
|
// The expected format of the resource import identifier is: project_id,zone_id,record_set_id
|
|
func (r *databaseResource) ImportState(
|
|
ctx context.Context,
|
|
req resource.ImportStateRequest,
|
|
resp *resource.ImportStateResponse,
|
|
) {
|
|
ctx = core.InitProviderContext(ctx)
|
|
|
|
if req.ID != "" {
|
|
idParts := strings.Split(req.ID, core.Separator)
|
|
|
|
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
|
|
core.LogAndAddError(
|
|
ctx, &resp.Diagnostics,
|
|
"Error importing database",
|
|
fmt.Sprintf(
|
|
"Expected import identifier with format: [project_id],[region],[instance_id],[database_name] Got: %q",
|
|
req.ID,
|
|
),
|
|
)
|
|
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_name"), idParts[3])...)
|
|
|
|
var identityData DatabaseResourceIdentityModel
|
|
identityData.ProjectID = types.StringValue(idParts[0])
|
|
identityData.Region = types.StringValue(idParts[1])
|
|
identityData.InstanceID = types.StringValue(idParts[2])
|
|
identityData.DatabaseName = types.StringValue(idParts[3])
|
|
|
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, &identityData)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
tflog.Info(ctx, "Sqlserverflexbeta database state imported")
|
|
return
|
|
}
|
|
|
|
// If no ID is provided, attempt to read identity attributes from the import configuration
|
|
var identityData DatabaseResourceIdentityModel
|
|
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
projectId := identityData.ProjectID.ValueString()
|
|
region := identityData.Region.ValueString()
|
|
instanceId := identityData.InstanceID.ValueString()
|
|
databaseName := identityData.DatabaseName.ValueString()
|
|
|
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), projectId)...)
|
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), region)...)
|
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceId)...)
|
|
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("database_name"), databaseName)...)
|
|
|
|
tflog.Info(ctx, "Sqlserverflexbeta database state imported")
|
|
}
|
|
|
|
// extractIdentityData extracts essential identifiers from the resource model, falling back to the identity model.
|
|
func (r *databaseResource) extractIdentityData(
|
|
model resourceModel,
|
|
identity DatabaseResourceIdentityModel,
|
|
) (projectId, region, instanceId, databaseName string, err error) {
|
|
if !model.Name.IsNull() && !model.Name.IsUnknown() {
|
|
databaseName = model.Name.ValueString()
|
|
} else {
|
|
if identity.DatabaseName.IsNull() || identity.DatabaseName.IsUnknown() {
|
|
return "", "", "", "", fmt.Errorf("database_name not found in config")
|
|
}
|
|
databaseName = identity.DatabaseName.ValueString()
|
|
}
|
|
|
|
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
|
|
projectId = model.ProjectId.ValueString()
|
|
} else {
|
|
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
|
|
return "", "", "", "", fmt.Errorf("project_id not found in config")
|
|
}
|
|
projectId = identity.ProjectID.ValueString()
|
|
}
|
|
|
|
if !model.Region.IsNull() && !model.Region.IsUnknown() {
|
|
region = r.providerData.GetRegionWithOverride(model.Region)
|
|
} else {
|
|
if identity.Region.IsNull() || identity.Region.IsUnknown() {
|
|
return "", "", "", "", fmt.Errorf("region not found in config")
|
|
}
|
|
region = r.providerData.GetRegionWithOverride(identity.Region)
|
|
}
|
|
|
|
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
|
|
instanceId = model.InstanceId.ValueString()
|
|
} else {
|
|
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
|
|
return "", "", "", "", fmt.Errorf("instance_id not found in config")
|
|
}
|
|
instanceId = identity.InstanceID.ValueString()
|
|
}
|
|
return projectId, region, instanceId, databaseName, nil
|
|
}
|