Skip to content

Commit 42bab1d

Browse files
authored
fix(mongodb): enable public & private endpoints definition (#3100)
* fix(mongodb): enable simultaneous public & private endpoints and update doc * add doc * check private network
1 parent 0900ae6 commit 42bab1d

File tree

4 files changed

+3722
-1
lines changed

4 files changed

+3722
-1
lines changed

docs/resources/mongodb_instance.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ resource "scaleway_mongodb_instance" "main" {
4949
}
5050
```
5151

52+
### Private Network and Public Network
53+
54+
```terraform
55+
resource scaleway_vpc_private_network pn01 {
56+
name = "my_private_network"
57+
region = "fr-par"
58+
}
59+
60+
resource "scaleway_mongodb_instance" "main" {
61+
name = "test-mongodb-basic1"
62+
version = "7.0.12"
63+
node_type = "MGDB-PLAY2-NANO"
64+
node_number = 1
65+
user_name = "my_initial_user"
66+
password = "thiZ_is_v&ry_s3cret"
67+
volume_size_in_gb = 5
68+
69+
private_network {
70+
pn_id = "${scaleway_vpc_private_network.pn02.id}"
71+
}
72+
73+
public_network {}
74+
75+
}
76+
```
77+
5278

5379
### Restore From Snapshot
5480

@@ -77,7 +103,9 @@ The following arguments are supported:
77103
- `snapshot_id` - (Optional) Snapshot ID to restore the MongoDB® instance from.
78104
- `private_network` - (Optional) Private Network endpoints of the Database Instance.
79105
- `pn_id` - (Required) The ID of the Private Network.
80-
- `public_network` - (Optional) Public network specs details.
106+
- `public_network` - (Optional) Public network endpoint configuration (no arguments).
107+
~> **Important** If neither private_network nor public_network is specified, a public network endpoint is created by default.
108+
81109

82110
## Attributes Reference
83111

@@ -91,6 +119,10 @@ In addition to all arguments above, the following attributes are exported:
91119
- `ips` - List of IP addresses for your endpoint.
92120
- `port` - TCP port of the endpoint.
93121
- `dns_records` - List of DNS records for your endpoint.
122+
- `public_network` - Private Network endpoints of the Database Instance.
123+
- `id` - The ID of the endpoint.
124+
- `port` - TCP port of the endpoint.
125+
- `dns_records` - List of DNS records for your endpoint.
94126

95127
## Import
96128

internal/services/mongodb/instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
288288
}
289289
}
290290

291+
if pubList, ok := d.GetOk("public_network"); ok {
292+
items := pubList.([]interface{})
293+
if len(items) > 0 {
294+
eps = append(eps, &mongodb.EndpointSpec{
295+
Public: &mongodb.EndpointSpecPublicDetails{},
296+
})
297+
}
298+
}
299+
291300
if len(eps) == 0 {
292301
eps = append(eps, &mongodb.EndpointSpec{
293302
Public: &mongodb.EndpointSpecPublicDetails{},

internal/services/mongodb/instance_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,55 @@ func TestAccMongoDBInstance_UpdatePrivateNetwork(t *testing.T) {
373373
})
374374
}
375375

376+
func TestAccMongoDBInstance_WithPublicNetwork(t *testing.T) {
377+
tt := acctest.NewTestTools(t)
378+
defer tt.Cleanup()
379+
380+
resource.ParallelTest(t, resource.TestCase{
381+
PreCheck: func() { acctest.PreCheck(t) },
382+
ProviderFactories: tt.ProviderFactories,
383+
CheckDestroy: IsInstanceDestroyed(tt),
384+
Steps: []resource.TestStep{
385+
{
386+
Config: `
387+
388+
resource "scaleway_vpc_private_network" "pn01" {
389+
name = "my_private_network"
390+
region = "fr-par"
391+
}
392+
393+
resource "scaleway_mongodb_instance" "main" {
394+
name = "test-mongodb-public-network"
395+
version = "7.0.12"
396+
node_type = "MGDB-PLAY2-NANO"
397+
node_number = 1
398+
user_name = "my_initial_user"
399+
password = "thiZ_is_v&ry_s3cret"
400+
volume_size_in_gb = 5
401+
402+
private_network {
403+
pn_id = scaleway_vpc_private_network.pn01.id
404+
}
405+
406+
public_network {}
407+
}
408+
`,
409+
Check: resource.ComposeTestCheckFunc(
410+
isMongoDBInstancePresent(tt, "scaleway_mongodb_instance.main"),
411+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "public_network.#", "1"),
412+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "public_network.0.id"),
413+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "public_network.0.port"),
414+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "public_network.0.dns_record"),
415+
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "private_network.#", "1"),
416+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
417+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.port"),
418+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.dns_records.#"),
419+
),
420+
},
421+
},
422+
})
423+
}
424+
376425
func capturePrivateNetworkID(previousID *string) resource.TestCheckFunc {
377426
return func(s *terraform.State) error {
378427
rs, ok := s.RootModule().Resources["scaleway_mongodb_instance.main"]

0 commit comments

Comments
 (0)