fix: fix lintings
This commit is contained in:
parent
d765c72b8f
commit
85deade39b
7 changed files with 52 additions and 130 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
|
@ -266,13 +267,19 @@ func setupMockServer() *httptest.Server {
|
|||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
err := json.NewEncoder(w).Encode(map[string]string{
|
||||
"id": "mock-id-123",
|
||||
"name": "test-resource",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
case http.MethodGet:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode([]map[string]string{})
|
||||
err := json.NewEncoder(w).Encode([]map[string]string{})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -284,7 +291,10 @@ func TestUnitResourceCreate(t *testing.T) {
|
|||
defer server.Close()
|
||||
|
||||
// Configure provider to use mock server URL
|
||||
os.Setenv("API_ENDPOINT", server.URL)
|
||||
err := os.Setenv("API_ENDPOINT", server.URL)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// Run unit tests against mock
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
|
||||
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
|
|
@ -503,45 +502,6 @@ func (r *userResource) IdentitySchema(
|
|||
}
|
||||
}
|
||||
|
||||
func mapFields(userResp *postgresflex.GetUserResponse, model *resourceModel, region string) error {
|
||||
if userResp == nil {
|
||||
return fmt.Errorf("response is nil")
|
||||
}
|
||||
if model == nil {
|
||||
return fmt.Errorf("model input is nil")
|
||||
}
|
||||
user := userResp
|
||||
|
||||
var userId int64
|
||||
if model.UserId.ValueInt64() != 0 {
|
||||
userId = model.UserId.ValueInt64()
|
||||
} else if user.Id != nil {
|
||||
userId = *user.Id
|
||||
} else {
|
||||
return fmt.Errorf("user id not present")
|
||||
}
|
||||
|
||||
model.UserId = types.Int64Value(userId)
|
||||
model.Name = types.StringPointerValue(user.Name)
|
||||
|
||||
if user.Roles == nil {
|
||||
model.Roles = types.List(types.SetNull(types.StringType))
|
||||
} else {
|
||||
var roles []attr.Value
|
||||
for _, role := range *user.Roles {
|
||||
roles = append(roles, types.StringValue(string(role)))
|
||||
}
|
||||
rolesSet, diags := types.SetValue(types.StringType, roles)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Roles = types.List(rolesSet)
|
||||
}
|
||||
model.Region = types.StringValue(region)
|
||||
model.Status = types.StringPointerValue(user.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
// getUserResource refreshes the resource state by calling the API and mapping the response to the model.
|
||||
// Returns true if the resource state was successfully refreshed, false if the resource does not exist.
|
||||
func (r *userResource) getUserResource(ctx context.Context, model *resourceModel, arg *clientArg) (bool, error) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package sqlserverflexbeta
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
|
|
@ -47,17 +48,7 @@ func mapResponseToModel(
|
|||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network response value",
|
||||
"access_scope",
|
||||
types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl",
|
||||
netAcl,
|
||||
"instance_address",
|
||||
types.StringValue(resp.Network.GetInstanceAddress()),
|
||||
"router_address",
|
||||
types.StringValue(resp.Network.GetRouterAddress()),
|
||||
)
|
||||
return errors.New("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
|
|
@ -113,17 +104,7 @@ func mapDataResponseToModel(
|
|||
)
|
||||
tfDiags.Append(diags...)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf(
|
||||
"error converting network response value",
|
||||
"access_scope",
|
||||
types.StringValue(string(resp.Network.GetAccessScope())),
|
||||
"acl",
|
||||
netAcl,
|
||||
"instance_address",
|
||||
types.StringValue(resp.Network.GetInstanceAddress()),
|
||||
"router_address",
|
||||
types.StringValue(resp.Network.GetRouterAddress()),
|
||||
)
|
||||
return errors.New("error converting network response value")
|
||||
}
|
||||
m.Network = net
|
||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||
|
|
|
|||
|
|
@ -7,8 +7,7 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbetaResGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance/resources_gen"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
)
|
||||
|
||||
func mapResponseToModel(
|
||||
|
|
@ -75,63 +74,25 @@ func mapResponseToModel(
|
|||
return nil
|
||||
}
|
||||
|
||||
// TODO: handle encryption field mapping when API supports it
|
||||
func handleEncryption(
|
||||
m *dataSourceModel,
|
||||
resp *sqlserverflexbeta.GetUserResponse,
|
||||
) sqlserverflexbetaResGen.EncryptionValue {
|
||||
/*
|
||||
if !resp.HasEncryption() ||
|
||||
|
||||
resp.Encryption == nil ||
|
||||
resp.Encryption.KekKeyId == nil ||
|
||||
resp.Encryption.KekKeyRingId == nil ||
|
||||
resp.Encryption.KekKeyVersion == nil ||
|
||||
resp.Encryption.ServiceAccount == nil {
|
||||
|
||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
}
|
||||
return m.Encryption
|
||||
}
|
||||
|
||||
enc := sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
if kVal, ok := resp.Encryption.GetKekKeyIdOk(); ok {
|
||||
enc.KekKeyId = types.StringValue(kVal)
|
||||
}
|
||||
if kkVal, ok := resp.Encryption.GetKekKeyRingIdOk(); ok {
|
||||
enc.KekKeyRingId = types.StringValue(kkVal)
|
||||
}
|
||||
if kkvVal, ok := resp.Encryption.GetKekKeyVersionOk(); ok {
|
||||
enc.KekKeyVersion = types.StringValue(kkvVal)
|
||||
}
|
||||
if sa, ok := resp.Encryption.GetServiceAccountOk(); ok {
|
||||
enc.ServiceAccount = types.StringValue(sa)
|
||||
}
|
||||
return enc
|
||||
*/
|
||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
||||
}
|
||||
|
||||
func toCreatePayload(
|
||||
ctx context.Context,
|
||||
model *dataSourceModel,
|
||||
) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
||||
var roles []sqlserverflexbeta.UserRole
|
||||
if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
diags := model.Roles.ElementsAs(ctx, &roles, false)
|
||||
if diags.HasError() {
|
||||
return nil, fmt.Errorf("failed to convert roles: %v", diags)
|
||||
}
|
||||
}
|
||||
|
||||
return &sqlserverflexbeta.CreateUserRequestPayload{
|
||||
DefaultDatabase: model.DefaultDatabase.ValueStringPointer(),
|
||||
Username: model.Username.ValueStringPointer(),
|
||||
Roles: &roles,
|
||||
}, nil
|
||||
}
|
||||
//func toCreatePayload(
|
||||
// ctx context.Context,
|
||||
// model *resourceModel,
|
||||
//) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||
// if model == nil {
|
||||
// return nil, fmt.Errorf("nil model")
|
||||
// }
|
||||
//
|
||||
// var roles []sqlserverflexbeta.UserRole
|
||||
// if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||
// diags := model.Roles.ElementsAs(ctx, &roles, false)
|
||||
// if diags.HasError() {
|
||||
// return nil, fmt.Errorf("failed to convert roles: %v", diags)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return &sqlserverflexbeta.CreateUserRequestPayload{
|
||||
// DefaultDatabase: model.DefaultDatabase.ValueStringPointer(),
|
||||
// Username: model.Username.ValueStringPointer(),
|
||||
// Roles: &roles,
|
||||
// }, nil
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -158,6 +158,18 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r
|
|||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "region", region)
|
||||
|
||||
//payload, err := toCreatePayload(ctx, &data)
|
||||
//if err != nil {
|
||||
// core.LogAndAddError(
|
||||
// ctx,
|
||||
// &resp.Diagnostics,
|
||||
// "Error creating User",
|
||||
// fmt.Sprintf("Creating API payload: %v", err),
|
||||
// )
|
||||
// return
|
||||
//}
|
||||
//payload = payload
|
||||
|
||||
// TODO: Create API call logic
|
||||
/*
|
||||
// Generate API request body from model
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func createTemporaryHome(createValidCredentialsFile bool, t *testing.T) string {
|
|||
// Define content, default = invalid token
|
||||
token := "foo_token"
|
||||
if createValidCredentialsFile {
|
||||
token = testutil.GetTestProjectServiceAccountToken("")
|
||||
token = testutil.GetTestProjectServiceAccountJson("")
|
||||
}
|
||||
content := fmt.Sprintf(`
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue