@@ -12,10 +12,12 @@ import (
1212 "github.com/scaleway/scaleway-sdk-go/scw"
1313 "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1414 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
15+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1516 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1617 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1718 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1819 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
20+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1921)
2022
2123func ResourceInstance () * schema.Resource {
@@ -104,6 +106,51 @@ func ResourceInstance() *schema.Resource {
104106 "version" ,
105107 },
106108 },
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+ },
107154 // Computed
108155 "public_network" : {
109156 Type : schema .TypeList ,
@@ -114,8 +161,9 @@ func ResourceInstance() *schema.Resource {
114161 Elem : & schema.Resource {
115162 Schema : map [string ]* schema.Schema {
116163 "id" : {
117- Type : schema .TypeString ,
118- Computed : true ,
164+ Type : schema .TypeString ,
165+ Computed : true ,
166+ Description : "ID of the public network" ,
119167 },
120168 "port" : {
121169 Type : schema .TypeInt ,
@@ -221,10 +269,32 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
221269 createReq .Tags = types .ExpandStrings (tags )
222270 }
223271
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
228298
229299 res , err = mongodbAPI .CreateInstance (createReq , scw .WithContext (ctx ))
230300 if err != nil {
@@ -283,6 +353,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
283353 _ = d .Set ("public_network" , publicNetworkEndpoint )
284354 }
285355
356+ privateNetworkEndpoint , privateNetworkExists := flattenPrivateNetwork (instance .Endpoints )
357+
358+ if privateNetworkExists {
359+ _ = d .Set ("private_network" , privateNetworkEndpoint )
360+ }
361+
286362 if len (instance .Settings ) > 0 {
287363 settingsMap := make (map [string ]string )
288364 for _ , setting := range instance .Settings {
@@ -402,6 +478,64 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
402478 }
403479 }
404480
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+
405539 _ , err = waitForInstance (ctx , mongodbAPI , region , ID , d .Timeout (schema .TimeoutCreate ))
406540 if err != nil {
407541 return diag .FromErr (err )
0 commit comments