@@ -12,10 +12,12 @@ import (
12
12
"github.com/scaleway/scaleway-sdk-go/scw"
13
13
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
14
14
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
15
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
15
16
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
16
17
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
17
18
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
18
19
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
20
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
19
21
)
20
22
21
23
func ResourceInstance () * schema.Resource {
@@ -104,6 +106,51 @@ func ResourceInstance() *schema.Resource {
104
106
"version" ,
105
107
},
106
108
},
109
+ "private_network" : {
110
+ Type : schema .TypeList ,
111
+ Optional : true ,
112
+ MaxItems : 1 ,
113
+ Description : "Private network to expose your mongodb instance" ,
114
+ Elem : & schema.Resource {
115
+ Schema : map [string ]* schema.Schema {
116
+ "pn_id" : {
117
+ Type : schema .TypeString ,
118
+ Required : true ,
119
+ ValidateDiagFunc : verify .IsUUIDorUUIDWithLocality (),
120
+ DiffSuppressFunc : dsf .Locality ,
121
+ Description : "The private network ID" ,
122
+ },
123
+ // Computed
124
+ "id" : {
125
+ Type : schema .TypeString ,
126
+ Computed : true ,
127
+ Description : "The private network ID" ,
128
+ },
129
+ "port" : {
130
+ Type : schema .TypeInt ,
131
+ Computed : true ,
132
+ Description : "TCP port of the endpoint" ,
133
+ },
134
+ "dns_records" : {
135
+ Type : schema .TypeList ,
136
+ Computed : true ,
137
+ Description : "List of DNS records for your endpoint" ,
138
+ Elem : & schema.Schema {
139
+ Type : schema .TypeString ,
140
+ },
141
+ },
142
+
143
+ "ips" : {
144
+ Type : schema .TypeList ,
145
+ Computed : true ,
146
+ Description : "List of IP addresses for your endpoint" ,
147
+ Elem : & schema.Schema {
148
+ Type : schema .TypeString ,
149
+ },
150
+ },
151
+ },
152
+ },
153
+ },
107
154
// Computed
108
155
"public_network" : {
109
156
Type : schema .TypeList ,
@@ -114,8 +161,9 @@ func ResourceInstance() *schema.Resource {
114
161
Elem : & schema.Resource {
115
162
Schema : map [string ]* schema.Schema {
116
163
"id" : {
117
- Type : schema .TypeString ,
118
- Computed : true ,
164
+ Type : schema .TypeString ,
165
+ Computed : true ,
166
+ Description : "ID of the public network" ,
119
167
},
120
168
"port" : {
121
169
Type : schema .TypeInt ,
@@ -221,10 +269,32 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
221
269
createReq .Tags = types .ExpandStrings (tags )
222
270
}
223
271
224
- epSpecs := make ([]* mongodb.EndpointSpec , 0 , 1 )
225
- spec := & mongodb.EndpointSpecPublicDetails {}
226
- epSpecs = append (epSpecs , & mongodb.EndpointSpec {Public : spec })
227
- createReq .Endpoints = epSpecs
272
+ var eps []* mongodb.EndpointSpec
273
+
274
+ if privateNetworkList , ok := d .GetOk ("private_network" ); ok {
275
+ privateNetworks := privateNetworkList .([]interface {})
276
+
277
+ if len (privateNetworks ) > 0 {
278
+ pn := privateNetworks [0 ].(map [string ]interface {})
279
+ privateNetworkID := locality .ExpandID (pn ["pn_id" ].(string ))
280
+
281
+ if privateNetworkID != "" {
282
+ eps = append (eps , & mongodb.EndpointSpec {
283
+ PrivateNetwork : & mongodb.EndpointSpecPrivateNetworkDetails {
284
+ PrivateNetworkID : privateNetworkID ,
285
+ },
286
+ })
287
+ }
288
+ }
289
+ }
290
+
291
+ if len (eps ) == 0 {
292
+ eps = append (eps , & mongodb.EndpointSpec {
293
+ Public : & mongodb.EndpointSpecPublicDetails {},
294
+ })
295
+ }
296
+
297
+ createReq .Endpoints = eps
228
298
229
299
res , err = mongodbAPI .CreateInstance (createReq , scw .WithContext (ctx ))
230
300
if err != nil {
@@ -283,6 +353,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
283
353
_ = d .Set ("public_network" , publicNetworkEndpoint )
284
354
}
285
355
356
+ privateNetworkEndpoint , privateNetworkExists := flattenPrivateNetwork (instance .Endpoints )
357
+
358
+ if privateNetworkExists {
359
+ _ = d .Set ("private_network" , privateNetworkEndpoint )
360
+ }
361
+
286
362
if len (instance .Settings ) > 0 {
287
363
settingsMap := make (map [string ]string )
288
364
for _ , setting := range instance .Settings {
@@ -402,6 +478,64 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
402
478
}
403
479
}
404
480
481
+ ////////////////////
482
+ // Endpoints
483
+ ////////////////////
484
+
485
+ if d .HasChange ("private_network" ) {
486
+ res , err := waitForInstance (ctx , mongodbAPI , region , ID , d .Timeout (schema .TimeoutUpdate ))
487
+ if err != nil {
488
+ return diag .FromErr (err )
489
+ }
490
+
491
+ for _ , e := range res .Endpoints {
492
+ if e .PrivateNetwork != nil {
493
+ err := mongodbAPI .DeleteEndpoint (
494
+ & mongodb.DeleteEndpointRequest {
495
+ EndpointID : e .ID , Region : region ,
496
+ },
497
+ scw .WithContext (ctx ))
498
+ if err != nil {
499
+ diag .FromErr (err )
500
+ }
501
+ }
502
+ }
503
+
504
+ _ , err = waitForInstance (ctx , mongodbAPI , region , ID , d .Timeout (schema .TimeoutUpdate ))
505
+ if err != nil {
506
+ return diag .FromErr (err )
507
+ }
508
+
509
+ var eps []* mongodb.EndpointSpec
510
+
511
+ if privateNetworkList , ok := d .GetOk ("private_network" ); ok {
512
+ privateNetworks := privateNetworkList .([]interface {})
513
+ if len (privateNetworks ) > 0 {
514
+ pn := privateNetworks [0 ].(map [string ]interface {})
515
+ privateNetworkID := locality .ExpandID (pn ["pn_id" ].(string ))
516
+
517
+ if privateNetworkID != "" {
518
+ eps = append (eps , & mongodb.EndpointSpec {
519
+ PrivateNetwork : & mongodb.EndpointSpecPrivateNetworkDetails {
520
+ PrivateNetworkID : privateNetworkID ,
521
+ },
522
+ })
523
+ }
524
+ }
525
+
526
+ if len (eps ) != 0 {
527
+ _ , err = mongodbAPI .CreateEndpoint (& mongodb.CreateEndpointRequest {
528
+ InstanceID : ID ,
529
+ Endpoint : eps [0 ],
530
+ Region : region ,
531
+ }, scw .WithContext (ctx ))
532
+ if err != nil {
533
+ return diag .FromErr (err )
534
+ }
535
+ }
536
+ }
537
+ }
538
+
405
539
_ , err = waitForInstance (ctx , mongodbAPI , region , ID , d .Timeout (schema .TimeoutCreate ))
406
540
if err != nil {
407
541
return diag .FromErr (err )
0 commit comments