fix: fix lintings
This commit is contained in:
parent
8d5c0560f1
commit
cdbfc56fd0
7 changed files with 52 additions and 130 deletions
|
|
@ -31,8 +31,6 @@ const (
|
||||||
GEN_REPO = "https://github.com/stackitcloud/stackit-sdk-generator.git"
|
GEN_REPO = "https://github.com/stackitcloud/stackit-sdk-generator.git"
|
||||||
)
|
)
|
||||||
|
|
||||||
var supportedVersions = []string{"alpha", "beta"}
|
|
||||||
|
|
||||||
type version struct {
|
type version struct {
|
||||||
verString string
|
verString string
|
||||||
major int
|
major int
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
@ -266,13 +267,19 @@ func setupMockServer() *httptest.Server {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
err := json.NewEncoder(w).Encode(map[string]string{
|
||||||
"id": "mock-id-123",
|
"id": "mock-id-123",
|
||||||
"name": "test-resource",
|
"name": "test-resource",
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
w.WriteHeader(http.StatusOK)
|
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()
|
defer server.Close()
|
||||||
|
|
||||||
// Configure provider to use mock server URL
|
// 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
|
// Run unit tests against mock
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
||||||
"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"
|
||||||
|
|
@ -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.
|
// 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.
|
// 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) {
|
func (r *userResource) getUserResource(ctx context.Context, model *resourceModel, arg *clientArg) (bool, error) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package sqlserverflexbeta
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
|
@ -47,17 +48,7 @@ func mapResponseToModel(
|
||||||
)
|
)
|
||||||
tfDiags.Append(diags...)
|
tfDiags.Append(diags...)
|
||||||
if diags.HasError() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf(
|
return errors.New("error converting network response value")
|
||||||
"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()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
m.Network = net
|
m.Network = net
|
||||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
||||||
|
|
@ -113,17 +104,7 @@ func mapDataResponseToModel(
|
||||||
)
|
)
|
||||||
tfDiags.Append(diags...)
|
tfDiags.Append(diags...)
|
||||||
if diags.HasError() {
|
if diags.HasError() {
|
||||||
return fmt.Errorf(
|
return errors.New("error converting network response value")
|
||||||
"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()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
m.Network = net
|
m.Network = net
|
||||||
m.Replicas = types.Int64Value(int64(resp.GetReplicas()))
|
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/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
|
||||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapResponseToModel(
|
func mapResponseToModel(
|
||||||
|
|
@ -75,63 +74,25 @@ func mapResponseToModel(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle encryption field mapping when API supports it
|
//func toCreatePayload(
|
||||||
func handleEncryption(
|
// ctx context.Context,
|
||||||
m *dataSourceModel,
|
// model *resourceModel,
|
||||||
resp *sqlserverflexbeta.GetUserResponse,
|
//) (*sqlserverflexbeta.CreateUserRequestPayload, error) {
|
||||||
) sqlserverflexbetaResGen.EncryptionValue {
|
// if model == nil {
|
||||||
/*
|
// return nil, fmt.Errorf("nil model")
|
||||||
if !resp.HasEncryption() ||
|
// }
|
||||||
|
//
|
||||||
resp.Encryption == nil ||
|
// var roles []sqlserverflexbeta.UserRole
|
||||||
resp.Encryption.KekKeyId == nil ||
|
// if !model.Roles.IsNull() && !model.Roles.IsUnknown() {
|
||||||
resp.Encryption.KekKeyRingId == nil ||
|
// diags := model.Roles.ElementsAs(ctx, &roles, false)
|
||||||
resp.Encryption.KekKeyVersion == nil ||
|
// if diags.HasError() {
|
||||||
resp.Encryption.ServiceAccount == nil {
|
// return nil, fmt.Errorf("failed to convert roles: %v", diags)
|
||||||
|
// }
|
||||||
if m.Encryption.IsNull() || m.Encryption.IsUnknown() {
|
// }
|
||||||
return sqlserverflexbetaResGen.NewEncryptionValueNull()
|
//
|
||||||
}
|
// return &sqlserverflexbeta.CreateUserRequestPayload{
|
||||||
return m.Encryption
|
// DefaultDatabase: model.DefaultDatabase.ValueStringPointer(),
|
||||||
}
|
// Username: model.Username.ValueStringPointer(),
|
||||||
|
// Roles: &roles,
|
||||||
enc := sqlserverflexbetaResGen.NewEncryptionValueNull()
|
// }, nil
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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, "project_id", projectId)
|
||||||
ctx = tflog.SetField(ctx, "region", region)
|
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
|
// TODO: Create API call logic
|
||||||
/*
|
/*
|
||||||
// Generate API request body from model
|
// Generate API request body from model
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,7 @@ func createTemporaryHome(createValidCredentialsFile bool, t *testing.T) string {
|
||||||
// Define content, default = invalid token
|
// Define content, default = invalid token
|
||||||
token := "foo_token"
|
token := "foo_token"
|
||||||
if createValidCredentialsFile {
|
if createValidCredentialsFile {
|
||||||
token = testutil.GetTestProjectServiceAccountToken("")
|
token = testutil.GetTestProjectServiceAccountJson("")
|
||||||
}
|
}
|
||||||
content := fmt.Sprintf(`
|
content := fmt.Sprintf(`
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue