88 "strings"
99 "time"
1010
11- matlas "go.mongodb.org/atlas/mongodbatlas "
11+ "go.mongodb.org/atlas-sdk/v20231115005/admin "
1212
1313 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1414 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
@@ -27,11 +27,11 @@ const (
2727
2828func Resource () * schema.Resource {
2929 return & schema.Resource {
30- CreateContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessCreate ,
31- ReadContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessRead ,
32- DeleteContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete ,
30+ CreateContext : resourceCreate ,
31+ ReadContext : resourceRead ,
32+ DeleteContext : resourceDelete ,
3333 Importer : & schema.ResourceImporter {
34- StateContext : resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState ,
34+ StateContext : resourceImport ,
3535 },
3636 Schema : map [string ]* schema.Schema {
3737 "project_id" : {
@@ -74,57 +74,53 @@ func Resource() *schema.Resource {
7474 }
7575}
7676
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
8079 projectID := d .Get ("project_id" ).(string )
8180 instanceName := d .Get ("instance_name" ).(string )
8281
83- privateLinkRequest := & matlas. ServerlessPrivateEndpointConnection {
84- Comment : "create" ,
82+ serverlessTenantCreateRequest := & admin. ServerlessTenantCreateRequest {
83+ Comment : conversion . StringPtr ( "create" ) ,
8584 }
8685
87- endPoint , _ , err := conn . ServerlessPrivateEndpoints . Create (ctx , projectID , instanceName , privateLinkRequest )
86+ endPoint , _ , err := connV2 . ServerlessPrivateEndpointsApi . CreateServerlessPrivateEndpoint (ctx , projectID , instanceName , serverlessTenantCreateRequest ). Execute ( )
8887 if err != nil {
89- return diag .Errorf (privatelinkendpointserviceserverless .ErrorServerlessServiceEndpointAdd , privateLinkRequest . CloudProviderEndpointID , err )
88+ return diag .Errorf (privatelinkendpointserviceserverless .ErrorServerlessServiceEndpointAdd , endPoint . GetCloudProviderEndpointId () , err )
9089 }
9190
9291 stateConf := & retry.StateChangeConf {
9392 Pending : []string {"RESERVATION_REQUESTED" , "INITIATING" , "DELETING" },
9493 Target : []string {"RESERVED" , "FAILED" , "DELETED" , "AVAILABLE" },
95- Refresh : resourcePrivateLinkEndpointServerlessRefreshFunc (ctx , conn , projectID , instanceName , endPoint .ID ),
94+ Refresh : resourceRefreshFunc (ctx , connV2 , projectID , instanceName , endPoint .GetId () ),
9695 Timeout : d .Timeout (schema .TimeoutCreate ),
9796 MinTimeout : 5 * time .Second ,
9897 Delay : 5 * time .Second ,
9998 }
100- // RESERVATION_REQUESTED, RESERVED, INITIATING, AVAILABLE, FAILED, DELETING.
101- // Wait, catching any errors
99+
102100 _ , err = stateConf .WaitForStateContext (ctx )
103101 if err != nil {
104- return diag .FromErr (fmt .Errorf (errorServerlessEndpointAdd , err , endPoint .ID ))
102+ return diag .FromErr (fmt .Errorf (errorServerlessEndpointAdd , err , endPoint .GetId () ))
105103 }
106104
107105 d .SetId (conversion .EncodeStateID (map [string ]string {
108106 "project_id" : projectID ,
109107 "instance_name" : instanceName ,
110- "endpoint_id" : endPoint .ID ,
108+ "endpoint_id" : endPoint .GetId () ,
111109 }))
112110
113- return resourceMongoDBAtlasPrivateLinkEndpointServerlessRead (ctx , d , meta )
111+ return resourceRead (ctx , d , meta )
114112}
115113
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
119116 ids := conversion .DecodeStateID (d .Id ())
120117 projectID := ids ["project_id" ]
121118 instanceName := ids ["instance_name" ]
122119 endpointID := ids ["endpoint_id" ]
123120
124- privateLinkResponse , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
121+ privateLinkResponse , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
125122 if err != nil {
126- // case 404/ 400
127- // deleted in the backend case
123+ // case 404/400: deleted in the backend case
128124 if strings .Contains (err .Error (), "404" ) || strings .Contains (err .Error (), "400" ) {
129125 d .SetId ("" )
130126 return nil
@@ -133,41 +129,39 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessRead(ctx context.Context,
133129 return diag .Errorf ("error getting Serverless private link endpoint information: %s" , err )
134130 }
135131
136- if err := d .Set ("endpoint_id" , privateLinkResponse .ID ); err != nil {
132+ if err := d .Set ("endpoint_id" , privateLinkResponse .GetId () ); err != nil {
137133 return diag .Errorf ("error setting `endpoint_id` for endpoint_id (%s): %s" , d .Id (), err )
138134 }
139135
140136 if err := d .Set ("instance_name" , instanceName ); err != nil {
141137 return diag .Errorf ("error setting `instance Name` for endpoint_id (%s): %s" , d .Id (), err )
142138 }
143139
144- if err := d .Set ("endpoint_service_name" , privateLinkResponse .EndpointServiceName ); err != nil {
140+ if err := d .Set ("endpoint_service_name" , privateLinkResponse .GetEndpointServiceName () ); err != nil {
145141 return diag .Errorf ("error setting `endpoint_service_name Name` for endpoint_id (%s): %s" , d .Id (), err )
146142 }
147143
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 {
149145 return diag .Errorf ("error setting `private_link_service_resource_id Name` for endpoint_id (%s): %s" , d .Id (), err )
150146 }
151147
152- if err := d .Set ("status" , privateLinkResponse .Status ); err != nil {
148+ if err := d .Set ("status" , privateLinkResponse .GetStatus () ); err != nil {
153149 return diag .FromErr (fmt .Errorf (privatelinkendpoint .ErrorPrivateLinkEndpointsSetting , "status" , d .Id (), err ))
154150 }
155151
156152 return nil
157153}
158154
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
162157 ids := conversion .DecodeStateID (d .Id ())
163158 projectID := ids ["project_id" ]
164159 instanceName := ids ["instance_name" ]
165160 endpointID := ids ["endpoint_id" ]
166161
167- _ , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
162+ _ , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
168163 if err != nil {
169- // case 404
170- // deleted in the backend case
164+ // case 404/400: deleted in the backend case
171165 if strings .Contains (err .Error (), "404" ) || strings .Contains (err .Error (), "400" ) {
172166 d .SetId ("" )
173167 return nil
@@ -176,15 +170,15 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete(ctx context.Context
176170 return diag .Errorf ("error getting Serverless private link endpoint information: %s" , err )
177171 }
178172
179- _ , err = conn . ServerlessPrivateEndpoints . Delete (ctx , projectID , instanceName , endpointID )
173+ _ , _ , err = connV2 . ServerlessPrivateEndpointsApi . DeleteServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
180174 if err != nil {
181175 return diag .Errorf ("error deleting serverless private link endpoint(%s): %s" , endpointID , err )
182176 }
183177
184178 stateConf := & retry.StateChangeConf {
185179 Pending : []string {"DELETING" },
186180 Target : []string {"DELETED" , "FAILED" },
187- Refresh : resourcePrivateLinkEndpointServerlessRefreshFunc (ctx , conn , projectID , instanceName , endpointID ),
181+ Refresh : resourceRefreshFunc (ctx , connV2 , projectID , instanceName , endpointID ),
188182 Timeout : d .Timeout (schema .TimeoutDelete ),
189183 MinTimeout : 5 * time .Second ,
190184 Delay : 5 * time .Second ,
@@ -198,8 +192,8 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessDelete(ctx context.Context
198192 return nil
199193}
200194
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
203197
204198 parts := strings .SplitN (d .Id (), "--" , 3 )
205199 if len (parts ) != 3 {
@@ -210,7 +204,7 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
210204 instanceName := parts [1 ]
211205 endpointID := parts [2 ]
212206
213- privateLinkResponse , _ , err := conn . ServerlessPrivateEndpoints . Get (ctx , projectID , instanceName , endpointID )
207+ privateLinkResponse , _ , err := connV2 . ServerlessPrivateEndpointsApi . GetServerlessPrivateEndpoint (ctx , projectID , instanceName , endpointID ). Execute ( )
214208 if err != nil {
215209 return nil , fmt .Errorf ("couldn't import serverless private link endpoint (%s) in projectID (%s) , error: %s" , endpointID , projectID , err )
216210 }
@@ -226,11 +220,11 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
226220 log .Printf ("[WARN] Error setting instance_name for (%s): %s" , endpointID , err )
227221 }
228222
229- if err := d .Set ("endpoint_service_name" , privateLinkResponse .EndpointServiceName ); err != nil {
223+ if err := d .Set ("endpoint_service_name" , privateLinkResponse .GetEndpointServiceName () ); err != nil {
230224 log .Printf ("[WARN] Error setting endpoint_service_name for (%s): %s" , endpointID , err )
231225 }
232226
233- if privateLinkResponse .PrivateLinkServiceResourceID != "" {
227+ if privateLinkResponse .GetPrivateLinkServiceResourceId () != "" {
234228 if err := d .Set ("provider_name" , "AZURE" ); err != nil {
235229 log .Printf ("[WARN] Error setting provider_name for (%s): %s" , endpointID , err )
236230 }
@@ -249,21 +243,22 @@ func resourceMongoDBAtlasPrivateLinkEndpointServerlessImportState(ctx context.Co
249243 return []* schema.ResourceData {d }, nil
250244}
251245
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 {
253247 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 ( )
255249 if err != nil {
256- if resp .Response . StatusCode == 404 || resp . Response .StatusCode == 400 {
250+ if resp .StatusCode == 404 || resp .StatusCode == 400 {
257251 return "" , "DELETED" , nil
258252 }
259-
260253 return nil , "REJECTED" , err
261254 }
262255
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
265260 }
266261
267- return p , p . Status , nil
262+ return p , status , nil
268263 }
269264}
0 commit comments