fix: add databases generated file
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / CI (pull_request) Failing after 7m44s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 9m33s
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / CI (pull_request) Failing after 7m44s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 9m33s
feat: add useful command
This commit is contained in:
parent
d8c63f848a
commit
73a8274c5d
3 changed files with 175 additions and 7 deletions
|
|
@ -28,15 +28,13 @@ data "stackitprivatepreview_sqlserverflexbeta_database" "example" {
|
||||||
- `database_name` (String) The name of the database.
|
- `database_name` (String) The name of the database.
|
||||||
- `instance_id` (String) The ID of the instance.
|
- `instance_id` (String) The ID of the instance.
|
||||||
- `project_id` (String) The STACKIT project ID.
|
- `project_id` (String) The STACKIT project ID.
|
||||||
|
|
||||||
### Optional
|
|
||||||
|
|
||||||
- `region` (String) The region which should be addressed
|
- `region` (String) The region which should be addressed
|
||||||
|
|
||||||
### Read-Only
|
### Read-Only
|
||||||
|
|
||||||
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
|
- `collation_name` (String) The collation of the database. This database collation should match the *collation_name* of one of the collations given by the **Get database collation list** endpoint.
|
||||||
- `compatibility_level` (Number) CompatibilityLevel of the Database.
|
- `compatibility_level` (Number) CompatibilityLevel of the Database.
|
||||||
- `id` (Number) The id of the database.
|
- `id` (String) The terraform internal identifier.
|
||||||
- `name` (String) The name of the database.
|
- `name` (String) The name of the database.
|
||||||
- `owner` (String) The owner of the database.
|
- `owner` (String) The owner of the database.
|
||||||
|
- `tf_original_api_id` (Number) The id of the database.
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,22 @@ data "stackitprivatepreview_sqlserverflexbeta_flavor" "flavor" {
|
||||||
<!-- schema generated by tfplugindocs -->
|
<!-- schema generated by tfplugindocs -->
|
||||||
## Schema
|
## Schema
|
||||||
|
|
||||||
### Read-Only
|
### Required
|
||||||
|
|
||||||
- `cpu` (Number) The cpu count of the instance.
|
- `cpu` (Number) The cpu count of the instance.
|
||||||
|
- `node_type` (String) defines the nodeType it can be either single or HA
|
||||||
|
- `project_id` (String) The project ID of the flavor.
|
||||||
|
- `ram` (Number) The memory of the instance in Gibibyte.
|
||||||
|
- `region` (String) The region of the flavor.
|
||||||
|
- `storage_class` (String) The memory of the instance in Gibibyte.
|
||||||
|
|
||||||
|
### Read-Only
|
||||||
|
|
||||||
- `description` (String) The flavor description.
|
- `description` (String) The flavor description.
|
||||||
|
- `flavor_id` (String) The id of the instance flavor.
|
||||||
- `id` (String) The id of the instance flavor.
|
- `id` (String) The id of the instance flavor.
|
||||||
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
|
- `max_gb` (Number) maximum storage which can be ordered for the flavor in Gigabyte.
|
||||||
- `memory` (Number) The memory of the instance in Gibibyte.
|
|
||||||
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
|
- `min_gb` (Number) minimum storage which is required to order in Gigabyte.
|
||||||
- `node_type` (String) defines the nodeType it can be either single or HA
|
|
||||||
- `storage_classes` (Attributes List) maximum storage which can be ordered for the flavor in Gigabyte. (see [below for nested schema](#nestedatt--storage_classes))
|
- `storage_classes` (Attributes List) maximum storage which can be ordered for the flavor in Gigabyte. (see [below for nested schema](#nestedatt--storage_classes))
|
||||||
|
|
||||||
<a id="nestedatt--storage_classes"></a>
|
<a id="nestedatt--storage_classes"></a>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,163 @@
|
||||||
|
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/database/datasources_gen"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ datasource.DataSource = (*databaseDataSource)(nil)
|
||||||
|
|
||||||
|
const errorPrefix = "[Sqlserverflexbeta - Database]"
|
||||||
|
|
||||||
|
func NewDatabaseDataSource() datasource.DataSource {
|
||||||
|
return &databaseDataSource{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type databaseDataSource struct {
|
||||||
|
client *sqlserverflexbetaPkg.APIClient
|
||||||
|
providerData core.ProviderData
|
||||||
|
}
|
||||||
|
|
||||||
|
type dsModel struct {
|
||||||
|
sqlserverflexbetaGen.DatabaseModel
|
||||||
|
TfId types.String `tfsdk:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *databaseDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||||
|
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_database"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||||
|
resp.Schema = sqlserverflexbetaGen.DatabaseDataSourceSchema(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 *databaseDataSource) 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 *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||||
|
var data dsModel
|
||||||
|
readErr := "Read DB error"
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
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.DatabaseName.ValueString()
|
||||||
|
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
resp.State.RemoveResource(ctx)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = core.LogResponse(ctx)
|
||||||
|
|
||||||
|
dbId, ok := databaseResp.GetIdOk()
|
||||||
|
if !ok {
|
||||||
|
core.LogAndAddError(
|
||||||
|
ctx,
|
||||||
|
&resp.Diagnostics,
|
||||||
|
readErr,
|
||||||
|
"Database creation waiting: returned id is nil",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data.Id = types.Int64Value(dbId)
|
||||||
|
data.TfId = utils.BuildInternalTerraformId(projectId, region, instanceId, databaseName)
|
||||||
|
data.Owner = types.StringValue(databaseResp.GetOwner())
|
||||||
|
|
||||||
|
// TODO: fill remaining fields
|
||||||
|
// data.CollationName = types.Sometype(apiResponse.GetCollationName())
|
||||||
|
// data.CompatibilityLevel = types.Sometype(apiResponse.GetCompatibilityLevel())
|
||||||
|
// data.DatabaseName = types.Sometype(apiResponse.GetDatabaseName())
|
||||||
|
// data.Id = types.Sometype(apiResponse.GetId())
|
||||||
|
// data.InstanceId = types.Sometype(apiResponse.GetInstanceId())
|
||||||
|
// data.Name = types.Sometype(apiResponse.GetName())
|
||||||
|
// data.Owner = types.Sometype(apiResponse.GetOwner())
|
||||||
|
// 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)...)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue