Skip to content

Commit 58a6e77

Browse files
Mia-Crossyfodil
andauthored
feat(rdb): read instance's private IPs (#3075)
Co-authored-by: Yacine FODIL <[email protected]>
1 parent bd155b9 commit 58a6e77

File tree

7 files changed

+3353
-2163
lines changed

7 files changed

+3353
-2163
lines changed

docs/resources/rdb_instance.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-1111111111
238238
- `port` - Port in the Private Network.
239239
- `name` - Name of the endpoint.
240240
- `hostname` - Hostname of the endpoint.
241+
- `private_ip` - The private IPv4 address associated with the resource.
242+
- `id` - The ID of the IPv4 address resource.
243+
- `address` - The private IPv4 address.
241244
- `certificate` - Certificate of the Database Instance.
242245
- `organization_id` - The organization ID the Database Instance is associated with.
243246

internal/services/ipam/testdata/data-source-ipamiprdb.cassette.yaml

Lines changed: 299 additions & 201 deletions
Large diffs are not rendered by default.

internal/services/rdb/instance.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"fmt"
77
"io"
88

9+
"github.com/hashicorp/go-cty/cty"
910
"github.com/hashicorp/terraform-plugin-log/tflog"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
14+
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1315
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
1416
"github.com/scaleway/scaleway-sdk-go/scw"
1517
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -18,6 +20,7 @@ import (
1820
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1921
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
2022
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
23+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
2124
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2225
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2326
)
@@ -322,6 +325,25 @@ func ResourceInstance() *schema.Resource {
322325
Optional: true,
323326
Description: "Enable or disable encryption at rest for the database instance",
324327
},
328+
"private_ip": {
329+
Type: schema.TypeList,
330+
Computed: true,
331+
Description: "The private IPv4 address associated with the resource",
332+
Elem: &schema.Resource{
333+
Schema: map[string]*schema.Schema{
334+
"id": {
335+
Type: schema.TypeString,
336+
Computed: true,
337+
Description: "The ID of the IPv4 address resource",
338+
},
339+
"address": {
340+
Type: schema.TypeString,
341+
Computed: true,
342+
Description: "The private IPv4 address",
343+
},
344+
},
345+
},
346+
},
325347
// Common
326348
"region": regional.Schema(),
327349
"organization_id": account.OrganizationIDSchema(),
@@ -640,15 +662,51 @@ func ResourceRdbInstanceRead(ctx context.Context, d *schema.ResourceData, m inte
640662
_ = d.Set("logs_policy", flattenInstanceLogsPolicy(res.LogsPolicy))
641663

642664
// set endpoints
665+
privateIPs := make([]map[string]interface{}, 0, 1)
666+
diags := diag.Diagnostics{}
667+
643668
if pnI, pnExist := flattenPrivateNetwork(res.Endpoints); pnExist {
644669
_ = d.Set("private_network", pnI)
670+
671+
for _, endpoint := range res.Endpoints {
672+
if endpoint.PrivateNetwork == nil {
673+
continue
674+
}
675+
676+
if endpoint.PrivateNetwork.ProvisioningMode == rdb.EndpointPrivateNetworkDetailsProvisioningModeIpam {
677+
resourceType := ipamAPI.ResourceTypeRdbInstance
678+
opts := &ipam.GetResourcePrivateIPsOptions{
679+
ResourceID: &res.ID,
680+
ResourceType: &resourceType,
681+
PrivateNetworkID: &endpoint.PrivateNetwork.PrivateNetworkID,
682+
}
683+
684+
endpointPrivateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
685+
if err != nil {
686+
if !httperrors.Is403(err) {
687+
return diag.FromErr(err)
688+
}
689+
690+
diags = append(diags, diag.Diagnostic{
691+
Severity: diag.Warning,
692+
Summary: err.Error(),
693+
Detail: "Got 403 while reading private IP from IPAM API, please check your IAM permissions",
694+
AttributePath: cty.GetAttrPath("private_ip"),
695+
})
696+
}
697+
698+
privateIPs = append(privateIPs, endpointPrivateIPs...)
699+
}
700+
}
645701
}
646702

703+
_ = d.Set("private_ip", privateIPs)
704+
647705
if lbI, lbExists := flattenLoadBalancer(res.Endpoints); lbExists {
648706
_ = d.Set("load_balancer", lbI)
649707
}
650708

651-
return nil
709+
return diags
652710
}
653711

654712
//gocyclo:ignore

internal/services/rdb/instance_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ func TestAccInstance_Endpoints(t *testing.T) {
990990
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "load_balancer.#", "0"),
991991
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "endpoint_ip", ""), // Deprecated attribute, might be deleted later
992992
resource.TestCheckResourceAttr("scaleway_rdb_instance.test_endpoints", "endpoint_port", "0"), // Deprecated attribute, might be deleted later
993+
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.test_endpoints", "private_ip.0.id"),
994+
resource.TestCheckResourceAttrSet("scaleway_rdb_instance.test_endpoints", "private_ip.0.address"),
993995
),
994996
},
995997
{

internal/services/rdb/testdata/instance-endpoints.cassette.yaml

Lines changed: 805 additions & 511 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)