@@ -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
0 commit comments