@@ -6,10 +6,12 @@ import (
6
6
"net/http"
7
7
"testing"
8
8
9
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
10
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
10
11
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/mock"
14
+ "github.com/stretchr/testify/require"
13
15
"go.mongodb.org/atlas-sdk/v20231115014/admin"
14
16
"go.mongodb.org/atlas-sdk/v20231115014/mockadmin"
15
17
)
@@ -21,6 +23,128 @@ var (
21
23
advancedClusters = []admin.AdvancedClusterDescription {{StateName : conversion .StringPtr ("NOT IDLE" )}}
22
24
)
23
25
26
+ func TestFlattenReplicationSpecs (t * testing.T ) {
27
+ var (
28
+ regionName = "EU_WEST_1"
29
+ providerName = "AWS"
30
+ expectedID = "id1"
31
+ unexpectedID = "id2"
32
+ expectedZoneName = "z1"
33
+ unexpectedZoneName = "z2"
34
+ regionConfigAdmin = []admin.CloudRegionConfig {{
35
+ ProviderName : & providerName ,
36
+ RegionName : & regionName ,
37
+ }}
38
+ regionConfigTfSameZone = map [string ]any {
39
+ "provider_name" : "AWS" ,
40
+ "region_name" : regionName ,
41
+ }
42
+ regionConfigTfDiffZone = map [string ]any {
43
+ "provider_name" : "AWS" ,
44
+ "region_name" : regionName ,
45
+ "zone_name" : unexpectedZoneName ,
46
+ }
47
+ apiSpecExpected = admin.ReplicationSpec {Id : & expectedID , ZoneName : & expectedZoneName , RegionConfigs : & regionConfigAdmin }
48
+ apiSpecDifferent = admin.ReplicationSpec {Id : & unexpectedID , ZoneName : & unexpectedZoneName , RegionConfigs : & regionConfigAdmin }
49
+ testSchema = map [string ]* schema.Schema {
50
+ "project_id" : {Type : schema .TypeString },
51
+ }
52
+ tfSameIDSameZone = map [string ]any {
53
+ "id" : expectedID ,
54
+ "num_shards" : 1 ,
55
+ "region_configs" : []any {regionConfigTfSameZone },
56
+ "zone_name" : expectedZoneName ,
57
+ }
58
+ tfNoIDSameZone = map [string ]any {
59
+ "id" : nil ,
60
+ "num_shards" : 1 ,
61
+ "region_configs" : []any {regionConfigTfSameZone },
62
+ "zone_name" : expectedZoneName ,
63
+ }
64
+ tfNoIDDiffZone = map [string ]any {
65
+ "id" : nil ,
66
+ "num_shards" : 1 ,
67
+ "region_configs" : []any {regionConfigTfDiffZone },
68
+ "zone_name" : unexpectedZoneName ,
69
+ }
70
+ tfdiffIDDiffZone = map [string ]any {
71
+ "id" : "unique" ,
72
+ "num_shards" : 1 ,
73
+ "region_configs" : []any {regionConfigTfDiffZone },
74
+ "zone_name" : unexpectedZoneName ,
75
+ }
76
+ )
77
+ testCases := map [string ]struct {
78
+ adminSpecs []admin.ReplicationSpec
79
+ tfInputSpecs []any
80
+ expectedLen int
81
+ }{
82
+ "empty admin spec should return empty list" : {
83
+ []admin.ReplicationSpec {},
84
+ []any {tfSameIDSameZone },
85
+ 0 ,
86
+ },
87
+ "existing id, should match admin" : {
88
+ []admin.ReplicationSpec {apiSpecExpected },
89
+ []any {tfSameIDSameZone },
90
+ 1 ,
91
+ },
92
+ "existing different id, should change to admin spec" : {
93
+ []admin.ReplicationSpec {apiSpecExpected },
94
+ []any {tfdiffIDDiffZone },
95
+ 1 ,
96
+ },
97
+ "missing id, should be set when zone_name matches" : {
98
+ []admin.ReplicationSpec {apiSpecExpected },
99
+ []any {tfNoIDSameZone },
100
+ 1 ,
101
+ },
102
+ "missing id and diff zone, should change to admin spec" : {
103
+ []admin.ReplicationSpec {apiSpecExpected },
104
+ []any {tfNoIDDiffZone },
105
+ 1 ,
106
+ },
107
+ "existing id, should match correct api spec using `id` and extra api spec added" : {
108
+ []admin.ReplicationSpec {apiSpecDifferent , apiSpecExpected },
109
+ []any {tfSameIDSameZone },
110
+ 2 ,
111
+ },
112
+ "missing id, should match correct api spec using `zone_name` and extra api spec added" : {
113
+ []admin.ReplicationSpec {apiSpecDifferent , apiSpecExpected },
114
+ []any {tfNoIDSameZone },
115
+ 2 ,
116
+ },
117
+ "two matching specs should be set to api specs" : {
118
+ []admin.ReplicationSpec {apiSpecExpected , apiSpecDifferent },
119
+ []any {tfSameIDSameZone , tfdiffIDDiffZone },
120
+ 2 ,
121
+ },
122
+ }
123
+ for name , tc := range testCases {
124
+ t .Run (name , func (t * testing.T ) {
125
+ peeringAPI := mockadmin.NetworkPeeringApi {}
126
+
127
+ peeringAPI .EXPECT ().ListPeeringContainerByCloudProviderWithParams (mock .Anything , mock .Anything ).Return (admin.ListPeeringContainerByCloudProviderApiRequest {ApiService : & peeringAPI })
128
+ containerResult := []admin.CloudProviderContainer {{Id : conversion .StringPtr ("c1" ), RegionName : & regionName , ProviderName : & providerName }}
129
+ peeringAPI .EXPECT ().ListPeeringContainerByCloudProviderExecute (mock .Anything ).Return (& admin.PaginatedCloudProviderContainer {Results : & containerResult }, nil , nil )
130
+
131
+ client := & admin.APIClient {
132
+ NetworkPeeringApi : & peeringAPI ,
133
+ }
134
+ resourceData := schema .TestResourceDataRaw (t , testSchema , map [string ]any {"project_id" : "p1" })
135
+
136
+ tfOutputSpecs , err := advancedcluster .FlattenAdvancedReplicationSpecs (context .Background (), tc .adminSpecs , tc .tfInputSpecs , resourceData , client )
137
+
138
+ require .NoError (t , err )
139
+ assert .Len (t , tfOutputSpecs , tc .expectedLen )
140
+ if tc .expectedLen != 0 {
141
+ assert .Equal (t , expectedID , tfOutputSpecs [0 ]["id" ])
142
+ assert .Equal (t , expectedZoneName , tfOutputSpecs [0 ]["zone_name" ])
143
+ }
144
+ })
145
+ }
146
+ }
147
+
24
148
type Result struct {
25
149
response any
26
150
error error
0 commit comments