Skip to content

Commit 7fd125e

Browse files
authored
feat(mongodb): read instance private ips (#3109)
* feat(mongodb): read instance private ips * fix
1 parent 3caad29 commit 7fd125e

File tree

5 files changed

+4143
-1513
lines changed

5 files changed

+4143
-1513
lines changed

docs/resources/mongodb_instance.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ In addition to all arguments above, the following attributes are exported:
119119
- `ips` - List of IP addresses for your endpoint.
120120
- `port` - TCP port of the endpoint.
121121
- `dns_records` - List of DNS records for your endpoint.
122+
- `private_ip` - The private IPv4 address associated with the instance.
123+
- `id` - The ID of the IPv4 address resource.
124+
- `address` - The private IPv4 address.
122125
- `public_network` - Private Network endpoints of the Database Instance.
123126
- `id` - The ID of the endpoint.
124127
- `port` - TCP port of the endpoint.

internal/services/mongodb/instance.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package mongodb
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"time"
78

9+
"github.com/hashicorp/go-cty/cty"
810
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1114
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
1215
"github.com/scaleway/scaleway-sdk-go/scw"
1316
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
@@ -16,6 +19,7 @@ import (
1619
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1720
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1821
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
22+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1923
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2024
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2125
)
@@ -152,6 +156,26 @@ func ResourceInstance() *schema.Resource {
152156
},
153157
},
154158
// Computed
159+
"private_ip": {
160+
Type: schema.TypeList,
161+
Computed: true,
162+
Optional: true,
163+
Description: "The private IPv4 address associated with the resource",
164+
Elem: &schema.Resource{
165+
Schema: map[string]*schema.Schema{
166+
"id": {
167+
Type: schema.TypeString,
168+
Computed: true,
169+
Description: "The ID of the IPv4 address resource",
170+
},
171+
"address": {
172+
Type: schema.TypeString,
173+
Computed: true,
174+
Description: "The private IPv4 address",
175+
},
176+
},
177+
},
178+
},
155179
"public_network": {
156180
Type: schema.TypeList,
157181
Optional: true,
@@ -362,10 +386,59 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
362386
_ = d.Set("public_network", publicNetworkEndpoint)
363387
}
364388

389+
diags := diag.Diagnostics{}
390+
privateIPs := []map[string]interface{}(nil)
391+
authorized := true
392+
365393
privateNetworkEndpoint, privateNetworkExists := flattenPrivateNetwork(instance.Endpoints)
366394

367395
if privateNetworkExists {
368396
_ = d.Set("private_network", privateNetworkEndpoint)
397+
398+
for _, endpoint := range instance.Endpoints {
399+
if endpoint.PrivateNetwork == nil {
400+
continue
401+
}
402+
403+
resourceType := ipamAPI.ResourceTypeMgdbInstance
404+
opts := &ipam.GetResourcePrivateIPsOptions{
405+
ResourceID: &instance.ID,
406+
ResourceType: &resourceType,
407+
PrivateNetworkID: &endpoint.PrivateNetwork.PrivateNetworkID,
408+
ProjectID: &instance.ProjectID,
409+
}
410+
411+
endpointPrivateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
412+
413+
switch {
414+
case err == nil:
415+
privateIPs = append(privateIPs, endpointPrivateIPs...)
416+
case httperrors.Is403(err):
417+
authorized = false
418+
419+
diags = append(diags, diag.Diagnostic{
420+
Severity: diag.Warning,
421+
Summary: "Unauthorized to read MongoDB Instance's private IP, please check your IAM permissions",
422+
Detail: err.Error(),
423+
AttributePath: cty.GetAttrPath("private_ip"),
424+
})
425+
default:
426+
diags = append(diags, diag.Diagnostic{
427+
Severity: diag.Warning,
428+
Summary: fmt.Sprintf("Unable to get private IP for instance %q", instance.Name),
429+
Detail: err.Error(),
430+
AttributePath: cty.GetAttrPath("private_ip"),
431+
})
432+
}
433+
434+
if !authorized {
435+
break
436+
}
437+
}
438+
}
439+
440+
if authorized {
441+
_ = d.Set("private_ip", privateIPs)
369442
}
370443

371444
if len(instance.Settings) > 0 {
@@ -377,7 +450,7 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
377450
_ = d.Set("settings", settingsMap)
378451
}
379452

380-
return nil
453+
return diags
381454
}
382455

383456
func ResourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

internal/services/mongodb/instance_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ func TestAccMongoDBInstance_WithPrivateNetwork(t *testing.T) {
245245
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
246246
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.port"),
247247
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.dns_records.0"),
248+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
249+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
248250
),
249251
},
250252
},
@@ -309,6 +311,8 @@ func TestAccMongoDBInstance_UpdatePrivateNetwork(t *testing.T) {
309311
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "private_network.#", "1"),
310312
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.pn_id"),
311313
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
314+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
315+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
312316
),
313317
},
314318
{
@@ -341,6 +345,8 @@ func TestAccMongoDBInstance_UpdatePrivateNetwork(t *testing.T) {
341345
resource.TestCheckResourceAttr("scaleway_mongodb_instance.main", "private_network.#", "1"),
342346
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.pn_id"),
343347
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_network.0.id"),
348+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.id"),
349+
resource.TestCheckResourceAttrSet("scaleway_mongodb_instance.main", "private_ip.0.address"),
344350
),
345351
},
346352
{

0 commit comments

Comments
 (0)