-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Implements Read in TPF data source (#2865)
* enable data source test checks * ds model * same Read impl for singular ds as resource Read * Read in plural ds * use conversion.CopyModel to reduce duplicated code * check out is not nil * remove unneeded funcs * pluralModel * overrideUsingLegacySchema * fix condition to use legacy schema * remove use of config.AdvancedClusterV2Schema in tests * fix pinned_fcv * revert test changes * refactor ds to use readClustersDS and readClusterDS * enable ds test checks * refactor plural ds to use ListClusters info * leftover comment * revert temporarily overrideUsingLegacySchema * split convertClusterAddAdvConfig into getBasicClusterModel and updateModelAdvancedConfig * pagination comment in ListClusters * check if use_replication_spec_per_shard should be added in ds * bring changes from master in resource.go * CopyModel tests * have resource and datasource models together, renaming resource_schema files to schema * rename AsymmetricShardUnsupportedError to AsymmetricShardUnsupported * use AllPages * wait in applyAdvancedConfigurationChanges * await after every update * don't include accept_data_risks_and_force_replica_set_reconfig in ds * move ds schemas together with resource one * wait to apply adv_config changes in Create * leftover * skip TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema * leftover
- Loading branch information
Showing
13 changed files
with
425 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package conversion | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// CopyModel creates a new struct with the same values as the source struct. Fields in destination struct that are not in source are left with zero value. | ||
func CopyModel[T any](src any) (*T, error) { | ||
dest := new(T) | ||
valSrc := reflect.ValueOf(src) | ||
valDest := reflect.ValueOf(dest) | ||
if valSrc.Kind() != reflect.Ptr || valDest.Kind() != reflect.Ptr { | ||
return nil, fmt.Errorf("params must be pointers") | ||
} | ||
valSrc = valSrc.Elem() | ||
valDest = valDest.Elem() | ||
if valSrc.Kind() != reflect.Struct || valDest.Kind() != reflect.Struct { | ||
return nil, fmt.Errorf("params must be pointers to structs") | ||
} | ||
typeSrc := valSrc.Type() | ||
typeDest := valDest.Type() | ||
for i := 0; i < typeDest.NumField(); i++ { | ||
fieldDest := typeDest.Field(i) | ||
name := fieldDest.Name | ||
{ | ||
fieldSrc, found := typeSrc.FieldByName(name) | ||
if !found { | ||
continue | ||
} | ||
if fieldDest.Type != fieldSrc.Type { | ||
return nil, fmt.Errorf("field has different type: %s", name) | ||
} | ||
} | ||
if !valDest.Field(i).CanSet() { | ||
return nil, fmt.Errorf("field can't be set, probably unexported: %s", name) | ||
} | ||
valDest.Field(i).Set(valSrc.FieldByName(name)) | ||
} | ||
return dest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package conversion_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCopyModel(t *testing.T) { | ||
type destType struct { | ||
AttrStr string | ||
attrUnexported string | ||
AttrInt int | ||
} | ||
|
||
testCases := map[string]struct { | ||
input any | ||
expected any | ||
expectedErrorStr string | ||
}{ | ||
"basic": { | ||
input: &struct { | ||
AttrStr string | ||
AttrInt int | ||
}{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
attrUnexported: "", | ||
}, | ||
}, | ||
"missing field": { | ||
input: &struct { | ||
AttrStr string | ||
}{ | ||
AttrStr: "val", | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
}, | ||
}, | ||
"extra field": { | ||
input: &struct { | ||
AttrStr string | ||
AttrExtra string | ||
AttrInt int | ||
}{ | ||
AttrStr: "val", | ||
AttrExtra: "extra", | ||
AttrInt: 1, | ||
}, | ||
expected: &destType{ | ||
AttrStr: "val", | ||
AttrInt: 1, | ||
}, | ||
}, | ||
"different type": { | ||
input: &struct { | ||
AttrStr bool | ||
}{ | ||
AttrStr: true, | ||
}, | ||
expectedErrorStr: "field has different type: AttrStr", | ||
}, | ||
"unexported": { | ||
input: &struct { | ||
attrUnexported string | ||
}{ | ||
attrUnexported: "val", | ||
}, | ||
expectedErrorStr: "field can't be set, probably unexported: attrUnexported", | ||
}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
dest, err := conversion.CopyModel[destType](tc.input) | ||
if err == nil { | ||
assert.Equal(t, tc.expected, dest) | ||
assert.Equal(t, "", tc.expectedErrorStr) | ||
} else { | ||
require.ErrorContains(t, err, tc.expectedErrorStr) | ||
assert.Nil(t, dest) | ||
assert.Nil(t, tc.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.