Skip to content

Commit

Permalink
Postgres Flex user update (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
k2g11 committed Apr 10, 2024
1 parent 14711a1 commit fe3eeab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"flag"
"log"

"github.com/SchwarzIT/terraform-provider-stackit/stackit"
Expand All @@ -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())
}
Expand Down
18 changes: 9 additions & 9 deletions stackit/internal/resources/postgres-flex/instance/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions stackit/internal/resources/postgres-flex/user/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe3eeab

Please sign in to comment.