fix: pgsql user waiter
This commit is contained in:
parent
459120d3b3
commit
4189a5ebcb
7 changed files with 101 additions and 61 deletions
|
|
@ -123,3 +123,7 @@ func StringFromTemplate(tplFile string, data any) (string, error) {
|
||||||
|
|
||||||
return tplBuf.String(), nil
|
return tplBuf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ResStr(prefix, resource, name string) string {
|
||||||
|
return fmt.Sprintf("%s_%s.%s", prefix, resource, name)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ import (
|
||||||
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
|
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const pfx = "stackitprivatepreview_postgresflexalpha"
|
||||||
|
|
||||||
func TestInstanceResourceSchema(t *testing.T) {
|
func TestInstanceResourceSchema(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
@ -149,6 +151,8 @@ func getExample() resData {
|
||||||
|
|
||||||
func TestAccInstance(t *testing.T) {
|
func TestAccInstance(t *testing.T) {
|
||||||
exData := getExample()
|
exData := getExample()
|
||||||
|
t.Logf(" ... working on instance %s", exData.TfName)
|
||||||
|
|
||||||
resName := fmt.Sprintf(
|
resName := fmt.Sprintf(
|
||||||
"stackitprivatepreview_postgresflexalpha_instance.%s",
|
"stackitprivatepreview_postgresflexalpha_instance.%s",
|
||||||
exData.TfName,
|
exData.TfName,
|
||||||
|
|
@ -211,6 +215,8 @@ func TestAccInstance(t *testing.T) {
|
||||||
|
|
||||||
func TestAccInstanceWithUsers(t *testing.T) {
|
func TestAccInstanceWithUsers(t *testing.T) {
|
||||||
data := getExample()
|
data := getExample()
|
||||||
|
t.Logf(" ... working on instance %s", data.TfName)
|
||||||
|
|
||||||
userName := "testUser"
|
userName := "testUser"
|
||||||
data.Users = []User{
|
data.Users = []User{
|
||||||
{
|
{
|
||||||
|
|
@ -220,16 +226,6 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
resName := fmt.Sprintf(
|
|
||||||
"stackitprivatepreview_postgresflexalpha_instance.%s",
|
|
||||||
data.TfName,
|
|
||||||
)
|
|
||||||
|
|
||||||
resUserName := fmt.Sprintf(
|
|
||||||
"stackitprivatepreview_postgresflexalpha_user.%s",
|
|
||||||
userName,
|
|
||||||
)
|
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||||
|
|
@ -241,10 +237,10 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
||||||
data,
|
data,
|
||||||
),
|
),
|
||||||
Check: resource.ComposeAggregateTestCheckFunc(
|
Check: resource.ComposeAggregateTestCheckFunc(
|
||||||
resource.TestCheckResourceAttr(resName, "name", data.Name),
|
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "instance", data.TfName), "name", data.Name),
|
||||||
resource.TestCheckResourceAttrSet(resName, "id"),
|
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "instance", data.TfName), "id"),
|
||||||
resource.TestCheckResourceAttr(resUserName, "name", userName),
|
resource.TestCheckResourceAttr(testutils.ResStr(pfx, "user", userName), "name", userName),
|
||||||
resource.TestCheckResourceAttrSet(resUserName, "id"),
|
resource.TestCheckResourceAttrSet(testutils.ResStr(pfx, "user", userName), "id"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
@ -253,6 +249,8 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
||||||
|
|
||||||
func TestAccInstanceWithDatabases(t *testing.T) {
|
func TestAccInstanceWithDatabases(t *testing.T) {
|
||||||
data := getExample()
|
data := getExample()
|
||||||
|
t.Logf(" ... working on instance %s", data.TfName)
|
||||||
|
|
||||||
dbName := "testDb"
|
dbName := "testDb"
|
||||||
userName := "testUser"
|
userName := "testUser"
|
||||||
data.Users = []User{
|
data.Users = []User{
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,14 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
||||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
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"
|
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"
|
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"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
|
|
@ -52,7 +54,7 @@ type UserResourceIdentityModel struct {
|
||||||
ProjectID types.String `tfsdk:"project_id"`
|
ProjectID types.String `tfsdk:"project_id"`
|
||||||
Region types.String `tfsdk:"region"`
|
Region types.String `tfsdk:"region"`
|
||||||
InstanceID types.String `tfsdk:"instance_id"`
|
InstanceID types.String `tfsdk:"instance_id"`
|
||||||
UserID types.Int64 `tfsdk:"database_id"`
|
UserID types.Int64 `tfsdk:"user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// userResource implements the resource handling for a PostgreSQL Flex user.
|
// userResource implements the resource handling for a PostgreSQL Flex user.
|
||||||
|
|
@ -150,13 +152,6 @@ func (r *userResource) Create(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read identity data
|
|
||||||
var identityData UserResourceIdentityModel
|
|
||||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = core.InitProviderContext(ctx)
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
arg := &clientArg{
|
arg := &clientArg{
|
||||||
|
|
@ -178,6 +173,7 @@ func (r *userResource) Create(
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Creating API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Creating API payload: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new user
|
// Create new user
|
||||||
userResp, err := r.client.CreateUserRequest(
|
userResp, err := r.client.CreateUserRequest(
|
||||||
ctx,
|
ctx,
|
||||||
|
|
@ -185,13 +181,13 @@ func (r *userResource) Create(
|
||||||
arg.region,
|
arg.region,
|
||||||
arg.instanceId,
|
arg.instanceId,
|
||||||
).CreateUserRequestPayload(*payload).Execute()
|
).CreateUserRequestPayload(*payload).Execute()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if userResp.Id == nil || *userResp.Id == 0 {
|
id, ok := userResp.GetIdOk()
|
||||||
|
if !ok || id == 0 {
|
||||||
core.LogAndAddError(
|
core.LogAndAddError(
|
||||||
ctx,
|
ctx,
|
||||||
&resp.Diagnostics,
|
&resp.Diagnostics,
|
||||||
|
|
@ -200,13 +196,8 @@ func (r *userResource) Create(
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
model.Id = types.Int64Value(userResp.GetId())
|
|
||||||
model.UserId = types.Int64Value(userResp.GetId())
|
|
||||||
model.Password = types.StringValue(userResp.GetPassword())
|
|
||||||
model.Status = types.StringValue(userResp.GetStatus())
|
|
||||||
model.ConnectionString = types.StringValue(userResp.GetConnectionString())
|
|
||||||
|
|
||||||
ctx = tflog.SetField(ctx, "user_id", userResp.GetId())
|
ctx = tflog.SetField(ctx, "user_id", id)
|
||||||
|
|
||||||
ctx = core.LogResponse(ctx)
|
ctx = core.LogResponse(ctx)
|
||||||
|
|
||||||
|
|
@ -215,28 +206,52 @@ func (r *userResource) Create(
|
||||||
ProjectID: types.StringValue(arg.projectId),
|
ProjectID: types.StringValue(arg.projectId),
|
||||||
Region: types.StringValue(arg.region),
|
Region: types.StringValue(arg.region),
|
||||||
InstanceID: types.StringValue(arg.instanceId),
|
InstanceID: types.StringValue(arg.instanceId),
|
||||||
UserID: types.Int64Value(userResp.GetId()),
|
UserID: types.Int64Value(id),
|
||||||
}
|
}
|
||||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
resp.Diagnostics.Append(resp.Identity.Set(ctx, identity)...)
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify creation
|
model.Id = types.Int64Value(id)
|
||||||
exists, err := r.getUserResource(ctx, &model, arg)
|
model.UserId = types.Int64Value(id)
|
||||||
|
model.Password = types.StringValue(userResp.GetPassword())
|
||||||
|
model.Status = types.StringValue(userResp.GetStatus())
|
||||||
|
model.ConnectionString = types.StringValue(userResp.GetConnectionString())
|
||||||
|
|
||||||
|
waitResp, err := postgresflexalphaWait.CreateUserWaitHandler(
|
||||||
|
ctx,
|
||||||
|
r.client,
|
||||||
|
arg.projectId,
|
||||||
|
arg.instanceId,
|
||||||
|
arg.region,
|
||||||
|
arg.userId,
|
||||||
|
).SetSleepBeforeWait(
|
||||||
|
10 * time.Second,
|
||||||
|
).SetTimeout(
|
||||||
|
15 * time.Minute,
|
||||||
|
).WaitWithContext(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
core.LogAndAddError(
|
core.LogAndAddError(
|
||||||
ctx, &resp.Diagnostics, "Error creating user",
|
ctx,
|
||||||
fmt.Sprintf("User ID '%v' resource not found after creation", model.UserId.ValueInt64()),
|
&resp.Diagnostics,
|
||||||
|
"create user",
|
||||||
|
fmt.Sprintf("Instance creation waiting: %v", err),
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if waitResp.Id == nil || *waitResp.Id != arg.userId {
|
||||||
|
core.LogAndAddError(
|
||||||
|
ctx,
|
||||||
|
&resp.Diagnostics,
|
||||||
|
"create user",
|
||||||
|
"Instance creation waiting: returned id is nil or wrong",
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Set state to fully populated data
|
// Set state to fully populated data
|
||||||
diags = resp.State.Set(ctx, model)
|
diags = resp.State.Set(ctx, model)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
|
|
@ -320,23 +335,12 @@ func (r *userResource) Update(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read identity data
|
|
||||||
var identityData UserResourceIdentityModel
|
|
||||||
resp.Diagnostics.Append(req.Identity.Get(ctx, &identityData)...)
|
|
||||||
if resp.Diagnostics.HasError() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = core.InitProviderContext(ctx)
|
ctx = core.InitProviderContext(ctx)
|
||||||
|
|
||||||
arg, errExt := r.extractIdentityData(model, identityData)
|
arg := &clientArg{
|
||||||
if errExt != nil {
|
projectId: model.ProjectId.ValueString(),
|
||||||
core.LogAndAddError(
|
instanceId: model.InstanceId.ValueString(),
|
||||||
ctx,
|
region: r.providerData.GetRegionWithOverride(model.Region),
|
||||||
&resp.Diagnostics,
|
|
||||||
extractErrorSummary,
|
|
||||||
fmt.Sprintf(extractErrorMessage, errExt),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = r.setTFLogFields(ctx, arg)
|
ctx = r.setTFLogFields(ctx, arg)
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ fields:
|
||||||
|
|
||||||
- name: 'owner'
|
- name: 'owner'
|
||||||
modifiers:
|
modifiers:
|
||||||
- 'RequiresReplace'
|
- 'UseStateForUnknown'
|
||||||
|
|
||||||
- name: 'database_name'
|
- name: 'database_name'
|
||||||
modifiers:
|
modifiers:
|
||||||
|
|
@ -43,6 +43,7 @@ fields:
|
||||||
|
|
||||||
- name: 'compatibility'
|
- name: 'compatibility'
|
||||||
modifiers:
|
modifiers:
|
||||||
|
- 'UseStateForUnknown'
|
||||||
- 'RequiresReplace'
|
- 'RequiresReplace'
|
||||||
|
|
||||||
- name: 'compatibility_level'
|
- name: 'compatibility_level'
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,7 @@ func getExample() resData {
|
||||||
|
|
||||||
func TestAccInstance(t *testing.T) {
|
func TestAccInstance(t *testing.T) {
|
||||||
exData := getExample()
|
exData := getExample()
|
||||||
|
t.Logf(" ... working on instance %s", exData.TfName)
|
||||||
|
|
||||||
updNameData := exData
|
updNameData := exData
|
||||||
updNameData.Name = "name-updated"
|
updNameData.Name = "name-updated"
|
||||||
|
|
|
||||||
|
|
@ -3,16 +3,13 @@ fields:
|
||||||
modifiers:
|
modifiers:
|
||||||
- 'UseStateForUnknown'
|
- 'UseStateForUnknown'
|
||||||
|
|
||||||
- name: 'user_id'
|
|
||||||
modifiers:
|
|
||||||
- 'UseStateForUnknown'
|
|
||||||
|
|
||||||
- name: 'instance_id'
|
- name: 'instance_id'
|
||||||
validators:
|
validators:
|
||||||
- validate.NoSeparator
|
- validate.NoSeparator
|
||||||
- validate.UUID
|
- validate.UUID
|
||||||
modifiers:
|
modifiers:
|
||||||
- 'UseStateForUnknown'
|
- 'UseStateForUnknown'
|
||||||
|
- 'RequiresReplace'
|
||||||
|
|
||||||
- name: 'project_id'
|
- name: 'project_id'
|
||||||
validators:
|
validators:
|
||||||
|
|
@ -28,6 +25,7 @@ fields:
|
||||||
|
|
||||||
- name: 'user_id'
|
- name: 'user_id'
|
||||||
modifiers:
|
modifiers:
|
||||||
|
- 'UseStateForUnknown'
|
||||||
- 'RequiresReplace'
|
- 'RequiresReplace'
|
||||||
|
|
||||||
- name: 'username'
|
- name: 'username'
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@ package postgresflexalpha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
|
|
@ -40,7 +43,7 @@ type APIClientInstanceInterface interface {
|
||||||
|
|
||||||
// APIClientUserInterface Interface needed for tests
|
// APIClientUserInterface Interface needed for tests
|
||||||
type APIClientUserInterface interface {
|
type APIClientUserInterface interface {
|
||||||
GetUserRequestExecute(ctx context.Context, projectId, region, instanceId string, userId int64) (
|
GetUserRequestExecute(ctx context.Context, projectId, region, instanceId string, userId int32) (
|
||||||
*postgresflex.GetUserResponse,
|
*postgresflex.GetUserResponse,
|
||||||
error,
|
error,
|
||||||
)
|
)
|
||||||
|
|
@ -202,3 +205,34 @@ func PartialUpdateInstanceWaitHandler(
|
||||||
handler.SetTimeout(45 * time.Minute).SetSleepBeforeWait(30 * time.Second)
|
handler.SetTimeout(45 * time.Minute).SetSleepBeforeWait(30 * time.Second)
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateUserWaitHandler will wait for instance creation
|
||||||
|
func CreateUserWaitHandler(
|
||||||
|
ctx context.Context,
|
||||||
|
a APIClientUserInterface,
|
||||||
|
projectId, instanceId, region string,
|
||||||
|
userId int64,
|
||||||
|
) *wait.AsyncActionHandler[postgresflex.GetUserResponse] {
|
||||||
|
handler := wait.New(
|
||||||
|
func() (waitFinished bool, response *postgresflex.GetUserResponse, err error) {
|
||||||
|
if userId > math.MaxInt32 {
|
||||||
|
return false, nil, fmt.Errorf("userId value is too big for int32")
|
||||||
|
}
|
||||||
|
userId32 := int32(userId)
|
||||||
|
s, err := a.GetUserRequestExecute(ctx, projectId, region, instanceId, userId32)
|
||||||
|
if err != nil {
|
||||||
|
var oapiErr *oapierror.GenericOpenAPIError
|
||||||
|
ok := errors.As(err, &oapiErr)
|
||||||
|
if !ok {
|
||||||
|
return false, nil, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError")
|
||||||
|
}
|
||||||
|
if oapiErr.StatusCode != http.StatusNotFound {
|
||||||
|
return false, nil, err
|
||||||
|
}
|
||||||
|
return false, nil, nil
|
||||||
|
}
|
||||||
|
return true, s, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue