From fe3eeab8e8cbb76a51dfb008a9216deaa6c193be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20Gei=C3=9Felhardt?= Date: Wed, 10 Apr 2024 16:23:06 +0200 Subject: [PATCH] Postgres Flex user update (#235) --- go.mod | 2 +- go.sum | 4 +- main.go | 14 ++++-- .../postgres-flex/instance/actions.go | 18 ++++---- .../resources/postgres-flex/user/actions.go | 46 +++++++++++++++++++ 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index a64d88bd..f3bcaf3f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/Masterminds/semver v1.5.0 - github.com/SchwarzIT/community-stackit-go-client v1.29.11 + github.com/SchwarzIT/community-stackit-go-client v1.29.12-0.20240304082813-7e948e03e8d9 github.com/go-test/deep v1.0.3 github.com/google/uuid v1.3.0 github.com/hashicorp/terraform-plugin-framework v1.2.0 diff --git a/go.sum b/go.sum index 8fcff0a3..7b25a61f 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,8 @@ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugX github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= -github.com/SchwarzIT/community-stackit-go-client v1.29.11 h1:Y4VZuSn6BwEyUDRQxj/sx6C7+eO6R5IO3R4VPYeM0Hs= -github.com/SchwarzIT/community-stackit-go-client v1.29.11/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg= +github.com/SchwarzIT/community-stackit-go-client v1.29.12-0.20240304082813-7e948e03e8d9 h1:VHeXGYNZLNWLQ04PnVEgoxaw5blx8A62aou5uDR2ATA= +github.com/SchwarzIT/community-stackit-go-client v1.29.12-0.20240304082813-7e948e03e8d9/go.mod h1:hlTfBNOKE1fokWE8g3KrI0AHo0SqzTKkS+LrIdhH8Qg= github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE= diff --git a/main.go b/main.go index e21967bb..1a643850 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "flag" "log" "github.com/SchwarzIT/terraform-provider-stackit/stackit" @@ -14,9 +15,16 @@ var ( ) func main() { - err := providerserver.Serve(context.Background(), stackit.New(version), providerserver.ServeOpts{ - Address: "registry.terraform.io/schwarzit/stackit", - }) + var debug bool + flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve") + flag.Parse() + + opts := providerserver.ServeOpts{ + Address: "github.com/schwarzit/stackit", + Debug: debug, + } + + err := providerserver.Serve(context.Background(), stackit.New(version), opts) if err != nil { log.Fatal(err.Error()) } diff --git a/stackit/internal/resources/postgres-flex/instance/actions.go b/stackit/internal/resources/postgres-flex/instance/actions.go index b1fc130d..e4e5092c 100644 --- a/stackit/internal/resources/postgres-flex/instance/actions.go +++ b/stackit/internal/resources/postgres-flex/instance/actions.go @@ -71,20 +71,20 @@ func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp * v := plan.Version.ValueString() body := instance.InstanceCreateInstanceRequest{ - Name: &name, - ACL: &instance.InstanceACL{ + Name: name, + ACL: instance.InstanceACL{ Items: &acl, }, - BackupSchedule: &bu, - FlavorID: &flavorID, + BackupSchedule: bu, + FlavorID: flavorID, Labels: &plan.Labels, - Options: &plan.Options, - Replicas: &repl, - Storage: &instance.InstanceStorage{ + Options: plan.Options, + Replicas: repl, + Storage: instance.InstanceStorage{ Class: &sc, Size: &ss, }, - Version: &v, + Version: v, } res, err := c.Instance.Create(ctx, plan.ProjectID.ValueString(), body) if agg := common.Validate(&resp.Diagnostics, res, err, "JSON201.ID"); agg != nil { @@ -226,7 +226,7 @@ func (r Resource) Update(ctx context.Context, req resource.UpdateRequest, resp * ss := int(storage.Size.ValueInt64()) v := plan.Version.ValueString() - body := instance.InstanceUpdateInstanceRequest{ + body := instance.InstancePartialUpdateInstanceRequest{ Name: &name, ACL: &instance.InstanceACL{ Items: &acl, diff --git a/stackit/internal/resources/postgres-flex/user/actions.go b/stackit/internal/resources/postgres-flex/user/actions.go index ca8766de..c1d935ba 100644 --- a/stackit/internal/resources/postgres-flex/user/actions.go +++ b/stackit/internal/resources/postgres-flex/user/actions.go @@ -161,6 +161,52 @@ func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *reso // Update - lifecycle function func (r Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var plan, state User + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) + if resp.Diagnostics.HasError() { + return + } + + var roles []string + usingRoleSet := plan.Roles.IsUnknown() + if usingRoleSet { + resp.Diagnostics.Append(plan.RoleSet.ElementsAs(ctx, &roles, true)...) + if resp.Diagnostics.HasError() { + return + } + } else { + // @TODO: remove roles in future release + resp.Diagnostics.Append(plan.Roles.ElementsAs(ctx, &roles, true)...) + if resp.Diagnostics.HasError() { + return + } + } + + body := users.PatchUserJSONRequestBody{ + Roles: &roles, + } + + res, err := r.client.PostgresFlex.Users.PatchUser(ctx, plan.ProjectID.ValueString(), plan.InstanceID.ValueString(), plan.ID.ValueString(), body) + if agg := common.Validate(&resp.Diagnostics, res, err); agg != nil { + resp.Diagnostics.AddError("failed updating postgres flex db user", agg.Error()) + return + } + + rolesState := []attr.Value{} + for _, v := range roles { + rolesState = append(rolesState, types.StringValue(v)) + } + state.RoleSet = types.SetValueMust(types.StringType, rolesState) + // @TODO: remove roles in future release + state.Roles = types.ListValueMust(types.StringType, rolesState) + + // update state + diags := resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } } // Delete - lifecycle function