Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mongodb): add support for v1alpha1 #2777

Merged
merged 28 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
49827b5
feat(mongodb): resource instance
jremy42 Sep 30, 2024
9569d5e
feat(mongodb): add test for instance
jremy42 Oct 3, 2024
dd3394f
feat(mongodb): add basic test for instance
jremy42 Oct 9, 2024
95bc488
feat(mongodb): add snapshot and test
jremy42 Oct 11, 2024
ffccd89
feat(mongodb): with private network
jremy42 Oct 15, 2024
9d0b3d1
feat(mongodb): delete private network
jremy42 Oct 15, 2024
b76484d
t
jremy42 Oct 15, 2024
898ae01
feat(mongodb): add data source and docs
jremy42 Oct 15, 2024
6e79c7a
feat(mongodb): add cassette with sdk
jremy42 Oct 17, 2024
2af9383
feat(mongodb): fix tflint
jremy42 Oct 17, 2024
26ee147
feat(mongodb): add datasource doc
jremy42 Oct 17, 2024
eca169b
feat(mongodb): fix lint
jremy42 Oct 17, 2024
f1387f7
feat(mongodb): fix lint
jremy42 Oct 17, 2024
20a41fc
feat(mongodb): fix lint
jremy42 Oct 17, 2024
399024a
feat(mongodb): fix lint
jremy42 Oct 17, 2024
b9870f0
feat(mongodb): fix lint
jremy42 Oct 17, 2024
245d3f2
feat(mongodb): fix lint
jremy42 Oct 17, 2024
dfe5719
feat(mongodb): add mongodb to workflow
jremy42 Oct 17, 2024
ad14126
feat(mongodb): add test for update tag and password
jremy42 Oct 18, 2024
680795f
fix(mongodb): add trademark in docs
jremy42 Oct 21, 2024
c6a3c00
feat(mongodb): add getsnapshot in test
jremy42 Oct 22, 2024
fe47373
feat(mongodb): refacto update snapshot and instance
jremy42 Oct 23, 2024
f6b4f77
feat(mongodb): replace TM by ® in docs
jremy42 Oct 23, 2024
8891670
feat(mongodb): remove id from snapshot doc
jremy42 Oct 24, 2024
534f2de
feat(mongodb): add warning when user_name change
jremy42 Oct 24, 2024
28f270b
feat(mongodb): add id doc snapshot
jremy42 Oct 24, 2024
fca955e
Merge branch 'master' into feat/add_support_for_mondoDB
jremy42 Oct 25, 2024
6f685b5
Merge branch 'master' into feat/add_support_for_mondoDB
jremy42 Oct 28, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/acceptance-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -31,6 +31,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
Loading