Skip to content

Commit 65453d4

Browse files
authored
feat(mongodb): vpc support (#2967)
* feat(mongodb): vpc support * feat(mongodb): add doc
1 parent 33e5e5a commit 65453d4

6 files changed

+9630
-6
lines changed

docs/resources/mongodb_instance.md

+31
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ resource "scaleway_mongodb_instance" "main" {
2525
}
2626
```
2727

28+
### Private Network
29+
30+
```terraform
31+
resource scaleway_vpc_private_network pn01 {
32+
name = "my_private_network"
33+
region = "fr-par"
34+
}
35+
36+
resource "scaleway_mongodb_instance" "main" {
37+
name = "test-mongodb-basic1"
38+
version = "7.0.12"
39+
node_type = "MGDB-PLAY2-NANO"
40+
node_number = 1
41+
user_name = "my_initial_user"
42+
password = "thiZ_is_v&ry_s3cret"
43+
volume_size_in_gb = 5
44+
45+
private_network {
46+
pn_id = "${scaleway_vpc_private_network.pn02.id}"
47+
}
48+
49+
}
50+
```
51+
2852

2953
### Restore From Snapshot
3054

@@ -51,6 +75,8 @@ The following arguments are supported:
5175
- `volume_type` - (Optional) Volume type of the instance.
5276
- `volume_size_in_gb` - (Optional) Volume size in GB.
5377
- `snapshot_id` - (Optional) Snapshot ID to restore the MongoDB® instance from.
78+
- `private_network` - (Optional) Private Network endpoints of the Database Instance.
79+
- `pn_id` - (Required) The ID of the Private Network.
5480
- `public_network` - (Optional) Public network specs details.
5581

5682
## Attributes Reference
@@ -60,6 +86,11 @@ In addition to all arguments above, the following attributes are exported:
6086
- `id` - The ID of the MongoDB® instance.
6187
- `created_at` - The date and time of the creation of the MongoDB® instance.
6288
- `updated_at` - The date and time of the last update of the MongoDB® instance.
89+
- `private_network` - Private Network endpoints of the Database Instance.
90+
- `id` - The ID of the endpoint.
91+
- `ips` - List of IP addresses for your endpoint.
92+
- `port` - TCP port of the endpoint.
93+
- `dns_records` - List of DNS records for your endpoint.
6394

6495
## Import
6596

internal/services/mongodb/instance.go

+140-6
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,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

Comments
 (0)