Skip to content

Commit bc94bf7

Browse files
authored
feat: Restructures files (#1657)
* make it compile * uncomment * rename alert configuration files * example of transition * remove duplicated functions * use transition functions to minimize changes * use encodeStateID * transition deprecation messages * transition AWS and AZURE constants * move resources in new fw to internal * move old provider to internal * start extracting acctest helper methods * fix TF test dependencies * split test transtion * skip test helpers * SkipTestExtCred * no need to expose alertConfigurationDS and alertConfigurationRS * fix type doc * removeLabel and acctest helper methods * extract helper functions from provider * migration test helper methods * DebugPlan and some destroy check and skip functions * fix some linter problems * remove unneeded DebugPlan transition * remove migration package * change test package to avoid circula dependencies * fixed some items shared with test * fixed linter * test factories * move todoacc into acc package * remove _acc_test files * move DebugPlan * remove MongoDBClient from transition * extract pre checks from transition * extract constants from transition * remove transition_test * extract data source and resource references from transition * remove redundant name on resources and data sources * remove transition * databaseuser * rename databaseuser files * project * encryptionatrest * projectipaccesslist * atlasuser * searchdeployment * cluster and advancedcluster * remove share * move testutil * remove folders in mongodbatlas * remove mongodbatlas from filenames * rename provider files * refactor database_user acc * keep same function order in acc files * remove generic check_destroy file * rename package util to conversion * remove common in config * remove flatten from config * move MultiEnvDefaultFunc * move encoded state to conversion * move state conversion test file * remove constant from config * refactor secretsManagerGetSecretValue and ConfigureCredentialsSTS * refactor SecretsManagerGetSecretValue * remove credentials file in config * move ValRegion and ExpandStringList to conversion * rename fw_base * don't export credential methods
1 parent 86df112 commit bc94bf7

File tree

308 files changed

+6275
-5661
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

308 files changed

+6275
-5661
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package constant
2+
3+
const (
4+
AWS = "AWS"
5+
AZURE = "AZURE"
6+
)
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package constant
2+
3+
const (
4+
DeprecationParamByDate = "this parameter is deprecated and will be removed by %s"
5+
DeprecationParamByDateWithReplacement = "this parameter is deprecated and will be removed by %s, please transition to %s"
6+
DeprecationParamByVersion = "this parameter is deprecated and will be removed in version %s"
7+
DeprecationResourceByDateWithReplacement = "this resource is deprecated and will be removed in %s, please transition to %s"
8+
)
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package conversion
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"log"
7+
"sort"
8+
"strings"
9+
)
10+
11+
func GetEncodedID(stateID, keyPosition string) string {
12+
id := ""
13+
if !hasMultipleValues(stateID) {
14+
return stateID
15+
}
16+
17+
decoded := DecodeStateID(stateID)
18+
id = decoded[keyPosition]
19+
20+
return id
21+
}
22+
23+
func EncodeStateID(values map[string]string) string {
24+
encode := func(e string) string { return base64.StdEncoding.EncodeToString([]byte(e)) }
25+
encodedValues := make([]string, 0)
26+
27+
// sort to make sure the same encoding is returned in case of same input
28+
keys := make([]string, 0, len(values))
29+
for key := range values {
30+
keys = append(keys, key)
31+
}
32+
33+
sort.Strings(keys)
34+
35+
for _, key := range keys {
36+
encodedValues = append(encodedValues, fmt.Sprintf("%s:%s", encode(key), encode(values[key])))
37+
}
38+
39+
return strings.Join(encodedValues, "-")
40+
}
41+
42+
func DecodeStateID(stateID string) map[string]string {
43+
decode := func(d string) string {
44+
decodedString, err := base64.StdEncoding.DecodeString(d)
45+
if err != nil {
46+
log.Printf("[WARN] error decoding state ID: %s", err)
47+
}
48+
49+
return string(decodedString)
50+
}
51+
decodedValues := make(map[string]string)
52+
encodedValues := strings.Split(stateID, "-")
53+
54+
for _, value := range encodedValues {
55+
keyValue := strings.Split(value, ":")
56+
if len(keyValue) > 1 {
57+
decodedValues[decode(keyValue[0])] = decode(keyValue[1])
58+
}
59+
}
60+
61+
return decodedValues
62+
}
63+
64+
func hasMultipleValues(value string) bool {
65+
if strings.Contains(value, "-") && strings.Contains(value, ":") {
66+
return true
67+
}
68+
69+
return false
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package conversion_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/go-test/deep"
7+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
8+
)
9+
10+
func TestEncodeDecodeID(t *testing.T) {
11+
expected := map[string]string{
12+
"project_id": "5cf5a45a9ccf6400e60981b6",
13+
"cluster_name": "test-acc-q4y272zo9y",
14+
"snapshot_id": "5e42e646553855a5aee40138",
15+
}
16+
17+
got := conversion.DecodeStateID(conversion.EncodeStateID(expected))
18+
19+
if diff := deep.Equal(expected, got); diff != nil {
20+
t.Fatalf("Bad testEncodeDecodeID return \n got = %#v\nwant = %#v \ndiff = %#v", got, expected, diff)
21+
}
22+
}
23+
24+
func TestDecodeID(t *testing.T) {
25+
expected := "Y2x1c3Rlcl9uYW1l:dGVzdC1hY2MtcTR5Mjcyem85eQ==-c25hcHNob3RfaWQ=:NWU0MmU2NDY1NTM4NTVhNWFlZTQwMTM4-cHJvamVjdF9pZA==:NWNmNWE0NWE5Y2NmNjQwMGU2MDk4MWI2"
26+
expected2 := "c25hcHNob3RfaWQ=:NWU0MmU2NDY1NTM4NTVhNWFlZTQwMTM4-cHJvamVjdF9pZA==:NWNmNWE0NWE5Y2NmNjQwMGU2MDk4MWI2-Y2x1c3Rlcl9uYW1l:dGVzdC1hY2MtcTR5Mjcyem85eQ=="
27+
28+
got := conversion.DecodeStateID(expected)
29+
got2 := conversion.DecodeStateID(expected2)
30+
31+
if diff := deep.Equal(got, got2); diff != nil {
32+
t.Fatalf("Bad TestDecodeID return \n got = %#v\nwant = %#v \ndiff = %#v", got, got2, diff)
33+
}
34+
}

internal/common/conversion/misc.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package conversion
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/spf13/cast"
8+
)
9+
10+
func ValRegion(reg any, opt ...string) (string, error) {
11+
region, err := cast.ToStringE(reg)
12+
if err != nil {
13+
return "", err
14+
}
15+
16+
if region == "" {
17+
return "", fmt.Errorf("region must be set")
18+
}
19+
20+
/*
21+
We need to check if the option will be similar to network_peering word
22+
(this comes in from the same resource) because network_pering resource
23+
has not the standard region name pattern "US_EAST_1",
24+
instead it needs the following one: "us-east-1".
25+
*/
26+
if len(opt) > 0 && strings.EqualFold("network_peering", opt[0]) {
27+
return strings.ToLower(strings.ReplaceAll(region, "_", "-")), nil
28+
}
29+
30+
return strings.ReplaceAll(region, "-", "_"), nil
31+
}
32+
33+
func ExpandStringList(list []any) (res []string) {
34+
for _, v := range list {
35+
res = append(res, v.(string))
36+
}
37+
38+
return
39+
}

internal/common/conversion/pointer.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package conversion
2+
3+
func Pointer[T any](x T) *T {
4+
return &x
5+
}
6+
7+
func IntPtr(v int) *int {
8+
if v != 0 {
9+
return &v
10+
}
11+
return nil
12+
}
13+
14+
func StringPtr(v string) *string {
15+
if v != "" {
16+
return &v
17+
}
18+
return nil
19+
}

mongodbatlas/framework/conversion/string.go internal/common/conversion/string.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/hashicorp/terraform-plugin-framework/types"
7-
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
87
)
98

109
func TypesSetToString(ctx context.Context, set types.Set) []string {
@@ -27,7 +26,7 @@ func StringNullIfEmpty(v string) types.String {
2726

2827
// StringPtrNullIfEmpty is similar to StringNullIfEmpty but can also handle nil string pointers.
2928
func StringPtrNullIfEmpty(p *string) types.String {
30-
if util.IsStringPresent(p) {
29+
if IsStringPresent(p) {
3130
return types.StringValue(*p)
3231
}
3332
return types.StringNull()

mongodbatlas/util/type_conversion.go internal/common/conversion/type_conversion.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package conversion
22

33
import (
44
"strings"

mongodbatlas/util/type_conversion_test.go internal/common/conversion/type_conversion_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package util_test
1+
package conversion_test
22

33
import (
44
"testing"
55
"time"
66

7-
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
7+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
88
)
99

1010
func TestTimeToStringWithoutNanos(t *testing.T) {
1111
inputTime := time.Date(2023, time.July, 18, 16, 12, 23, 0, time.UTC)
1212
expectedOutput := "2023-07-18T16:12:23Z"
1313

14-
result := util.TimeToString(inputTime)
14+
result := conversion.TimeToString(inputTime)
1515

1616
if result != expectedOutput {
1717
t.Errorf("TimeToString(%v) = %v; want %v", inputTime, result, expectedOutput)
@@ -22,7 +22,7 @@ func TestTimeToStringWithNanos(t *testing.T) {
2222
inputTime := time.Date(2023, time.July, 18, 16, 12, 23, 456_000_000, time.UTC)
2323
expectedOutput := "2023-07-18T16:12:23.456Z"
2424

25-
result := util.TimeToString(inputTime)
25+
result := conversion.TimeToString(inputTime)
2626

2727
if result != expectedOutput {
2828
t.Errorf("TimeToString(%v) = %v; want %v", inputTime, result, expectedOutput)
@@ -45,7 +45,7 @@ func TestIsStringPresent(t *testing.T) {
4545
{&str, true},
4646
}
4747
for _, test := range tests {
48-
if resp := util.IsStringPresent(test.strPtr); resp != test.expected {
48+
if resp := conversion.IsStringPresent(test.strPtr); resp != test.expected {
4949
t.Errorf("IsStringPresent(%v) = %v; want %v", test.strPtr, resp, test.expected)
5050
}
5151
}
@@ -62,7 +62,7 @@ func TestMongoDBRegionToAWSRegion(t *testing.T) {
6262
}
6363

6464
for _, test := range tests {
65-
if resp := util.MongoDBRegionToAWSRegion(test.region); resp != test.expected {
65+
if resp := conversion.MongoDBRegionToAWSRegion(test.region); resp != test.expected {
6666
t.Errorf("MongoDBRegionToAWSRegion(%v) = %v; want %v", test.region, resp, test.expected)
6767
}
6868
}

mongodbatlas/framework/retry/retry_state.go internal/common/retrystrategy/retry_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package retry
1+
package retrystrategy
22

33
const (
44
RetryStrategyPendingState = "PENDING"

mongodbatlas/framework/validator/aws_kms_config_validator.go internal/common/validate/aws_kms_config_validator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package validate
22

33
import (
44
"context"
@@ -7,18 +7,18 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
88
)
99

10-
type awsKmsConfigValidator struct{}
10+
type AwsKmsConfigValidator struct{}
1111

12-
func (v awsKmsConfigValidator) Description(_ context.Context) string {
12+
func (v AwsKmsConfigValidator) Description(_ context.Context) string {
1313
return "for credentials: `access_key_id` and `secret_access_key` are allowed but not `role_id`." +
1414
" For roles: `access_key_id` and `secret_access_key` are not allowed but `role_id` is allowed"
1515
}
1616

17-
func (v awsKmsConfigValidator) MarkdownDescription(ctx context.Context) string {
17+
func (v AwsKmsConfigValidator) MarkdownDescription(ctx context.Context) string {
1818
return v.Description(ctx)
1919
}
2020

21-
func (v awsKmsConfigValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, response *validator.ObjectResponse) {
21+
func (v AwsKmsConfigValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, response *validator.ObjectResponse) {
2222
// If the value is unknown or null, there is nothing to validate.
2323
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
2424
return
@@ -50,5 +50,5 @@ func (v awsKmsConfigValidator) ValidateObject(ctx context.Context, req validator
5050
}
5151

5252
func AwsKmsConfig() validator.Object {
53-
return awsKmsConfigValidator{}
53+
return AwsKmsConfigValidator{}
5454
}

mongodbatlas/framework/validator/aws_kms_config_validator_test.go internal/common/validate/aws_kms_config_validator_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package validate_test
22

33
import (
44
"context"
@@ -8,6 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/diag"
99
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1010
"github.com/hashicorp/terraform-plugin-framework/types"
11+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1112
)
1213

1314
func TestValidAwsKmsConfig(t *testing.T) {
@@ -84,7 +85,7 @@ func TestValidAwsKmsConfig(t *testing.T) {
8485
for _, tt := range tests {
8586
wantErr := tt.wantErr
8687

87-
awsKmsConfigValidator := awsKmsConfigValidator{}
88+
AwsKmsConfigValidator := validate.AwsKmsConfigValidator{}
8889
validatorRequest := validator.ObjectRequest{
8990
ConfigValue: types.ObjectValueMust(tt.awsKmsConfigType, tt.awsKmsConfigValue),
9091
}
@@ -95,7 +96,7 @@ func TestValidAwsKmsConfig(t *testing.T) {
9596

9697
t.Run(tt.name, func(t *testing.T) {
9798
t.Parallel()
98-
awsKmsConfigValidator.ValidateObject(context.Background(), validatorRequest, &validatorResponse)
99+
AwsKmsConfigValidator.ValidateObject(context.Background(), validatorRequest, &validatorResponse)
99100

100101
if validatorResponse.Diagnostics.HasError() && !wantErr {
101102
t.Errorf("error = %v, wantErr %v", validatorResponse.Diagnostics.Errors(), wantErr)

mongodbatlas/framework/validator/cidr_validator.go internal/common/validate/cidr_validator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package validate
22

33
import (
44
"context"

mongodbatlas/framework/validator/cidr_validator_test.go internal/common/validate/cidr_validator_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package validate_test
22

33
import (
44
"context"
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/diag"
88
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1011
)
1112

1213
func TestValidCIDR(t *testing.T) {
@@ -39,7 +40,7 @@ func TestValidCIDR(t *testing.T) {
3940
for _, tt := range tests {
4041
val := tt.cidr
4142
wantErr := tt.wantErr
42-
cidrValidator := CIDRValidator{}
43+
cidrValidator := validate.CIDRValidator{}
4344

4445
validatorRequest := validator.StringRequest{
4546
ConfigValue: types.StringValue(val),

mongodbatlas/framework/validator/duration_validator.go internal/common/validate/duration_validator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package validate
22

33
import (
44
"context"
@@ -9,21 +9,21 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1010
)
1111

12-
type durationValidator struct {
12+
type DurationValidator struct {
1313
MinMinutes int
1414
MaxMinutes int
1515
}
1616

17-
func (v durationValidator) Description(_ context.Context) string {
17+
func (v DurationValidator) Description(_ context.Context) string {
1818
ds := "string value must be defined as a valid duration, and must be between %d and %d minutes, inclusive. Valid time units are ns, us (or µs), ms, s, h, or m."
1919
return fmt.Sprintf(ds, v.MinMinutes, v.MaxMinutes)
2020
}
2121

22-
func (v durationValidator) MarkdownDescription(ctx context.Context) string {
22+
func (v DurationValidator) MarkdownDescription(ctx context.Context) string {
2323
return v.Description(ctx)
2424
}
2525

26-
func (v durationValidator) ValidateString(ctx context.Context, req validator.StringRequest, response *validator.StringResponse) {
26+
func (v DurationValidator) ValidateString(ctx context.Context, req validator.StringRequest, response *validator.StringResponse) {
2727
// If the value is unknown or null, there is nothing to validate.
2828
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
2929
return
@@ -52,7 +52,7 @@ func (v durationValidator) ValidateString(ctx context.Context, req validator.Str
5252
}
5353

5454
func ValidDurationBetween(minMinutes, maxMinutes int) validator.String {
55-
return durationValidator{
55+
return DurationValidator{
5656
MinMinutes: minMinutes,
5757
MaxMinutes: maxMinutes,
5858
}

0 commit comments

Comments
 (0)