8
8
"strings"
9
9
"time"
10
10
11
- matlas "go.mongodb.org/atlas/mongodbatlas "
11
+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
12
12
13
13
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
@@ -27,11 +27,11 @@ const (
27
27
28
28
func Resource () * schema.Resource {
29
29
return & schema.Resource {
30
- CreateContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessCreate ,
31
- ReadContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessRead ,
32
- DeleteContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete ,
30
+ CreateContext : resourceCreate ,
31
+ ReadContext : resourceRead ,
32
+ DeleteContext : resourceDelete ,
33
33
Importer : & schema.ResourceImporter {
34
- StateContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState ,
34
+ StateContext : resourceImport ,
35
35
},
36
36
Schema : map [string ]* schema.Schema {
37
37
"project_id" : {
@@ -74,57 +74,53 @@ func Resource() *schema.Resource {
74
74
}
75
75
}
76
76
77
- func resourceMongoDBAtlasPrivateLinkEndpointServerlessCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
78
- // Get client connection.
79
- conn := meta .(* config.MongoDBClient ).Atlas
77
+ func resourceCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
78
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
80
79
projectID := d .Get ("project_id" ).(string )
81
80
instanceName := d .Get ("instance_name" ).(string )
82
81
83
- privateLinkRequest := & matlas. ServerlessPrivateEndpointConnection {
84
- Comment : "create" ,
82
+ serverlessTenantCreateRequest := & admin. ServerlessTenantCreateRequest {
83
+ Comment : conversion . StringPtr ( "create" ) ,
85
84
}
86
85
87
- endPoint , _ , err := conn . ServerlessPrivateEndpoints . Create (ctx , projectID , instanceName , privateLinkRequest )
86
+ endPoint , _ , err := connV2 . ServerlessPrivateEndpointsApi . CreateServerlessPrivateEndpoint (ctx , projectID , instanceName , serverlessTenantCreateRequest ). Execute ( )
88
87
if err != nil {
89
- return diag .Errorf (privatelinkendpointserviceserverless .ErrorServerlessServiceEndpointAdd , privateLinkRequest . CloudProviderEndpointID , err )
88
+ return diag .Errorf (privatelinkendpointserviceserverless .ErrorServerlessServiceEndpointAdd , endPoint . GetCloudProviderEndpointId () , err )
90
89
}
91
90
92
91
stateConf := & retry.StateChangeConf {
93
92
Pending : []string {"RESERVATION_REQUESTED" , "INITIATING" , "DELETING" },
94
93
Target : []string {"RESERVED" , "FAILED" , "DELETED" , "AVAILABLE" },
95
- Refresh : resourcePrivateLinkEndpointServerlessRefreshFunc (ctx , conn , projectID , instanceName , endPoint .ID ),
94
+ Refresh : resourceRefreshFunc (ctx , connV2 , projectID , instanceName , endPoint .GetId () ),
96
95
Timeout : d .Timeout (schema .TimeoutCreate ),
97
96
MinTimeout : 5 * time .Second ,
98
97
Delay : 5 * time .Second ,
99
98
}
100
- // RESERVATION_REQUESTED, RESERVED, INITIATING, AVAILABLE, FAILED, DELETING.
101
- // Wait, catching any errors
99
+
102
100
_ , err = stateConf .WaitForStateContext (ctx )
103
101
if err != nil {
104
- return diag .FromErr (fmt .Errorf (errorServerlessEndpointAdd , err , endPoint .ID ))
102
+ return diag .FromErr (fmt .Errorf (errorServerlessEndpointAdd , err , endPoint .GetId () ))
105
103
}
106
104
107
105
d .SetId (conversion .EncodeStateID (map [string ]string {
108
106
"project_id" : projectID ,
109
107
"instance_name" : instanceName ,
110
- "endpoint_id" : endPoint .ID ,
108
+ "endpoint_id" : endPoint .GetId () ,
111
109
}))
112
110
113
- return resourceMongoDBAtlasPrivateLinkEndpointServerlessRead (ctx , d , meta )
111
+ return resourceRead (ctx , d , meta )
114
112
}
115
113
116
- func resourceMongoDBAtlasPrivateLinkEndpointServerlessRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
117
- // Get client connection.
118
- conn := meta .(* config.MongoDBClient ).Atlas
114
+ func resourceRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
115
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
119
116
ids := conversion .DecodeStateID (d .Id ())
120
117
projectID := ids ["project_id" ]
121
118
instanceName := ids ["instance_name" ]
122
119
endpointID := ids ["endpoint_id" ]
123
120
124
- privateLinkResponse , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
121
+ privateLinkResponse , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
125
122
if err != nil {
126
- // case 404/ 400
127
- // deleted in the backend case
123
+ // case 404/400: deleted in the backend case
128
124
if strings .Contains (err .Error (), "404" ) || strings .Contains (err .Error (), "400" ) {
129
125
d .SetId ("" )
130
126
return nil
@@ -133,41 +129,39 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessRead(ctx context.Context,
133
129
return diag .Errorf ("error getting Serverless private link endpoint information: %s" , err )
134
130
}
135
131
136
- if err := d .Set ("endpoint_id" , privateLinkResponse .ID ); err != nil {
132
+ if err := d .Set ("endpoint_id" , privateLinkResponse .GetId () ); err != nil {
137
133
return diag .Errorf ("error setting `endpoint_id` for endpoint_id (%s): %s" , d .Id (), err )
138
134
}
139
135
140
136
if err := d .Set ("instance_name" , instanceName ); err != nil {
141
137
return diag .Errorf ("error setting `instance Name` for endpoint_id (%s): %s" , d .Id (), err )
142
138
}
143
139
144
- if err := d .Set ("endpoint_service_name" , privateLinkResponse .EndpointServiceName ); err != nil {
140
+ if err := d .Set ("endpoint_service_name" , privateLinkResponse .GetEndpointServiceName () ); err != nil {
145
141
return diag .Errorf ("error setting `endpoint_service_name Name` for endpoint_id (%s): %s" , d .Id (), err )
146
142
}
147
143
148
- if err := d .Set ("private_link_service_resource_id" , privateLinkResponse .PrivateLinkServiceResourceID ); err != nil {
144
+ if err := d .Set ("private_link_service_resource_id" , privateLinkResponse .GetPrivateLinkServiceResourceId () ); err != nil {
149
145
return diag .Errorf ("error setting `private_link_service_resource_id Name` for endpoint_id (%s): %s" , d .Id (), err )
150
146
}
151
147
152
- if err := d .Set ("status" , privateLinkResponse .Status ); err != nil {
148
+ if err := d .Set ("status" , privateLinkResponse .GetStatus () ); err != nil {
153
149
return diag .FromErr (fmt .Errorf (privatelinkendpoint .ErrorPrivateLinkEndpointsSetting , "status" , d .Id (), err ))
154
150
}
155
151
156
152
return nil
157
153
}
158
154
159
- func resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
160
- // Get client connection.
161
- conn := meta .(* config.MongoDBClient ).Atlas
155
+ func resourceDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
156
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
162
157
ids := conversion .DecodeStateID (d .Id ())
163
158
projectID := ids ["project_id" ]
164
159
instanceName := ids ["instance_name" ]
165
160
endpointID := ids ["endpoint_id" ]
166
161
167
- _ , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
162
+ _ , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
168
163
if err != nil {
169
- // case 404
170
- // deleted in the backend case
164
+ // case 404/400: deleted in the backend case
171
165
if strings .Contains (err .Error (), "404" ) || strings .Contains (err .Error (), "400" ) {
172
166
d .SetId ("" )
173
167
return nil
@@ -176,15 +170,15 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete(ctx context.Context
176
170
return diag .Errorf ("error getting Serverless private link endpoint information: %s" , err )
177
171
}
178
172
179
- _ , err = conn . ServerlessPrivateEndpoints . Delete (ctx , projectID , instanceName , endpointID )
173
+ _ , _ , err = connV2 . ServerlessPrivateEndpointsApi . DeleteServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
180
174
if err != nil {
181
175
return diag .Errorf ("error deleting serverless private link endpoint(%s): %s" , endpointID , err )
182
176
}
183
177
184
178
stateConf := & retry.StateChangeConf {
185
179
Pending : []string {"DELETING" },
186
180
Target : []string {"DELETED" , "FAILED" },
187
- Refresh : resourcePrivateLinkEndpointServerlessRefreshFunc (ctx , conn , projectID , instanceName , endpointID ),
181
+ Refresh : resourceRefreshFunc (ctx , connV2 , projectID , instanceName , endpointID ),
188
182
Timeout : d .Timeout (schema .TimeoutDelete ),
189
183
MinTimeout : 5 * time .Second ,
190
184
Delay : 5 * time .Second ,
@@ -198,8 +192,8 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete(ctx context.Context
198
192
return nil
199
193
}
200
194
201
- func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
202
- conn := meta .(* config.MongoDBClient ).Atlas
195
+ func resourceImport (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
196
+ connV2 := meta .(* config.MongoDBClient ).AtlasV2
203
197
204
198
parts := strings .SplitN (d .Id (), "--" , 3 )
205
199
if len (parts ) != 3 {
@@ -210,7 +204,7 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
210
204
instanceName := parts [1 ]
211
205
endpointID := parts [2 ]
212
206
213
- privateLinkResponse , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
207
+ privateLinkResponse , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
214
208
if err != nil {
215
209
return nil , fmt .Errorf ("couldn't import serverless private link endpoint (%s) in projectID (%s) , error: %s" , endpointID , projectID , err )
216
210
}
@@ -226,11 +220,11 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
226
220
log .Printf ("[WARN] Error setting instance_name for (%s): %s" , endpointID , err )
227
221
}
228
222
229
- if err := d .Set ("endpoint_service_name" , privateLinkResponse .EndpointServiceName ); err != nil {
223
+ if err := d .Set ("endpoint_service_name" , privateLinkResponse .GetEndpointServiceName () ); err != nil {
230
224
log .Printf ("[WARN] Error setting endpoint_service_name for (%s): %s" , endpointID , err )
231
225
}
232
226
233
- if privateLinkResponse .PrivateLinkServiceResourceID != "" {
227
+ if privateLinkResponse .GetPrivateLinkServiceResourceId () != "" {
234
228
if err := d .Set ("provider_name" , "AZURE" ); err != nil {
235
229
log .Printf ("[WARN] Error setting provider_name for (%s): %s" , endpointID , err )
236
230
}
@@ -249,21 +243,22 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
249
243
return []* schema.ResourceData {d }, nil
250
244
}
251
245
252
- func resourcePrivateLinkEndpointServerlessRefreshFunc (ctx context.Context , client * matlas. Client , projectID , instanceName , privateLinkID string ) retry.StateRefreshFunc {
246
+ func resourceRefreshFunc (ctx context.Context , client * admin. APIClient , projectID , instanceName , privateLinkID string ) retry.StateRefreshFunc {
253
247
return func () (any , string , error ) {
254
- p , resp , err := client .ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , privateLinkID )
248
+ p , resp , err := client .ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , privateLinkID ). Execute ( )
255
249
if err != nil {
256
- if resp .Response . StatusCode == 404 || resp . Response .StatusCode == 400 {
250
+ if resp .StatusCode == 404 || resp .StatusCode == 400 {
257
251
return "" , "DELETED" , nil
258
252
}
259
-
260
253
return nil , "REJECTED" , err
261
254
}
262
255
263
- if p .Status != "WAITING_FOR_USER" {
264
- return "" , p .Status , nil
256
+ status := p .GetStatus ()
257
+
258
+ if status != "WAITING_FOR_USER" {
259
+ return "" , status , nil
265
260
}
266
261
267
- return p , p . Status , nil
262
+ return p , status , nil
268
263
}
269
264
}
0 commit comments