Onboard Argus alert configs (#449)

* Onboard Argus alert config receivers (#439)

* initial schema and to payload

* finish receiver resource implementation (schema, topayload and mapfields)

* fix toUpdate payload lists

* fix resource and datasource on alert config attr removal, add testing

* fix linting and testing

* initial test map fields

* improve testing, remove logging

* rename vars in acc testing

* refactor mapAlertConfig

* improve mock alert config, fix testing

* make the mock alert config receivers match the default

* generate docs

* Onboard Argus Alert Config global configuration (#446)

* initial implementation

* initial map fields

* extend datasource, finish resource impl and extend acc testing

* remove unmapped fields

* add all attributes back

* remove commented inhibit rules

* generate docs, fix testing

* address PR comments

* Onboard Alert Config route (#447)

* Initial implementation

* add schema to datasource, improve acc testing

* fix linting, generate docs

* address PR comments

* fix and improve acceptance tests

* update test titles (comments)

* address acceptance comments (#452)

* Onboard Argus Alert Config child routes in Route (#463)

* initial schema and map fields implementation

* initial working solution

* improve implementation, generate docs

* fix description, add more unit testing

* address PR comments

* add unit and acc testing, fix datasource schema, fix plan_id mapping

* add checks to acceptance testing

* update acceptance tests
This commit is contained in:
Diogo Ferrão 2024-07-18 15:05:35 +01:00 committed by GitHub
parent f762c4800b
commit a35b887315
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 3081 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import (
"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) {
@ -58,6 +59,22 @@ func ToTerraformStringMap(ctx context.Context, m map[string]string) (basetypes.M
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 {

View file

@ -0,0 +1,82 @@
package conversion
import (
"context"
"reflect"
"testing"
"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)
}
})
}
}