feat(postgresql): Region adjustment (#713)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
This commit is contained in:
parent
e989102d6b
commit
6cc1dffc22
19 changed files with 458 additions and 148 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
|
|
@ -34,6 +35,7 @@ type DataSourceModel struct {
|
|||
Roles types.Set `tfsdk:"roles"`
|
||||
Host types.String `tfsdk:"host"`
|
||||
Port types.Int64 `tfsdk:"port"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
}
|
||||
|
||||
// NewUserDataSource is a helper function to simplify the provider implementation.
|
||||
|
|
@ -43,7 +45,8 @@ func NewUserDataSource() datasource.DataSource {
|
|||
|
||||
// userDataSource is the data source implementation.
|
||||
type userDataSource struct {
|
||||
client *postgresflex.APIClient
|
||||
client *postgresflex.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// Metadata returns the data source type name.
|
||||
|
|
@ -58,7 +61,8 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
|
|||
return
|
||||
}
|
||||
|
||||
providerData, ok := req.ProviderData.(core.ProviderData)
|
||||
var ok bool
|
||||
r.providerData, ok = req.ProviderData.(core.ProviderData)
|
||||
if !ok {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
|
||||
return
|
||||
|
|
@ -66,15 +70,15 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
|
|||
|
||||
var apiClient *postgresflex.APIClient
|
||||
var err error
|
||||
if providerData.PostgresFlexCustomEndpoint != "" {
|
||||
if r.providerData.PostgresFlexCustomEndpoint != "" {
|
||||
apiClient, err = postgresflex.NewAPIClient(
|
||||
config.WithCustomAuth(providerData.RoundTripper),
|
||||
config.WithEndpoint(providerData.PostgresFlexCustomEndpoint),
|
||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClient, err = postgresflex.NewAPIClient(
|
||||
config.WithCustomAuth(providerData.RoundTripper),
|
||||
config.WithRegion(providerData.GetRegion()),
|
||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
config.WithRegion(r.providerData.GetRegion()),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -91,10 +95,11 @@ func (r *userDataSource) Configure(ctx context.Context, req datasource.Configure
|
|||
func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
descriptions := map[string]string{
|
||||
"main": "Postgres Flex user data source schema. Must have a `region` specified in the provider configuration.",
|
||||
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".",
|
||||
"id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".",
|
||||
"user_id": "User ID.",
|
||||
"instance_id": "ID of the PostgresFlex instance.",
|
||||
"project_id": "STACKIT project ID to which the instance is associated.",
|
||||
"region": "The resource region. If not defined, the provider region is used.",
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
|
|
@ -140,6 +145,11 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
|
|||
"port": schema.Int64Attribute{
|
||||
Computed: true,
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
// the region cannot be found automatically, so it has to be passed
|
||||
Optional: true,
|
||||
Description: descriptions["region"],
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -155,11 +165,18 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
|
|||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueString()
|
||||
var region string
|
||||
if utils.IsUndefined(model.Region) {
|
||||
region = r.providerData.GetRegion()
|
||||
} else {
|
||||
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)
|
||||
|
||||
recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute()
|
||||
recordSetResp, err := r.client.GetUser(ctx, projectId, region, instanceId, userId).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 {
|
||||
|
|
@ -170,7 +187,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
|
|||
}
|
||||
|
||||
// Map response body to schema and populate Computed attribute values
|
||||
err = mapDataSourceFields(recordSetResp, &model)
|
||||
err = mapDataSourceFields(recordSetResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -185,7 +202,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
|
|||
tflog.Info(ctx, "Postgres Flex user read")
|
||||
}
|
||||
|
||||
func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSourceModel) error {
|
||||
func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSourceModel, region string) error {
|
||||
if userResp == nil || userResp.Item == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -204,6 +221,7 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSour
|
|||
}
|
||||
idParts := []string{
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
userId,
|
||||
}
|
||||
|
|
@ -228,5 +246,6 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSour
|
|||
}
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Region = types.StringValue(region)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@ import (
|
|||
)
|
||||
|
||||
func TestMapDataSourceFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *postgresflex.GetUserResponse
|
||||
region string
|
||||
expected DataSourceModel
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -22,8 +24,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
&postgresflex.GetUserResponse{
|
||||
Item: &postgresflex.UserResponse{},
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -31,6 +34,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Roles: types.SetNull(types.StringType),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -48,8 +52,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(1234)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -59,8 +64,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
}),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -75,8 +81,9 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -84,18 +91,21 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
Roles: 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",
|
||||
&postgresflex.GetUserResponse{},
|
||||
testRegion,
|
||||
DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -104,6 +114,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
&postgresflex.GetUserResponse{
|
||||
Item: &postgresflex.UserResponse{},
|
||||
},
|
||||
testRegion,
|
||||
DataSourceModel{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -115,7 +126,7 @@ func TestMapDataSourceFields(t *testing.T) {
|
|||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
}
|
||||
err := mapDataSourceFields(tt.input, state)
|
||||
err := mapDataSourceFields(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ var (
|
|||
_ resource.Resource = &userResource{}
|
||||
_ resource.ResourceWithConfigure = &userResource{}
|
||||
_ resource.ResourceWithImportState = &userResource{}
|
||||
_ resource.ResourceWithModifyPlan = &userResource{}
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
|
|
@ -47,6 +48,7 @@ type Model struct {
|
|||
Host types.String `tfsdk:"host"`
|
||||
Port types.Int64 `tfsdk:"port"`
|
||||
Uri types.String `tfsdk:"uri"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
}
|
||||
|
||||
// NewUserResource is a helper function to simplify the provider implementation.
|
||||
|
|
@ -56,7 +58,38 @@ func NewUserResource() resource.Resource {
|
|||
|
||||
// userResource is the resource implementation.
|
||||
type userResource struct {
|
||||
client *postgresflex.APIClient
|
||||
client *postgresflex.APIClient
|
||||
providerData core.ProviderData
|
||||
}
|
||||
|
||||
// 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 Model
|
||||
// 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 Model
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// Metadata returns the resource type name.
|
||||
|
|
@ -71,7 +104,8 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
|
|||
return
|
||||
}
|
||||
|
||||
providerData, ok := req.ProviderData.(core.ProviderData)
|
||||
var ok bool
|
||||
r.providerData, ok = req.ProviderData.(core.ProviderData)
|
||||
if !ok {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
|
||||
return
|
||||
|
|
@ -79,15 +113,15 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ
|
|||
|
||||
var apiClient *postgresflex.APIClient
|
||||
var err error
|
||||
if providerData.PostgresFlexCustomEndpoint != "" {
|
||||
if r.providerData.PostgresFlexCustomEndpoint != "" {
|
||||
apiClient, err = postgresflex.NewAPIClient(
|
||||
config.WithCustomAuth(providerData.RoundTripper),
|
||||
config.WithEndpoint(providerData.PostgresFlexCustomEndpoint),
|
||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
config.WithEndpoint(r.providerData.PostgresFlexCustomEndpoint),
|
||||
)
|
||||
} else {
|
||||
apiClient, err = postgresflex.NewAPIClient(
|
||||
config.WithCustomAuth(providerData.RoundTripper),
|
||||
config.WithRegion(providerData.GetRegion()),
|
||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
||||
config.WithRegion(r.providerData.GetRegion()),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -106,11 +140,12 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
|
|||
|
||||
descriptions := map[string]string{
|
||||
"main": "Postgres Flex user resource schema. Must have a `region` specified in the provider configuration.",
|
||||
"id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".",
|
||||
"id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".",
|
||||
"user_id": "User ID.",
|
||||
"instance_id": "ID of the PostgresFlex instance.",
|
||||
"project_id": "STACKIT project ID to which the instance is associated.",
|
||||
"roles": "Database access levels for the user. " + utils.SupportedValuesDocumentation(rolesOptions),
|
||||
"region": "The resource region. If not defined, the provider region is used.",
|
||||
}
|
||||
|
||||
resp.Schema = schema.Schema{
|
||||
|
|
@ -190,6 +225,15 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
|
|||
Computed: true,
|
||||
Sensitive: true,
|
||||
},
|
||||
"region": schema.StringAttribute{
|
||||
Optional: true,
|
||||
// must be computed to allow for storing the override value from the provider
|
||||
Computed: true,
|
||||
Description: descriptions["region"],
|
||||
PlanModifiers: []planmodifier.String{
|
||||
stringplanmodifier.RequiresReplace(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -204,8 +248,10 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
}
|
||||
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()) {
|
||||
|
|
@ -223,7 +269,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
return
|
||||
}
|
||||
// Create new user
|
||||
userResp, err := r.client.CreateUser(ctx, projectId, instanceId).CreateUserPayload(*payload).Execute()
|
||||
userResp, err := r.client.CreateUser(ctx, projectId, region, instanceId).CreateUserPayload(*payload).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
|
|
@ -236,7 +282,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
ctx = tflog.SetField(ctx, "user_id", userId)
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFieldsCreate(userResp, &model)
|
||||
err = mapFieldsCreate(userResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -261,11 +307,16 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
|
|||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueString()
|
||||
region := model.Region.ValueString()
|
||||
if region == "" {
|
||||
region = r.providerData.GetRegion()
|
||||
}
|
||||
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.GetUser(ctx, projectId, instanceId, userId).Execute()
|
||||
recordSetResp, err := r.client.GetUser(ctx, projectId, region, instanceId, userId).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 {
|
||||
|
|
@ -277,7 +328,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
|
|||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(recordSetResp, &model)
|
||||
err = mapFields(recordSetResp, &model, region)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
@ -311,16 +362,18 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r
|
|||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
userId := model.UserId.ValueString()
|
||||
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.DeleteUser(ctx, projectId, instanceId, userId).Execute()
|
||||
err := r.client.DeleteUser(ctx, projectId, region, instanceId, userId).Execute()
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err))
|
||||
}
|
||||
_, err = wait.DeleteUserWaitHandler(ctx, r.client, projectId, instanceId, userId).WaitWithContext(ctx)
|
||||
_, err = wait.DeleteUserWaitHandler(ctx, r.client, projectId, region, instanceId, userId).WaitWithContext(ctx)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Instance deletion waiting: %v", err))
|
||||
return
|
||||
|
|
@ -332,17 +385,18 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r
|
|||
// 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) {
|
||||
idParts := strings.Split(req.ID, core.Separator)
|
||||
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
|
||||
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],[instance_id],[user_id], got %q", req.ID),
|
||||
fmt.Sprintf("Expected import identifier with format [project_id],[region],[instance_id],[user_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("instance_id"), idParts[1])...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[2])...)
|
||||
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"), idParts[3])...)
|
||||
core.LogAndAddWarning(ctx, &resp.Diagnostics,
|
||||
"Postgresflex user imported with empty password and empty uri",
|
||||
"The user password and uri are not imported as they are only available upon creation of a new user. The password and uri fields will be empty.",
|
||||
|
|
@ -350,7 +404,7 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState
|
|||
tflog.Info(ctx, "Postgresflex user state imported")
|
||||
}
|
||||
|
||||
func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) error {
|
||||
func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model, region string) error {
|
||||
if userResp == nil || userResp.Item == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -365,6 +419,7 @@ func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) er
|
|||
userId := *user.Id
|
||||
idParts := []string{
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
userId,
|
||||
}
|
||||
|
|
@ -395,10 +450,11 @@ func mapFieldsCreate(userResp *postgresflex.CreateUserResponse, model *Model) er
|
|||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Uri = types.StringPointerValue(user.Uri)
|
||||
model.Region = types.StringValue(region)
|
||||
return nil
|
||||
}
|
||||
|
||||
func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error {
|
||||
func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error {
|
||||
if userResp == nil || userResp.Item == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
|
|
@ -417,6 +473,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error {
|
|||
}
|
||||
idParts := []string{
|
||||
model.ProjectId.ValueString(),
|
||||
region,
|
||||
model.InstanceId.ValueString(),
|
||||
userId,
|
||||
}
|
||||
|
|
@ -441,6 +498,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model) error {
|
|||
}
|
||||
model.Host = types.StringPointerValue(user.Host)
|
||||
model.Port = types.Int64PointerValue(user.Port)
|
||||
model.Region = types.StringValue(region)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@ import (
|
|||
)
|
||||
|
||||
func TestMapFieldsCreate(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *postgresflex.CreateUserResponse
|
||||
region string
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -25,8 +27,9 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Password: utils.Ptr(""),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -36,6 +39,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Uri: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -56,8 +60,9 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Uri: utils.Ptr("uri"),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -71,6 +76,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Uri: types.StringValue("uri"),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -87,8 +93,9 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Uri: nil,
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -98,18 +105,21 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Uri: types.StringNull(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&postgresflex.CreateUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -118,6 +128,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
&postgresflex.CreateUserResponse{
|
||||
Item: &postgresflex.User{},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -128,6 +139,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
Id: utils.Ptr("uid"),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -138,7 +150,7 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
ProjectId: tt.expected.ProjectId,
|
||||
InstanceId: tt.expected.InstanceId,
|
||||
}
|
||||
err := mapFieldsCreate(tt.input, state)
|
||||
err := mapFieldsCreate(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -156,9 +168,11 @@ func TestMapFieldsCreate(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
const testRegion = "region"
|
||||
tests := []struct {
|
||||
description string
|
||||
input *postgresflex.GetUserResponse
|
||||
region string
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -167,8 +181,9 @@ func TestMapFields(t *testing.T) {
|
|||
&postgresflex.GetUserResponse{
|
||||
Item: &postgresflex.UserResponse{},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -176,6 +191,7 @@ func TestMapFields(t *testing.T) {
|
|||
Roles: types.SetNull(types.StringType),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Null(),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -193,8 +209,9 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(1234)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -204,8 +221,9 @@ func TestMapFields(t *testing.T) {
|
|||
types.StringValue("role_2"),
|
||||
types.StringValue(""),
|
||||
}),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Host: types.StringValue("host"),
|
||||
Port: types.Int64Value(1234),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
|
|
@ -220,8 +238,9 @@ func TestMapFields(t *testing.T) {
|
|||
Port: utils.Ptr(int64(2123456789)),
|
||||
},
|
||||
},
|
||||
testRegion,
|
||||
Model{
|
||||
Id: types.StringValue("pid,iid,uid"),
|
||||
Id: types.StringValue("pid,region,iid,uid"),
|
||||
UserId: types.StringValue("uid"),
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
|
|
@ -229,18 +248,21 @@ func TestMapFields(t *testing.T) {
|
|||
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
|
||||
Host: types.StringNull(),
|
||||
Port: types.Int64Value(2123456789),
|
||||
Region: types.StringValue(testRegion),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
nil,
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response_2",
|
||||
&postgresflex.GetUserResponse{},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -249,6 +271,7 @@ func TestMapFields(t *testing.T) {
|
|||
&postgresflex.GetUserResponse{
|
||||
Item: &postgresflex.UserResponse{},
|
||||
},
|
||||
testRegion,
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
|
|
@ -260,7 +283,7 @@ func TestMapFields(t *testing.T) {
|
|||
InstanceId: tt.expected.InstanceId,
|
||||
UserId: tt.expected.UserId,
|
||||
}
|
||||
err := mapFields(tt.input, state)
|
||||
err := mapFields(tt.input, state, tt.region)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue