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
|
|
@ -8,8 +8,8 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
postgresflex "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"
|
||||
postgresflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/datasources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
|
|
@ -40,7 +40,7 @@ type dataSourceModel struct {
|
|||
|
||||
// userDataSource is the data source implementation.
|
||||
type userDataSource struct {
|
||||
client *postgresflex.APIClient
|
||||
client *v3alpha1api.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
|
|
@ -101,24 +101,24 @@ func (r *userDataSource) Read(
|
|||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId64 := model.UserId.ValueInt64()
|
||||
if userId64 > math.MaxInt32 {
|
||||
projectID := model.ProjectId.ValueString()
|
||||
instanceID := model.InstanceId.ValueString()
|
||||
userID64 := model.UserId.ValueInt64()
|
||||
if userID64 > math.MaxInt32 {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (userId)")
|
||||
return
|
||||
}
|
||||
userId := int32(userId64) // nolint:gosec // check is performed above
|
||||
userID := int32(userID64) // nolint:gosec // check is performed above
|
||||
|
||||
region := r.providerData.GetRegionWithOverride(model.Region)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceId)
|
||||
ctx = tflog.SetField(ctx, "project_id", projectID)
|
||||
ctx = tflog.SetField(ctx, "instance_id", instanceID)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
ctx = tflog.SetField(ctx, "user_id", userID)
|
||||
|
||||
recordSetResp, err := r.client.GetUserRequest(ctx, projectId, region, instanceId, userId).Execute()
|
||||
recordSetResp, err := r.client.DefaultAPI.GetUserRequest(ctx, projectID, region, instanceID, userID).Execute()
|
||||
if err != nil {
|
||||
handleReadError(ctx, &diags, err, projectId, instanceId, userId)
|
||||
handleReadError(ctx, &diags, err, projectID, instanceID, userID)
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
|
@ -151,8 +151,8 @@ func handleReadError(
|
|||
ctx context.Context,
|
||||
diags *diag.Diagnostics,
|
||||
err error,
|
||||
projectId, instanceId string,
|
||||
userId int32,
|
||||
projectID, instanceID string,
|
||||
userID int32,
|
||||
) {
|
||||
utils.LogError(
|
||||
ctx,
|
||||
|
|
@ -161,23 +161,23 @@ func handleReadError(
|
|||
"Reading user",
|
||||
fmt.Sprintf(
|
||||
"User with ID %q or instance with ID %q does not exist in project %q.",
|
||||
userId,
|
||||
instanceId,
|
||||
projectId,
|
||||
userID,
|
||||
instanceID,
|
||||
projectID,
|
||||
),
|
||||
map[int]string{
|
||||
http.StatusBadRequest: fmt.Sprintf(
|
||||
"Invalid user request parameters for project %q and instance %q.",
|
||||
projectId,
|
||||
instanceId,
|
||||
projectID,
|
||||
instanceID,
|
||||
),
|
||||
http.StatusNotFound: fmt.Sprintf(
|
||||
"User, instance %q, or project %q or user %q not found.",
|
||||
instanceId,
|
||||
projectId,
|
||||
userId,
|
||||
instanceID,
|
||||
projectID,
|
||||
userID,
|
||||
),
|
||||
http.StatusForbidden: fmt.Sprintf("Forbidden access to project %q.", projectId),
|
||||
http.StatusForbidden: fmt.Sprintf("Forbidden access to project %q.", projectID),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
postgresflex "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/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
)
|
||||
|
||||
// mapDataSourceFields maps API response to data source model, preserving existing ID.
|
||||
func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *dataSourceModel, region string) error {
|
||||
func mapDataSourceFields(userResp *v3alpha1api.GetUserResponse, model *dataSourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -22,27 +22,24 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *dataSour
|
|||
}
|
||||
user := userResp
|
||||
|
||||
var userId int64
|
||||
if model.UserId.ValueInt64() != 0 {
|
||||
userId = model.UserId.ValueInt64()
|
||||
} else if user.Id != nil {
|
||||
userId = *user.Id
|
||||
} else {
|
||||
var userID int64
|
||||
if model.UserId.ValueInt64() == 0 {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
userID = model.UserId.ValueInt64()
|
||||
|
||||
model.TerraformID = utils.BuildInternalTerraformId(
|
||||
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
|
||||
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userID, 10),
|
||||
)
|
||||
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.UserId = types.Int64Value(userID)
|
||||
model.Name = types.StringValue(user.GetName())
|
||||
|
||||
if user.Roles == nil {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
} else {
|
||||
var roles []attr.Value
|
||||
for _, role := range *user.Roles {
|
||||
for _, role := range user.Roles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
rolesSet, diags := types.SetValue(types.StringType, roles)
|
||||
|
|
@ -52,24 +49,24 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *dataSour
|
|||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
|
||||
model.Id = types.Int64Value(userId)
|
||||
model.Id = types.Int64Value(userID)
|
||||
model.Region = types.StringValue(region)
|
||||
model.Status = types.StringValue(user.GetStatus())
|
||||
return nil
|
||||
}
|
||||
|
||||
// toPayloadRoles converts a string slice to the API's role type.
|
||||
func toPayloadRoles(roles *[]string) *[]postgresflex.UserRole {
|
||||
var userRoles = make([]postgresflex.UserRole, 0, len(*roles))
|
||||
for _, role := range *roles {
|
||||
userRoles = append(userRoles, postgresflex.UserRole(role))
|
||||
func toPayloadRoles(roles []string) []v3alpha1api.UserRole {
|
||||
var userRoles = make([]v3alpha1api.UserRole, 0, len(roles))
|
||||
for _, role := range roles {
|
||||
userRoles = append(userRoles, v3alpha1api.UserRole(role))
|
||||
}
|
||||
return &userRoles
|
||||
return userRoles
|
||||
}
|
||||
|
||||
// toUpdatePayload creates an API update payload from the resource model.
|
||||
func toUpdatePayload(model *resourceModel, roles *[]string) (
|
||||
*postgresflex.UpdateUserRequestPayload,
|
||||
func toUpdatePayload(model *resourceModel, roles []string) (
|
||||
*v3alpha1api.UpdateUserRequestPayload,
|
||||
error,
|
||||
) {
|
||||
if model == nil {
|
||||
|
|
@ -79,14 +76,14 @@ func toUpdatePayload(model *resourceModel, roles *[]string) (
|
|||
return nil, fmt.Errorf("nil roles")
|
||||
}
|
||||
|
||||
return &postgresflex.UpdateUserRequestPayload{
|
||||
return &v3alpha1api.UpdateUserRequestPayload{
|
||||
Name: model.Name.ValueStringPointer(),
|
||||
Roles: toPayloadRoles(roles),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// toCreatePayload creates an API create payload from the resource model.
|
||||
func toCreatePayload(model *resourceModel, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) {
|
||||
func toCreatePayload(model *resourceModel, roles []string) (*v3alpha1api.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
|
@ -94,14 +91,14 @@ func toCreatePayload(model *resourceModel, roles *[]string) (*postgresflex.Creat
|
|||
return nil, fmt.Errorf("nil roles")
|
||||
}
|
||||
|
||||
return &postgresflex.CreateUserRequestPayload{
|
||||
return &v3alpha1api.CreateUserRequestPayload{
|
||||
Roles: toPayloadRoles(roles),
|
||||
Name: model.Name.ValueStringPointer(),
|
||||
Name: model.Name.ValueString(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// mapResourceFields maps API response to the resource model, preserving existing ID.
|
||||
func mapResourceFields(userResp *postgresflex.GetUserResponse, model *resourceModel, region string) error {
|
||||
func mapResourceFields(userResp *v3alpha1api.GetUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -110,24 +107,24 @@ func mapResourceFields(userResp *postgresflex.GetUserResponse, model *resourceMo
|
|||
}
|
||||
user := userResp
|
||||
|
||||
var userId int64
|
||||
var userID int64
|
||||
if !model.UserId.IsNull() && !model.UserId.IsUnknown() && model.UserId.ValueInt64() != 0 {
|
||||
userId = model.UserId.ValueInt64()
|
||||
} else if user.Id != nil {
|
||||
userId = *user.Id
|
||||
userID = model.UserId.ValueInt64()
|
||||
} else if user.Id != 0 {
|
||||
userID = int64(user.Id)
|
||||
} else {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
|
||||
model.Id = types.Int64Value(userId)
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Name = types.StringPointerValue(user.Name)
|
||||
model.Id = types.Int64Value(userID)
|
||||
model.UserId = types.Int64Value(userID)
|
||||
model.Name = types.StringValue(user.Name)
|
||||
|
||||
if user.Roles == nil {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
} else {
|
||||
var roles []attr.Value
|
||||
for _, role := range *user.Roles {
|
||||
for _, role := range user.Roles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
rolesSet, diags := types.SetValue(types.StringType, roles)
|
||||
|
|
@ -137,6 +134,6 @@ func mapResourceFields(userResp *postgresflex.GetUserResponse, model *resourceMo
|
|||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
model.Region = types.StringValue(region)
|
||||
model.Status = types.StringPointerValue(user.Status)
|
||||
model.Status = types.StringValue(user.Status)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
data "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/datasources_gen"
|
||||
)
|
||||
|
||||
|
|
@ -43,12 +44,12 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
{
|
||||
"simple_values",
|
||||
&postgresflex.GetUserResponse{
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Roles: []postgresflex.UserRole{
|
||||
"role_1",
|
||||
"role_2",
|
||||
"",
|
||||
},
|
||||
Name: utils.Ptr("username"),
|
||||
Name: "username",
|
||||
},
|
||||
testRegion,
|
||||
dataSourceModel{
|
||||
|
|
@ -77,10 +78,10 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Roles: &[]postgresflex.UserRole{},
|
||||
Name: nil,
|
||||
Status: utils.Ptr("status"),
|
||||
Id: int32(1),
|
||||
Roles: []postgresflex.UserRole{},
|
||||
Name: "",
|
||||
Status: "status",
|
||||
},
|
||||
testRegion,
|
||||
dataSourceModel{
|
||||
|
|
@ -160,7 +161,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
{
|
||||
"default_values",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Id: int32(1),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -168,11 +169,11 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringNull(),
|
||||
Name: types.StringValue(""),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Password: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
Status: types.StringValue(""),
|
||||
//ConnectionString: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
|
|
@ -180,9 +181,9 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
{
|
||||
"simple_values",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("username"),
|
||||
Status: utils.Ptr("status"),
|
||||
Id: int32(1),
|
||||
Name: "username",
|
||||
Status: "status",
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -202,9 +203,9 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: nil,
|
||||
Status: nil,
|
||||
Id: int32(1),
|
||||
Name: "",
|
||||
Status: "",
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -212,11 +213,11 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringNull(),
|
||||
Name: types.StringValue(""),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Password: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
Status: types.StringValue(""),
|
||||
//ConnectionString: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
|
|
@ -259,7 +260,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(state, &tt.expected)
|
||||
diff := cmp.Diff(&tt.expected, state)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
|
@ -281,7 +282,7 @@ func TestMapFields(t *testing.T) {
|
|||
{
|
||||
"default_values",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Id: int32(1),
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -289,10 +290,10 @@ func TestMapFields(t *testing.T) {
|
|||
UserId: types.Int64Value(int64(1)),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringNull(),
|
||||
Name: types.StringValue(""),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
Status: types.StringValue(""),
|
||||
//ConnectionString: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
|
|
@ -300,13 +301,13 @@ func TestMapFields(t *testing.T) {
|
|||
{
|
||||
"simple_values",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Id: int32(1),
|
||||
Roles: []postgresflex.UserRole{
|
||||
"role_1",
|
||||
"role_2",
|
||||
"",
|
||||
},
|
||||
Name: utils.Ptr("username"),
|
||||
Name: "username",
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -325,7 +326,7 @@ func TestMapFields(t *testing.T) {
|
|||
),
|
||||
),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
Status: types.StringValue(""),
|
||||
//ConnectionString: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
|
|
@ -333,8 +334,8 @@ func TestMapFields(t *testing.T) {
|
|||
{
|
||||
"null_fields_and_int_conversions",
|
||||
&postgresflex.GetUserResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: nil,
|
||||
Id: int32(1),
|
||||
Name: "",
|
||||
},
|
||||
testRegion,
|
||||
resourceModel{
|
||||
|
|
@ -342,10 +343,10 @@ func TestMapFields(t *testing.T) {
|
|||
UserId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
Name: types.StringNull(),
|
||||
Name: types.StringValue(""),
|
||||
Roles: types.List(types.SetNull(types.StringType)),
|
||||
Region: types.StringValue(testRegion),
|
||||
Status: types.StringNull(),
|
||||
Status: types.StringValue(""),
|
||||
//ConnectionString: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
|
|
@ -401,17 +402,17 @@ func TestToCreatePayload(t *testing.T) {
|
|||
tests := []struct {
|
||||
description string
|
||||
input *resourceModel
|
||||
inputRoles *[]string
|
||||
inputRoles []string
|
||||
expected *postgresflex.CreateUserRequestPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&resourceModel{},
|
||||
&[]string{},
|
||||
[]string{},
|
||||
&postgresflex.CreateUserRequestPayload{
|
||||
Name: nil,
|
||||
Roles: &[]postgresflex.UserRole{},
|
||||
Name: "",
|
||||
Roles: []postgresflex.UserRole{},
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -420,13 +421,13 @@ func TestToCreatePayload(t *testing.T) {
|
|||
&resourceModel{
|
||||
Name: types.StringValue("username"),
|
||||
},
|
||||
&[]string{
|
||||
[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
&postgresflex.CreateUserRequestPayload{
|
||||
Name: utils.Ptr("username"),
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Name: "username",
|
||||
Roles: []postgresflex.UserRole{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
|
|
@ -438,21 +439,21 @@ func TestToCreatePayload(t *testing.T) {
|
|||
&resourceModel{
|
||||
Name: types.StringNull(),
|
||||
},
|
||||
&[]string{
|
||||
[]string{
|
||||
"",
|
||||
},
|
||||
&postgresflex.CreateUserRequestPayload{
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Roles: []postgresflex.UserRole{
|
||||
"",
|
||||
},
|
||||
Name: nil,
|
||||
Name: "",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
&[]string{},
|
||||
[]string{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
|
|
@ -489,16 +490,16 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
tests := []struct {
|
||||
description string
|
||||
input *resourceModel
|
||||
inputRoles *[]string
|
||||
inputRoles []string
|
||||
expected *postgresflex.UpdateUserRequestPayload
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
&resourceModel{},
|
||||
&[]string{},
|
||||
[]string{},
|
||||
&postgresflex.UpdateUserRequestPayload{
|
||||
Roles: &[]postgresflex.UserRole{},
|
||||
Roles: []postgresflex.UserRole{},
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -507,13 +508,13 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
&resourceModel{
|
||||
Name: types.StringValue("username"),
|
||||
},
|
||||
&[]string{
|
||||
[]string{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
&postgresflex.UpdateUserRequestPayload{
|
||||
Name: utils.Ptr("username"),
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Roles: []postgresflex.UserRole{
|
||||
"role_1",
|
||||
"role_2",
|
||||
},
|
||||
|
|
@ -525,11 +526,11 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
&resourceModel{
|
||||
Name: types.StringNull(),
|
||||
},
|
||||
&[]string{
|
||||
[]string{
|
||||
"",
|
||||
},
|
||||
&postgresflex.UpdateUserRequestPayload{
|
||||
Roles: &[]postgresflex.UserRole{
|
||||
Roles: []postgresflex.UserRole{
|
||||
"",
|
||||
},
|
||||
},
|
||||
|
|
@ -538,7 +539,7 @@ func TestToUpdatePayload(t *testing.T) {
|
|||
{
|
||||
"nil_model",
|
||||
nil,
|
||||
&[]string{},
|
||||
[]string{},
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
||||
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
postgresflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/resources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
postgresflexalphaWait "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/wait/postgresflexalpha"
|
||||
|
|
@ -60,7 +60,7 @@ type UserResourceIdentityModel struct {
|
|||
|
||||
// userResource implements the resource handling for a PostgreSQL Flex user.
|
||||
type userResource struct {
|
||||
client *postgresflex.APIClient
|
||||
client *v3alpha1api.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
|
|
@ -189,8 +189,8 @@ func (r *userResource) Create(
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
arg := &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
projectID: model.ProjectId.ValueString(),
|
||||
instanceID: model.InstanceId.ValueString(),
|
||||
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||
}
|
||||
|
||||
|
|
@ -202,18 +202,18 @@ func (r *userResource) Create(
|
|||
}
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toCreatePayload(&model, &roles)
|
||||
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(
|
||||
userResp, err := r.client.DefaultAPI.CreateUserRequest(
|
||||
ctx,
|
||||
arg.projectId,
|
||||
arg.projectID,
|
||||
arg.region,
|
||||
arg.instanceId,
|
||||
arg.instanceID,
|
||||
).CreateUserRequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
||||
|
|
@ -221,7 +221,7 @@ func (r *userResource) Create(
|
|||
}
|
||||
|
||||
id, ok := userResp.GetIdOk()
|
||||
if !ok || id == 0 {
|
||||
if !ok || *id == 0 {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
|
|
@ -230,7 +230,7 @@ func (r *userResource) Create(
|
|||
)
|
||||
return
|
||||
}
|
||||
arg.userId = id
|
||||
arg.userID = int64(*id)
|
||||
|
||||
ctx = tflog.SetField(ctx, "user_id", id)
|
||||
|
||||
|
|
@ -238,28 +238,28 @@ func (r *userResource) Create(
|
|||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(arg.projectId),
|
||||
ProjectID: types.StringValue(arg.projectID),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64Value(id),
|
||||
InstanceID: types.StringValue(arg.instanceID),
|
||||
UserID: types.Int64Value(int64(*id)),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
model.Id = types.Int64Value(id)
|
||||
model.UserId = types.Int64Value(id)
|
||||
model.Id = types.Int64Value(int64(*id))
|
||||
model.UserId = types.Int64Value(int64(*id))
|
||||
model.Password = types.StringValue(userResp.GetPassword())
|
||||
model.Status = types.StringValue(userResp.GetStatus())
|
||||
|
||||
waitResp, err := postgresflexalphaWait.GetUserByIdWaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
arg.projectId,
|
||||
arg.instanceId,
|
||||
r.client.DefaultAPI,
|
||||
arg.projectID,
|
||||
arg.instanceID,
|
||||
arg.region,
|
||||
id,
|
||||
int64(*id),
|
||||
).SetSleepBeforeWait(
|
||||
10 * time.Second,
|
||||
).SetTimeout(
|
||||
|
|
@ -276,7 +276,7 @@ func (r *userResource) Create(
|
|||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil {
|
||||
if waitResp.Id == 0 {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
|
|
@ -285,7 +285,7 @@ func (r *userResource) Create(
|
|||
)
|
||||
return
|
||||
}
|
||||
if waitResp.Id == nil || *waitResp.Id != id {
|
||||
if waitResp.Id != *id {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
|
|
@ -324,8 +324,8 @@ func (r *userResource) Read(
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
arg := &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
projectID: model.ProjectId.ValueString(),
|
||||
instanceID: model.InstanceId.ValueString(),
|
||||
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||
}
|
||||
|
||||
|
|
@ -336,9 +336,9 @@ func (r *userResource) Read(
|
|||
// Read resource state
|
||||
waitResp, err := postgresflexalphaWait.GetUserByIdWaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
arg.projectId,
|
||||
arg.instanceId,
|
||||
r.client.DefaultAPI,
|
||||
arg.projectID,
|
||||
arg.instanceID,
|
||||
arg.region,
|
||||
model.UserId.ValueInt64(),
|
||||
).SetSleepBeforeWait(
|
||||
|
|
@ -357,7 +357,7 @@ func (r *userResource) Read(
|
|||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil || *waitResp.Id != model.UserId.ValueInt64() {
|
||||
if int64(waitResp.Id) != model.UserId.ValueInt64() {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
|
|
@ -366,16 +366,16 @@ func (r *userResource) Read(
|
|||
)
|
||||
return
|
||||
}
|
||||
arg.userId = *waitResp.Id
|
||||
arg.userID = int64(waitResp.Id)
|
||||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(arg.projectId),
|
||||
ProjectID: types.StringValue(arg.projectID),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64Value(arg.userId),
|
||||
InstanceID: types.StringValue(arg.instanceID),
|
||||
UserID: types.Int64Value(arg.userID),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -407,8 +407,8 @@ func (r *userResource) Update(
|
|||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
arg := &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
projectID: model.ProjectId.ValueString(),
|
||||
instanceID: model.InstanceId.ValueString(),
|
||||
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||
}
|
||||
|
||||
|
|
@ -429,26 +429,26 @@ func (r *userResource) Update(
|
|||
}
|
||||
|
||||
// Generate API request body from model
|
||||
payload, err := toUpdatePayload(&model, &roles)
|
||||
payload, err := toUpdatePayload(&model, roles)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating user", fmt.Sprintf("Updating API payload: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
userId64 := arg.userId
|
||||
if userId64 > math.MaxInt32 {
|
||||
userID64 := arg.userID
|
||||
if userID64 > math.MaxInt32 {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (userId)")
|
||||
return
|
||||
}
|
||||
userId := int32(userId64) // nolint:gosec // check is performed above
|
||||
userID := int32(userID64) // nolint:gosec // check is performed above
|
||||
|
||||
// Update existing instance
|
||||
err = r.client.UpdateUserRequest(
|
||||
err = r.client.DefaultAPI.UpdateUserRequest(
|
||||
ctx,
|
||||
arg.projectId,
|
||||
arg.projectID,
|
||||
arg.region,
|
||||
arg.instanceId,
|
||||
userId,
|
||||
arg.instanceID,
|
||||
userID,
|
||||
).UpdateUserRequestPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating user", err.Error())
|
||||
|
|
@ -459,10 +459,10 @@ func (r *userResource) Update(
|
|||
|
||||
// Set data returned by API in identity
|
||||
identity := UserResourceIdentityModel{
|
||||
ProjectID: types.StringValue(arg.projectId),
|
||||
ProjectID: types.StringValue(arg.projectID),
|
||||
Region: types.StringValue(arg.region),
|
||||
InstanceID: types.StringValue(arg.instanceId),
|
||||
UserID: types.Int64Value(userId64),
|
||||
InstanceID: types.StringValue(arg.instanceID),
|
||||
UserID: types.Int64Value(userID64),
|
||||
}
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -472,9 +472,9 @@ func (r *userResource) Update(
|
|||
// Verify update
|
||||
waitResp, err := postgresflexalphaWait.GetUserByIdWaitHandler(
|
||||
ctx,
|
||||
r.client,
|
||||
arg.projectId,
|
||||
arg.instanceId,
|
||||
r.client.DefaultAPI,
|
||||
arg.projectID,
|
||||
arg.instanceID,
|
||||
arg.region,
|
||||
model.UserId.ValueInt64(),
|
||||
).SetSleepBeforeWait(
|
||||
|
|
@ -493,7 +493,7 @@ func (r *userResource) Update(
|
|||
return
|
||||
}
|
||||
|
||||
if waitResp.Id == nil || *waitResp.Id != model.UserId.ValueInt64() {
|
||||
if int64(waitResp.Id) != model.UserId.ValueInt64() {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
|
|
@ -502,7 +502,7 @@ func (r *userResource) Update(
|
|||
)
|
||||
return
|
||||
}
|
||||
arg.userId = *waitResp.Id
|
||||
arg.userID = int64(waitResp.Id)
|
||||
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, stateModel)
|
||||
|
|
@ -547,15 +547,15 @@ func (r *userResource) Delete(
|
|||
ctx = r.setTFLogFields(ctx, arg)
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
userId64 := arg.userId
|
||||
if userId64 > math.MaxInt32 {
|
||||
userID64 := arg.userID
|
||||
if userID64 > math.MaxInt32 {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error in type conversion", "int value too large (userId)")
|
||||
return
|
||||
}
|
||||
userId := int32(userId64) // nolint:gosec // check is performed above
|
||||
userID := int32(userID64) // nolint:gosec // check is performed above
|
||||
|
||||
// Delete existing record set
|
||||
err := r.client.DeleteUserRequest(ctx, arg.projectId, arg.region, arg.instanceId, userId).Execute()
|
||||
err := r.client.DefaultAPI.DeleteUserRequest(ctx, arg.projectID, arg.region, arg.instanceID, userID).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err))
|
||||
}
|
||||
|
|
@ -571,7 +571,7 @@ func (r *userResource) Delete(
|
|||
// if exists {
|
||||
// core.LogAndAddError(
|
||||
// ctx, &resp.Diagnostics, "Error deleting user",
|
||||
// fmt.Sprintf("User ID '%v' resource still exists after deletion", model.UserId.ValueInt64()),
|
||||
// fmt.Sprintf("User ID '%v' resource still exists after deletion", model.UserId.ValueInt32()),
|
||||
// )
|
||||
// return
|
||||
//}
|
||||
|
|
@ -607,10 +607,10 @@ func (r *userResource) IdentitySchema(
|
|||
|
||||
// clientArg holds the arguments for API calls.
|
||||
type clientArg struct {
|
||||
projectId string
|
||||
instanceId string
|
||||
projectID string
|
||||
instanceID string
|
||||
region string
|
||||
userId int64
|
||||
userID int64
|
||||
}
|
||||
|
||||
// ImportState imports a resource into the Terraform state on success.
|
||||
|
|
@ -637,7 +637,7 @@ func (r *userResource) ImportState(
|
|||
return
|
||||
}
|
||||
|
||||
userId, err := strconv.ParseInt(idParts[3], 10, 64)
|
||||
userID, err := strconv.ParseInt(idParts[3], 10, 64)
|
||||
if err != nil {
|
||||
core.LogAndAddError(
|
||||
ctx,
|
||||
|
|
@ -651,7 +651,7 @@ func (r *userResource) ImportState(
|
|||
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)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), userID)...)
|
||||
|
||||
tflog.Info(ctx, "Postgres Flex user state imported")
|
||||
|
||||
|
|
@ -665,15 +665,15 @@ func (r *userResource) ImportState(
|
|||
return
|
||||
}
|
||||
|
||||
projectId := identityData.ProjectID.ValueString()
|
||||
projectID := identityData.ProjectID.ValueString()
|
||||
region := identityData.Region.ValueString()
|
||||
instanceId := identityData.InstanceID.ValueString()
|
||||
userId := identityData.UserID.ValueInt64()
|
||||
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("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)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), instanceID)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), userID)...)
|
||||
|
||||
tflog.Info(ctx, "Postgres Flex user state imported")
|
||||
}
|
||||
|
|
@ -683,25 +683,24 @@ func (r *userResource) extractIdentityData(
|
|||
model resourceModel,
|
||||
identity UserResourceIdentityModel,
|
||||
) (*clientArg, error) {
|
||||
var projectId, region, instanceId string
|
||||
var userId int64
|
||||
|
||||
var projectID, region, instanceID string
|
||||
var userID int64
|
||||
if !model.UserId.IsNull() && !model.UserId.IsUnknown() {
|
||||
userId = model.UserId.ValueInt64()
|
||||
userID = model.UserId.ValueInt64()
|
||||
} else {
|
||||
if identity.UserID.IsNull() || identity.UserID.IsUnknown() {
|
||||
return nil, fmt.Errorf("user_id not found in config")
|
||||
}
|
||||
userId = identity.UserID.ValueInt64()
|
||||
userID = identity.UserID.ValueInt64()
|
||||
}
|
||||
|
||||
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
|
||||
projectId = model.ProjectId.ValueString()
|
||||
projectID = model.ProjectId.ValueString()
|
||||
} else {
|
||||
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
|
||||
return nil, fmt.Errorf("project_id not found in config")
|
||||
}
|
||||
projectId = identity.ProjectID.ValueString()
|
||||
projectID = identity.ProjectID.ValueString()
|
||||
}
|
||||
|
||||
if !model.Region.IsNull() && !model.Region.IsUnknown() {
|
||||
|
|
@ -714,27 +713,27 @@ func (r *userResource) extractIdentityData(
|
|||
}
|
||||
|
||||
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
|
||||
instanceId = model.InstanceId.ValueString()
|
||||
instanceID = model.InstanceId.ValueString()
|
||||
} else {
|
||||
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
|
||||
return nil, fmt.Errorf("instance_id not found in config")
|
||||
}
|
||||
instanceId = identity.InstanceID.ValueString()
|
||||
instanceID = identity.InstanceID.ValueString()
|
||||
}
|
||||
return &clientArg{
|
||||
projectId: projectId,
|
||||
instanceId: instanceId,
|
||||
projectID: projectID,
|
||||
instanceID: instanceID,
|
||||
region: region,
|
||||
userId: userId,
|
||||
userID: userID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// setTFLogFields adds relevant fields to the context for terraform logging purposes.
|
||||
func (r *userResource) setTFLogFields(ctx context.Context, arg *clientArg) context.Context {
|
||||
ctx = tflog.SetField(ctx, "project_id", arg.projectId)
|
||||
ctx = tflog.SetField(ctx, "instance_id", arg.instanceId)
|
||||
ctx = tflog.SetField(ctx, "project_id", arg.projectID)
|
||||
ctx = tflog.SetField(ctx, "instance_id", arg.instanceID)
|
||||
ctx = tflog.SetField(ctx, "region", arg.region)
|
||||
ctx = tflog.SetField(ctx, "user_id", arg.userId)
|
||||
ctx = tflog.SetField(ctx, "user_id", arg.userID)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue