Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/resources/mongodb_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ resource "scaleway_mongodb_instance" "main" {
}
```

### Private Network

```terraform
resource scaleway_vpc_private_network pn01 {
name = "my_private_network"
region = "fr-par"
}

resource "scaleway_mongodb_instance" "main" {
name = "test-mongodb-basic1"
version = "7.0.12"
node_type = "MGDB-PLAY2-NANO"
node_number = 1
user_name = "my_initial_user"
password = "thiZ_is_v&ry_s3cret"
volume_size_in_gb = 5

private_network {
pn_id = "${scaleway_vpc_private_network.pn02.id}"
}

}
```


### Restore From Snapshot

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

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

## Import

Expand Down
146 changes: 140 additions & 6 deletions internal/services/mongodb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceInstance() *schema.Resource {
Expand Down Expand Up @@ -104,6 +106,51 @@ func ResourceInstance() *schema.Resource {
"version",
},
},
"private_network": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Private network to expose your mongodb instance",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pn_id": {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
DiffSuppressFunc: dsf.Locality,
Description: "The private network ID",
},
// Computed
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The private network ID",
},
"port": {
Type: schema.TypeInt,
Computed: true,
Description: "TCP port of the endpoint",
},
"dns_records": {
Type: schema.TypeList,
Computed: true,
Description: "List of DNS records for your endpoint",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"ips": {
Type: schema.TypeList,
Computed: true,
Description: "List of IP addresses for your endpoint",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
// Computed
"public_network": {
Type: schema.TypeList,
Expand All @@ -114,8 +161,9 @@ func ResourceInstance() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "ID of the public network",
},
"port": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -221,10 +269,32 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
createReq.Tags = types.ExpandStrings(tags)
}

epSpecs := make([]*mongodb.EndpointSpec, 0, 1)
spec := &mongodb.EndpointSpecPublicDetails{}
epSpecs = append(epSpecs, &mongodb.EndpointSpec{Public: spec})
createReq.Endpoints = epSpecs
var eps []*mongodb.EndpointSpec

if privateNetworkList, ok := d.GetOk("private_network"); ok {
privateNetworks := privateNetworkList.([]interface{})

if len(privateNetworks) > 0 {
pn := privateNetworks[0].(map[string]interface{})
privateNetworkID := locality.ExpandID(pn["pn_id"].(string))

if privateNetworkID != "" {
eps = append(eps, &mongodb.EndpointSpec{
PrivateNetwork: &mongodb.EndpointSpecPrivateNetworkDetails{
PrivateNetworkID: privateNetworkID,
},
})
}
}
}

if len(eps) == 0 {
eps = append(eps, &mongodb.EndpointSpec{
Public: &mongodb.EndpointSpecPublicDetails{},
})
}

createReq.Endpoints = eps

res, err = mongodbAPI.CreateInstance(createReq, scw.WithContext(ctx))
if err != nil {
Expand Down Expand Up @@ -283,6 +353,12 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
_ = d.Set("public_network", publicNetworkEndpoint)
}

privateNetworkEndpoint, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)

if privateNetworkExists {
_ = d.Set("private_network", privateNetworkEndpoint)
}

if len(instance.Settings) > 0 {
settingsMap := make(map[string]string)
for _, setting := range instance.Settings {
Expand Down Expand Up @@ -402,6 +478,64 @@ func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
}
}

////////////////////
// Endpoints
////////////////////

if d.HasChange("private_network") {
res, err := waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
}

for _, e := range res.Endpoints {
if e.PrivateNetwork != nil {
err := mongodbAPI.DeleteEndpoint(
&mongodb.DeleteEndpointRequest{
EndpointID: e.ID, Region: region,
},
scw.WithContext(ctx))
if err != nil {
diag.FromErr(err)
}
}
}

_, err = waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
}

var eps []*mongodb.EndpointSpec

if privateNetworkList, ok := d.GetOk("private_network"); ok {
privateNetworks := privateNetworkList.([]interface{})
if len(privateNetworks) > 0 {
pn := privateNetworks[0].(map[string]interface{})
privateNetworkID := locality.ExpandID(pn["pn_id"].(string))

if privateNetworkID != "" {
eps = append(eps, &mongodb.EndpointSpec{
PrivateNetwork: &mongodb.EndpointSpecPrivateNetworkDetails{
PrivateNetworkID: privateNetworkID,
},
})
}
}

if len(eps) != 0 {
_, err = mongodbAPI.CreateEndpoint(&mongodb.CreateEndpointRequest{
InstanceID: ID,
Endpoint: eps[0],
Region: region,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}
}
}

_, err = waitForInstance(ctx, mongodbAPI, region, ID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
Expand Down
Loading
Loading