Skip to content

Commit

Permalink
feat: Restructures files (#1657)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lantoli authored Nov 27, 2023
1 parent 86df112 commit bc94bf7
Show file tree
Hide file tree
Showing 308 changed files with 6,275 additions and 5,661 deletions.
6 changes: 6 additions & 0 deletions internal/common/constant/cloud_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constant

const (
AWS = "AWS"
AZURE = "AZURE"
)
8 changes: 8 additions & 0 deletions internal/common/constant/deprecation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package constant

const (
DeprecationParamByDate = "this parameter is deprecated and will be removed by %s"
DeprecationParamByDateWithReplacement = "this parameter is deprecated and will be removed by %s, please transition to %s"
DeprecationParamByVersion = "this parameter is deprecated and will be removed in version %s"
DeprecationResourceByDateWithReplacement = "this resource is deprecated and will be removed in %s, please transition to %s"
)
70 changes: 70 additions & 0 deletions internal/common/conversion/encode_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package conversion

import (
"encoding/base64"
"fmt"
"log"
"sort"
"strings"
)

func GetEncodedID(stateID, keyPosition string) string {
id := ""
if !hasMultipleValues(stateID) {
return stateID
}

decoded := DecodeStateID(stateID)
id = decoded[keyPosition]

return id
}

func EncodeStateID(values map[string]string) string {
encode := func(e string) string { return base64.StdEncoding.EncodeToString([]byte(e)) }
encodedValues := make([]string, 0)

// sort to make sure the same encoding is returned in case of same input
keys := make([]string, 0, len(values))
for key := range values {
keys = append(keys, key)
}

sort.Strings(keys)

for _, key := range keys {
encodedValues = append(encodedValues, fmt.Sprintf("%s:%s", encode(key), encode(values[key])))
}

return strings.Join(encodedValues, "-")
}

func DecodeStateID(stateID string) map[string]string {
decode := func(d string) string {
decodedString, err := base64.StdEncoding.DecodeString(d)
if err != nil {
log.Printf("[WARN] error decoding state ID: %s", err)
}

return string(decodedString)
}
decodedValues := make(map[string]string)
encodedValues := strings.Split(stateID, "-")

for _, value := range encodedValues {
keyValue := strings.Split(value, ":")
if len(keyValue) > 1 {
decodedValues[decode(keyValue[0])] = decode(keyValue[1])
}
}

return decodedValues
}

func hasMultipleValues(value string) bool {
if strings.Contains(value, "-") && strings.Contains(value, ":") {
return true
}

return false
}
34 changes: 34 additions & 0 deletions internal/common/conversion/encode_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package conversion_test

import (
"testing"

"github.com/go-test/deep"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
)

func TestEncodeDecodeID(t *testing.T) {
expected := map[string]string{
"project_id": "5cf5a45a9ccf6400e60981b6",
"cluster_name": "test-acc-q4y272zo9y",
"snapshot_id": "5e42e646553855a5aee40138",
}

got := conversion.DecodeStateID(conversion.EncodeStateID(expected))

if diff := deep.Equal(expected, got); diff != nil {
t.Fatalf("Bad testEncodeDecodeID return \n got = %#v\nwant = %#v \ndiff = %#v", got, expected, diff)
}
}

func TestDecodeID(t *testing.T) {
expected := "Y2x1c3Rlcl9uYW1l:dGVzdC1hY2MtcTR5Mjcyem85eQ==-c25hcHNob3RfaWQ=:NWU0MmU2NDY1NTM4NTVhNWFlZTQwMTM4-cHJvamVjdF9pZA==:NWNmNWE0NWE5Y2NmNjQwMGU2MDk4MWI2"
expected2 := "c25hcHNob3RfaWQ=:NWU0MmU2NDY1NTM4NTVhNWFlZTQwMTM4-cHJvamVjdF9pZA==:NWNmNWE0NWE5Y2NmNjQwMGU2MDk4MWI2-Y2x1c3Rlcl9uYW1l:dGVzdC1hY2MtcTR5Mjcyem85eQ=="

got := conversion.DecodeStateID(expected)
got2 := conversion.DecodeStateID(expected2)

if diff := deep.Equal(got, got2); diff != nil {
t.Fatalf("Bad TestDecodeID return \n got = %#v\nwant = %#v \ndiff = %#v", got, got2, diff)
}
}
39 changes: 39 additions & 0 deletions internal/common/conversion/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package conversion

import (
"fmt"
"strings"

"github.com/spf13/cast"
)

func ValRegion(reg any, opt ...string) (string, error) {
region, err := cast.ToStringE(reg)
if err != nil {
return "", err
}

if region == "" {
return "", fmt.Errorf("region must be set")
}

/*
We need to check if the option will be similar to network_peering word
(this comes in from the same resource) because network_pering resource
has not the standard region name pattern "US_EAST_1",
instead it needs the following one: "us-east-1".
*/
if len(opt) > 0 && strings.EqualFold("network_peering", opt[0]) {
return strings.ToLower(strings.ReplaceAll(region, "_", "-")), nil
}

return strings.ReplaceAll(region, "-", "_"), nil
}

func ExpandStringList(list []any) (res []string) {
for _, v := range list {
res = append(res, v.(string))
}

return
}
19 changes: 19 additions & 0 deletions internal/common/conversion/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package conversion

func Pointer[T any](x T) *T {
return &x
}

func IntPtr(v int) *int {
if v != 0 {
return &v
}
return nil
}

func StringPtr(v string) *string {
if v != "" {
return &v
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

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

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

// StringPtrNullIfEmpty is similar to StringNullIfEmpty but can also handle nil string pointers.
func StringPtrNullIfEmpty(p *string) types.String {
if util.IsStringPresent(p) {
if IsStringPresent(p) {
return types.StringValue(*p)
}
return types.StringNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package conversion

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package util_test
package conversion_test

import (
"testing"
"time"

"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/util"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
)

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

result := util.TimeToString(inputTime)
result := conversion.TimeToString(inputTime)

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

result := util.TimeToString(inputTime)
result := conversion.TimeToString(inputTime)

if result != expectedOutput {
t.Errorf("TimeToString(%v) = %v; want %v", inputTime, result, expectedOutput)
Expand All @@ -45,7 +45,7 @@ func TestIsStringPresent(t *testing.T) {
{&str, true},
}
for _, test := range tests {
if resp := util.IsStringPresent(test.strPtr); resp != test.expected {
if resp := conversion.IsStringPresent(test.strPtr); resp != test.expected {
t.Errorf("IsStringPresent(%v) = %v; want %v", test.strPtr, resp, test.expected)
}
}
Expand All @@ -62,7 +62,7 @@ func TestMongoDBRegionToAWSRegion(t *testing.T) {
}

for _, test := range tests {
if resp := util.MongoDBRegionToAWSRegion(test.region); resp != test.expected {
if resp := conversion.MongoDBRegionToAWSRegion(test.region); resp != test.expected {
t.Errorf("MongoDBRegionToAWSRegion(%v) = %v; want %v", test.region, resp, test.expected)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package retry
package retrystrategy

const (
RetryStrategyPendingState = "PENDING"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package validate

import (
"context"
Expand All @@ -7,18 +7,18 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type awsKmsConfigValidator struct{}
type AwsKmsConfigValidator struct{}

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

func (v awsKmsConfigValidator) MarkdownDescription(ctx context.Context) string {
func (v AwsKmsConfigValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

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

func AwsKmsConfig() validator.Object {
return awsKmsConfigValidator{}
return AwsKmsConfigValidator{}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package validate_test

import (
"context"
Expand All @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
)

func TestValidAwsKmsConfig(t *testing.T) {
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestValidAwsKmsConfig(t *testing.T) {
for _, tt := range tests {
wantErr := tt.wantErr

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

t.Run(tt.name, func(t *testing.T) {
t.Parallel()
awsKmsConfigValidator.ValidateObject(context.Background(), validatorRequest, &validatorResponse)
AwsKmsConfigValidator.ValidateObject(context.Background(), validatorRequest, &validatorResponse)

if validatorResponse.Diagnostics.HasError() && !wantErr {
t.Errorf("error = %v, wantErr %v", validatorResponse.Diagnostics.Errors(), wantErr)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package validate

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package validate_test

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
)

func TestValidCIDR(t *testing.T) {
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestValidCIDR(t *testing.T) {
for _, tt := range tests {
val := tt.cidr
wantErr := tt.wantErr
cidrValidator := CIDRValidator{}
cidrValidator := validate.CIDRValidator{}

validatorRequest := validator.StringRequest{
ConfigValue: types.StringValue(val),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package validator
package validate

import (
"context"
Expand All @@ -9,21 +9,21 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type durationValidator struct {
type DurationValidator struct {
MinMinutes int
MaxMinutes int
}

func (v durationValidator) Description(_ context.Context) string {
func (v DurationValidator) Description(_ context.Context) string {
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."
return fmt.Sprintf(ds, v.MinMinutes, v.MaxMinutes)
}

func (v durationValidator) MarkdownDescription(ctx context.Context) string {
func (v DurationValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

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

func ValidDurationBetween(minMinutes, maxMinutes int) validator.String {
return durationValidator{
return DurationValidator{
MinMinutes: minMinutes,
MaxMinutes: maxMinutes,
}
Expand Down
Loading

0 comments on commit bc94bf7

Please sign in to comment.