From 1a2c3dd2279260e4cad7c2ecbc0c5208f1843481 Mon Sep 17 00:00:00 2001 From: Jonathan Remy Date: Wed, 3 Sep 2025 16:58:15 +0200 Subject: [PATCH 1/2] fix(mongodb): use regional ID parsing instead of zonal --- docs/data-sources/mongodb_instance.md | 4 +- docs/resources/mongodb_instance.md | 6 ++- docs/resources/mongodb_snapshot.md | 4 +- .../services/mongodb/data_source_instance.go | 8 ++-- internal/services/mongodb/helpers.go | 44 ++----------------- internal/services/mongodb/instance.go | 5 +-- internal/services/mongodb/instance_test.go | 7 +-- internal/services/mongodb/snapshot.go | 13 +++--- 8 files changed, 25 insertions(+), 66 deletions(-) diff --git a/docs/data-sources/mongodb_instance.md b/docs/data-sources/mongodb_instance.md index f37427cdc4..a0ba470e09 100644 --- a/docs/data-sources/mongodb_instance.md +++ b/docs/data-sources/mongodb_instance.md @@ -39,7 +39,7 @@ output "mongodb_version" { - `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. +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) 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. @@ -64,5 +64,5 @@ In addition to all above arguments, the following attributes are exported: 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 +terraform import scaleway_mongodb_instance.main fr-par/11111111-1111-1111-1111-111111111111 ``` diff --git a/docs/resources/mongodb_instance.md b/docs/resources/mongodb_instance.md index a4b550657e..81d1ec9ff9 100644 --- a/docs/resources/mongodb_instance.md +++ b/docs/resources/mongodb_instance.md @@ -104,6 +104,9 @@ The following arguments are supported: - `private_network` - (Optional) Private Network endpoints of the Database Instance. - `pn_id` - (Required) The ID of the Private Network. - `public_network` - (Optional) Public network endpoint configuration (no arguments). +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® instance should be created. +- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the MongoDB® instance is associated with. + ~> **Important** If neither private_network nor public_network is specified, a public network endpoint is created by default. @@ -114,6 +117,7 @@ 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. +- `region` - The region 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. @@ -133,5 +137,5 @@ In addition to all arguments above, the following attributes are exported: 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 +terraform import scaleway_mongodb_instance.main fr-par/11111111-1111-1111-1111-111111111111 ``` diff --git a/docs/resources/mongodb_snapshot.md b/docs/resources/mongodb_snapshot.md index 270eee0f80..ddb6020438 100644 --- a/docs/resources/mongodb_snapshot.md +++ b/docs/resources/mongodb_snapshot.md @@ -33,7 +33,7 @@ The following arguments are supported: ~> **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. +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® snapshot should be created. ## Attributes Reference @@ -58,5 +58,5 @@ In addition to all arguments above, the following attributes are exported: 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 +terraform import scaleway_mongodb_snapshot.main fr-par/11111111-1111-1111-1111-111111111111 ``` diff --git a/internal/services/mongodb/data_source_instance.go b/internal/services/mongodb/data_source_instance.go index 3b707064be..948009c945 100644 --- a/internal/services/mongodb/data_source_instance.go +++ b/internal/services/mongodb/data_source_instance.go @@ -35,7 +35,7 @@ func DataSourceInstance() *schema.Resource { } func DataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { - mongodbAPI, zone, region, err := newAPIWithZoneAndRegion(d, m) + mongodbAPI, region, err := newAPIWithRegion(d, m) if err != nil { return diag.FromErr(err) } @@ -65,10 +65,10 @@ func DataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m any) instanceID = foundInstance.ID } - zonedID := datasource.NewZonedID(instanceID, zone) - d.SetId(zonedID) + regionalID := datasource.NewRegionalID(instanceID, region) + d.SetId(regionalID) - err = d.Set("instance_id", zonedID) + err = d.Set("instance_id", regionalID) if err != nil { return diag.FromErr(err) } diff --git a/internal/services/mongodb/helpers.go b/internal/services/mongodb/helpers.go index ce321a5cc5..f0be2b4a8a 100644 --- a/internal/services/mongodb/helpers.go +++ b/internal/services/mongodb/helpers.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1" "github.com/scaleway/scaleway-sdk-go/scw" - "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) @@ -26,30 +26,6 @@ func newAPI(m any) *mongodb.API { return mongodb.NewAPI(meta.ExtractScwClient(m)) } -// newAPIWithZone returns a new mongoDB API and the zone for a Create request -func newAPIWithZone(d *schema.ResourceData, m any) (*mongodb.API, scw.Zone, error) { - zone, err := meta.ExtractZone(d, m) - if err != nil { - return nil, "", err - } - - return newAPI(m), zone, nil -} - -func newAPIWithZoneAndRegion(d *schema.ResourceData, m any) (*mongodb.API, scw.Zone, scw.Region, error) { - zone, err := meta.ExtractZone(d, m) - if err != nil { - return nil, "", "", err - } - - region, err := meta.ExtractRegion(d, m) - if err != nil { - return nil, "", "", err - } - - return newAPI(m), zone, region, nil -} - func newAPIWithRegion(d *schema.ResourceData, m any) (*mongodb.API, scw.Region, error) { region, err := meta.ExtractRegion(d, m) if err != nil { @@ -59,23 +35,9 @@ func newAPIWithRegion(d *schema.ResourceData, m any) (*mongodb.API, scw.Region, return newAPI(m), region, nil } -// NewAPIWithZoneAndID returns a mongoDB API with zone and ID extracted from the state -func NewAPIWithZoneAndID(m any, id string) (*mongodb.API, scw.Zone, string, error) { - zone, ID, err := zonal.ParseID(id) - if err != nil { - return nil, "", "", err - } - - return newAPI(m), zone, ID, nil -} - +// NewAPIWithRegionAndID returns a mongoDB API with region and ID extracted from the state func NewAPIWithRegionAndID(m any, id string) (*mongodb.API, scw.Region, string, error) { - zone, ID, err := zonal.ParseID(id) - if err != nil { - return nil, "", "", err - } - - region, err := zone.Region() + region, ID, err := regional.ParseID(id) if err != nil { return nil, "", "", err } diff --git a/internal/services/mongodb/instance.go b/internal/services/mongodb/instance.go index d8e805b415..b0a3dec19c 100644 --- a/internal/services/mongodb/instance.go +++ b/internal/services/mongodb/instance.go @@ -20,7 +20,6 @@ import ( "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/services/ipam" "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" @@ -263,7 +262,7 @@ func ResourceInstance() *schema.Resource { } func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { - mongodbAPI, zone, err := newAPIWithZone(d, m) + mongodbAPI, region, err := newAPIWithRegion(d, m) if err != nil { return diag.FromErr(err) } @@ -361,7 +360,7 @@ func ResourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m any) } } - d.SetId(zonal.NewIDString(zone, res.ID)) + d.SetId(regional.NewIDString(region, res.ID)) _, err = waitForInstance(ctx, mongodbAPI, res.Region, res.ID, d.Timeout(schema.TimeoutCreate)) if err != nil { diff --git a/internal/services/mongodb/instance_test.go b/internal/services/mongodb/instance_test.go index 8704c90ee3..4bf6dd58d0 100644 --- a/internal/services/mongodb/instance_test.go +++ b/internal/services/mongodb/instance_test.go @@ -502,12 +502,7 @@ func IsInstanceDestroyed(tt *acctest.TestTools) resource.TestCheckFunc { continue } - mongodbAPI, zone, ID, err := mongodb.NewAPIWithZoneAndID(tt.Meta, rs.Primary.ID) - if err != nil { - return err - } - - extractRegion, err := zone.Region() + mongodbAPI, extractRegion, ID, err := mongodb.NewAPIWithRegionAndID(tt.Meta, rs.Primary.ID) if err != nil { return err } diff --git a/internal/services/mongodb/snapshot.go b/internal/services/mongodb/snapshot.go index ee33230aba..57ddc765b6 100644 --- a/internal/services/mongodb/snapshot.go +++ b/internal/services/mongodb/snapshot.go @@ -10,7 +10,6 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" "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/types" "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" ) @@ -86,7 +85,7 @@ func ResourceSnapshot() *schema.Resource { } func ResourceSnapshotCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { - mongodbAPI, zone, region, err := newAPIWithZoneAndRegion(d, m) + mongodbAPI, region, err := newAPIWithRegion(d, m) if err != nil { return diag.FromErr(err) } @@ -105,7 +104,7 @@ func ResourceSnapshotCreate(ctx context.Context, d *schema.ResourceData, m any) } if snapshot != nil { - d.SetId(zonal.NewIDString(zone, snapshot.ID)) + d.SetId(regional.NewIDString(region, snapshot.ID)) _, err = waitForSnapshot(ctx, mongodbAPI, region, instanceID, snapshot.ID, d.Timeout(schema.TimeoutCreate)) if err != nil { @@ -122,7 +121,7 @@ func ResourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m any) di return diag.FromErr(err) } - zone, snapshotID, err := zonal.ParseID(d.Id()) + _, snapshotID, err := regional.ParseID(d.Id()) if err != nil { return diag.FromErr(err) } @@ -134,7 +133,7 @@ func ResourceSnapshotRead(ctx context.Context, d *schema.ResourceData, m any) di return diag.FromErr(err) } - _ = d.Set("instance_id", zonal.NewIDString(zone, *snapshot.InstanceID)) + _ = d.Set("instance_id", regional.NewIDString(region, *snapshot.InstanceID)) _ = d.Set("name", snapshot.Name) _ = d.Set("instance_name", snapshot.InstanceName) _ = d.Set("size", int64(snapshot.SizeBytes)) @@ -154,7 +153,7 @@ func ResourceSnapshotUpdate(ctx context.Context, d *schema.ResourceData, m any) return diag.FromErr(err) } - _, snapshotID, err := zonal.ParseID(d.Id()) + _, snapshotID, err := regional.ParseID(d.Id()) if err != nil { return diag.FromErr(err) } @@ -200,7 +199,7 @@ func ResourceSnapshotDelete(_ context.Context, d *schema.ResourceData, m any) di return diag.FromErr(err) } - _, snapshotID, err := zonal.ParseID(d.Id()) + _, snapshotID, err := regional.ParseID(d.Id()) if err != nil { return diag.FromErr(err) } From 066cdb1fc612f938ff0aae2a36c35a66b0ea97af Mon Sep 17 00:00:00 2001 From: Jonathan Remy Date: Thu, 4 Sep 2025 05:47:15 +0200 Subject: [PATCH 2/2] docs: fix MongoDB documentation templates --- templates/data-sources/mongodb_instance.md.tmpl | 4 ++-- templates/resources/mongodb_instance.md.tmpl | 6 +++++- templates/resources/mongodb_snapshot.md.tmpl | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/templates/data-sources/mongodb_instance.md.tmpl b/templates/data-sources/mongodb_instance.md.tmpl index f37427cdc4..a0ba470e09 100644 --- a/templates/data-sources/mongodb_instance.md.tmpl +++ b/templates/data-sources/mongodb_instance.md.tmpl @@ -39,7 +39,7 @@ output "mongodb_version" { - `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. +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) 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. @@ -64,5 +64,5 @@ In addition to all above arguments, the following attributes are exported: 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 +terraform import scaleway_mongodb_instance.main fr-par/11111111-1111-1111-1111-111111111111 ``` diff --git a/templates/resources/mongodb_instance.md.tmpl b/templates/resources/mongodb_instance.md.tmpl index a4b550657e..81d1ec9ff9 100644 --- a/templates/resources/mongodb_instance.md.tmpl +++ b/templates/resources/mongodb_instance.md.tmpl @@ -104,6 +104,9 @@ The following arguments are supported: - `private_network` - (Optional) Private Network endpoints of the Database Instance. - `pn_id` - (Required) The ID of the Private Network. - `public_network` - (Optional) Public network endpoint configuration (no arguments). +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® instance should be created. +- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the MongoDB® instance is associated with. + ~> **Important** If neither private_network nor public_network is specified, a public network endpoint is created by default. @@ -114,6 +117,7 @@ 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. +- `region` - The region 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. @@ -133,5 +137,5 @@ In addition to all arguments above, the following attributes are exported: 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 +terraform import scaleway_mongodb_instance.main fr-par/11111111-1111-1111-1111-111111111111 ``` diff --git a/templates/resources/mongodb_snapshot.md.tmpl b/templates/resources/mongodb_snapshot.md.tmpl index 270eee0f80..ddb6020438 100644 --- a/templates/resources/mongodb_snapshot.md.tmpl +++ b/templates/resources/mongodb_snapshot.md.tmpl @@ -33,7 +33,7 @@ The following arguments are supported: ~> **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. +- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) in which the MongoDB® snapshot should be created. ## Attributes Reference @@ -58,5 +58,5 @@ In addition to all arguments above, the following attributes are exported: 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 +terraform import scaleway_mongodb_snapshot.main fr-par/11111111-1111-1111-1111-111111111111 ```