Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/data-sources/sqlserverflex_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackit_sqlserverflex_database Data Source - stackit"
subcategory: ""
description: |-
SQLServer Flex database data source schema.
---

# stackit_sqlserverflex_database (Data Source)

SQLServer Flex database data source schema.

## Example Usage

```terraform
data "stackit_sqlserverflex_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-database"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `instance_id` (String) ID of the SQLServer Flex instance.
- `name` (String) Name of the database.
- `project_id` (String) STACKIT project ID.

### Optional

- `region` (String) The resource region. If not defined, the provider region is used.
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))

### Read-Only

- `collation` (String) The collation of the database.
- `compatibility` (Number) Compatibility level of the database.
- `database_id` (Number) The id of the database.
- `id` (String) Terraform internal ID, structured as "`project_id`,`region`,`instance_id`,`name`"
- `owner` (String) The owner of the database.

<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
70 changes: 70 additions & 0 deletions docs/resources/sqlserverflex_database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "stackit_sqlserverflex_database Resource - stackit"
subcategory: ""
description: |-
SQLServer Flex database resource schema.
---

# stackit_sqlserverflex_database (Resource)

SQLServer Flex database resource schema.

## Example Usage

```terraform
resource "stackit_sqlserverflex_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-database"
owner = "example-user"
collation = "SQL_Latin1_General_CP1_CI_AS"
compatibility = 160
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `instance_id` (String) ID of the SQLServer Flex instance.
- `name` (String) Name of the database.
- `owner` (String) The owner of the database.
- `project_id` (String) STACKIT project ID.

### Optional

- `collation` (String) The collation of the database.
- `compatibility` (Number) Compatibility level of the database.
- `region` (String) The resource region. If not defined, the provider region is used.
- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts))

### Read-Only

- `database_id` (Number) The id of the database.
- `id` (String) Terraform internal ID, structured as "`project_id`,`region`,`instance_id`,`name`"

<a id="nestedatt--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).
- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs.
- `read` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours).

## Import

Import is supported using the following syntax:

In Terraform v1.5.0 and later, the [` + "`" + `import` + "`" + ` block](https://developer.hashicorp.com/terraform/language/import) can be used with the ` + "`" + `id` + "`" + ` attribute, for example:

```terraform
# Terraform import
import {
to = stackit_sqlserverflex_database.example
id = "project_id,region,instance_id,name"
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "stackit_sqlserverflex_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-database"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terraform import
import {
to = stackit_sqlserverflex_database.example
id = "project_id,region,instance_id,name"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "stackit_sqlserverflex_database" "example" {
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
name = "example-database"
owner = "example-user"
collation = "SQL_Latin1_General_CP1_CI_AS"
compatibility = 160
}
182 changes: 182 additions & 0 deletions stackit/internal/services/sqlserverflex/database/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package database

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
sdk "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex/v3api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)

var (
_ datasource.DataSource = &databaseDataSource{}
_ datasource.DataSourceWithConfigure = &databaseDataSource{}
)

func NewDatabaseDataSource() datasource.DataSource {
return &databaseDataSource{}
}

type databaseDataSource struct {
client *sdk.APIClient
providerData core.ProviderData
}

type SharedModel struct {
Id types.String `tfsdk:"id"`
ProjectId types.String `tfsdk:"project_id"`
Region types.String `tfsdk:"region"`
InstanceId types.String `tfsdk:"instance_id"`
Name types.String `tfsdk:"name"`
Collation types.String `tfsdk:"collation"`
Compatibility types.Int64 `tfsdk:"compatibility"`
Owner types.String `tfsdk:"owner"`
DatabaseId types.Int64 `tfsdk:"database_id"`
}

type DatasourceModel struct {
SharedModel
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

func (d *databaseDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_sqlserverflex_database"
}

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
}

apiClient := sqlserverflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
d.client = apiClient
tflog.Info(ctx, "SqlserverFlex client configured")
}

func (d *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: descriptionDatasource,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: descriptionId,
Computed: true,
},
"project_id": schema.StringAttribute{
Description: descriptionProjectId,
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"region": schema.StringAttribute{
Description: descriptionRegion,
Optional: true,
Computed: true,
},
"instance_id": schema.StringAttribute{
Description: descriptionInstanceId,
Required: true,
Validators: []validator.String{
validate.UUID(),
validate.NoSeparator(),
},
},
"name": schema.StringAttribute{
Description: descriptionName,
Required: true,
},
"collation": schema.StringAttribute{
Description: descriptionCollation,
Computed: true,
},
"compatibility": schema.Int64Attribute{
Description: descriptionCompatibility,
Computed: true,
},
"owner": schema.StringAttribute{
Description: descriptionOwner,
Computed: true,
},
"database_id": schema.Int64Attribute{
Description: descriptionDatabaseId,
Computed: true,
},
"timeouts": timeouts.Attributes(ctx),
},
}
}

func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform
var model DatasourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}

readTimeout, diags := model.Timeouts.Read(ctx, core.DefaultOperationTimeout)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
ctx, cancel := context.WithTimeout(ctx, readTimeout)
defer cancel()

projectId := model.ProjectId.ValueString()
instanceId := model.InstanceId.ValueString()
name := model.Name.ValueString()
region := d.providerData.GetRegionWithOverride(model.Region)

ctx = core.InitProviderContext(ctx)

utils.SetAndLogStateFields(ctx, &resp.Diagnostics, &resp.State, map[string]any{
"project_id": projectId,
"region": region,
"instance_id": instanceId,
"name": model.Name,
})

apiResp, err := d.client.DefaultAPI.GetDatabase(ctx, projectId, region, instanceId, name).Execute()
if err != nil {
utils.LogError(ctx, &resp.Diagnostics, err, "read SqlserverFlex database",
fmt.Sprintf("database with name %q does not exist in instance %q", name, instanceId),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q, or instance with ID %q not found or forbidden access", projectId, instanceId),
},
)
resp.State.RemoveResource(ctx)
return
}

ctx = core.LogResponse(ctx)

err = mapFields(apiResp, &model.SharedModel, region)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading database", fmt.Sprintf("mapping response: %v", err))
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "SqlserverFlex database read")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package database

const (
descriptionResource = "SQLServer Flex database resource schema."
descriptionDatasource = "SQLServer Flex database data source schema."
descriptionId = "Terraform internal ID, structured as \"`project_id`,`region`,`instance_id`,`name`\""
descriptionProjectId = "STACKIT project ID."
descriptionRegion = "The resource region. If not defined, the provider region is used."
descriptionInstanceId = "ID of the SQLServer Flex instance."
descriptionName = "Name of the database."
descriptionCollation = "The collation of the database."
descriptionCompatibility = "Compatibility level of the database."
descriptionOwner = "The owner of the database."
descriptionDatabaseId = "The id of the database."
)
Loading
Loading