feat(postgresql): Region adjustment (#713)

Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
This commit is contained in:
Alexander Dahmen 2025-03-21 13:52:10 +01:00 committed by GitHub
parent e989102d6b
commit 6cc1dffc22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 458 additions and 148 deletions

View file

@ -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
}