chore: work save
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 3m57s
CI Workflow / CI run tests (pull_request) Failing after 5m5s
CI Workflow / CI run build and linting (pull_request) Failing after 4m50s
CI Workflow / Code coverage report (pull_request) Has been skipped
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 3m57s
CI Workflow / CI run tests (pull_request) Failing after 5m5s
CI Workflow / CI run build and linting (pull_request) Failing after 4m50s
CI Workflow / Code coverage report (pull_request) Has been skipped
This commit is contained in:
parent
411e99739a
commit
d6d3a795bb
118 changed files with 3101 additions and 18065 deletions
|
|
@ -1,174 +0,0 @@
|
|||
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/diag"
|
||||
"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 dataSourceModel struct {
|
||||
sqlserverflexbetaGen.DatabaseModel
|
||||
TerraformId types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type databaseDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
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 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)
|
||||
|
||||
// Extract identifiers from the plan
|
||||
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 {
|
||||
handleReadError(ctx, &resp.Diagnostics, err, projectId, instanceId)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
// Map response body to schema and populate Computed attribute values
|
||||
err = mapFields(databaseResp, &data, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error reading database",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "SQL Server Flex beta database read")
|
||||
}
|
||||
|
||||
// handleReadError centralizes API error handling for the Read operation.
|
||||
func handleReadError(ctx context.Context, diags *diag.Diagnostics, err error, projectId, instanceId string) {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
diags,
|
||||
err,
|
||||
"Reading database",
|
||||
fmt.Sprintf(
|
||||
"Could not retrieve database for instance %q in project %q.",
|
||||
instanceId,
|
||||
projectId,
|
||||
),
|
||||
map[int]string{
|
||||
http.StatusBadRequest: fmt.Sprintf(
|
||||
"Invalid request parameters for project %q and instance %q.",
|
||||
projectId,
|
||||
instanceId,
|
||||
),
|
||||
http.StatusNotFound: fmt.Sprintf(
|
||||
"Database, instance %q, or project %q not found.",
|
||||
instanceId,
|
||||
projectId,
|
||||
),
|
||||
http.StatusForbidden: fmt.Sprintf("Forbidden access to project %q.", projectId),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
"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/utils"
|
||||
)
|
||||
|
||||
// mapFields maps fields from a ListDatabase API response to a resourceModel for the data source.
|
||||
func mapFields(source *sqlserverflexbeta.GetDatabaseResponse, model *dataSourceModel, region string) error {
|
||||
if source == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if source.Id == nil || *source.Id == 0 {
|
||||
return fmt.Errorf("id not present")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model given is nil")
|
||||
}
|
||||
|
||||
var databaseId int64
|
||||
if model.Id.ValueInt64() != 0 {
|
||||
databaseId = model.Id.ValueInt64()
|
||||
} else if source.Id != nil {
|
||||
databaseId = *source.Id
|
||||
} else {
|
||||
return fmt.Errorf("database id not present")
|
||||
}
|
||||
|
||||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseName = types.StringValue(source.GetName())
|
||||
model.Name = types.StringValue(source.GetName())
|
||||
model.Owner = types.StringValue(source.GetOwner())
|
||||
model.Region = types.StringValue(region)
|
||||
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
||||
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
||||
model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel())
|
||||
model.CollationName = types.StringValue(source.GetCollationName())
|
||||
|
||||
model.TerraformId = utils.BuildInternalTerraformId(
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
model.DatabaseName.ValueString(),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// mapResourceFields maps fields from a ListDatabase API response to a resourceModel for the resource.
|
||||
func mapResourceFields(source *sqlserverflexbeta.GetDatabaseResponse, model *resourceModel, region string) error {
|
||||
if source == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if source.Id == nil || *source.Id == 0 {
|
||||
return fmt.Errorf("id not present")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
|
||||
var databaseId int64
|
||||
if model.Id.ValueInt64() != 0 {
|
||||
databaseId = model.Id.ValueInt64()
|
||||
} else if source.Id != nil {
|
||||
databaseId = *source.Id
|
||||
} else {
|
||||
return fmt.Errorf("database id not present")
|
||||
}
|
||||
|
||||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseName = types.StringValue(source.GetName())
|
||||
model.Name = types.StringValue(source.GetName())
|
||||
model.Owner = types.StringValue(source.GetOwner())
|
||||
model.Region = types.StringValue(region)
|
||||
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
||||
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
||||
|
||||
model.Compatibility = types.Int64Value(source.GetCompatibilityLevel())
|
||||
model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel())
|
||||
|
||||
model.Collation = types.StringValue(source.GetCollationName()) // it does not come back from api
|
||||
model.CollationName = types.StringValue(source.GetCollationName())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// toCreatePayload converts the resource model to an API create payload.
|
||||
func toCreatePayload(model *resourceModel) (*sqlserverflexbeta.CreateDatabaseRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
return &sqlserverflexbeta.CreateDatabaseRequestPayload{
|
||||
Name: model.Name.ValueStringPointer(),
|
||||
Owner: model.Owner.ValueStringPointer(),
|
||||
Collation: model.Collation.ValueStringPointer(),
|
||||
Compatibility: model.Compatibility.ValueInt64Pointer(),
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -1,232 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/database/datasources_gen"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
type given struct {
|
||||
source *sqlserverflexbeta.GetDatabaseResponse
|
||||
model *dataSourceModel
|
||||
region string
|
||||
}
|
||||
type expected struct {
|
||||
model *dataSourceModel
|
||||
err bool
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
given given
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
name: "should map fields correctly",
|
||||
given: given{
|
||||
source: &sqlserverflexbeta.GetDatabaseResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
CollationName: utils.Ptr("collation"),
|
||||
CompatibilityLevel: utils.Ptr(int64(150)),
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
},
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
},
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: expected{
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
DatabaseName: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
Region: types.StringValue("eu01"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
CompatibilityLevel: types.Int64Value(150),
|
||||
CollationName: types.StringValue("collation"),
|
||||
},
|
||||
TerraformId: types.StringValue("my-project,eu01,my-instance,my-db"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should fail on nil source",
|
||||
given: given{
|
||||
source: nil,
|
||||
model: &dataSourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
{
|
||||
name: "should fail on nil source ID",
|
||||
given: given{
|
||||
source: &sqlserverflexbeta.GetDatabaseResponse{Id: nil},
|
||||
model: &dataSourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
{
|
||||
name: "should fail on nil model",
|
||||
given: given{
|
||||
source: &sqlserverflexbeta.GetDatabaseResponse{Id: utils.Ptr(int64(1))},
|
||||
model: nil,
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(
|
||||
tc.name, func(t *testing.T) {
|
||||
err := mapFields(tc.given.source, tc.given.model, tc.given.region)
|
||||
if (err != nil) != tc.expected.err {
|
||||
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
||||
}
|
||||
if err == nil {
|
||||
if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
|
||||
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapResourceFields(t *testing.T) {
|
||||
type given struct {
|
||||
source *sqlserverflexbeta.GetDatabaseResponse
|
||||
model *resourceModel
|
||||
region string
|
||||
}
|
||||
type expected struct {
|
||||
model *resourceModel
|
||||
err bool
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
given given
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
name: "should map fields correctly",
|
||||
given: given{
|
||||
source: &sqlserverflexbeta.GetDatabaseResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
},
|
||||
model: &resourceModel{
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: expected{
|
||||
model: &resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Compatibility: types.Int64Value(0),
|
||||
CompatibilityLevel: types.Int64Value(0),
|
||||
Collation: types.StringValue(""),
|
||||
CollationName: types.StringValue(""),
|
||||
DatabaseName: types.StringValue("my-db"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
Region: types.StringValue("eu01"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should fail on nil source",
|
||||
given: given{
|
||||
source: nil,
|
||||
model: &resourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(
|
||||
tc.name, func(t *testing.T) {
|
||||
err := mapResourceFields(tc.given.source, tc.given.model, tc.given.region)
|
||||
if (err != nil) != tc.expected.err {
|
||||
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
||||
}
|
||||
if err == nil {
|
||||
if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
|
||||
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
type given struct {
|
||||
model *resourceModel
|
||||
}
|
||||
type expected struct {
|
||||
payload *sqlserverflexbeta.CreateDatabaseRequestPayload
|
||||
err bool
|
||||
}
|
||||
|
||||
testcases := []struct {
|
||||
name string
|
||||
given given
|
||||
expected expected
|
||||
}{
|
||||
{
|
||||
name: "should convert model to payload",
|
||||
given: given{
|
||||
model: &resourceModel{
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
},
|
||||
expected: expected{
|
||||
payload: &sqlserverflexbeta.CreateDatabaseRequestPayload{
|
||||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "should fail on nil model",
|
||||
given: given{model: nil},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(
|
||||
tc.name, func(t *testing.T) {
|
||||
actual, err := toCreatePayload(tc.given.model)
|
||||
if (err != nil) != tc.expected.err {
|
||||
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
|
||||
}
|
||||
if err == nil {
|
||||
if diff := cmp.Diff(tc.expected.payload, actual); diff != "" {
|
||||
t.Errorf("payload mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
fields:
|
||||
- name: 'id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'instance_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'project_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'region'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'name'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'collation'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'owner'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'database_name'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'collation_name'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'compatibility'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'compatibility_level'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
|
@ -1,559 +0,0 @@
|
|||
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")
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResourceFields(waitResp, &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
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
databaseName := model.DatabaseName.ValueString()
|
||||
|
||||
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", "there is no way to update a database")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
databaseName := model.DatabaseName.ValueString()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: wait handler??
|
||||
|
||||
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 mode
|
||||
|
|
@ -1,355 +0,0 @@
|
|||
package sqlserverFlexBetaFlavor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"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-framework/types/basetypes"
|
||||
"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/flavor/datasources_gen"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &flavorDataSource{}
|
||||
_ datasource.DataSourceWithConfigure = &flavorDataSource{}
|
||||
)
|
||||
|
||||
type FlavorModel struct {
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
StorageClass types.String `tfsdk:"storage_class"`
|
||||
Cpu types.Int64 `tfsdk:"cpu"`
|
||||
Description types.String `tfsdk:"description"`
|
||||
Id types.String `tfsdk:"id"`
|
||||
FlavorId types.String `tfsdk:"flavor_id"`
|
||||
MaxGb types.Int64 `tfsdk:"max_gb"`
|
||||
Memory types.Int64 `tfsdk:"ram"`
|
||||
MinGb types.Int64 `tfsdk:"min_gb"`
|
||||
NodeType types.String `tfsdk:"node_type"`
|
||||
StorageClasses types.List `tfsdk:"storage_classes"`
|
||||
}
|
||||
|
||||
// NewFlavorDataSource is a helper function to simplify the provider implementation.
|
||||
func NewFlavorDataSource() datasource.DataSource {
|
||||
return &flavorDataSource{}
|
||||
}
|
||||
|
||||
// flavorDataSource is the data source implementation.
|
||||
type flavorDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
func (r *flavorDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_flavor"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (r *flavorDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.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 := 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
|
||||
}
|
||||
r.client = apiClient
|
||||
tflog.Info(ctx, "SQL Server Flex instance client configured")
|
||||
}
|
||||
|
||||
func (r *flavorDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = schema.Schema{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"project_id": schema.StringAttribute{
|
||||
Required: true,
|
||||
Description: "The project ID of the flavor.",
|
||||
MarkdownDescription: "The project ID of the flavor.",
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Required: true,
|
||||
Description: "The region of the flavor.",
|
||||
MarkdownDescription: "The region of the flavor.",
|
||||
},
|
||||
"cpu": schema.Int64Attribute{
|
||||
Required: true,
|
||||
Description: "The cpu count of the instance.",
|
||||
MarkdownDescription: "The cpu count of the instance.",
|
||||
},
|
||||
"ram": schema.Int64Attribute{
|
||||
Required: true,
|
||||
Description: "The memory of the instance in Gibibyte.",
|
||||
MarkdownDescription: "The memory of the instance in Gibibyte.",
|
||||
},
|
||||
"storage_class": schema.StringAttribute{
|
||||
Required: true,
|
||||
Description: "The memory of the instance in Gibibyte.",
|
||||
MarkdownDescription: "The memory of the instance in Gibibyte.",
|
||||
},
|
||||
"node_type": schema.StringAttribute{
|
||||
Required: true,
|
||||
Description: "defines the nodeType it can be either single or HA",
|
||||
MarkdownDescription: "defines the nodeType it can be either single or HA",
|
||||
},
|
||||
"flavor_id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The id of the instance flavor.",
|
||||
MarkdownDescription: "The id of the instance flavor.",
|
||||
},
|
||||
"description": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The flavor description.",
|
||||
MarkdownDescription: "The flavor description.",
|
||||
},
|
||||
"id": schema.StringAttribute{
|
||||
Computed: true,
|
||||
Description: "The id of the instance flavor.",
|
||||
MarkdownDescription: "The id of the instance flavor.",
|
||||
},
|
||||
"max_gb": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
Description: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
MarkdownDescription: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
},
|
||||
"min_gb": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
Description: "minimum storage which is required to order in Gigabyte.",
|
||||
MarkdownDescription: "minimum storage which is required to order in Gigabyte.",
|
||||
},
|
||||
"storage_classes": schema.ListNestedAttribute{
|
||||
NestedObject: schema.NestedAttributeObject{
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"class": schema.StringAttribute{
|
||||
Computed: true,
|
||||
},
|
||||
"max_io_per_sec": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"max_through_in_mb": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
CustomType: sqlserverflexbetaGen.StorageClassesType{
|
||||
ObjectType: types.ObjectType{
|
||||
AttrTypes: sqlserverflexbetaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
},
|
||||
},
|
||||
},
|
||||
Computed: true,
|
||||
Description: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
MarkdownDescription: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
},
|
||||
},
|
||||
//Attributes: map[string]schema.Attribute{
|
||||
// "project_id": schema.StringAttribute{
|
||||
// Required: true,
|
||||
// Description: "The cpu count of the instance.",
|
||||
// MarkdownDescription: "The cpu count of the instance.",
|
||||
// },
|
||||
// "region": schema.StringAttribute{
|
||||
// Required: true,
|
||||
// Description: "The flavor description.",
|
||||
// MarkdownDescription: "The flavor description.",
|
||||
// },
|
||||
// "cpu": schema.Int64Attribute{
|
||||
// Required: true,
|
||||
// Description: "The cpu count of the instance.",
|
||||
// MarkdownDescription: "The cpu count of the instance.",
|
||||
// },
|
||||
// "ram": schema.Int64Attribute{
|
||||
// Required: true,
|
||||
// Description: "The memory of the instance in Gibibyte.",
|
||||
// MarkdownDescription: "The memory of the instance in Gibibyte.",
|
||||
// },
|
||||
// "storage_class": schema.StringAttribute{
|
||||
// Required: true,
|
||||
// Description: "The memory of the instance in Gibibyte.",
|
||||
// MarkdownDescription: "The memory of the instance in Gibibyte.",
|
||||
// },
|
||||
// "description": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// Description: "The flavor description.",
|
||||
// MarkdownDescription: "The flavor description.",
|
||||
// },
|
||||
// "id": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// Description: "The terraform id of the instance flavor.",
|
||||
// MarkdownDescription: "The terraform id of the instance flavor.",
|
||||
// },
|
||||
// "flavor_id": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// Description: "The flavor id of the instance flavor.",
|
||||
// MarkdownDescription: "The flavor id of the instance flavor.",
|
||||
// },
|
||||
// "max_gb": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// Description: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
// MarkdownDescription: "maximum storage which can be ordered for the flavor in Gigabyte.",
|
||||
// },
|
||||
// "min_gb": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// Description: "minimum storage which is required to order in Gigabyte.",
|
||||
// MarkdownDescription: "minimum storage which is required to order in Gigabyte.",
|
||||
// },
|
||||
// "node_type": schema.StringAttribute{
|
||||
// Required: true,
|
||||
// Description: "defines the nodeType it can be either single or replica",
|
||||
// MarkdownDescription: "defines the nodeType it can be either single or replica",
|
||||
// },
|
||||
// "storage_classes": schema.ListNestedAttribute{
|
||||
// Computed: true,
|
||||
// NestedObject: schema.NestedAttributeObject{
|
||||
// Attributes: map[string]schema.Attribute{
|
||||
// "class": schema.StringAttribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "max_io_per_sec": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// "max_through_in_mb": schema.Int64Attribute{
|
||||
// Computed: true,
|
||||
// },
|
||||
// },
|
||||
// CustomType: sqlserverflexalphaGen.StorageClassesType{
|
||||
// ObjectType: types.ObjectType{
|
||||
// AttrTypes: sqlserverflexalphaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
}
|
||||
}
|
||||
|
||||
func (r *flavorDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var model FlavorModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
flavors, err := getAllFlavors(ctx, r.client, projectId, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading flavors", fmt.Sprintf("getAllFlavors: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
var foundFlavors []sqlserverflexbetaPkg.ListFlavors
|
||||
for _, flavor := range flavors {
|
||||
if model.Cpu.ValueInt64() != *flavor.Cpu {
|
||||
continue
|
||||
}
|
||||
if model.Memory.ValueInt64() != *flavor.Memory {
|
||||
continue
|
||||
}
|
||||
if model.NodeType.ValueString() != *flavor.NodeType {
|
||||
continue
|
||||
}
|
||||
for _, sc := range *flavor.StorageClasses {
|
||||
if model.StorageClass.ValueString() != *sc.Class {
|
||||
continue
|
||||
}
|
||||
foundFlavors = append(foundFlavors, flavor)
|
||||
}
|
||||
}
|
||||
if len(foundFlavors) == 0 {
|
||||
resp.Diagnostics.AddError("get flavor", "could not find requested flavor")
|
||||
return
|
||||
}
|
||||
if len(foundFlavors) > 1 {
|
||||
resp.Diagnostics.AddError("get flavor", "found too many matching flavors")
|
||||
return
|
||||
}
|
||||
|
||||
f := foundFlavors[0]
|
||||
model.Description = types.StringValue(*f.Description)
|
||||
model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, *f.Id)
|
||||
model.FlavorId = types.StringValue(*f.Id)
|
||||
model.MaxGb = types.Int64Value(*f.MaxGB)
|
||||
model.MinGb = types.Int64Value(*f.MinGB)
|
||||
|
||||
if f.StorageClasses == nil {
|
||||
model.StorageClasses = types.ListNull(sqlserverflexbetaGen.StorageClassesType{
|
||||
ObjectType: basetypes.ObjectType{
|
||||
AttrTypes: sqlserverflexbetaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
},
|
||||
})
|
||||
} else {
|
||||
var scList []attr.Value
|
||||
for _, sc := range *f.StorageClasses {
|
||||
scList = append(
|
||||
scList,
|
||||
sqlserverflexbetaGen.NewStorageClassesValueMust(
|
||||
sqlserverflexbetaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"class": types.StringValue(*sc.Class),
|
||||
"max_io_per_sec": types.Int64Value(*sc.MaxIoPerSec),
|
||||
"max_through_in_mb": types.Int64Value(*sc.MaxThroughInMb),
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
storageClassesList := types.ListValueMust(
|
||||
sqlserverflexbetaGen.StorageClassesType{
|
||||
ObjectType: basetypes.ObjectType{
|
||||
AttrTypes: sqlserverflexbetaGen.StorageClassesValue{}.AttributeTypes(ctx),
|
||||
},
|
||||
},
|
||||
scList,
|
||||
)
|
||||
model.StorageClasses = storageClassesList
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "SQL Server Flex flavors read")
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,65 +0,0 @@
|
|||
package sqlserverFlexBetaFlavor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
)
|
||||
|
||||
type flavorsClientReader interface {
|
||||
GetFlavorsRequest(
|
||||
ctx context.Context,
|
||||
projectId, region string,
|
||||
) sqlserverflexbeta.ApiGetFlavorsRequestRequest
|
||||
}
|
||||
|
||||
func getAllFlavors(ctx context.Context, client flavorsClientReader, projectId, region string) (
|
||||
[]sqlserverflexbeta.ListFlavors,
|
||||
error,
|
||||
) {
|
||||
getAllFilter := func(_ sqlserverflexbeta.ListFlavors) bool { return true }
|
||||
flavorList, err := getFlavorsByFilter(ctx, client, projectId, region, getAllFilter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return flavorList, nil
|
||||
}
|
||||
|
||||
// getFlavorsByFilter is a helper function to retrieve flavors using a filtern function.
|
||||
// Hint: The API does not have a GetFlavors endpoint, only ListFlavors
|
||||
func getFlavorsByFilter(
|
||||
ctx context.Context,
|
||||
client flavorsClientReader,
|
||||
projectId, region string,
|
||||
filter func(db sqlserverflexbeta.ListFlavors) bool,
|
||||
) ([]sqlserverflexbeta.ListFlavors, error) {
|
||||
if projectId == "" || region == "" {
|
||||
return nil, fmt.Errorf("listing sqlserverflexbeta flavors: projectId and region are required")
|
||||
}
|
||||
|
||||
const pageSize = 25
|
||||
|
||||
var result = make([]sqlserverflexbeta.ListFlavors, 0)
|
||||
|
||||
for page := int64(1); ; page++ {
|
||||
res, err := client.GetFlavorsRequest(ctx, projectId, region).
|
||||
Page(page).Size(pageSize).Sort(sqlserverflexbeta.FLAVORSORT_INDEX_ASC).Execute()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("requesting flavors list (page %d): %w", page, err)
|
||||
}
|
||||
|
||||
// If the API returns no flavors, we have reached the end of the list.
|
||||
if res.Flavors == nil || len(*res.Flavors) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
for _, flavor := range *res.Flavors {
|
||||
if filter(flavor) {
|
||||
result = append(result, flavor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
@ -1,135 +0,0 @@
|
|||
package sqlserverFlexBetaFlavor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
)
|
||||
|
||||
type mockRequest struct {
|
||||
executeFunc func() (*sqlserverflexbeta.GetFlavorsResponse, error)
|
||||
}
|
||||
|
||||
func (m *mockRequest) Page(_ int64) sqlserverflexbeta.ApiGetFlavorsRequestRequest { return m }
|
||||
func (m *mockRequest) Size(_ int64) sqlserverflexbeta.ApiGetFlavorsRequestRequest { return m }
|
||||
func (m *mockRequest) Sort(_ sqlserverflexbeta.FlavorSort) sqlserverflexbeta.ApiGetFlavorsRequestRequest {
|
||||
return m
|
||||
}
|
||||
func (m *mockRequest) Execute() (*sqlserverflexbeta.GetFlavorsResponse, error) {
|
||||
return m.executeFunc()
|
||||
}
|
||||
|
||||
type mockFlavorsClient struct {
|
||||
executeRequest func() sqlserverflexbeta.ApiGetFlavorsRequestRequest
|
||||
}
|
||||
|
||||
func (m *mockFlavorsClient) GetFlavorsRequest(_ context.Context, _, _ string) sqlserverflexbeta.ApiGetFlavorsRequestRequest {
|
||||
return m.executeRequest()
|
||||
}
|
||||
|
||||
var mockResp = func(page int64) (*sqlserverflexbeta.GetFlavorsResponse, error) {
|
||||
if page == 1 {
|
||||
return &sqlserverflexbeta.GetFlavorsResponse{
|
||||
Flavors: &[]sqlserverflexbeta.ListFlavors{
|
||||
{Id: utils.Ptr("flavor-1"), Description: utils.Ptr("first")},
|
||||
{Id: utils.Ptr("flavor-2"), Description: utils.Ptr("second")},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
if page == 2 {
|
||||
return &sqlserverflexbeta.GetFlavorsResponse{
|
||||
Flavors: &[]sqlserverflexbeta.ListFlavors{
|
||||
{Id: utils.Ptr("flavor-3"), Description: utils.Ptr("three")},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &sqlserverflexbeta.GetFlavorsResponse{
|
||||
Flavors: &[]sqlserverflexbeta.ListFlavors{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestGetFlavorsByFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
projectId string
|
||||
region string
|
||||
mockErr error
|
||||
filter func(sqlserverflexbeta.ListFlavors) bool
|
||||
wantCount int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
description: "Success - Get all flavors (2 pages)",
|
||||
projectId: "pid", region: "reg",
|
||||
filter: func(_ sqlserverflexbeta.ListFlavors) bool { return true },
|
||||
wantCount: 3,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
description: "Success - Filter flavors by description",
|
||||
projectId: "pid", region: "reg",
|
||||
filter: func(f sqlserverflexbeta.ListFlavors) bool { return *f.Description == "first" },
|
||||
wantCount: 1,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
description: "Error - Missing parameters",
|
||||
projectId: "", region: "reg",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
var currentPage int64
|
||||
client := &mockFlavorsClient{
|
||||
executeRequest: func() sqlserverflexbeta.ApiGetFlavorsRequestRequest {
|
||||
return &mockRequest{
|
||||
executeFunc: func() (*sqlserverflexbeta.GetFlavorsResponse, error) {
|
||||
currentPage++
|
||||
return mockResp(currentPage)
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
actual, err := getFlavorsByFilter(context.Background(), client, tt.projectId, tt.region, tt.filter)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("getFlavorsByFilter() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr && len(actual) != tt.wantCount {
|
||||
t.Errorf("getFlavorsByFilter() got %d flavors, want %d", len(actual), tt.wantCount)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllFlavors(t *testing.T) {
|
||||
var currentPage int64
|
||||
client := &mockFlavorsClient{
|
||||
executeRequest: func() sqlserverflexbeta.ApiGetFlavorsRequestRequest {
|
||||
return &mockRequest{
|
||||
executeFunc: func() (*sqlserverflexbeta.GetFlavorsResponse, error) {
|
||||
currentPage++
|
||||
return mockResp(currentPage)
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
res, err := getAllFlavors(context.Background(), client, "pid", "reg")
|
||||
if err != nil {
|
||||
t.Errorf("getAllFlavors() unexpected error: %v", err)
|
||||
}
|
||||
if len(res) != 3 {
|
||||
t.Errorf("getAllFlavors() expected 3 flavor, got %d", len(res))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,156 +0,0 @@
|
|||
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.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))
|
||||
}
|
||||
|
|
@ -1,146 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"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/instance/datasources_gen"
|
||||
)
|
||||
|
||||
var _ datasource.DataSource = (*instanceDataSource)(nil)
|
||||
|
||||
const errorPrefix = "[Sqlserverflexbeta - Instance]"
|
||||
|
||||
func NewInstanceDataSource() datasource.DataSource {
|
||||
return &instanceDataSource{}
|
||||
}
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
sqlserverflexbetaGen.InstanceModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
type instanceDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *instanceDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_instance"
|
||||
}
|
||||
|
||||
func (d *instanceDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = sqlserverflexbetaGen.InstanceDataSourceSchema(ctx)
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *instanceDataSource) 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 *instanceDataSource) 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)
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
instanceResp, err := d.client.GetInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading instance",
|
||||
fmt.Sprintf("instance with ID %q does not exist in project %q.", instanceId, 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)
|
||||
|
||||
err = mapDataResponseToModel(ctx, instanceResp, &data, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
fmt.Sprintf("%s Read", errorPrefix),
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
}
|
||||
|
|
@ -1,272 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
"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"
|
||||
sqlserverflexbetaDataGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/datasources_gen"
|
||||
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/resources_gen"
|
||||
)
|
||||
|
||||
func mapResponseToModel(
|
||||
ctx context.Context,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
m *sqlserverflexbetaResGen.InstanceModel,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
m.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
m.Encryption = handleEncryption(ctx, m, resp)
|
||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
m.Id = types.StringValue(resp.GetId())
|
||||
m.InstanceId = types.StringValue(resp.GetId())
|
||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
m.Name = types.StringValue(resp.GetName())
|
||||
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network acl response value",
|
||||
)
|
||||
}
|
||||
net, diags := sqlserverflexbetaResGen.NewNetworkValue(
|
||||
sqlserverflexbetaResGen.NetworkValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl": netAcl,
|
||||
"instance_address": types.StringValue(resp.Network.GetInstanceAddress()),
|
||||
"router_address": types.StringValue(resp.Network.GetRouterAddress()),
|
||||
},
|
||||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return errors.New("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
|
||||
stor, diags := sqlserverflexbetaResGen.NewStorageValue(
|
||||
sqlserverflexbetaResGen.StorageValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"class": types.StringValue(resp.Storage.GetClass()),
|
||||
"size": types.Int64Value(resp.Storage.GetSize()),
|
||||
},
|
||||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("error converting storage response value")
|
||||
}
|
||||
m.Storage = stor
|
||||
|
||||
m.Version = types.StringValue(string(resp.GetVersion()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapDataResponseToModel(
|
||||
ctx context.Context,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
m *dataSourceModel,
|
||||
tfDiags diag.Diagnostics,
|
||||
) error {
|
||||
m.BackupSchedule = types.StringValue(resp.GetBackupSchedule())
|
||||
m.Edition = types.StringValue(string(resp.GetEdition()))
|
||||
m.Encryption = handleDSEncryption(ctx, m, resp)
|
||||
m.FlavorId = types.StringValue(resp.GetFlavorId())
|
||||
m.Id = types.StringValue(resp.GetId())
|
||||
m.InstanceId = types.StringValue(resp.GetId())
|
||||
m.IsDeletable = types.BoolValue(resp.GetIsDeletable())
|
||||
m.Name = types.StringValue(resp.GetName())
|
||||
netAcl, diags := types.ListValueFrom(ctx, types.StringType, resp.Network.GetAcl())
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network acl response value",
|
||||
)
|
||||
}
|
||||
net, diags := sqlserverflexbetaDataGen.NewNetworkValue(
|
||||
sqlserverflexbetaDataGen.NetworkValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"access_scope": types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl": netAcl,
|
||||
"instance_address": types.StringValue(resp.Network.GetInstanceAddress()),
|
||||
"router_address": types.StringValue(resp.Network.GetRouterAddress()),
|
||||
},
|
||||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return errors.New("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
m.RetentionDays = types.Int64Value(resp.GetRetentionDays())
|
||||
m.Status = types.StringValue(string(resp.GetStatus()))
|
||||
|
||||
stor, diags := sqlserverflexbetaDataGen.NewStorageValue(
|
||||
sqlserverflexbetaDataGen.StorageValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"class": types.StringValue(resp.Storage.GetClass()),
|
||||
"size": types.Int64Value(resp.Storage.GetSize()),
|
||||
},
|
||||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("error converting storage response value")
|
||||
}
|
||||
m.Storage = stor
|
||||
|
||||
m.Version = types.StringValue(string(resp.GetVersion()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleEncryption(
|
||||
ctx context.Context,
|
||||
m *sqlserverflexbetaResGen.InstanceModel,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
) sqlserverflexbetaResGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
resp.Encryption == nil ||
|
||||
resp.Encryption.KekKeyId == nil ||
|
||||
resp.Encryption.KekKeyRingId == nil ||
|
||||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
}
|
||||
|
||||
enc := sqlserverflexbetaResGen.NewEncryptionValueMust(
|
||||
sqlserverflexbetaResGen.EncryptionValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
|
||||
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
|
||||
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
|
||||
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
|
||||
},
|
||||
)
|
||||
return enc
|
||||
}
|
||||
|
||||
func handleDSEncryption(
|
||||
ctx context.Context,
|
||||
m *dataSourceModel,
|
||||
resp *sqlserverflexbeta.GetInstanceResponse,
|
||||
) sqlserverflexbetaDataGen.EncryptionValue {
|
||||
if !resp.HasEncryption() ||
|
||||
resp.Encryption == nil ||
|
||||
resp.Encryption.KekKeyId == nil ||
|
||||
resp.Encryption.KekKeyRingId == nil ||
|
||||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
return sqlserverflexbetaDataGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
}
|
||||
|
||||
enc := sqlserverflexbetaDataGen.NewEncryptionValueMust(
|
||||
sqlserverflexbetaDataGen.EncryptionValue{}.AttributeTypes(ctx),
|
||||
map[string]attr.Value{
|
||||
"kek_key_id": types.StringValue(resp.Encryption.GetKekKeyId()),
|
||||
"kek_key_ring_id": types.StringValue(resp.Encryption.GetKekKeyRingId()),
|
||||
"kek_key_version": types.StringValue(resp.Encryption.GetKekKeyVersion()),
|
||||
"service_account": types.StringValue(resp.Encryption.GetServiceAccount()),
|
||||
},
|
||||
)
|
||||
return enc
|
||||
}
|
||||
|
||||
func toCreatePayload(
|
||||
ctx context.Context,
|
||||
model *sqlserverflexbetaResGen.InstanceModel,
|
||||
) (*sqlserverflexbeta.CreateInstanceRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
storagePayload := &sqlserverflexbeta.CreateInstanceRequestPayloadGetStorageArgType{}
|
||||
if !model.Storage.IsNull() && !model.Storage.IsUnknown() {
|
||||
storagePayload.Class = model.Storage.Class.ValueStringPointer()
|
||||
storagePayload.Size = model.Storage.Size.ValueInt64Pointer()
|
||||
}
|
||||
|
||||
var encryptionPayload *sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType = nil
|
||||
if !model.Encryption.IsNull() && !model.Encryption.IsUnknown() {
|
||||
encryptionPayload = &sqlserverflexbeta.CreateInstanceRequestPayloadGetEncryptionArgType{}
|
||||
encryptionPayload.KekKeyId = model.Encryption.KekKeyId.ValueStringPointer()
|
||||
encryptionPayload.KekKeyRingId = model.Encryption.KekKeyRingId.ValueStringPointer()
|
||||
encryptionPayload.KekKeyVersion = model.Encryption.KekKeyVersion.ValueStringPointer()
|
||||
encryptionPayload.ServiceAccount = model.Encryption.ServiceAccount.ValueStringPointer()
|
||||
}
|
||||
|
||||
networkPayload := &sqlserverflexbeta.CreateInstanceRequestPayloadGetNetworkArgType{}
|
||||
if !model.Network.IsNull() && !model.Network.IsUnknown() {
|
||||
networkPayload.AccessScope = sqlserverflexbeta.CreateInstanceRequestPayloadNetworkGetAccessScopeAttributeType(
|
||||
model.Network.AccessScope.ValueStringPointer(),
|
||||
)
|
||||
|
||||
var resList []string
|
||||
diags := model.Network.Acl.ElementsAs(ctx, &resList, false)
|
||||
if diags.HasError() {
|
||||
return nil, fmt.Errorf("error converting network acl list")
|
||||
}
|
||||
networkPayload.Acl = &resList
|
||||
}
|
||||
|
||||
return &sqlserverflexbeta.CreateInstanceRequestPayload{
|
||||
BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule),
|
||||
Encryption: encryptionPayload,
|
||||
FlavorId: conversion.StringValueToPointer(model.FlavorId),
|
||||
Name: conversion.StringValueToPointer(model.Name),
|
||||
Network: networkPayload,
|
||||
RetentionDays: conversion.Int64ValueToPointer(model.RetentionDays),
|
||||
Storage: storagePayload,
|
||||
Version: sqlserverflexbeta.CreateInstanceRequestPayloadGetVersionAttributeType(
|
||||
conversion.StringValueToPointer(model.Version),
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func toUpdatePayload(
|
||||
ctx context.Context,
|
||||
m *sqlserverflexbetaResGen.InstanceModel,
|
||||
resp *resource.UpdateResponse,
|
||||
) (*sqlserverflexbeta.UpdateInstanceRequestPayload, error) {
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
if m.Replicas.ValueInt64() > math.MaxUint32 {
|
||||
return nil, fmt.Errorf("replicas value is too big for uint32")
|
||||
}
|
||||
replVal := sqlserverflexbeta.Replicas(uint32(m.Replicas.ValueInt64())) // nolint:gosec // check is performed above
|
||||
|
||||
var netAcl []string
|
||||
diags := m.Network.Acl.ElementsAs(ctx, &netAcl, false)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return nil, fmt.Errorf("error converting model network acl value")
|
||||
}
|
||||
return &sqlserverflexbeta.UpdateInstanceRequestPayload{
|
||||
BackupSchedule: m.BackupSchedule.ValueStringPointer(),
|
||||
FlavorId: m.FlavorId.ValueStringPointer(),
|
||||
Name: m.Name.ValueStringPointer(),
|
||||
Network: sqlserverflexbeta.NewUpdateInstanceRequestPayloadNetwork(netAcl),
|
||||
Replicas: &replVal,
|
||||
RetentionDays: m.RetentionDays.ValueInt64Pointer(),
|
||||
Storage: &sqlserverflexbeta.StorageUpdate{Size: m.Storage.Size.ValueInt64Pointer()},
|
||||
Version: sqlserverflexbeta.UpdateInstanceRequestPayloadGetVersionAttributeType(
|
||||
m.Version.ValueStringPointer(),
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -1,283 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
sqlserverflexbetaPkgGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbetaRs "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/resources_gen"
|
||||
)
|
||||
|
||||
func Test_handleDSEncryption(t *testing.T) {
|
||||
type args struct {
|
||||
m *dataSourceModel
|
||||
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want sqlserverflexbetaRs.EncryptionValue
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
if got := handleDSEncryption(t.Context(), tt.args.m, tt.args.resp); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("handleDSEncryption() = %v, want %v", got, tt.want)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_handleEncryption(t *testing.T) {
|
||||
type args struct {
|
||||
m *sqlserverflexbetaRs.InstanceModel
|
||||
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want sqlserverflexbetaRs.EncryptionValue
|
||||
}{
|
||||
{
|
||||
name: "nil response",
|
||||
args: args{
|
||||
m: &sqlserverflexbetaRs.InstanceModel{},
|
||||
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{},
|
||||
},
|
||||
want: sqlserverflexbetaRs.EncryptionValue{},
|
||||
},
|
||||
{
|
||||
name: "nil response",
|
||||
args: args{
|
||||
m: &sqlserverflexbetaRs.InstanceModel{},
|
||||
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{
|
||||
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{},
|
||||
},
|
||||
},
|
||||
want: sqlserverflexbetaRs.NewEncryptionValueNull(),
|
||||
},
|
||||
{
|
||||
name: "response with values",
|
||||
args: args{
|
||||
m: &sqlserverflexbetaRs.InstanceModel{},
|
||||
resp: &sqlserverflexbetaPkgGen.GetInstanceResponse{
|
||||
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{
|
||||
KekKeyId: utils.Ptr("kek_key_id"),
|
||||
KekKeyRingId: utils.Ptr("kek_key_ring_id"),
|
||||
KekKeyVersion: utils.Ptr("kek_key_version"),
|
||||
ServiceAccount: utils.Ptr("kek_svc_acc"),
|
||||
},
|
||||
},
|
||||
},
|
||||
want: sqlserverflexbetaRs.NewEncryptionValueMust(
|
||||
sqlserverflexbetaRs.EncryptionValue{}.AttributeTypes(context.TODO()),
|
||||
map[string]attr.Value{
|
||||
"kek_key_id": types.StringValue("kek_key_id"),
|
||||
"kek_key_ring_id": types.StringValue("kek_key_ring_id"),
|
||||
"kek_key_version": types.StringValue("kek_key_version"),
|
||||
"service_account": types.StringValue("kek_svc_acc"),
|
||||
},
|
||||
),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
got := handleEncryption(t.Context(), tt.args.m, tt.args.resp)
|
||||
|
||||
diff := cmp.Diff(tt.want, got)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
||||
//if !reflect.DeepEqual(got, tt.want) {
|
||||
// t.Errorf("handleEncryption() = %v, want %v", got, tt.want)
|
||||
//}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mapDataResponseToModel(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
|
||||
m *dataSourceModel
|
||||
tfDiags diag.Diagnostics
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
if err := mapDataResponseToModel(
|
||||
tt.args.ctx,
|
||||
tt.args.resp,
|
||||
tt.args.m,
|
||||
tt.args.tfDiags,
|
||||
); (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapDataResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_mapResponseToModel(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
resp *sqlserverflexbetaPkgGen.GetInstanceResponse
|
||||
m *sqlserverflexbetaRs.InstanceModel
|
||||
tfDiags diag.Diagnostics
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
if err := mapResponseToModel(
|
||||
tt.args.ctx,
|
||||
tt.args.resp,
|
||||
tt.args.m,
|
||||
tt.args.tfDiags,
|
||||
); (err != nil) != tt.wantErr {
|
||||
t.Errorf("mapResponseToModel() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_toCreatePayload(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
model *sqlserverflexbetaRs.InstanceModel
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *sqlserverflexbetaPkgGen.CreateInstanceRequestPayload
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "simple",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
model: &sqlserverflexbetaRs.InstanceModel{
|
||||
Encryption: sqlserverflexbetaRs.NewEncryptionValueMust(
|
||||
sqlserverflexbetaRs.EncryptionValue{}.AttributeTypes(context.Background()),
|
||||
map[string]attr.Value{
|
||||
"kek_key_id": types.StringValue("kek_key_id"),
|
||||
"kek_key_ring_id": types.StringValue("kek_key_ring_id"),
|
||||
"kek_key_version": types.StringValue("kek_key_version"),
|
||||
"service_account": types.StringValue("sacc"),
|
||||
},
|
||||
),
|
||||
Storage: sqlserverflexbetaRs.StorageValue{},
|
||||
},
|
||||
},
|
||||
want: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayload{
|
||||
BackupSchedule: nil,
|
||||
Encryption: &sqlserverflexbetaPkgGen.InstanceEncryption{
|
||||
KekKeyId: utils.Ptr("kek_key_id"),
|
||||
KekKeyRingId: utils.Ptr("kek_key_ring_id"),
|
||||
KekKeyVersion: utils.Ptr("kek_key_version"),
|
||||
ServiceAccount: utils.Ptr("sacc"),
|
||||
},
|
||||
FlavorId: nil,
|
||||
Name: nil,
|
||||
Network: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadNetwork{},
|
||||
RetentionDays: nil,
|
||||
Storage: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadGetStorageArgType{},
|
||||
Version: nil,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "nil object",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
model: &sqlserverflexbetaRs.InstanceModel{
|
||||
Encryption: sqlserverflexbetaRs.NewEncryptionValueNull(),
|
||||
Storage: sqlserverflexbetaRs.StorageValue{},
|
||||
},
|
||||
},
|
||||
want: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayload{
|
||||
BackupSchedule: nil,
|
||||
Encryption: nil,
|
||||
FlavorId: nil,
|
||||
Name: nil,
|
||||
Network: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadNetwork{},
|
||||
RetentionDays: nil,
|
||||
Storage: &sqlserverflexbetaPkgGen.CreateInstanceRequestPayloadGetStorageArgType{},
|
||||
Version: nil,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
got, err := toCreatePayload(tt.args.ctx, tt.args.model)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("toCreatePayload() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if diff := cmp.Diff(tt.want, got); diff != "" {
|
||||
t.Errorf("model mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_toUpdatePayload(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m *sqlserverflexbetaRs.InstanceModel
|
||||
resp *resource.UpdateResponse
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *sqlserverflexbetaPkgGen.UpdateInstanceRequestPayload
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
got, err := toUpdatePayload(tt.args.ctx, tt.args.m, tt.args.resp)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("toUpdatePayload() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("toUpdatePayload() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,124 +0,0 @@
|
|||
fields:
|
||||
- name: 'id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'instance_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'project_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'name'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'backup_schedule'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'encryption.kek_key_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.kek_key_version'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.kek_key_ring_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'encryption.service_account'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'network.access_scope'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'network.acl'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'network.instance_address'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'network.router_address'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'status'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'region'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'retention_days'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'edition'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'version'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'replicas'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'storage'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'storage.class'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'storage.size'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'flavor_id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'is_deletable'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
|
@ -1,548 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"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/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/pkg_gen/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/instance/resources_gen"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &instanceResource{}
|
||||
_ resource.ResourceWithConfigure = &instanceResource{}
|
||||
_ resource.ResourceWithImportState = &instanceResource{}
|
||||
_ resource.ResourceWithModifyPlan = &instanceResource{}
|
||||
_ resource.ResourceWithIdentity = &instanceResource{}
|
||||
)
|
||||
|
||||
func NewInstanceResource() resource.Resource {
|
||||
return &instanceResource{}
|
||||
}
|
||||
|
||||
type instanceResource struct {
|
||||
client *sqlserverflexbeta.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexbetaResGen.InstanceModel
|
||||
|
||||
type InstanceResourceIdentityModel struct {
|
||||
ProjectID types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
InstanceID types.String `tfsdk:"instance_id"`
|
||||
}
|
||||
|
||||
func (r *instanceResource) Metadata(
|
||||
_ context.Context,
|
||||
req resource.MetadataRequest,
|
||||
resp *resource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_instance"
|
||||
}
|
||||
|
||||
//go:embed planModifiers.yaml
|
||||
var modifiersFileByte []byte
|
||||
|
||||
func (r *instanceResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
s := sqlserverflexbetaResGen.InstanceResourceSchema(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 *instanceResource) 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
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *instanceResource) 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.Instance client configured")
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *instanceResource) 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
|
||||
}
|
||||
|
||||
if req.Plan.Raw.IsNull() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (r *instanceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var data resourceModel
|
||||
crateErr := "[SQL Server Flex BETA - 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()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(ctx, &data)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
crateErr,
|
||||
fmt.Sprintf("Creating API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Create new Instance
|
||||
createResp, err := r.client.CreateInstanceRequest(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
).CreateInstanceRequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, crateErr, fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
InstanceId := *createResp.Id
|
||||
|
||||
// Example data value setting
|
||||
data.InstanceId = types.StringValue("id-from-response")
|
||||
|
||||
identity := InstanceResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(InstanceId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
waitResp, err := wait.CreateInstanceWaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
projectId,
|
||||
InstanceId,
|
||||
region,
|
||||
).SetSleepBeforeWait(
|
||||
10 * time.Second,
|
||||
).SetTimeout(
|
||||
90 * time.Minute,
|
||||
).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
crateErr,
|
||||
fmt.Sprintf("Instance creation waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
crateErr,
|
||||
"Instance creation waiting: returned id is nil",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, waitResp, &data, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
crateErr,
|
||||
fmt.Sprintf("processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Save data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance created")
|
||||
}
|
||||
|
||||
func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := data.ProjectId.ValueString()
|
||||
region := data.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
instanceResp, err := r.client.GetInstanceRequest(ctx, projectId, region, instanceId).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 {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, instanceResp, &data, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error reading instance",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Save identity into Terraform state
|
||||
identity := InstanceResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance read")
|
||||
}
|
||||
|
||||
func (r *instanceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var data resourceModel
|
||||
updateInstanceError := "Error updating instance"
|
||||
|
||||
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()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
instanceId := data.InstanceId.ValueString()
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toUpdatePayload(ctx, &data, resp)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
updateInstanceError,
|
||||
fmt.Sprintf("Creating API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
// Update existing instance
|
||||
err = r.client.UpdateInstanceRequest(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
instanceId,
|
||||
).UpdateInstanceRequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, updateInstanceError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
waitResp, err := wait.
|
||||
UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).
|
||||
SetSleepBeforeWait(15 * time.Second).
|
||||
SetTimeout(45 * time.Minute).
|
||||
WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
updateInstanceError,
|
||||
fmt.Sprintf("Instance update waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapResponseToModel(ctx, waitResp, &data, resp.Diagnostics)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
updateInstanceError,
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
identity := InstanceResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Save updated data into Terraform state
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance updated")
|
||||
}
|
||||
|
||||
func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var data resourceModel
|
||||
|
||||
// Read Terraform prior state data into the model
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Read identity data
|
||||
var identityData InstanceResourceIdentityModel
|
||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := identityData.ProjectID.ValueString()
|
||||
region := identityData.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
instanceId := identityData.InstanceID.ValueString()
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
||||
// Delete existing instance
|
||||
err := r.client.DeleteInstanceRequest(ctx, projectId, region, instanceId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
delResp, err := wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error deleting instance",
|
||||
fmt.Sprintf("Instance deletion waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if delResp != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error deleting instance",
|
||||
"wait handler returned non nil result",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "sqlserverflexbeta.Instance deleted")
|
||||
}
|
||||
|
||||
// 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 *instanceResource) 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) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
core.LogAndAddError(
|
||||
ctx, &resp.Diagnostics,
|
||||
"Error importing instance",
|
||||
fmt.Sprintf(
|
||||
"Expected import identifier with format [project_id],[region],[instance_id] 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])...)
|
||||
return
|
||||
}
|
||||
|
||||
// If no ID is provided, attempt to read identity attributes from the import configuration
|
||||
var identityData InstanceResourceIdentityModel
|
||||
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()
|
||||
|
||||
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)...)
|
||||
|
||||
tflog.Info(ctx, "Sqlserverflexbeta instance state imported")
|
||||
}
|
||||
|
|
@ -1,494 +0,0 @@
|
|||
package sqlserverflexbeta_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
|
||||
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance"
|
||||
|
||||
// The fwresource import alias is so there is no collision
|
||||
// with the more typical acceptance testing import:
|
||||
// "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
)
|
||||
|
||||
const providerPrefix = "stackitprivatepreview_sqlserverflexbeta"
|
||||
|
||||
var testInstances []string
|
||||
|
||||
func init() {
|
||||
sweeperName := fmt.Sprintf("%s_%s", providerPrefix, "sweeper")
|
||||
|
||||
resource.AddTestSweepers(sweeperName, &resource.Sweeper{
|
||||
Name: sweeperName,
|
||||
F: func(region string) error {
|
||||
ctx := context.Background()
|
||||
apiClientConfigOptions := []config.ConfigurationOption{}
|
||||
apiClient, err := sqlserverflexbetaResGen.NewAPIClient(apiClientConfigOptions...)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, region).
|
||||
Size(100).
|
||||
Execute()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
for _, inst := range instances.GetInstances() {
|
||||
if strings.HasPrefix(inst.GetName(), "tf-acc-") {
|
||||
for _, item := range testInstances {
|
||||
if inst.GetName() == item {
|
||||
delErr := apiClient.DeleteInstanceRequestExecute(ctx, testutils.ProjectId, region, inst.GetId())
|
||||
if delErr != nil {
|
||||
// TODO: maybe just warn?
|
||||
log.Fatalln(delErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInstanceResourceSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx := context.Background()
|
||||
schemaRequest := fwresource.SchemaRequest{}
|
||||
schemaResponse := &fwresource.SchemaResponse{}
|
||||
|
||||
// Instantiate the resource.Resource and call its Schema method
|
||||
sqlserverflexbeta.NewInstanceResource().Schema(ctx, schemaRequest, schemaResponse)
|
||||
|
||||
if schemaResponse.Diagnostics.HasError() {
|
||||
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
|
||||
}
|
||||
|
||||
// Validate the schema
|
||||
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)
|
||||
|
||||
if diagnostics.HasError() {
|
||||
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
testutils.Setup()
|
||||
code := m.Run()
|
||||
// shutdown()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func testAccPreCheck(t *testing.T) {
|
||||
if _, ok := os.LookupEnv("TF_ACC_PROJECT_ID"); !ok {
|
||||
t.Fatalf("could not find env var TF_ACC_PROJECT_ID")
|
||||
}
|
||||
}
|
||||
|
||||
type resData struct {
|
||||
ServiceAccountFilePath string
|
||||
ProjectId string
|
||||
Region string
|
||||
Name string
|
||||
TfName string
|
||||
FlavorId string
|
||||
BackupSchedule string
|
||||
UseEncryption bool
|
||||
KekKeyId string
|
||||
KekKeyRingId string
|
||||
KekKeyVersion uint8
|
||||
KekServiceAccount string
|
||||
PerformanceClass string
|
||||
Size uint32
|
||||
AclString string
|
||||
AccessScope string
|
||||
RetentionDays uint32
|
||||
Version string
|
||||
Users []User
|
||||
Databases []Database
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
ProjectId string
|
||||
Roles []string
|
||||
}
|
||||
|
||||
type Database struct {
|
||||
Name string
|
||||
ProjectId string
|
||||
Owner string
|
||||
Collation string
|
||||
Compatibility string
|
||||
}
|
||||
|
||||
func resName(res, name string) string {
|
||||
return fmt.Sprintf("%s_%s.%s", providerPrefix, res, name)
|
||||
}
|
||||
|
||||
func getExample() resData {
|
||||
name := acctest.RandomWithPrefix("tf-acc")
|
||||
return resData{
|
||||
Region: os.Getenv("TF_ACC_REGION"),
|
||||
ServiceAccountFilePath: os.Getenv("TF_ACC_SERVICE_ACCOUNT_FILE"),
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Name: name,
|
||||
TfName: name,
|
||||
FlavorId: "4.16-Single",
|
||||
BackupSchedule: "0 0 * * *",
|
||||
UseEncryption: false,
|
||||
RetentionDays: 33,
|
||||
PerformanceClass: "premium-perf2-stackit",
|
||||
Size: 10,
|
||||
AclString: "0.0.0.0/0",
|
||||
AccessScope: "PUBLIC",
|
||||
Version: "2022",
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccInstance(t *testing.T) {
|
||||
exData := getExample()
|
||||
|
||||
updNameData := exData
|
||||
updNameData.Name = "name-updated"
|
||||
|
||||
updSizeData := exData
|
||||
updSizeData.Size = 25
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
testInstances = append(testInstances, exData.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
exData,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
|
||||
// TODO: check all fields
|
||||
),
|
||||
},
|
||||
// Update name and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
updNameData,
|
||||
),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", updNameData.Name),
|
||||
),
|
||||
},
|
||||
// Update size and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
updSizeData,
|
||||
),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(
|
||||
testutils.ResStr(providerPrefix, "instance", exData.TfName),
|
||||
"storage.size",
|
||||
strconv.Itoa(int(updSizeData.Size)),
|
||||
),
|
||||
),
|
||||
},
|
||||
{
|
||||
RefreshState: true,
|
||||
},
|
||||
//// Import test
|
||||
//{
|
||||
// ResourceName: resName("instance", exData.TfName),
|
||||
// ImportState: true,
|
||||
// ImportStateVerify: true,
|
||||
// },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccInstanceReApply(t *testing.T) {
|
||||
exData := getExample()
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
testInstances = append(testInstances, exData.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
exData,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
|
||||
// TODO: check all fields
|
||||
),
|
||||
},
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
exData,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
|
||||
// TODO: check all fields
|
||||
),
|
||||
},
|
||||
{
|
||||
RefreshState: true,
|
||||
},
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
exData,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
resource.TestCheckResourceAttr(resName("instance", exData.TfName), "name", exData.Name),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", exData.TfName), "id"),
|
||||
// TODO: check all fields
|
||||
),
|
||||
},
|
||||
// Import test
|
||||
{
|
||||
ResourceName: resName("instance", exData.TfName),
|
||||
ImportStateKind: resource.ImportBlockWithResourceIdentity,
|
||||
ImportState: true,
|
||||
// ImportStateVerify is not supported with plannable import blocks
|
||||
// ImportStateVerify: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccInstanceNoEncryption(t *testing.T) {
|
||||
data := getExample()
|
||||
|
||||
dbName := "testDb"
|
||||
userName := "testUser"
|
||||
data.Users = []User{
|
||||
{
|
||||
Name: userName,
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Roles: []string{
|
||||
"##STACKIT_DatabaseManager##",
|
||||
"##STACKIT_LoginManager##",
|
||||
"##STACKIT_ProcessManager##",
|
||||
"##STACKIT_SQLAgentManager##",
|
||||
"##STACKIT_SQLAgentUser##",
|
||||
"##STACKIT_ServerManager##",
|
||||
},
|
||||
},
|
||||
}
|
||||
data.Databases = []Database{
|
||||
{
|
||||
Name: dbName,
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Owner: userName,
|
||||
},
|
||||
}
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
testInstances = append(testInstances, data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
data,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
// check instance values are set
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "backup_schedule"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "edition"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "flavor_id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "instance_id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "is_deletable"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "name"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "replicas"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "retention_days"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "status"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "version"),
|
||||
|
||||
resource.TestCheckNoResourceAttr(resName("instance", data.TfName), "encryption"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.instance_address"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.router_address"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.class"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.size"),
|
||||
|
||||
// check instance values are correct
|
||||
resource.TestCheckResourceAttr(resName("instance", data.TfName), "name", data.Name),
|
||||
|
||||
// check user values are set
|
||||
resource.TestCheckResourceAttrSet(resName("user", userName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("user", userName), "username"),
|
||||
// resource.TestCheckResourceAttrSet(resName("user", userName), "roles"),
|
||||
|
||||
// func(s *terraform.State) error {
|
||||
// return nil
|
||||
// },
|
||||
|
||||
// check user values are correct
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "username", userName),
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", strconv.Itoa(len(data.Users[0].Roles))),
|
||||
|
||||
// check database values are set
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "name"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "owner"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "compatibility"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "collation"),
|
||||
|
||||
// check database values are correct
|
||||
resource.TestCheckResourceAttr(resName("database", dbName), "name", dbName),
|
||||
resource.TestCheckResourceAttr(resName("database", dbName), "owner", userName),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccInstanceEncryption(t *testing.T) {
|
||||
data := getExample()
|
||||
|
||||
dbName := "testDb"
|
||||
userName := "testUser"
|
||||
data.Users = []User{
|
||||
{
|
||||
Name: userName,
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Roles: []string{"##STACKIT_DatabaseManager##", "##STACKIT_LoginManager##"},
|
||||
},
|
||||
}
|
||||
data.Databases = []Database{
|
||||
{
|
||||
Name: dbName,
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Owner: userName,
|
||||
},
|
||||
}
|
||||
|
||||
data.UseEncryption = true
|
||||
data.KekKeyId = "fe039bcf-8d7b-431a-801d-9e81371a6b7b"
|
||||
data.KekKeyRingId = "6a2d95ab-3c4c-4963-a2bb-08d17a320e27"
|
||||
data.KekKeyVersion = 1
|
||||
data.KekServiceAccount = "henselinm-u2v3ex1@sa.stackit.cloud"
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
testInstances = append(testInstances, data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
{
|
||||
Config: testutils.StringFromTemplateMust(
|
||||
"testdata/instance_template.gompl",
|
||||
data,
|
||||
),
|
||||
Check: resource.ComposeAggregateTestCheckFunc(
|
||||
// check instance values are set
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "backup_schedule"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "edition"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "flavor_id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "instance_id"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "is_deletable"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "name"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "replicas"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "retention_days"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "status"),
|
||||
resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "version"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_id"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_version"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.kek_key_ring_id"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "encryption.service_account"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.access_scope"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.acl"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.instance_address"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "network.router_address"),
|
||||
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.class"),
|
||||
// resource.TestCheckResourceAttrSet(resName("instance", data.TfName), "storage.size"),
|
||||
|
||||
// check instance values are correct
|
||||
resource.TestCheckResourceAttr(resName("instance", data.TfName), "name", data.Name),
|
||||
|
||||
// check user values are set
|
||||
resource.TestCheckResourceAttrSet(resName("user", userName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("user", userName), "username"),
|
||||
|
||||
// func(s *terraform.State) error {
|
||||
// return nil
|
||||
// },
|
||||
|
||||
// check user values are correct
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "username", userName),
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", "2"),
|
||||
|
||||
// check database values are set
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "id"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "name"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "owner"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "compatibility"),
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "collation"),
|
||||
|
||||
// check database values are correct
|
||||
resource.TestCheckResourceAttr(resName("database", dbName), "name", dbName),
|
||||
resource.TestCheckResourceAttr(resName("database", dbName), "owner", userName),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
provider "stackitprivatepreview" {
|
||||
default_region = "{{ .Region }}"
|
||||
service_account_key_path = "{{ .ServiceAccountFilePath }}"
|
||||
}
|
||||
|
||||
resource "stackitprivatepreview_sqlserverflexbeta_instance" "{{ .TfName }}" {
|
||||
project_id = "{{ .ProjectId }}"
|
||||
name = "{{ .Name }}"
|
||||
backup_schedule = "{{ .BackupSchedule }}"
|
||||
retention_days = {{ .RetentionDays }}
|
||||
flavor_id = "{{ .FlavorId }}"
|
||||
storage = {
|
||||
class = "{{ .PerformanceClass }}"
|
||||
size = {{ .Size }}
|
||||
}
|
||||
{{ if .UseEncryption }}
|
||||
encryption = {
|
||||
kek_key_id = "{{ .KekKeyId }}"
|
||||
kek_key_ring_id = "{{ .KekKeyRingId }}"
|
||||
kek_key_version = {{ .KekKeyVersion }}
|
||||
service_account = "{{ .KekServiceAccount }}"
|
||||
}
|
||||
{{ end }}
|
||||
network = {
|
||||
acl = ["{{ .AclString }}"]
|
||||
access_scope = "{{ .AccessScope }}"
|
||||
}
|
||||
version = "{{ .Version }}"
|
||||
}
|
||||
|
||||
{{ if .Users }}
|
||||
{{ $tfName := .TfName }}
|
||||
{{ range $user := .Users }}
|
||||
resource "stackitprivatepreview_sqlserverflexbeta_user" "{{ $user.Name }}" {
|
||||
project_id = "{{ $user.ProjectId }}"
|
||||
instance_id = stackitprivatepreview_sqlserverflexbeta_instance.{{ $tfName }}.instance_id
|
||||
username = "{{ $user.Name }}"
|
||||
roles = [{{ range $i, $v := $user.Roles }}{{if $i}},{{end}}"{{$v}}"{{end}}]
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ if .Databases }}
|
||||
{{ $tfName := .TfName }}
|
||||
{{ range $db := .Databases }}
|
||||
resource "stackitprivatepreview_sqlserverflexbeta_database" "{{ $db.Name }}" {
|
||||
depends_on = [stackitprivatepreview_sqlserverflexbeta_user.{{ $db.Owner }}]
|
||||
project_id = "{{ $db.ProjectId }}"
|
||||
instance_id = stackitprivatepreview_sqlserverflexbeta_instance.{{ $tfName }}.instance_id
|
||||
name = "{{ $db.Name }}"
|
||||
owner = "{{ $db.Owner }}"
|
||||
{{ if $db.Collation }}
|
||||
collation = "{{ $db.Collation }}"
|
||||
{{ end }}
|
||||
{{ if $db.Compatibility }}
|
||||
compatibility = "{{ $db.Compatibility }}"
|
||||
{{ end }}
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
|
@ -1,139 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
|
||||
"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"
|
||||
sqlserverflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
|
||||
"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/user/datasources_gen"
|
||||
)
|
||||
|
||||
var _ datasource.DataSource = (*userDataSource)(nil)
|
||||
|
||||
func NewUserDataSource() datasource.DataSource {
|
||||
return &userDataSource{}
|
||||
}
|
||||
|
||||
type dataSourceModel struct {
|
||||
DefaultDatabase types.String `tfsdk:"default_database"`
|
||||
Host types.String `tfsdk:"host"`
|
||||
Id types.String `tfsdk:"id"`
|
||||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
Port types.Int64 `tfsdk:"port"`
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
Roles types.List `tfsdk:"roles"`
|
||||
Status types.String `tfsdk:"status"`
|
||||
UserId types.Int64 `tfsdk:"user_id"`
|
||||
Username types.String `tfsdk:"username"`
|
||||
}
|
||||
|
||||
type userDataSource struct {
|
||||
client *sqlserverflexbetaPkg.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (d *userDataSource) Metadata(
|
||||
_ context.Context,
|
||||
req datasource.MetadataRequest,
|
||||
resp *datasource.MetadataResponse,
|
||||
) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_user"
|
||||
}
|
||||
|
||||
func (d *userDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
resp.Schema = sqlserverflexbetaGen.UserDataSourceSchema(ctx)
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the data source.
|
||||
func (d *userDataSource) 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, "SQL SERVER Flex alpha database client configured")
|
||||
}
|
||||
|
||||
func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
|
||||
var model dataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueInt64()
|
||||
region := d.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
recordSetResp, err := d.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
err,
|
||||
"Reading user",
|
||||
fmt.Sprintf(
|
||||
"User with ID %q or instance with ID %q does not exist in project %q.",
|
||||
userId,
|
||||
instanceId,
|
||||
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)
|
||||
|
||||
// Map response body to schema and populate Computed attribute values
|
||||
err = mapDataSourceFields(recordSetResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error reading user",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "SQLServer Flex beta instance read")
|
||||
}
|
||||
|
|
@ -1,198 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
"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"
|
||||
"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"
|
||||
)
|
||||
|
||||
// mapDataSourceFields maps the API response to a dataSourceModel.
|
||||
func mapDataSourceFields(userResp *sqlserverflexbeta.GetUserResponse, model *dataSourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
user := userResp
|
||||
|
||||
// Handle user ID
|
||||
var userId int64
|
||||
if model.UserId.ValueInt64() != 0 {
|
||||
userId = model.UserId.ValueInt64()
|
||||
} else if user.Id != nil {
|
||||
userId = *user.Id
|
||||
} else {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
|
||||
// Set main attributes
|
||||
model.Id = utils.BuildInternalTerraformId(
|
||||
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
|
||||
)
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Username = types.StringPointerValue(user.Username)
|
||||
|
||||
// Map roles
|
||||
if user.Roles == nil {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
} else {
|
||||
var roles []attr.Value
|
||||
resRoles := *user.Roles
|
||||
slices.Sort(resRoles)
|
||||
for _, role := range resRoles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
rolesSet, diags := types.SetValue(types.StringType, roles)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
|
||||
// Set remaining attributes
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Region = types.StringValue(region)
|
||||
model.Status = types.StringPointerValue(user.Status)
|
||||
model.DefaultDatabase = types.StringPointerValue(user.DefaultDatabase)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// mapFields maps the API response to a resourceModel.
|
||||
func mapFields(userResp *sqlserverflexbeta.GetUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
user := userResp
|
||||
|
||||
// Handle user ID
|
||||
var userId int64
|
||||
if model.UserId.ValueInt64() != 0 {
|
||||
userId = model.UserId.ValueInt64()
|
||||
} else if user.Id != nil {
|
||||
userId = *user.Id
|
||||
} else {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
|
||||
// Set main attributes
|
||||
model.Id = types.Int64Value(userId)
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Username = types.StringPointerValue(user.Username)
|
||||
|
||||
// Map roles
|
||||
if userResp.Roles != nil {
|
||||
resRoles := *userResp.Roles
|
||||
slices.Sort(resRoles)
|
||||
|
||||
var roles []attr.Value
|
||||
for _, role := range resRoles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
|
||||
rolesSet, diags := types.ListValue(types.StringType, roles)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = rolesSet
|
||||
}
|
||||
|
||||
// Ensure roles is not null
|
||||
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
}
|
||||
|
||||
// Set connection details
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Region = types.StringValue(region)
|
||||
return nil
|
||||
}
|
||||
|
||||
// mapFieldsCreate maps the API response from creating a user to a resourceModel.
|
||||
func mapFieldsCreate(userResp *sqlserverflexbeta.CreateUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
user := userResp
|
||||
|
||||
if user.Id == nil {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
userId := *user.Id
|
||||
model.Id = types.Int64Value(userId)
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Username = types.StringPointerValue(user.Username)
|
||||
|
||||
if user.Password == nil {
|
||||
return fmt.Errorf("user password not present")
|
||||
}
|
||||
model.Password = types.StringValue(*user.Password)
|
||||
|
||||
if user.Roles != nil {
|
||||
resRoles := *user.Roles
|
||||
slices.Sort(resRoles)
|
||||
|
||||
var roles []attr.Value
|
||||
for _, role := range resRoles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
rolesList, diags := types.ListValue(types.StringType, roles)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = rolesList
|
||||
}
|
||||
|
||||
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
}
|
||||
|
||||
model.Password = types.StringPointerValue(user.Password)
|
||||
model.Uri = types.StringPointerValue(user.Uri)
|
||||
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Region = types.StringValue(region)
|
||||
model.Status = types.StringPointerValue(user.Status)
|
||||
model.DefaultDatabase = types.StringPointerValue(user.DefaultDatabase)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// toCreatePayload converts a resourceModel to an API CreateUserRequestPayload.
|
||||
func toCreatePayload(
|
||||
model *resourceModel,
|
||||
roles []string,
|
||||
) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
pl := sqlserverflexbeta.CreateUserRequestPayload{
|
||||
Username: conversion.StringValueToPointer(model.Username),
|
||||
Roles: &roles,
|
||||
}
|
||||
slices.Sort(roles)
|
||||
if !model.DefaultDatabase.IsNull() || !model.DefaultDatabase.IsUnknown() {
|
||||
pl.DefaultDatabase = conversion.StringValueToPointer(model.DefaultDatabase)
|
||||
}
|
||||
|
||||
return &pl, nil
|
||||
}
|
||||
|
|
@ -1,527 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
)
|
||||
|
||||
func TestMapDataSourceFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *sqlserverflexbeta.GetUserResponse
|
||||
region string
|
||||
expected dataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
DefaultDatabase: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&sqlserverflexbeta.GetUserResponse{
|
||||
Roles: &[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
"",
|
||||
},
|
||||
Username: utils.Ptr("username"),
|
||||
Host: utils.Ptr("host"),
|
||||
Port: utils.Ptr(int64(1234)),
|
||||
Status: utils.Ptr("active"),
|
||||
DefaultDatabase: utils.Ptr("default_db"),
|
||||
},
|
||||
testRegion,
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringValue("username"),
|
||||
Roles: types.List(
|
||||
types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue(""),
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
},
|
||||
),
|
||||
),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringValue("active"),
|
||||
DefaultDatabase: types.StringValue("default_db"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&sqlserverflexbeta.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
Host: nil,
|
||||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
dataSourceModel{
|
||||
Id: types.StringValue("pid,region,iid,1"),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
dataSourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &dataSourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
}
|
||||
err := mapDataSourceFields(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(&tt.expected, state)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapFieldsCreate(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *sqlserverflexbeta.CreateUserResponse
|
||||
region string
|
||||
expected resourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&sqlserverflexbeta.CreateUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Password: utils.Ptr(""),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Password: types.StringValue(""),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&sqlserverflexbeta.CreateUserResponse{
|
||||
Id: utils.Ptr(int64(2)),
|
||||
Roles: &[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
"",
|
||||
},
|
||||
Username: utils.Ptr("username"),
|
||||
Password: utils.Ptr("password"),
|
||||
Host: utils.Ptr("host"),
|
||||
Port: utils.Ptr(int64(1234)),
|
||||
Status: utils.Ptr("status"),
|
||||
DefaultDatabase: utils.Ptr("default_db"),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(2),
|
||||
UserId: types.Int64Value(2),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringValue("username"),
|
||||
Roles: types.List(
|
||||
types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue(""),
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
},
|
||||
),
|
||||
),
|
||||
Password: types.StringValue("password"),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringValue("status"),
|
||||
DefaultDatabase: types.StringValue("default_db"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&sqlserverflexbeta.CreateUserResponse{
|
||||
Id: utils.Ptr(int64(3)),
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
Password: utils.Ptr(""),
|
||||
Host: nil,
|
||||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(3),
|
||||
UserId: types.Int64Value(3),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
||||
Password: types.StringValue(""),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
DefaultDatabase: types.StringNull(),
|
||||
Status: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexbeta.CreateUserResponse{},
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexbeta.CreateUserResponse{},
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_password",
|
||||
&sqlserverflexbeta.CreateUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &resourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
}
|
||||
err := mapFieldsCreate(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(&tt.expected, state)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *sqlserverflexbeta.GetUserResponse
|
||||
region string
|
||||
expected resourceModel
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"simple_values",
|
||||
&sqlserverflexbeta.GetUserResponse{
|
||||
Roles: &[]string{
|
||||
"role_2",
|
||||
"role_1",
|
||||
"",
|
||||
},
|
||||
Username: utils.Ptr("username"),
|
||||
Host: utils.Ptr("host"),
|
||||
Port: utils.Ptr(int64(1234)),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(2),
|
||||
UserId: types.Int64Value(2),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringValue("username"),
|
||||
Roles: types.List(
|
||||
types.SetValueMust(
|
||||
types.StringType, []attr.Value{
|
||||
types.StringValue(""),
|
||||
types.StringValue("role_1"),
|
||||
types.StringValue("role_2"),
|
||||
},
|
||||
),
|
||||
),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&sqlserverflexbeta.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
Host: nil,
|
||||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Username: types.StringNull(),
|
||||
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no_resource_id",
|
||||
&sqlserverflexbeta.GetUserResponse{},
|
||||
testRegion,
|
||||
resourceModel{},
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
state := &resourceModel{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
}
|
||||
err := mapFields(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(&tt.expected, state)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input *resourceModel
|
||||
inputRoles []string
|
||||
expected *sqlserverflexbeta.CreateUserRequestPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&resourceModel{},
|
||||
[]string{},
|
||||
&sqlserverflexbeta.CreateUserRequestPayload{
|
||||
Roles: &[]string{},
|
||||
Username: nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"default_values",
|
||||
&resourceModel{
|
||||
Username: types.StringValue("username"),
|
||||
},
|
||||
[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
&sqlserverflexbeta.CreateUserRequestPayload{
|
||||
Roles: &[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
Username: utils.Ptr("username"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&resourceModel{
|
||||
Username: types.StringNull(),
|
||||
},
|
||||
[]string{
|
||||
"",
|
||||
},
|
||||
&sqlserverflexbeta.CreateUserRequestPayload{
|
||||
Roles: &[]string{
|
||||
"",
|
||||
},
|
||||
Username: nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
[]string{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_roles",
|
||||
&resourceModel{
|
||||
Username: types.StringValue("username"),
|
||||
},
|
||||
[]string{},
|
||||
&sqlserverflexbeta.CreateUserRequestPayload{
|
||||
Roles: &[]string{},
|
||||
Username: utils.Ptr("username"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
output, err := toCreatePayload(tt.input, tt.inputRoles)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
fields:
|
||||
- name: 'id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'instance_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'project_id'
|
||||
validators:
|
||||
- validate.NoSeparator
|
||||
- validate.UUID
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'region'
|
||||
modifiers:
|
||||
- 'RequiresReplace'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'user_id'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'username'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'roles'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
- 'RequiresReplace'
|
||||
|
||||
- name: 'password'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'uri'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
||||
- name: 'status'
|
||||
modifiers:
|
||||
- 'UseStateForUnknown'
|
||||
|
|
@ -1,577 +0,0 @@
|
|||
package sqlserverflexbeta
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"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/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"
|
||||
sqlserverflexbetaUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/utils"
|
||||
sqlserverflexbetaWait "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/user/resources_gen"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &userResource{}
|
||||
_ resource.ResourceWithConfigure = &userResource{}
|
||||
_ resource.ResourceWithImportState = &userResource{}
|
||||
_ resource.ResourceWithModifyPlan = &userResource{}
|
||||
_ resource.ResourceWithIdentity = &userResource{}
|
||||
_ resource.ResourceWithValidateConfig = &userResource{}
|
||||
)
|
||||
|
||||
func NewUserResource() resource.Resource {
|
||||
return &userResource{}
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = sqlserverflexbetaResGen.UserModel
|
||||
|
||||
// UserResourceIdentityModel describes the resource's identity attributes.
|
||||
type UserResourceIdentityModel struct {
|
||||
ProjectID types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
InstanceID types.String `tfsdk:"instance_id"`
|
||||
UserID types.Int64 `tfsdk:"user_id"`
|
||||
}
|
||||
|
||||
type userResource struct {
|
||||
client *sqlserverflexbeta.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
func (r *userResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_sqlserverflexbeta_user"
|
||||
}
|
||||
|
||||
// Configure adds the provider configured client to the resource.
|
||||
func (r *userResource) 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
|
||||
}
|
||||
|
||||
apiClient := sqlserverflexbetaUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
r.client = apiClient
|
||||
tflog.Info(ctx, "SQLServer Beta Flex user client configured")
|
||||
}
|
||||
|
||||
// ModifyPlan implements resource.ResourceWithModifyPlan.
|
||||
// Use the modifier to set the effective region in the current plan.
|
||||
func (r *userResource) 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
|
||||
}
|
||||
|
||||
//// TODO: verify if this is needed - START
|
||||
// var planRoles []string
|
||||
// diags := planModel.Roles.ElementsAs(ctx, &planRoles, false)
|
||||
// resp.Diagnostics.Append(diags...)
|
||||
// if diags.HasError() {
|
||||
// return
|
||||
//}
|
||||
// slices.Sort(planRoles)
|
||||
// var roles []attr.Value
|
||||
// for _, role := range planRoles {
|
||||
// roles = append(roles, types.StringValue(string(role)))
|
||||
//}
|
||||
// rolesSet, diags := types.ListValue(types.StringType, roles)
|
||||
// resp.Diagnostics.Append(diags...)
|
||||
// if diags.HasError() {
|
||||
// return
|
||||
//}
|
||||
// planModel.Roles = rolesSet
|
||||
//// TODO: verify if this is needed - END
|
||||
|
||||
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed planModifiers.yaml
|
||||
var modifiersFileByte []byte
|
||||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *userResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
s := sqlserverflexbetaResGen.UserResourceSchema(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
|
||||
}
|
||||
|
||||
// IdentitySchema defines the schema for the resource's identity attributes.
|
||||
func (r *userResource) IdentitySchema(
|
||||
_ context.Context,
|
||||
_ resource.IdentitySchemaRequest,
|
||||
response *resource.IdentitySchemaResponse,
|
||||
) {
|
||||
response.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
|
||||
},
|
||||
"user_id": identityschema.Int64Attribute{
|
||||
RequiredForImport: true, // can be defaulted by the provider configuration
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *userResource) ValidateConfig(
|
||||
ctx context.Context,
|
||||
req resource.ValidateConfigRequest,
|
||||
resp *resource.ValidateConfigResponse,
|
||||
) {
|
||||
var data resourceModel
|
||||
|
||||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var roles []string
|
||||
diags := data.Roles.ElementsAs(ctx, &roles, false)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
var resRoles []string
|
||||
for _, role := range roles {
|
||||
if slices.Contains(resRoles, role) {
|
||||
resp.Diagnostics.AddAttributeError(
|
||||
path.Root("roles"),
|
||||
"Attribute Configuration Error",
|
||||
"defined roles MUST NOT contain duplicates",
|
||||
)
|
||||
return
|
||||
}
|
||||
resRoles = append(resRoles, role)
|
||||
}
|
||||
}
|
||||
|
||||
// Create creates the resource and sets the initial Terraform state.
|
||||
func (r *userResource) Create(
|
||||
ctx context.Context,
|
||||
req resource.CreateRequest,
|
||||
resp *resource.CreateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
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()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
var roles []string
|
||||
if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
diags = model.Roles.ElementsAs(ctx, &roles, false)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
slices.Sort(roles)
|
||||
}
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(&model, roles)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Creating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
// Create new user
|
||||
userResp, err := r.client.CreateUserRequest(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
instanceId,
|
||||
).CreateUserRequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
if userResp == nil || userResp.Id == nil || *userResp.Id == 0 {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating user",
|
||||
"API didn't return user Id. A user might have been created",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
userId := *userResp.Id
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
UserID: types.Int64Value(userId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
err = mapFieldsCreate(userResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating user",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
waitResp, err := sqlserverflexbetaWait.CreateUserWaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
projectId,
|
||||
instanceId,
|
||||
region,
|
||||
userId,
|
||||
).SetSleepBeforeWait(
|
||||
90 * time.Second,
|
||||
).SetTimeout(
|
||||
90 * time.Minute,
|
||||
).WaitWithContext(ctx)
|
||||
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"create user",
|
||||
fmt.Sprintf("Instance creation waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"create user",
|
||||
"Instance creation waiting: returned id is nil",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(waitResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error creating user",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "SQLServer Flex user created")
|
||||
}
|
||||
|
||||
// Read refreshes the Terraform state with the latest data.
|
||||
func (r *userResource) Read(
|
||||
ctx context.Context,
|
||||
req resource.ReadRequest,
|
||||
resp *resource.ReadResponse,
|
||||
) { // 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
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueInt64()
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
recordSetResp, err := r.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(
|
||||
err,
|
||||
&oapiErr,
|
||||
)
|
||||
//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 {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(recordSetResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error reading user",
|
||||
fmt.Sprintf("Processing API payload: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
UserID: types.Int64Value(userId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
tflog.Info(ctx, "SQLServer Flex user read")
|
||||
}
|
||||
|
||||
// Update updates the resource and sets the updated Terraform state on success.
|
||||
func (r *userResource) Update(
|
||||
ctx context.Context,
|
||||
_ resource.UpdateRequest,
|
||||
resp *resource.UpdateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
// Update shouldn't be called
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error updating user",
|
||||
"an SQL server user can not be updated, only created",
|
||||
)
|
||||
}
|
||||
|
||||
// Delete deletes the resource and removes the Terraform state on success.
|
||||
func (r *userResource) Delete(
|
||||
ctx context.Context,
|
||||
req resource.DeleteRequest,
|
||||
resp *resource.DeleteResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
// Retrieve values from plan
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueInt64()
|
||||
region := model.Region.ValueString()
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
// Delete existing record set
|
||||
// err := r.client.DeleteUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
err := r.client.DeleteUserRequestExecute(ctx, projectId, region, instanceId, userId)
|
||||
if err != nil {
|
||||
var oapiErr *oapierror.GenericOpenAPIError
|
||||
ok := errors.As(err, &oapiErr)
|
||||
if !ok {
|
||||
// TODO err handling
|
||||
return
|
||||
}
|
||||
|
||||
switch oapiErr.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
// case http.StatusInternalServerError:
|
||||
// tflog.Warn(ctx, "[delete user] Wait handler got error 500")
|
||||
// return false, nil, nil
|
||||
default:
|
||||
// TODO err handling
|
||||
return
|
||||
}
|
||||
}
|
||||
// Delete existing record set
|
||||
_, err = sqlserverflexbetaWait.DeleteUserWaitHandler(ctx, r.client, projectId, region, instanceId, userId).
|
||||
WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "User Delete Error", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
resp.State.RemoveResource(ctx)
|
||||
|
||||
tflog.Info(ctx, "SQLServer Flex user deleted")
|
||||
}
|
||||
|
||||
// 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 *userResource) 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 user",
|
||||
fmt.Sprintf(
|
||||
"Expected import identifier with format [project_id],[region],[instance_id],[user_id], got %q",
|
||||
req.ID,
|
||||
),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := strconv.ParseInt(idParts[3], 10, 64)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"Error importing user",
|
||||
fmt.Sprintf("Invalid user_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("user_id"), userId)...)
|
||||
|
||||
tflog.Info(ctx, "SQLServer Flex user state imported")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// If no ID is provided, attempt to read identity attributes from the import configuration
|
||||
var identityData UserResourceIdentityModel
|
||||
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()
|
||||
userId := identityData.UserID.ValueInt64()
|
||||
|
||||
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("user_id"), userId)...)
|
||||
|
||||
core.LogAndAddWarning(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"SQLServer Flex user imported with empty password",
|
||||
"The user password is not imported as it is only available upon creation of a new user. The password field will be empty.",
|
||||
)
|
||||
tflog.Info(ctx, "SQLServer Flex user state imported")
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
sqlserverflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func ConfigureClient(
|
||||
ctx context.Context,
|
||||
providerData *core.ProviderData,
|
||||
diags *diag.Diagnostics,
|
||||
) *sqlserverflex.APIClient {
|
||||
apiClientConfigOptions := []config.ConfigurationOption{
|
||||
config.WithCustomAuth(providerData.RoundTripper),
|
||||
utils.UserAgentConfigOption(providerData.Version),
|
||||
}
|
||||
if providerData.SQLServerFlexCustomEndpoint != "" {
|
||||
apiClientConfigOptions = append(
|
||||
apiClientConfigOptions,
|
||||
config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion()))
|
||||
}
|
||||
apiClient, err := sqlserverflex.NewAPIClient(apiClientConfigOptions...)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
diags,
|
||||
"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 nil
|
||||
}
|
||||
|
||||
return apiClient
|
||||
}
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
|
||||
sqlserverflex "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/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
testVersion = "1.2.3"
|
||||
testCustomEndpoint = "https://sqlserverflex-custom-endpoint.api.stackit.cloud"
|
||||
)
|
||||
|
||||
func TestConfigureClient(t *testing.T) {
|
||||
/* mock authentication by setting service account token env variable */
|
||||
os.Clearenv()
|
||||
err := os.Setenv(sdkClients.ServiceAccountToken, "mock-val")
|
||||
if err != nil {
|
||||
t.Errorf("error setting env variable: %v", err)
|
||||
}
|
||||
|
||||
type args struct {
|
||||
providerData *core.ProviderData
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
expected *sqlserverflex.APIClient
|
||||
}{
|
||||
{
|
||||
name: "default endpoint",
|
||||
args: args{
|
||||
providerData: &core.ProviderData{
|
||||
Version: testVersion,
|
||||
},
|
||||
},
|
||||
expected: func() *sqlserverflex.APIClient {
|
||||
apiClient, err := sqlserverflex.NewAPIClient(
|
||||
config.WithRegion("eu01"),
|
||||
utils.UserAgentConfigOption(testVersion),
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("error configuring client: %v", err)
|
||||
}
|
||||
return apiClient
|
||||
}(),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "custom endpoint",
|
||||
args: args{
|
||||
providerData: &core.ProviderData{
|
||||
Version: testVersion,
|
||||
SQLServerFlexCustomEndpoint: testCustomEndpoint,
|
||||
},
|
||||
},
|
||||
expected: func() *sqlserverflex.APIClient {
|
||||
apiClient, err := sqlserverflex.NewAPIClient(
|
||||
utils.UserAgentConfigOption(testVersion),
|
||||
config.WithEndpoint(testCustomEndpoint),
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("error configuring client: %v", err)
|
||||
}
|
||||
return apiClient
|
||||
}(),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
actual := ConfigureClient(ctx, tt.args.providerData, &diags)
|
||||
if diags.HasError() != tt.wantErr {
|
||||
t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, tt.expected) {
|
||||
t.Errorf("ConfigureClient() = %v, want %v", actual, tt.expected)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue