Skip to content

Commit 902b0a9

Browse files
committed
Merge branch 'master' into CLOUDP-328093_tests_stream_processor
* master: chore: Replace fwtypes.JSONStringType with jsontypes.NormalizedType (#3452) chore: Use SP10 instead of default SP30 for stream_instance acceptance tests (#3453) chore: JSON attributes in auto-generated resources (#3445) chore: Updates repository to use supported Terraform versions (#3450)
2 parents 5737914 + 5b069b6 commit 902b0a9

30 files changed

+696
-228
lines changed

.github/workflows/test-suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
inputs:
1010
terraform_matrix:
1111
description: 'Terraform version matrix (JSON array)'
12-
default: '["1.12.x","1.11.x","1.10.x","1.9.x","1.8.x","1.7.x","1.6.x","1.5.x"]'
12+
default: '["1.12.x","1.11.x","1.10.x","1.9.x","1.8.x","1.7.x","1.6.x"]'
1313
provider_matrix:
1414
description: 'Previous MongoDB Atlas Provider version matrix for migration tests (JSON array)'
1515
default: '[""]' # "" for latest version

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ require (
4141

4242
require (
4343
github.com/hashicorp/terraform-json v0.25.0
44+
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0
4445
go.mongodb.org/atlas-sdk/v20250312004 v20250312004.0.0
4546
)
4647

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ github.com/hashicorp/terraform-json v0.25.0 h1:rmNqc/CIfcWawGiwXmRuiXJKEiJu1ntGo
532532
github.com/hashicorp/terraform-json v0.25.0/go.mod h1:sMKS8fiRDX4rVlR6EJUMudg1WcanxCMoWwTLkgZP/vc=
533533
github.com/hashicorp/terraform-plugin-framework v1.15.0 h1:LQ2rsOfmDLxcn5EeIwdXFtr03FVsNktbbBci8cOKdb4=
534534
github.com/hashicorp/terraform-plugin-framework v1.15.0/go.mod h1:hxrNI/GY32KPISpWqlCoTLM9JZsGH3CyYlir09bD/fI=
535+
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0 h1:SJXL5FfJJm17554Kpt9jFXngdM6fXbnUnZ6iT2IeiYA=
536+
github.com/hashicorp/terraform-plugin-framework-jsontypes v0.2.0/go.mod h1:p0phD0IYhsu9bR4+6OetVvvH59I6LwjXGnTVEr8ox6E=
535537
github.com/hashicorp/terraform-plugin-framework-timeouts v0.5.0 h1:I/N0g/eLZ1ZkLZXUQ0oRSXa8YG/EF0CEuQP1wXdrzKw=
536538
github.com/hashicorp/terraform-plugin-framework-timeouts v0.5.0/go.mod h1:t339KhmxnaF4SzdpxmqW8HnQBHVGYazwtfxU0qCs4eE=
537539
github.com/hashicorp/terraform-plugin-framework-validators v0.18.0 h1:OQnlOt98ua//rCw+QhBbSqfW3QbwtVrcdWeQN5gI3Hw=

internal/common/autogen/marshal.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77

8+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
89
"github.com/hashicorp/terraform-plugin-framework/attr"
910
"github.com/hashicorp/terraform-plugin-framework/types"
1011
)
@@ -105,8 +106,14 @@ func getModelAttr(val attr.Value) (any, error) {
105106
return getListAttr(v.Elements())
106107
case types.Set:
107108
return getListAttr(v.Elements())
109+
case jsontypes.Normalized:
110+
var valueJSON any
111+
if err := json.Unmarshal([]byte(v.ValueString()), &valueJSON); err != nil {
112+
return nil, fmt.Errorf("marshal failed for JSON custom type: %v", err)
113+
}
114+
return valueJSON, nil
108115
default:
109-
return nil, fmt.Errorf("unmarshal not supported yet for type %T", v)
116+
return nil, fmt.Errorf("marshal not supported yet for type %T", v)
110117
}
111118
}
112119

internal/common/autogen/marshal_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package autogen_test
33
import (
44
"testing"
55

6+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
67
"github.com/hashicorp/terraform-plugin-framework/attr"
78
"github.com/hashicorp/terraform-plugin-framework/types"
89
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen"
@@ -42,17 +43,17 @@ const epsilon = 10e-15 // float tolerance in test equality
4243

4344
func TestMarshalBasic(t *testing.T) {
4445
model := struct {
45-
AttrFloat types.Float64 `tfsdk:"attr_float"`
46-
AttrString types.String `tfsdk:"attr_string"`
47-
// values with tag `omitjson` are not marshaled, and they don't need to be Terraform types
48-
AttrOmit types.String `tfsdk:"attr_omit" autogen:"omitjson"`
49-
AttrOmitNoTerraform string `autogen:"omitjson"`
50-
AttrUnkown types.String `tfsdk:"attr_unknown"`
51-
AttrNull types.String `tfsdk:"attr_null"`
52-
AttrInt types.Int64 `tfsdk:"attr_int"`
53-
AttrBoolTrue types.Bool `tfsdk:"attr_bool_true"`
54-
AttrBoolFalse types.Bool `tfsdk:"attr_bool_false"`
55-
AttrBoolNull types.Bool `tfsdk:"attr_bool_null"`
46+
AttrFloat types.Float64 `tfsdk:"attr_float"`
47+
AttrString types.String `tfsdk:"attr_string"`
48+
AttrOmit types.String `tfsdk:"attr_omit" autogen:"omitjson"`
49+
AttrUnkown types.String `tfsdk:"attr_unknown"`
50+
AttrNull types.String `tfsdk:"attr_null"`
51+
AttrJSON jsontypes.Normalized `tfsdk:"attr_json"`
52+
AttrOmitNoTerraform string `autogen:"omitjson"`
53+
AttrInt types.Int64 `tfsdk:"attr_int"`
54+
AttrBoolTrue types.Bool `tfsdk:"attr_bool_true"`
55+
AttrBoolFalse types.Bool `tfsdk:"attr_bool_false"`
56+
AttrBoolNull types.Bool `tfsdk:"attr_bool_null"`
5657
}{
5758
AttrFloat: types.Float64Value(1.234),
5859
AttrString: types.StringValue("hello"),
@@ -64,8 +65,9 @@ func TestMarshalBasic(t *testing.T) {
6465
AttrBoolTrue: types.BoolValue(true),
6566
AttrBoolFalse: types.BoolValue(false),
6667
AttrBoolNull: types.BoolNull(), // null values are not marshaled
68+
AttrJSON: jsontypes.NewNormalizedValue("{\"hello\": \"there\"}"),
6769
}
68-
const expectedJSON = `{ "attrString": "hello", "attrInt": 1, "attrFloat": 1.234, "attrBoolTrue": true, "attrBoolFalse": false }`
70+
const expectedJSON = `{ "attrString": "hello", "attrInt": 1, "attrFloat": 1.234, "attrBoolTrue": true, "attrBoolFalse": false, "attrJSON": {"hello": "there"} }`
6971
raw, err := autogen.Marshal(&model, false)
7072
require.NoError(t, err)
7173
assert.JSONEq(t, expectedJSON, string(raw))

internal/common/autogen/unknown.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"reflect"
77

8+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
89
"github.com/hashicorp/terraform-plugin-framework/attr"
910
"github.com/hashicorp/terraform-plugin-framework/types"
1011
)
@@ -116,6 +117,9 @@ func getNullAttr(attrType attr.Type) (attr.Value, error) {
116117
if mapType, ok := attrType.(types.MapType); ok {
117118
return types.MapNull(mapType.ElemType), nil
118119
}
120+
if _, ok := attrType.(jsontypes.NormalizedType); ok {
121+
return jsontypes.NewNormalizedNull(), nil
122+
}
119123
return nil, fmt.Errorf("unmarshal to get null value not supported yet for type %T", attrType)
120124
}
121125
}

internal/common/autogen/unmarshal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"strings"
99

10+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1011
"github.com/hashicorp/terraform-plugin-framework/attr"
1112
"github.com/hashicorp/terraform-plugin-framework/types"
1213
)
@@ -108,6 +109,13 @@ func getTfAttr(value any, valueType attr.Type, oldVal attr.Value, name string) (
108109
}
109110
return nil, errUnmarshal(value, valueType, "Number", nameErr)
110111
case map[string]any:
112+
if _, ok := valueType.(jsontypes.NormalizedType); ok {
113+
jsonBytes, err := json.Marshal(v)
114+
if err != nil {
115+
return nil, fmt.Errorf("failed to marshal object to JSON for attribute %s: %v", nameErr, err)
116+
}
117+
return jsontypes.NewNormalizedValue(string(jsonBytes)), nil
118+
}
111119
if obj, ok := oldVal.(types.Object); ok {
112120
objNew, err := setObjAttrModel(obj, v)
113121
if err != nil {
@@ -124,6 +132,13 @@ func getTfAttr(value any, valueType attr.Type, oldVal attr.Value, name string) (
124132
}
125133
return nil, errUnmarshal(value, valueType, "Object", nameErr)
126134
case []any:
135+
if _, ok := valueType.(jsontypes.NormalizedType); ok {
136+
jsonBytes, err := json.Marshal(v)
137+
if err != nil {
138+
return nil, fmt.Errorf("failed to marshal array to JSON for attribute %s: %v", nameErr, err)
139+
}
140+
return jsontypes.NewNormalizedValue(string(jsonBytes)), nil
141+
}
127142
if list, ok := oldVal.(types.List); ok {
128143
listNew, err := setListAttrModel(list, v, nameErr)
129144
if err != nil {

internal/common/autogen/unmarshal_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package autogen_test
33
import (
44
"testing"
55

6+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
67
"github.com/hashicorp/terraform-plugin-framework/attr"
78
"github.com/hashicorp/terraform-plugin-framework/types"
89
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen"
@@ -12,14 +13,15 @@ import (
1213

1314
func TestUnmarshalBasic(t *testing.T) {
1415
var model struct {
15-
AttrFloat types.Float64 `tfsdk:"attr_float"`
16-
AttrFloatWithInt types.Float64 `tfsdk:"attr_float_with_int"`
17-
AttrString types.String `tfsdk:"attr_string"`
18-
AttrNotInJSON types.String `tfsdk:"attr_not_in_json"`
19-
AttrInt types.Int64 `tfsdk:"attr_int"`
20-
AttrIntWithFloat types.Int64 `tfsdk:"attr_int_with_float"`
21-
AttrTrue types.Bool `tfsdk:"attr_true"`
22-
AttrFalse types.Bool `tfsdk:"attr_false"`
16+
AttrFloat types.Float64 `tfsdk:"attr_float"`
17+
AttrFloatWithInt types.Float64 `tfsdk:"attr_float_with_int"`
18+
AttrString types.String `tfsdk:"attr_string"`
19+
AttrNotInJSON types.String `tfsdk:"attr_not_in_json"`
20+
AttrJSON jsontypes.Normalized `tfsdk:"attr_json"`
21+
AttrInt types.Int64 `tfsdk:"attr_int"`
22+
AttrIntWithFloat types.Int64 `tfsdk:"attr_int_with_float"`
23+
AttrTrue types.Bool `tfsdk:"attr_true"`
24+
AttrFalse types.Bool `tfsdk:"attr_false"`
2325
}
2426
const (
2527
// attribute_not_in_model is ignored because it is not in the model, no error is thrown.
@@ -34,7 +36,8 @@ func TestUnmarshalBasic(t *testing.T) {
3436
"attrFloat": 456.1,
3537
"attrFloatWithInt": 13,
3638
"attrNotInModel": "val",
37-
"attrNull": null
39+
"attrNull": null,
40+
"attrJSON": {"hello": "there"}
3841
}
3942
`
4043
)
@@ -47,6 +50,7 @@ func TestUnmarshalBasic(t *testing.T) {
4750
assert.InEpsilon(t, float64(456.1), model.AttrFloat.ValueFloat64(), epsilon)
4851
assert.InEpsilon(t, float64(13), model.AttrFloatWithInt.ValueFloat64(), epsilon)
4952
assert.True(t, model.AttrNotInJSON.IsNull()) // attributes not in JSON response are not changed, so null is kept.
53+
assert.JSONEq(t, "{\"hello\":\"there\"}", model.AttrJSON.ValueString())
5054
}
5155

5256
func TestUnmarshalNestedAllTypes(t *testing.T) {
@@ -67,6 +71,7 @@ func TestUnmarshalNestedAllTypes(t *testing.T) {
6771
AttrMapSimple types.Map `tfsdk:"attr_map_simple"`
6872
AttrMapSimpleExisting types.Map `tfsdk:"attr_map_simple_existing"`
6973
AttrMapObj types.Map `tfsdk:"attr_map_obj"`
74+
AttrJSONList types.List `tfsdk:"attr_json_list"`
7075
}
7176
model := modelst{
7277
AttrObj: types.ObjectValueMust(objTypeTest.AttrTypes, map[string]attr.Value{
@@ -100,7 +105,8 @@ func TestUnmarshalNestedAllTypes(t *testing.T) {
100105
"existing": types.StringValue("valexisting"),
101106
"existingCHANGE": types.StringValue("before"),
102107
}),
103-
AttrMapObj: types.MapUnknown(objTypeTest),
108+
AttrMapObj: types.MapUnknown(objTypeTest),
109+
AttrJSONList: types.ListUnknown(jsontypes.NormalizedType{}),
104110
}
105111
// attrUnexisting is ignored because it is in JSON but not in the model, no error is returned
106112
const (
@@ -226,7 +232,11 @@ func TestUnmarshalNestedAllTypes(t *testing.T) {
226232
"attrFloat": 22.2,
227233
"attrBool": true
228234
}
229-
}
235+
},
236+
"attrJSONList": [
237+
{"hello1": "there1"},
238+
{"hello2": "there2"}
239+
]
230240
}
231241
`
232242
)
@@ -375,6 +385,10 @@ func TestUnmarshalNestedAllTypes(t *testing.T) {
375385
"attr_bool": types.BoolValue(true),
376386
}),
377387
}),
388+
AttrJSONList: types.ListValueMust(jsontypes.NormalizedType{}, []attr.Value{
389+
jsontypes.NewNormalizedValue(`{"hello1":"there1"}`),
390+
jsontypes.NewNormalizedValue(`{"hello2":"there2"}`),
391+
}),
378392
}
379393
require.NoError(t, autogen.Unmarshal([]byte(jsonResp), &model))
380394
assert.Equal(t, modelExpected, model)

internal/common/fwtypes/json_string.go

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)