package sqlserverflexalpha import ( "context" _ "embed" "errors" "fmt" "net/http" "strconv" "strings" "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/sqlserverflexalpha" "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" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" sqlserverflexalphaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/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 = sqlserverflexalphaGen.DatabaseModel // DatabaseResourceIdentityModel describes the resource's identity attributes. 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"` } type databaseResource struct { client *sqlserverflexalpha.APIClient providerData core.ProviderData } func (r *databaseResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_sqlserverflexalpha_database" } //go:embed planModifiers.yaml var modifiersFileByte []byte func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { s := sqlserverflexalphaGen.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.PostgresFlexCustomEndpoint != "" { apiClientConfigOptions = append( apiClientConfigOptions, config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint), ) } else { apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(r.providerData.GetRegion())) } apiClient, err := sqlserverflexalpha.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, "sqlserverflexalpha.Database client configured") } func (r *databaseResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { var model resourceModel diags := req.Plan.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } ctx = core.InitProviderContext(ctx) projectId := model.ProjectId.ValueString() region := model.Region.ValueString() instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "region", region) // Generate API request body from model payload, err := toCreatePayload(&model) if err != nil { core.LogAndAddError( ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Creating API payload: %v", err), ) return } // Create new database databaseResp, err := r.client.CreateDatabaseRequest( ctx, projectId, region, instanceId, ).CreateDatabaseRequestPayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating database", fmt.Sprintf("Calling API: %v", err)) return } ctx = core.LogResponse(ctx) if databaseResp == nil || databaseResp.Id == nil { core.LogAndAddError( ctx, &resp.Diagnostics, "Error creating database", "API didn't return database Id. A database might have been created", ) return } databaseId := *databaseResp.Id databaseName := model.DatabaseName.String() ctx = tflog.SetField(ctx, "database_id", databaseId) ctx = tflog.SetField(ctx, "database_name", databaseName) 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 } 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, &model, 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, model)...) if resp.Diagnostics.HasError() { return } tflog.Info(ctx, "sqlserverflexalpha.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 } // 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 } // Set refreshed state resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) if resp.Diagnostics.HasError() { return } tflog.Info(ctx, "sqlserverflexalpha.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", err)) } ctx = core.LogResponse(ctx) resp.State.RemoveResource(ctx) tflog.Info(ctx, "sqlserverflexalpha.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 var configModel resourceModel // skip initial empty configuration to avoid follow-up errors if req.Config.Raw.IsNull() { return } 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 } 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 import identifier format is: [project_id],[region],[instance_id],[database_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 } databaseId, err := strconv.ParseInt(idParts[3], 10, 64) if err != nil { core.LogAndAddError( ctx, &resp.Diagnostics, "Error importing database", fmt.Sprintf("Invalid database_id format: %q. It must be a valid integer.", idParts[3]), ) 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"), databaseId)...) core.LogAndAddWarning( ctx, &resp.Diagnostics, "Sqlserverflexalpha database imported with empty password", "The database password is not imported as it is only available upon creation of a new database. The password field will be empty.", ) tflog.Info(ctx, "Sqlserverflexalpha database state imported") } // 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, "Sqlserverflexalpha 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 }