package sqlserverflexbeta import ( "context" "fmt" "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/stackit-sdk-go/core/config" "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" sqlserverflexbetaPkg "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta" sqlserverflexbetaGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/flavors/datasources_gen" ) var _ datasource.DataSource = (*flavorsDataSource)(nil) const errorPrefix = "[Sqlserverflexbeta - Flavors]" func NewFlavorsDataSource() datasource.DataSource { return &flavorsDataSource{} } type dataSourceModel struct { sqlserverflexbetaGen.FlavorsModel TerraformId types.String `tfsdk:"id"` } type flavorsDataSource struct { client *sqlserverflexbetaPkg.APIClient providerData core.ProviderData } func (d *flavorsDataSource) Metadata( _ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse, ) { resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_flavors" } func (d *flavorsDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = sqlserverflexbetaGen.FlavorsDataSourceSchema(ctx) resp.Schema.Attributes["id"] = schema.StringAttribute{ Computed: true, Description: "The terraform internal identifier.", MarkdownDescription: "The terraform internal identifier.", } } // Configure adds the provider configured client to the data source. func (d *flavorsDataSource) Configure( ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse, ) { var ok bool d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } apiClientConfigOptions := []config.ConfigurationOption{ config.WithCustomAuth(d.providerData.RoundTripper), utils.UserAgentConfigOption(d.providerData.Version), } if d.providerData.SQLServerFlexCustomEndpoint != "" { apiClientConfigOptions = append( apiClientConfigOptions, config.WithEndpoint(d.providerData.SQLServerFlexCustomEndpoint), ) } else { apiClientConfigOptions = append( apiClientConfigOptions, config.WithRegion(d.providerData.GetRegion()), ) } apiClient, err := sqlserverflexbetaPkg.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 } d.client = apiClient tflog.Info(ctx, fmt.Sprintf("%s client configured", errorPrefix)) } func (d *flavorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data dataSourceModel // Read Terraform configuration data into the model resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) if resp.Diagnostics.HasError() { return } ctx = core.InitProviderContext(ctx) projectId := data.ProjectId.ValueString() region := d.providerData.GetRegionWithOverride(data.Region) // TODO: implement right identifier for flavors flavorsId := data.FlavorsModel.Flavors ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "region", region) // TODO: implement needed fields ctx = tflog.SetField(ctx, "flavors_id", flavorsId) // TODO: refactor to correct implementation _, err := d.client.GetFlavorsRequest(ctx, projectId, region).Execute() if err != nil { utils.LogError( ctx, &resp.Diagnostics, err, "Reading flavors", fmt.Sprintf("flavors with ID %q does not exist in project %q.", flavorsId, projectId), map[int]string{ http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), }, ) resp.State.RemoveResource(ctx) return } ctx = core.LogResponse(ctx) // TODO: refactor to correct implementation of internal tf id data.TerraformId = utils.BuildInternalTerraformId(projectId, region) // TODO: fill remaining fields // data.Flavors = types.Sometype(apiResponse.GetFlavors()) // data.Page = types.Sometype(apiResponse.GetPage()) // data.Pagination = types.Sometype(apiResponse.GetPagination()) // data.ProjectId = types.Sometype(apiResponse.GetProjectId()) // data.Region = types.Sometype(apiResponse.GetRegion()) // data.Size = types.Sometype(apiResponse.GetSize()) // data.Sort = types.Sometype(apiResponse.GetSort())// Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) tflog.Info(ctx, fmt.Sprintf("%s read successful", errorPrefix)) }