Skip to content

Commit

Permalink
feat(mongodb): add support for v1alpha1 (#2777)
Browse files Browse the repository at this point in the history
* feat(mongodb): resource instance

* feat(mongodb): add test for instance

* feat(mongodb): add basic test for instance

* feat(mongodb): add snapshot and test

* feat(mongodb): with private network

* feat(mongodb): delete private network

* t

* feat(mongodb): add data source and docs

* feat(mongodb): add cassette with sdk

* feat(mongodb): fix tflint

* feat(mongodb): add datasource doc

* feat(mongodb): fix lint

* feat(mongodb): fix lint

* feat(mongodb): fix lint

* feat(mongodb): fix lint

* feat(mongodb): fix lint

* feat(mongodb): fix lint

* feat(mongodb): add mongodb to workflow

* feat(mongodb): add test for update tag and password

* fix(mongodb): add trademark in docs

* feat(mongodb): add getsnapshot in test

* feat(mongodb): refacto update snapshot and instance

* feat(mongodb): replace TM by  ® in docs

* feat(mongodb): remove id from snapshot doc

* feat(mongodb): add warning when user_name change

* feat(mongodb): add id doc snapshot
  • Loading branch information
jremy42 authored Oct 28, 2024
1 parent 50bfea5 commit 5817807
Show file tree
Hide file tree
Showing 25 changed files with 59,581 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
- lb
- marketplace
- mnq
- mongodb
- object
- rdb
- redis
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- lb
- marketplace
- mnq
- mongodb
- object
- rdb
- redis
Expand Down
68 changes: 68 additions & 0 deletions docs/data-sources/mongodb_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
subcategory: "MongoDB®"
page_title: "Scaleway: scaleway_mongodb_instance"
---

# scaleway_mongodb_instance

Gets information about a MongoDB® Instance.

For further information refer to the Managed Databases for MongoDB® [API documentation](https://developers.scaleway.com/en/products/mongodb/api/)

## Example Usage

```hcl
# Get info by name
data "scaleway_mongodb_instance" "my_instance" {
name = "foobar"
}
# Get info by instance ID
data "scaleway_mongodb_instance" "my_instance" {
instance_id = "11111111-1111-1111-1111-111111111111"
}
# Get other attributes
output "mongodb_version" {
description = "Version of the MongoDB instance"
value = data.scaleway_mongodb_instance.my_instance.version
}
```

## Argument Reference

- `name` - (Optional) The name of the MongoDB® instance.

- `instance_id` - (Optional) The MongoDB® instance ID.

-> **Note** You must specify at least one: `name` or `instance_id`.

- `project_id` - (Optional) The ID of the project the MongoDB® instance is in. Can be used to filter instances when using `name`.

- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#zones) in which the MongoDB® Instance exists.

- `organization_id` - (Defaults to [provider](../index.md#organization_id) `organization_id`) The ID of the organization the MongoDB® instance is in.

## Attributes Reference

In addition to all above arguments, the following attributes are exported:

- `id` - The ID of the MongoDB® Instance.
- `name` - The name of the MongoDB® instance.
- `version` - The version of MongoDB® running on the instance.
- `node_type` - The type of MongoDB® node.
- `node_number` - The number of nodes in the MongoDB® cluster.
- `created_at` - The date and time the MongoDB® instance was created.
- `project_id` - The ID of the project the instance belongs to.
- `tags` - A list of tags attached to the MongoDB® instance.
- `volume_type` - The type of volume attached to the MongoDB® instance.
- `volume_size_in_gb` - The size of the attached volume, in GB.
- `public_network` - The details of the public network configuration, if applicable.

## Import

MongoDB® instance can be imported using the `id`, e.g.

```bash
terraform import scaleway_mongodb_instance.main fr-par-1/11111111-1111-1111-1111-111111111111
```
70 changes: 70 additions & 0 deletions docs/resources/mongodb_instance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
subcategory: "MongoDB®"
page_title: "Scaleway: scaleway_mongodb_instance"
---

# Resource: scaleway_mongodb_instance

Creates and manages Scaleway MongoDB® instance.
For more information refer to [the API documentation](https://www.scaleway.com/en/docs/managed-databases/mongodb/).

## Example Usage

### Basic

```terraform
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
}
```


### Restore From Snapshot

```terraform
resource "scaleway_mongodb_instance" "restored_instance" {
snapshot_id = "${scaleway_vpc_private_network.pn.idscaleway_mongodb_snapshot.main_snapshot.id}"
name = "restored-mongodb-from-snapshot"
node_type = "MGDB-PLAY2-NANO"
node_number = 1
}
```

## Argument Reference

The following arguments are supported:

- `version` - (Optional) MongoDB® version of the instance.
- `node_type` - (Required) The type of MongoDB® intance to create.
- `user_name` - (Optional) Name of the user created when the intance is created.
- `password` - (Optional) Password of the user.
- `name` - (Optional) Name of the MongoDB® instance.
- `tags` - (Optional) List of tags attached to the MongoDB® instance.
- `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.
- `public_network` - (Optional) Public network specs details.

## Attributes Reference

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.

## Import

MongoDB® instance can be imported using the `id`, e.g.

```bash
terraform import scaleway_mongodb_instance.main fr-par-1/11111111-1111-1111-1111-111111111111
```
62 changes: 62 additions & 0 deletions docs/resources/mongodb_snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
subcategory: "MongoDB®"
page_title: "Scaleway: scaleway_mongodb_snapshot"
---

# Resource: scaleway_mongodb_snapshot

Creates and manages Scaleway MongoDB® snapshots.
For more information refer to [the API documentation](https://www.scaleway.com/en/docs/managed-databases/mongodb/).

## Example Usage


```terraform
resource "scaleway_mongodb_snapshot" "main" {
instance_id = "${scaleway_mongodb_instance.main.id}"
name = "name-snapshot"
expires_at = "2024-12-31T23:59:59Z"
}
```


## Argument Reference

The following arguments are supported:

- `instance_id` - (Required) The ID of the MongoDB® instance from which the snapshot was created.

- `name` - (Optional) The name of the MongoDB® snapshot.

- `expires_at` - (Required) The expiration date of the MongoDB® snapshot in ISO 8601 format (e.g. `2024-12-31T23:59:59Z`).

~> **Important:** Once set, `expires_at` cannot be removed.

- `region` - (Defaults to [provider](../index.md) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® snapshot should be created.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

- `id` - The ID of the snapshot.

- `instance_name` - The name of the MongoDB® instance from which the snapshot was created.

- `size` - The size of the MongoDB® snapshot in bytes.

- `node_type` - The type of node associated with the MongoDB® snapshot.

- `volume_type` - The type of volume used for the MongoDB® snapshot.

- `created_at` - The date and time when the MongoDB® snapshot was created.

- `updated_at` - The date and time of the last update of the MongoDB® snapshot.

## Import

MongoDB® snapshots can be imported using the `{region}/{id}`, e.g.

```bash
terraform import scaleway_mongodb_snapshot.main fr-par-1/11111111-1111-1111-1111-111111111111
```
4 changes: 4 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/lb"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/marketplace"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/mnq"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/mongodb"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/object"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/rdb"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/redis"
Expand Down Expand Up @@ -188,6 +189,8 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_mnq_sqs": mnq.ResourceSQS(),
"scaleway_mnq_sqs_credentials": mnq.ResourceSQSCredentials(),
"scaleway_mnq_sqs_queue": mnq.ResourceSQSQueue(),
"scaleway_mongodb_instance": mongodb.ResourceInstance(),
"scaleway_mongodb_snapshot": mongodb.ResourceSnapshot(),
"scaleway_object": object.ResourceObject(),
"scaleway_object_bucket": object.ResourceBucket(),
"scaleway_object_bucket_acl": object.ResourceBucketACL(),
Expand Down Expand Up @@ -281,6 +284,7 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_marketplace_image": marketplace.DataSourceImage(),
"scaleway_mnq_sqs": mnq.DataSourceSQS(),
"scaleway_mnq_sns": mnq.DataSourceSNS(),
"scaleway_mongodb_instance": mongodb.DataSourceInstance(),
"scaleway_object_bucket": object.DataSourceBucket(),
"scaleway_object_bucket_policy": object.DataSourceBucketPolicy(),
"scaleway_rdb_acl": rdb.DataSourceACL(),
Expand Down
111 changes: 111 additions & 0 deletions internal/services/mongodb/data_source_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package mongodb

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func DataSourceInstance() *schema.Resource {
dsSchema := datasource.SchemaFromResourceSchema(ResourceInstance().Schema)

datasource.AddOptionalFieldsToSchema(dsSchema, "name", "region", "project_id")

dsSchema["name"].ConflictsWith = []string{"instance_id"}
dsSchema["instance_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "instance id",
ConflictsWith: []string{"name"},
ValidateDiagFunc: verify.IsUUIDorUUIDWithLocality(),
}

return &schema.Resource{
ReadContext: DataSourceInstanceRead,
Schema: dsSchema,
}
}

func DataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mongodbAPI, zone, region, err := newAPIWithZoneAndRegion(d, m)
if err != nil {
return diag.FromErr(err)
}

instanceID, ok := d.GetOk("instance_id")
if !ok {
instanceName := d.Get("name").(string)
res, err := mongodbAPI.ListInstances(&mongodb.ListInstancesRequest{
Region: region,
Name: types.ExpandStringPtr(instanceName),
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

foundInstance, err := datasource.FindExact(
res.Instances,
func(s *mongodb.Instance) bool { return s.Name == instanceName },
instanceName,
)
if err != nil {
return diag.FromErr(err)
}

instanceID = foundInstance.ID
}

zonedID := datasource.NewZonedID(instanceID, zone)
d.SetId(zonedID)
err = d.Set("instance_id", zonedID)
if err != nil {
return diag.FromErr(err)
}

getReq := &mongodb.GetInstanceRequest{
Region: region,
InstanceID: locality.ExpandID(instanceID.(string)),
}
instance, err := mongodbAPI.GetInstance(getReq, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("name", instance.Name)
_ = d.Set("version", instance.Version)
_ = d.Set("node_number", int(instance.NodeNumber))
_ = d.Set("node_type", instance.NodeType)
_ = d.Set("project_id", instance.ProjectID)
_ = d.Set("tags", instance.Tags)
_ = d.Set("created_at", instance.CreatedAt.Format(time.RFC3339))
_ = d.Set("region", instance.Region.String())

if instance.Volume != nil {
_ = d.Set("volume_type", instance.Volume.Type)
_ = d.Set("volume_size_in_gb", int(instance.Volume.Size/scw.GB))
}

publicNetworkEndpoint, publicNetworkExists := flattenPublicNetwork(instance.Endpoints)
if publicNetworkExists {
_ = d.Set("public_network", publicNetworkEndpoint)
}

if len(instance.Settings) > 0 {
settingsMap := make(map[string]string)
for _, setting := range instance.Settings {
settingsMap[setting.Name] = setting.Value
}
_ = d.Set("settings", settingsMap)
}

return nil
}
Loading

0 comments on commit 5817807

Please sign in to comment.