fix: builder and sdk changes (#81)
## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: marcel.henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #81
This commit is contained in:
parent
635a9abf20
commit
1033d7e034
145 changed files with 5944 additions and 5298 deletions
|
|
@ -11,13 +11,13 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
"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"
|
||||
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||
pgDsGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
)
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
|
|
@ -32,13 +32,13 @@ func NewDatabaseDataSource() datasource.DataSource {
|
|||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
pgDsGen.DatabaseModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// databaseDataSource is the data source implementation.
|
||||
type databaseDataSource struct {
|
||||
client *postgresflexalpha.APIClient
|
||||
client *v3alpha1api.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +73,7 @@ func (r *databaseDataSource) Configure(
|
|||
|
||||
// Schema defines the schema for the data source.
|
||||
func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
s := postgresflexalpha2.DatabaseDataSourceSchema(ctx)
|
||||
s := pgDsGen.DatabaseDataSourceSchema(ctx)
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
||||
"`database_id`\\\".\",",
|
||||
|
|
@ -144,7 +144,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
|||
model *dataSourceModel,
|
||||
projectId, region, instanceId string,
|
||||
diags *diag.Diagnostics,
|
||||
) (*postgresflexalpha.ListDatabase, error) {
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
isIdSet := !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown()
|
||||
isNameSet := !model.Name.IsNull() && !model.Name.IsUnknown()
|
||||
|
||||
|
|
@ -159,12 +159,12 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
|||
if isIdSet {
|
||||
databaseId := model.DatabaseId.ValueInt64()
|
||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
return getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
|
||||
return getDatabaseById(ctx, r.client.DefaultAPI, projectId, region, instanceId, databaseId)
|
||||
}
|
||||
|
||||
databaseName := model.Name.ValueString()
|
||||
ctx = tflog.SetField(ctx, "name", databaseName)
|
||||
return getDatabaseByName(ctx, r.client, projectId, region, instanceId, databaseName)
|
||||
return getDatabaseByName(ctx, r.client.DefaultAPI, projectId, region, instanceId, databaseName)
|
||||
}
|
||||
|
||||
// handleReadError centralizes API error handling for the Read operation.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
)
|
||||
|
||||
// databaseClientReader represents the contract to listing databases from postgresflex.APIClient.
|
||||
|
|
@ -15,7 +15,7 @@ type databaseClientReader interface {
|
|||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) postgresflex.ApiListDatabasesRequestRequest
|
||||
) v3alpha1api.ApiListDatabasesRequestRequest
|
||||
}
|
||||
|
||||
// getDatabaseById gets a database by its ID.
|
||||
|
|
@ -24,9 +24,9 @@ func getDatabaseById(
|
|||
client databaseClientReader,
|
||||
projectId, region, instanceId string,
|
||||
databaseId int64,
|
||||
) (*postgresflex.ListDatabase, error) {
|
||||
filter := func(db postgresflex.ListDatabase) bool {
|
||||
return db.Id != nil && *db.Id == databaseId
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
filter := func(db v3alpha1api.ListDatabase) bool {
|
||||
return int64(db.Id) == databaseId
|
||||
}
|
||||
return getDatabase(ctx, client, projectId, region, instanceId, filter)
|
||||
}
|
||||
|
|
@ -36,9 +36,9 @@ func getDatabaseByName(
|
|||
ctx context.Context,
|
||||
client databaseClientReader,
|
||||
projectId, region, instanceId, databaseName string,
|
||||
) (*postgresflex.ListDatabase, error) {
|
||||
filter := func(db postgresflex.ListDatabase) bool {
|
||||
return db.Name != nil && *db.Name == databaseName
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
filter := func(db v3alpha1api.ListDatabase) bool {
|
||||
return db.Name == databaseName
|
||||
}
|
||||
return getDatabase(ctx, client, projectId, region, instanceId, filter)
|
||||
}
|
||||
|
|
@ -49,8 +49,8 @@ func getDatabase(
|
|||
ctx context.Context,
|
||||
client databaseClientReader,
|
||||
projectId, region, instanceId string,
|
||||
filter func(db postgresflex.ListDatabase) bool,
|
||||
) (*postgresflex.ListDatabase, error) {
|
||||
filter func(db v3alpha1api.ListDatabase) bool,
|
||||
) (*v3alpha1api.ListDatabase, error) {
|
||||
if projectId == "" || region == "" || instanceId == "" {
|
||||
return nil, fmt.Errorf("all parameters (project, region, instance) are required")
|
||||
}
|
||||
|
|
@ -59,18 +59,18 @@ func getDatabase(
|
|||
|
||||
for page := int32(1); ; page++ {
|
||||
res, err := client.ListDatabasesRequest(ctx, projectId, region, instanceId).
|
||||
Page(page).Size(pageSize).Sort(postgresflex.DATABASESORT_DATABASE_ID_ASC).Execute()
|
||||
Page(page).Size(pageSize).Sort(v3alpha1api.DATABASESORT_DATABASE_ID_ASC).Execute()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("requesting database list (page %d): %w", page, err)
|
||||
}
|
||||
|
||||
// If the API returns no databases, we have reached the end of the list.
|
||||
if res.Databases == nil || len(*res.Databases) == 0 {
|
||||
if len(res.Databases) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Iterate over databases to find a match
|
||||
for _, db := range *res.Databases {
|
||||
for _, db := range res.Databases {
|
||||
if filter(db) {
|
||||
foundDb := db
|
||||
return &foundDb, nil
|
||||
|
|
@ -82,10 +82,6 @@ func getDatabase(
|
|||
}
|
||||
|
||||
// cleanString removes leading and trailing quotes which are sometimes returned by the API.
|
||||
func cleanString(s *string) *string {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
res := strings.Trim(*s, "\"")
|
||||
return &res
|
||||
func cleanString(s string) string {
|
||||
return strings.Trim(s, "\"")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,127 +5,99 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
)
|
||||
|
||||
type mockRequest struct {
|
||||
executeFunc func() (*postgresflex.ListDatabasesResponse, error)
|
||||
}
|
||||
|
||||
func (m *mockRequest) Page(_ int32) postgresflex.ApiListDatabasesRequestRequest { return m }
|
||||
func (m *mockRequest) Size(_ int32) postgresflex.ApiListDatabasesRequestRequest { return m }
|
||||
func (m *mockRequest) Sort(_ postgresflex.DatabaseSort) postgresflex.ApiListDatabasesRequestRequest {
|
||||
return m
|
||||
}
|
||||
func (m *mockRequest) Execute() (*postgresflex.ListDatabasesResponse, error) {
|
||||
return m.executeFunc()
|
||||
}
|
||||
|
||||
type mockDBClient struct {
|
||||
executeRequest func() postgresflex.ApiListDatabasesRequestRequest
|
||||
}
|
||||
|
||||
var _ databaseClientReader = (*mockDBClient)(nil)
|
||||
|
||||
func (m *mockDBClient) ListDatabasesRequest(
|
||||
_ context.Context,
|
||||
_, _, _ string,
|
||||
) postgresflex.ApiListDatabasesRequestRequest {
|
||||
return m.executeRequest()
|
||||
}
|
||||
|
||||
func TestGetDatabase(t *testing.T) {
|
||||
mockResp := func(page int64) (*postgresflex.ListDatabasesResponse, error) {
|
||||
mockResp := func(page int32) (*v3alpha1api.ListDatabasesResponse, error) {
|
||||
if page == 1 {
|
||||
return &postgresflex.ListDatabasesResponse{
|
||||
Databases: &[]postgresflex.ListDatabase{
|
||||
{Id: utils.Ptr(int64(1)), Name: utils.Ptr("first")},
|
||||
{Id: utils.Ptr(int64(2)), Name: utils.Ptr("second")},
|
||||
return &v3alpha1api.ListDatabasesResponse{
|
||||
Databases: []v3alpha1api.ListDatabase{
|
||||
{Id: int32(1), Name: "first"},
|
||||
{Id: int32(2), Name: "second"},
|
||||
},
|
||||
Pagination: &postgresflex.Pagination{
|
||||
Page: utils.Ptr(int64(1)),
|
||||
TotalPages: utils.Ptr(int64(2)),
|
||||
Size: utils.Ptr(int64(3)),
|
||||
Pagination: v3alpha1api.Pagination{
|
||||
Page: int32(1),
|
||||
TotalPages: int32(2),
|
||||
Size: int32(3),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
if page == 2 {
|
||||
return &postgresflex.ListDatabasesResponse{
|
||||
Databases: &[]postgresflex.ListDatabase{{Id: utils.Ptr(int64(3)), Name: utils.Ptr("three")}},
|
||||
Pagination: &postgresflex.Pagination{
|
||||
Page: utils.Ptr(int64(2)),
|
||||
TotalPages: utils.Ptr(int64(2)),
|
||||
Size: utils.Ptr(int64(3)),
|
||||
return &v3alpha1api.ListDatabasesResponse{
|
||||
Databases: []v3alpha1api.ListDatabase{{Id: int32(3), Name: "three"}},
|
||||
Pagination: v3alpha1api.Pagination{
|
||||
Page: int32(2),
|
||||
TotalPages: int32(2),
|
||||
Size: int32(3),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &postgresflex.ListDatabasesResponse{
|
||||
Databases: &[]postgresflex.ListDatabase{},
|
||||
Pagination: &postgresflex.Pagination{
|
||||
Page: utils.Ptr(int64(3)),
|
||||
TotalPages: utils.Ptr(int64(2)),
|
||||
Size: utils.Ptr(int64(3)),
|
||||
return &v3alpha1api.ListDatabasesResponse{
|
||||
Databases: []v3alpha1api.ListDatabase{},
|
||||
Pagination: v3alpha1api.Pagination{
|
||||
Page: int32(3),
|
||||
TotalPages: int32(2),
|
||||
Size: int32(3),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
description string
|
||||
projectId string
|
||||
projectID string
|
||||
region string
|
||||
instanceId string
|
||||
instanceID string
|
||||
wantErr bool
|
||||
wantDbName string
|
||||
wantDbId int64
|
||||
wantDbID int32
|
||||
}{
|
||||
{
|
||||
description: "Success - Found by name on first page",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantErr: false,
|
||||
wantDbName: "second",
|
||||
},
|
||||
{
|
||||
description: "Success - Found by id on first page",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantErr: false,
|
||||
wantDbId: 2,
|
||||
wantDbID: 2,
|
||||
},
|
||||
{
|
||||
description: "Success - Found by name on second page",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantErr: false,
|
||||
wantDbName: "three",
|
||||
},
|
||||
{
|
||||
description: "Success - Found by id on second page",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantErr: false,
|
||||
wantDbId: 1,
|
||||
wantDbID: 1,
|
||||
},
|
||||
{
|
||||
description: "Error - API failure",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
description: "Error - Missing parameters",
|
||||
projectId: "", region: "reg", instanceId: "inst",
|
||||
projectID: "", region: "reg", instanceID: "inst",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
description: "Error - Search by name not found after all pages",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantDbName: "non-existent",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
description: "Error - Search by id not found after all pages",
|
||||
projectId: "pid", region: "reg", instanceId: "inst",
|
||||
wantDbId: 999999,
|
||||
projectID: "pid", region: "reg", instanceID: "inst",
|
||||
wantDbID: 999999,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
|
@ -133,47 +105,46 @@ func TestGetDatabase(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(
|
||||
tt.description, func(t *testing.T) {
|
||||
var currentPage int64
|
||||
client := &mockDBClient{
|
||||
executeRequest: func() postgresflex.ApiListDatabasesRequestRequest {
|
||||
return &mockRequest{
|
||||
executeFunc: func() (*postgresflex.ListDatabasesResponse, error) {
|
||||
currentPage++
|
||||
return mockResp(currentPage)
|
||||
},
|
||||
}
|
||||
},
|
||||
var currentPage int32
|
||||
|
||||
mockCall := func(_ v3alpha1api.ApiListDatabasesRequestRequest) (*v3alpha1api.ListDatabasesResponse, error) {
|
||||
currentPage++
|
||||
return mockResp(currentPage)
|
||||
}
|
||||
|
||||
var actual *postgresflex.ListDatabase
|
||||
client := &v3alpha1api.DefaultAPIServiceMock{
|
||||
ListDatabasesRequestExecuteMock: &mockCall,
|
||||
}
|
||||
|
||||
var actual *v3alpha1api.ListDatabase
|
||||
var errDB error
|
||||
|
||||
if tt.wantDbName != "" {
|
||||
actual, errDB = getDatabaseByName(
|
||||
t.Context(),
|
||||
client,
|
||||
tt.projectId,
|
||||
tt.projectID,
|
||||
tt.region,
|
||||
tt.instanceId,
|
||||
tt.instanceID,
|
||||
tt.wantDbName,
|
||||
)
|
||||
} else if tt.wantDbId != 0 {
|
||||
} else if tt.wantDbID != 0 {
|
||||
actual, errDB = getDatabaseById(
|
||||
t.Context(),
|
||||
client,
|
||||
tt.projectId,
|
||||
tt.projectID,
|
||||
tt.region,
|
||||
tt.instanceId,
|
||||
tt.wantDbId,
|
||||
tt.instanceID,
|
||||
int64(tt.wantDbID),
|
||||
)
|
||||
} else {
|
||||
actual, errDB = getDatabase(
|
||||
context.Background(),
|
||||
client,
|
||||
tt.projectId,
|
||||
tt.projectID,
|
||||
tt.region,
|
||||
tt.instanceId,
|
||||
func(_ postgresflex.ListDatabase) bool { return false },
|
||||
tt.instanceID,
|
||||
func(_ v3alpha1api.ListDatabase) bool { return false },
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -182,14 +153,14 @@ func TestGetDatabase(t *testing.T) {
|
|||
return
|
||||
}
|
||||
if !tt.wantErr && tt.wantDbName != "" && actual != nil {
|
||||
if *actual.Name != tt.wantDbName {
|
||||
t.Errorf("getDatabaseByNameOrID() got name = %v, want %v", *actual.Name, tt.wantDbName)
|
||||
if actual.Name != tt.wantDbName {
|
||||
t.Errorf("getDatabaseByNameOrID() got name = %v, want %v", actual.Name, tt.wantDbName)
|
||||
}
|
||||
}
|
||||
|
||||
if !tt.wantErr && tt.wantDbId != 0 && actual != nil {
|
||||
if *actual.Id != tt.wantDbId {
|
||||
t.Errorf("getDatabaseByNameOrID() got id = %v, want %v", *actual.Id, tt.wantDbId)
|
||||
if !tt.wantErr && tt.wantDbID != 0 && actual != nil {
|
||||
if actual.Id != tt.wantDbID {
|
||||
t.Errorf("getDatabaseByNameOrID() got id = %v, want %v", actual.Id, tt.wantDbID)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
@ -200,23 +171,18 @@ func TestGetDatabase(t *testing.T) {
|
|||
func TestCleanString(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
given *string
|
||||
expected *string
|
||||
given string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "should remove quotes",
|
||||
given: utils.Ptr("\"quoted\""),
|
||||
expected: utils.Ptr("quoted"),
|
||||
},
|
||||
{
|
||||
name: "should handle nil",
|
||||
given: nil,
|
||||
expected: nil,
|
||||
given: "\"quoted\"",
|
||||
expected: "quoted",
|
||||
},
|
||||
{
|
||||
name: "should not change unquoted string",
|
||||
given: utils.Ptr("unquoted"),
|
||||
expected: utils.Ptr("unquoted"),
|
||||
given: "unquoted",
|
||||
expected: "unquoted",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,21 +5,21 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"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 *postgresflexalpha.ListDatabase,
|
||||
source *v3alpha1api.ListDatabase,
|
||||
model *dataSourceModel,
|
||||
region string,
|
||||
) error {
|
||||
if source == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if source.Id == nil || *source.Id == 0 {
|
||||
if source.Id == 0 {
|
||||
return fmt.Errorf("id not present")
|
||||
}
|
||||
if model == nil {
|
||||
|
|
@ -29,8 +29,8 @@ func mapFields(
|
|||
var databaseId int64
|
||||
if model.DatabaseId.ValueInt64() != 0 {
|
||||
databaseId = model.DatabaseId.ValueInt64()
|
||||
} else if source.Id != nil {
|
||||
databaseId = *source.Id
|
||||
} else if source.Id != 0 {
|
||||
databaseId = int64(source.Id)
|
||||
} else {
|
||||
return fmt.Errorf("database id not present")
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ func mapFields(
|
|||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseId = types.Int64Value(databaseId)
|
||||
model.Name = types.StringValue(source.GetName())
|
||||
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||
model.Owner = types.StringValue(cleanString(source.Owner))
|
||||
model.Region = types.StringValue(region)
|
||||
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
||||
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
||||
|
|
@ -53,11 +53,11 @@ func mapFields(
|
|||
}
|
||||
|
||||
// mapResourceFields maps fields from a GetDatabase API response to a resourceModel for the resource.
|
||||
func mapResourceFields(source *postgresflexalpha.GetDatabaseResponse, model *resourceModel) error {
|
||||
func mapResourceFields(source *v3alpha1api.GetDatabaseResponse, model *resourceModel) error {
|
||||
if source == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if source.Id == nil || *source.Id == 0 {
|
||||
if source.Id == 0 {
|
||||
return fmt.Errorf("id not present")
|
||||
}
|
||||
if model == nil {
|
||||
|
|
@ -67,8 +67,8 @@ func mapResourceFields(source *postgresflexalpha.GetDatabaseResponse, model *res
|
|||
var databaseId int64
|
||||
if model.Id.ValueInt64() != 0 {
|
||||
databaseId = model.Id.ValueInt64()
|
||||
} else if source.Id != nil {
|
||||
databaseId = *source.Id
|
||||
} else if source.Id != 0 {
|
||||
databaseId = int64(source.Id)
|
||||
} else {
|
||||
return fmt.Errorf("database id not present")
|
||||
}
|
||||
|
|
@ -76,18 +76,18 @@ func mapResourceFields(source *postgresflexalpha.GetDatabaseResponse, model *res
|
|||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseId = types.Int64Value(databaseId)
|
||||
model.Name = types.StringValue(source.GetName())
|
||||
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||
model.Owner = types.StringValue(cleanString(source.Owner))
|
||||
return nil
|
||||
}
|
||||
|
||||
// toCreatePayload converts the resource model to an API create payload.
|
||||
func toCreatePayload(model *resourceModel) (*postgresflexalpha.CreateDatabaseRequestPayload, error) {
|
||||
func toCreatePayload(model *resourceModel) (*v3alpha1api.CreateDatabaseRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
return &postgresflexalpha.CreateDatabaseRequestPayload{
|
||||
Name: model.Name.ValueStringPointer(),
|
||||
return &v3alpha1api.CreateDatabaseRequestPayload{
|
||||
Name: model.Name.ValueString(),
|
||||
Owner: model.Owner.ValueStringPointer(),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ import (
|
|||
"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/postgresflexalpha"
|
||||
postgresflexalpha "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||
)
|
||||
|
||||
|
|
@ -31,9 +32,9 @@ func TestMapFields(t *testing.T) {
|
|||
name: "should map fields correctly",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("\"my-owner\""),
|
||||
Id: int32(1),
|
||||
Name: "my-db",
|
||||
Owner: "my-owner",
|
||||
},
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
|
|
@ -62,8 +63,8 @@ func TestMapFields(t *testing.T) {
|
|||
name: "should preserve existing model ID",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
Id: int32(1),
|
||||
Name: "my-db",
|
||||
},
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
|
|
@ -77,9 +78,10 @@ func TestMapFields(t *testing.T) {
|
|||
expected: expected{
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringNull(), DatabaseId: types.Int64Value(1),
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue(""),
|
||||
DatabaseId: types.Int64Value(1),
|
||||
Region: types.StringValue("eu01"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
|
|
@ -99,7 +101,7 @@ func TestMapFields(t *testing.T) {
|
|||
{
|
||||
name: "should fail on nil source ID",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{Id: nil},
|
||||
source: &postgresflexalpha.ListDatabase{Id: 0},
|
||||
model: &dataSourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
|
|
@ -107,7 +109,7 @@ func TestMapFields(t *testing.T) {
|
|||
{
|
||||
name: "should fail on nil model",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{Id: utils.Ptr(int64(1))},
|
||||
source: &postgresflexalpha.ListDatabase{Id: int32(1)},
|
||||
model: nil,
|
||||
},
|
||||
expected: expected{err: true},
|
||||
|
|
@ -150,9 +152,9 @@ func TestMapResourceFields(t *testing.T) {
|
|||
name: "should map fields correctly",
|
||||
given: given{
|
||||
source: &postgresflexalpha.GetDatabaseResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
Id: int32(1),
|
||||
Name: "my-db",
|
||||
Owner: "my-owner",
|
||||
},
|
||||
model: &resourceModel{},
|
||||
},
|
||||
|
|
@ -216,7 +218,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
},
|
||||
expected: expected{
|
||||
payload: &postgresflexalpha.CreateDatabaseRequestPayload{
|
||||
Name: utils.Ptr("my-db"),
|
||||
Name: "my-db",
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -14,14 +14,14 @@ import (
|
|||
"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/services/postgresflex/v3alpha1api"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
"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"
|
||||
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
|
||||
postgresflexalphaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
postgresflexalpha3 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/postgresflexalpha"
|
||||
postgresflexalphaWait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/postgresflexalpha"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -43,7 +43,7 @@ func NewDatabaseResource() resource.Resource {
|
|||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = postgresflexalpha2.DatabaseModel
|
||||
type resourceModel = postgresflexalphaResGen.DatabaseModel
|
||||
|
||||
// DatabaseResourceIdentityModel describes the resource's identity attributes.
|
||||
type DatabaseResourceIdentityModel struct {
|
||||
|
|
@ -55,7 +55,7 @@ type DatabaseResourceIdentityModel struct {
|
|||
|
||||
// databaseResource is the resource implementation.
|
||||
type databaseResource struct {
|
||||
client *postgresflexalpha.APIClient
|
||||
client *v3alpha1api.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +122,7 @@ var modifiersFileByte []byte
|
|||
|
||||
// Schema defines the schema for the resource.
|
||||
func (r *databaseResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
s := postgresflexalpha2.DatabaseResourceSchema(ctx)
|
||||
s := postgresflexalphaResGen.DatabaseResourceSchema(ctx)
|
||||
|
||||
fields, err := utils.ReadModifiersConfig(modifiersFileByte)
|
||||
if err != nil {
|
||||
|
|
@ -198,7 +198,7 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
// Create new database
|
||||
databaseResp, err := r.client.CreateDatabaseRequest(
|
||||
databaseResp, err := r.client.DefaultAPI.CreateDatabaseRequest(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
|
|
@ -209,16 +209,17 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
if databaseResp == nil || databaseResp.Id == nil {
|
||||
dbID, ok := databaseResp.GetIdOk()
|
||||
if !ok {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
funcErrorSummary,
|
||||
"API didn't return database Id. A database might have been created",
|
||||
"API didn't return database Id. A database might although have been created",
|
||||
)
|
||||
return
|
||||
}
|
||||
databaseId := *databaseResp.Id
|
||||
databaseId := int64(*dbID)
|
||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
|
|
@ -234,7 +235,7 @@ func (r *databaseResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
database, err := postgresflexalpha3.GetDatabaseByIdWaitHandler(ctx, r.client, projectId, instanceId, region, databaseId).
|
||||
database, err := postgresflexalphaWait.GetDatabaseByIdWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region, databaseId).
|
||||
SetTimeout(15 * time.Minute).
|
||||
SetSleepBeforeWait(15 * time.Second).
|
||||
WaitWithContext(ctx)
|
||||
|
|
@ -293,7 +294,7 @@ func (r *databaseResource) Read(
|
|||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
|
||||
databaseResp, err := postgresflexalpha3.GetDatabaseByIdWaitHandler(ctx, r.client, projectId, instanceId, region, databaseId).
|
||||
databaseResp, err := postgresflexalphaWait.GetDatabaseByIdWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region, databaseId).
|
||||
SetTimeout(15 * time.Minute).
|
||||
SetSleepBeforeWait(15 * time.Second).
|
||||
WaitWithContext(ctx)
|
||||
|
|
@ -321,13 +322,12 @@ func (r *databaseResource) Read(
|
|||
return
|
||||
}
|
||||
|
||||
// TODO: use values from api to identify drift
|
||||
// Save identity into Terraform state
|
||||
identity := DatabaseResourceIdentityModel{
|
||||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId),
|
||||
DatabaseID: types.Int64Value(int64(databaseResp.GetId())),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -361,13 +361,7 @@ func (r *databaseResource) Update(
|
|||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
databaseId64 := model.DatabaseId.ValueInt64()
|
||||
|
||||
if databaseId64 > math.MaxInt32 {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (databaseId)")
|
||||
return
|
||||
}
|
||||
databaseId := int32(databaseId64) // nolint:gosec // check is performed above
|
||||
databaseId := model.DatabaseId.ValueInt64()
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
|
|
@ -383,7 +377,7 @@ func (r *databaseResource) Update(
|
|||
}
|
||||
|
||||
modified := false
|
||||
var payload postgresflexalpha.UpdateDatabasePartiallyRequestPayload
|
||||
var payload v3alpha1api.UpdateDatabasePartiallyRequestPayload
|
||||
if stateModel.Name != model.Name {
|
||||
payload.Name = model.Name.ValueStringPointer()
|
||||
modified = true
|
||||
|
|
@ -399,13 +393,18 @@ func (r *databaseResource) Update(
|
|||
return
|
||||
}
|
||||
|
||||
if databaseId > math.MaxInt32 {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "error updating database", "databaseID out of bounds for int32")
|
||||
return
|
||||
}
|
||||
databaseID32 := int32(databaseId) //nolint:gosec // TODO
|
||||
// Update existing database
|
||||
err := r.client.UpdateDatabasePartiallyRequest(
|
||||
err := r.client.DefaultAPI.UpdateDatabasePartiallyRequest(
|
||||
ctx,
|
||||
projectId,
|
||||
region,
|
||||
instanceId,
|
||||
databaseId,
|
||||
databaseID32,
|
||||
).UpdateDatabasePartiallyRequestPayload(payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "error updating database", err.Error())
|
||||
|
|
@ -414,7 +413,7 @@ func (r *databaseResource) Update(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
databaseResp, err := postgresflexalpha3.GetDatabaseByIdWaitHandler(ctx, r.client, projectId, instanceId, region, databaseId64).
|
||||
databaseResp, err := postgresflexalphaWait.GetDatabaseByIdWaitHandler(ctx, r.client.DefaultAPI, projectId, instanceId, region, databaseId).
|
||||
SetTimeout(15 * time.Minute).
|
||||
SetSleepBeforeWait(15 * time.Second).
|
||||
WaitWithContext(ctx)
|
||||
|
|
@ -442,7 +441,7 @@ func (r *databaseResource) Update(
|
|||
ProjectID: types.StringValue(projectId),
|
||||
Region: types.StringValue(region),
|
||||
InstanceID: types.StringValue(instanceId),
|
||||
DatabaseID: types.Int64Value(databaseId64),
|
||||
DatabaseID: types.Int64Value(databaseId),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -500,7 +499,7 @@ func (r *databaseResource) Delete(
|
|||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
|
||||
// Delete existing record set
|
||||
err := r.client.DeleteDatabaseRequestExecute(ctx, projectId, region, instanceId, databaseId)
|
||||
err := r.client.DefaultAPI.DeleteDatabaseRequest(ctx, projectId, region, instanceId, databaseId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting database", fmt.Sprintf("Calling API: %v", err))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue