Alpha (#4)
* chore: initial push to be able to work together * chore: add missing wait folder * chore: add missing folders * chore: cleanup alpha branch * feat: mssql alpha instance (#2) * fix: remove unused attribute types and functions from backup models * fix: update API client references to use sqlserverflexalpha package * fix: update package references to use sqlserverflexalpha and modify user data source model * fix: add sqlserverflexalpha user data source to provider * fix: add sqlserverflexalpha user resource and update related functionality * chore: add stackit_sqlserverflexalpha_user resource and instance_id variable * fix: refactor sqlserverflexalpha user resource and enhance schema with status and default_database --------- Co-authored-by: Andre Harms <andre.harms@stackit.cloud> Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud> * feat: add sqlserver instance * chore: fixing tests * chore: update docs --------- Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud> Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
This commit is contained in:
parent
45073a716b
commit
2733834fc9
351 changed files with 62744 additions and 3 deletions
201
stackit/internal/conversion/conversion.go
Normal file
201
stackit/internal/conversion/conversion.go
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
// Copyright (c) STACKIT
|
||||
|
||||
package conversion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
)
|
||||
|
||||
func ToString(ctx context.Context, v attr.Value) (string, error) {
|
||||
if t := v.Type(ctx); t != types.StringType {
|
||||
return "", fmt.Errorf("type mismatch. expected 'types.StringType' but got '%s'", t.String())
|
||||
}
|
||||
if v.IsNull() || v.IsUnknown() {
|
||||
return "", fmt.Errorf("value is unknown or null")
|
||||
}
|
||||
tv, err := v.ToTerraformValue(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var s string
|
||||
if err := tv.Copy().As(&s); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func ToOptStringMap(tfMap map[string]attr.Value) (*map[string]string, error) { //nolint: gocritic //pointer needed to map optional fields
|
||||
labels := make(map[string]string, len(tfMap))
|
||||
for l, v := range tfMap {
|
||||
valueString, ok := v.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("error converting map value: expected to string, got %v", v)
|
||||
}
|
||||
labels[l] = valueString.ValueString()
|
||||
}
|
||||
|
||||
labelsPointer := &labels
|
||||
if len(labels) == 0 {
|
||||
labelsPointer = nil
|
||||
}
|
||||
return labelsPointer, nil
|
||||
}
|
||||
|
||||
func ToTerraformStringMap(ctx context.Context, m map[string]string) (basetypes.MapValue, error) {
|
||||
labels := make(map[string]attr.Value, len(m))
|
||||
for l, v := range m {
|
||||
stringValue := types.StringValue(v)
|
||||
labels[l] = stringValue
|
||||
}
|
||||
res, diags := types.MapValueFrom(ctx, types.StringType, m)
|
||||
if diags.HasError() {
|
||||
return types.MapNull(types.StringType), fmt.Errorf("converting to MapValue: %v", diags.Errors())
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ToStringInterfaceMap converts a basetypes.MapValue of Strings to a map[string]interface{}.
|
||||
func ToStringInterfaceMap(ctx context.Context, m basetypes.MapValue) (map[string]interface{}, error) {
|
||||
labels := map[string]string{}
|
||||
diags := m.ElementsAs(ctx, &labels, false)
|
||||
if diags.HasError() {
|
||||
return nil, fmt.Errorf("converting from MapValue: %w", core.DiagsToError(diags))
|
||||
}
|
||||
|
||||
interfaceMap := make(map[string]interface{}, len(labels))
|
||||
for k, v := range labels {
|
||||
interfaceMap[k] = v
|
||||
}
|
||||
|
||||
return interfaceMap, nil
|
||||
}
|
||||
|
||||
// StringValueToPointer converts basetypes.StringValue to a pointer to string.
|
||||
// It returns nil if the value is null or unknown.
|
||||
func StringValueToPointer(s basetypes.StringValue) *string {
|
||||
if s.IsNull() || s.IsUnknown() {
|
||||
return nil
|
||||
}
|
||||
value := s.ValueString()
|
||||
return &value
|
||||
}
|
||||
|
||||
// Int64ValueToPointer converts basetypes.Int64Value to a pointer to int64.
|
||||
// It returns nil if the value is null or unknown.
|
||||
func Int64ValueToPointer(s basetypes.Int64Value) *int64 {
|
||||
if s.IsNull() || s.IsUnknown() {
|
||||
return nil
|
||||
}
|
||||
value := s.ValueInt64()
|
||||
return &value
|
||||
}
|
||||
|
||||
// Float64ValueToPointer converts basetypes.Float64Value to a pointer to float64.
|
||||
// It returns nil if the value is null or unknown.
|
||||
func Float64ValueToPointer(s basetypes.Float64Value) *float64 {
|
||||
if s.IsNull() || s.IsUnknown() {
|
||||
return nil
|
||||
}
|
||||
value := s.ValueFloat64()
|
||||
return &value
|
||||
}
|
||||
|
||||
// BoolValueToPointer converts basetypes.BoolValue to a pointer to bool.
|
||||
// It returns nil if the value is null or unknown.
|
||||
func BoolValueToPointer(s basetypes.BoolValue) *bool {
|
||||
if s.IsNull() || s.IsUnknown() {
|
||||
return nil
|
||||
}
|
||||
value := s.ValueBool()
|
||||
return &value
|
||||
}
|
||||
|
||||
// StringListToPointer converts basetypes.ListValue to a pointer to a list of strings.
|
||||
// It returns nil if the value is null or unknown.
|
||||
func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
|
||||
if list.IsNull() || list.IsUnknown() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
listStr := []string{}
|
||||
for i, el := range list.Elements() {
|
||||
elStr, ok := el.(types.String)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("element %d is not a string", i)
|
||||
}
|
||||
listStr = append(listStr, elStr.ValueString())
|
||||
}
|
||||
|
||||
return &listStr, nil
|
||||
}
|
||||
|
||||
// ToJSONMApPartialUpdatePayload returns a map[string]interface{} to be used in a PATCH request payload.
|
||||
// It takes a current map as it is in the terraform state and a desired map as it is in the user configuratiom
|
||||
// and builds a map which sets to null keys that should be removed, updates the values of existing keys and adds new keys
|
||||
// This method is needed because in partial updates, e.g. if the key is not provided it is ignored and not removed
|
||||
func ToJSONMapPartialUpdatePayload(ctx context.Context, current, desired types.Map) (map[string]interface{}, error) {
|
||||
currentMap, err := ToStringInterfaceMap(ctx, current)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
desiredMap, err := ToStringInterfaceMap(ctx, desired)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("converting to Go map: %w", err)
|
||||
}
|
||||
|
||||
mapPayload := map[string]interface{}{}
|
||||
// Update and remove existing keys
|
||||
for k := range currentMap {
|
||||
if desiredValue, ok := desiredMap[k]; ok {
|
||||
mapPayload[k] = desiredValue
|
||||
} else {
|
||||
mapPayload[k] = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Add new keys
|
||||
for k, desiredValue := range desiredMap {
|
||||
if _, ok := mapPayload[k]; !ok {
|
||||
mapPayload[k] = desiredValue
|
||||
}
|
||||
}
|
||||
return mapPayload, nil
|
||||
}
|
||||
|
||||
func ParseProviderData(ctx context.Context, providerData any, diags *diag.Diagnostics) (core.ProviderData, bool) {
|
||||
// Prevent panic if the provider has not been configured.
|
||||
if providerData == nil {
|
||||
return core.ProviderData{}, false
|
||||
}
|
||||
|
||||
stackitProviderData, ok := providerData.(core.ProviderData)
|
||||
if !ok {
|
||||
core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Expected configure type core.ProviderData, got %T", providerData))
|
||||
return core.ProviderData{}, false
|
||||
}
|
||||
return stackitProviderData, true
|
||||
}
|
||||
|
||||
func ParseEphemeralProviderData(ctx context.Context, providerData any, diags *diag.Diagnostics) (core.EphemeralProviderData, bool) {
|
||||
// Prevent panic if the provider has not been configured.
|
||||
if providerData == nil {
|
||||
return core.EphemeralProviderData{}, false
|
||||
}
|
||||
|
||||
stackitProviderData, ok := providerData.(core.EphemeralProviderData)
|
||||
if !ok {
|
||||
core.LogAndAddError(ctx, diags, "Error configuring API client", "Expected configure type core.EphemeralProviderData")
|
||||
return core.EphemeralProviderData{}, false
|
||||
}
|
||||
return stackitProviderData, true
|
||||
}
|
||||
396
stackit/internal/conversion/conversion_test.go
Normal file
396
stackit/internal/conversion/conversion_test.go
Normal file
|
|
@ -0,0 +1,396 @@
|
|||
// Copyright (c) STACKIT
|
||||
|
||||
package conversion
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
)
|
||||
|
||||
func TestFromTerraformStringMapToInterfaceMap(t *testing.T) {
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
m basetypes.MapValue
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "base",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
m: types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
"key3": types.StringValue("value3"),
|
||||
}),
|
||||
},
|
||||
want: map[string]interface{}{
|
||||
"key": "value",
|
||||
"key2": "value2",
|
||||
"key3": "value3",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "empty",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
m: types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
},
|
||||
want: map[string]interface{}{},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
m: types.MapNull(types.StringType),
|
||||
},
|
||||
want: map[string]interface{}{},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid type map (non-string)",
|
||||
args: args{
|
||||
ctx: context.Background(),
|
||||
m: types.MapValueMust(types.Int64Type, map[string]attr.Value{
|
||||
"key": types.Int64Value(1),
|
||||
}),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ToStringInterfaceMap(tt.args.ctx, tt.args.m)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FromTerraformStringMapToInterfaceMap() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("FromTerraformStringMapToInterfaceMap() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestToJSONMapUpdatePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
currentLabels types.Map
|
||||
desiredLabels types.Map
|
||||
expected map[string]interface{}
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"nothing_to_update",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"update_key_value",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("updated_value"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "updated_value",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"remove_key",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "value",
|
||||
"key2": nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"add_new_key",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "value",
|
||||
"key2": "value2",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"empty_desired_map",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
map[string]interface{}{
|
||||
"key": nil,
|
||||
"key2": nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_desired_map",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
types.MapNull(types.StringType),
|
||||
map[string]interface{}{
|
||||
"key": nil,
|
||||
"key2": nil,
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"empty_current_map",
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{}),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "value",
|
||||
"key2": "value2",
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"nil_current_map",
|
||||
types.MapNull(types.StringType),
|
||||
types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||
"key": types.StringValue("value"),
|
||||
"key2": types.StringValue("value2"),
|
||||
}),
|
||||
map[string]interface{}{
|
||||
"key": "value",
|
||||
"key2": "value2",
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
output, err := ToJSONMapPartialUpdatePayload(context.Background(), tt.currentLabels, tt.desiredLabels)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && err != nil {
|
||||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(output, tt.expected)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseProviderData(t *testing.T) {
|
||||
type args struct {
|
||||
providerData any
|
||||
}
|
||||
type want struct {
|
||||
ok bool
|
||||
providerData core.ProviderData
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want want
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "provider has not been configured",
|
||||
args: args{
|
||||
providerData: nil,
|
||||
},
|
||||
want: want{
|
||||
ok: false,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid provider data",
|
||||
args: args{
|
||||
providerData: struct{}{},
|
||||
},
|
||||
want: want{
|
||||
ok: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "valid provider data 1",
|
||||
args: args{
|
||||
providerData: core.ProviderData{},
|
||||
},
|
||||
want: want{
|
||||
ok: true,
|
||||
providerData: core.ProviderData{},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid provider data 2",
|
||||
args: args{
|
||||
providerData: core.ProviderData{
|
||||
DefaultRegion: "eu02",
|
||||
RabbitMQCustomEndpoint: "https://rabbitmq-custom-endpoint.api.stackit.cloud",
|
||||
Version: "1.2.3",
|
||||
},
|
||||
},
|
||||
want: want{
|
||||
ok: true,
|
||||
providerData: core.ProviderData{
|
||||
DefaultRegion: "eu02",
|
||||
RabbitMQCustomEndpoint: "https://rabbitmq-custom-endpoint.api.stackit.cloud",
|
||||
Version: "1.2.3",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
actual, ok := ParseProviderData(ctx, tt.args.providerData, &diags)
|
||||
if diags.HasError() != tt.wantErr {
|
||||
t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr)
|
||||
}
|
||||
if ok != tt.want.ok {
|
||||
t.Errorf("ParseProviderData() got = %v, want %v", ok, tt.want.ok)
|
||||
}
|
||||
if !reflect.DeepEqual(actual, tt.want.providerData) {
|
||||
t.Errorf("ParseProviderData() got = %v, want %v", actual, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEphemeralProviderData(t *testing.T) {
|
||||
type args struct {
|
||||
providerData any
|
||||
}
|
||||
type want struct {
|
||||
ok bool
|
||||
providerData core.EphemeralProviderData
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want want
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "provider has not been configured",
|
||||
args: args{
|
||||
providerData: nil,
|
||||
},
|
||||
want: want{
|
||||
ok: false,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid provider data",
|
||||
args: args{
|
||||
providerData: struct{}{},
|
||||
},
|
||||
want: want{
|
||||
ok: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "valid provider data 1",
|
||||
args: args{
|
||||
providerData: core.EphemeralProviderData{},
|
||||
},
|
||||
want: want{
|
||||
ok: true,
|
||||
providerData: core.EphemeralProviderData{},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid provider data 2",
|
||||
args: args{
|
||||
providerData: core.EphemeralProviderData{
|
||||
PrivateKey: "",
|
||||
PrivateKeyPath: "/home/dev/foo/private-key.json",
|
||||
ServiceAccountKey: "",
|
||||
ServiceAccountKeyPath: "/home/dev/foo/key.json",
|
||||
TokenCustomEndpoint: "",
|
||||
},
|
||||
},
|
||||
want: want{
|
||||
ok: true,
|
||||
providerData: core.EphemeralProviderData{
|
||||
PrivateKey: "",
|
||||
PrivateKeyPath: "/home/dev/foo/private-key.json",
|
||||
ServiceAccountKey: "",
|
||||
ServiceAccountKeyPath: "/home/dev/foo/key.json",
|
||||
TokenCustomEndpoint: "",
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
actual, ok := ParseEphemeralProviderData(ctx, tt.args.providerData, &diags)
|
||||
if diags.HasError() != tt.wantErr {
|
||||
t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr)
|
||||
}
|
||||
if ok != tt.want.ok {
|
||||
t.Errorf("ParseProviderData() got = %v, want %v", ok, tt.want.ok)
|
||||
}
|
||||
if !reflect.DeepEqual(actual, tt.want.providerData) {
|
||||
t.Errorf("ParseProviderData() got = %v, want %v", actual, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue