Skip to content

Commit 5cc070b

Browse files
committed
feat(mongodb): vpc support
1 parent ff12156 commit 5cc070b

File tree

5 files changed

+9595
-4
lines changed

5 files changed

+9595
-4
lines changed

internal/services/mongodb/instance.go

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2123
func ResourceInstance() *schema.Resource {
@@ -104,6 +106,50 @@ func ResourceInstance() *schema.Resource {
104106
"version",
105107
},
106108
},
109+
"private_network": {
110+
Type: schema.TypeList,
111+
Optional: true,
112+
MaxItems: 1,
113+
Description: "List of private network to expose your database 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+
},
128+
"port": {
129+
Type: schema.TypeInt,
130+
Computed: true,
131+
Description: "TCP port of the endpoint",
132+
},
133+
"dns_records": {
134+
Type: schema.TypeList,
135+
Computed: true,
136+
Description: "List of DNS records for your endpoint",
137+
Elem: &schema.Schema{
138+
Type: schema.TypeString,
139+
},
140+
},
141+
142+
"ips": {
143+
Type: schema.TypeList,
144+
Computed: true,
145+
Description: "List of IP addresses for your endpoint",
146+
Elem: &schema.Schema{
147+
Type: schema.TypeString,
148+
},
149+
},
150+
},
151+
},
152+
},
107153
// Computed
108154
"public_network": {
109155
Type: schema.TypeList,
@@ -221,10 +267,32 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
221267
createReq.Tags = types.ExpandStrings(tags)
222268
}
223269

224-
epSpecs := make([]*mongodb.EndpointSpec, 0, 1)
225-
spec := &mongodb.EndpointSpecPublicDetails{}
226-
epSpecs = append(epSpecs, &mongodb.EndpointSpec{Public: spec})
227-
createReq.Endpoints = epSpecs
270+
var eps []*mongodb.EndpointSpec
271+
272+
if privateNetworkList, ok := d.GetOk("private_network"); ok {
273+
privateNetworks := privateNetworkList.([]interface{})
274+
275+
if len(privateNetworks) > 0 {
276+
pn := privateNetworks[0].(map[string]interface{})
277+
privateNetworkID := locality.ExpandID(pn["pn_id"].(string))
278+
279+
if privateNetworkID != "" {
280+
eps = append(eps, &mongodb.EndpointSpec{
281+
PrivateNetwork: &mongodb.EndpointSpecPrivateNetworkDetails{
282+
PrivateNetworkID: privateNetworkID,
283+
},
284+
})
285+
}
286+
}
287+
}
288+
289+
if len(eps) == 0 {
290+
eps = append(eps, &mongodb.EndpointSpec{
291+
Public: &mongodb.EndpointSpecPublicDetails{},
292+
})
293+
}
294+
295+
createReq.Endpoints = eps
228296

229297
res, err = mongodbAPI.CreateInstance(createReq, scw.WithContext(ctx))
230298
if err != nil {
@@ -283,6 +351,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
283351
_ = d.Set("public_network", publicNetworkEndpoint)
284352
}
285353

354+
privateNetworkEndpoint, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)
355+
356+
if privateNetworkExists {
357+
_ = d.Set("private_network", privateNetworkEndpoint)
358+
}
359+
286360
if len(instance.Settings) > 0 {
287361
settingsMap := make(map[string]string)
288362
for _, setting := range instance.Settings {
@@ -402,6 +476,64 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
402476
}
403477
}
404478

479+
////////////////////
480+
// Endpoints
481+
////////////////////
482+
483+
if d.HasChange("private_network") {
484+
res, err := waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
485+
if err != nil {
486+
return diag.FromErr(err)
487+
}
488+
489+
for _, e := range res.Endpoints {
490+
if e.PrivateNetwork != nil {
491+
err := mongodbAPI.DeleteEndpoint(
492+
&mongodb.DeleteEndpointRequest{
493+
EndpointID: e.ID, Region: region,
494+
},
495+
scw.WithContext(ctx))
496+
if err != nil {
497+
diag.FromErr(err)
498+
}
499+
}
500+
}
501+
502+
_, err = waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
503+
if err != nil {
504+
return diag.FromErr(err)
505+
}
506+
507+
var eps []*mongodb.EndpointSpec
508+
509+
if privateNetworkList, ok := d.GetOk("private_network"); ok {
510+
privateNetworks := privateNetworkList.([]interface{})
511+
if len(privateNetworks) > 0 {
512+
pn := privateNetworks[0].(map[string]interface{})
513+
privateNetworkID := locality.ExpandID(pn["pn_id"].(string))
514+
515+
if privateNetworkID != "" {
516+
eps = append(eps, &mongodb.EndpointSpec{
517+
PrivateNetwork: &mongodb.EndpointSpecPrivateNetworkDetails{
518+
PrivateNetworkID: privateNetworkID,
519+
},
520+
})
521+
}
522+
}
523+
524+
if len(eps) != 0 {
525+
_, err = mongodbAPI.CreateEndpoint(&mongodb.CreateEndpointRequest{
526+
InstanceID: ID,
527+
Endpoint: eps[0],
528+
Region: region,
529+
}, scw.WithContext(ctx))
530+
if err != nil {
531+
return diag.FromErr(err)
532+
}
533+
}
534+
}
535+
}
536+
405537
_, err = waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutCreate))
406538
if err != nil {
407539
return diag.FromErr(err)

0 commit comments

Comments
 (0)