@@ -7,10 +7,12 @@ import (
77 "strings"
88 "time"
99
10+ "github.com/hashicorp/go-cty/cty"
1011 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1112 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
15+ ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1416 "github.com/scaleway/scaleway-sdk-go/api/redis/v1"
1517 "github.com/scaleway/scaleway-sdk-go/scw"
1618 "github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -19,6 +21,7 @@ import (
1921 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
2022 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
2123 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
24+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
2225 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2326 "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2427)
@@ -193,6 +196,25 @@ func ResourceCluster() *schema.Resource {
193196 },
194197 },
195198 },
199+ "private_ips" : {
200+ Type : schema .TypeList ,
201+ Computed : true ,
202+ Description : "List of private IPv4 addresses associated with the resource" ,
203+ Elem : & schema.Resource {
204+ Schema : map [string ]* schema.Schema {
205+ "id" : {
206+ Type : schema .TypeString ,
207+ Computed : true ,
208+ Description : "The ID of the IPv4 address resource" ,
209+ },
210+ "address" : {
211+ Type : schema .TypeString ,
212+ Computed : true ,
213+ Description : "The private IPv4 address" ,
214+ },
215+ },
216+ },
217+ },
196218 "certificate" : {
197219 Type : schema .TypeString ,
198220 Computed : true ,
@@ -344,11 +366,56 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
344366 }
345367
346368 // set endpoints
369+ allPrivateIPs := []map [string ]interface {}(nil )
370+ diags := diag.Diagnostics {}
371+
347372 pnI , pnExists := flattenPrivateNetwork (cluster .Endpoints )
348373 if pnExists {
349374 _ = d .Set ("private_network" , pnI )
375+
376+ privateNetworkIDs := []string (nil )
377+
378+ for _ , endpoint := range cluster .Endpoints {
379+ if endpoint .PrivateNetwork != nil {
380+ privateNetworkIDs = append (privateNetworkIDs , endpoint .PrivateNetwork .ID )
381+ }
382+ }
383+
384+ resourceType := ipamAPI .ResourceTypeRedisCluster
385+
386+ region , err := zone .Region ()
387+ if err != nil {
388+ return diag .FromErr (err )
389+ }
390+
391+ for _ , privateNetworkID := range privateNetworkIDs {
392+ opts := & ipam.GetResourcePrivateIPsOptions {
393+ ResourceType : & resourceType ,
394+ PrivateNetworkID : & privateNetworkID ,
395+ ResourceID : & cluster .ID ,
396+ }
397+
398+ privateIPs , err := ipam .GetResourcePrivateIPs (ctx , m , region , opts )
399+ if err != nil {
400+ if ! httperrors .Is403 (err ) {
401+ return diag .FromErr (err )
402+ }
403+
404+ diags = append (diags , diag.Diagnostic {
405+ Severity : diag .Warning ,
406+ Summary : err .Error (),
407+ Detail : "Got 403 while reading private IP from IPAM API, please check your IAM permissions" ,
408+ AttributePath : cty .GetAttrPath ("private_ips" ),
409+ })
410+ }
411+
412+ if privateIPs != nil {
413+ allPrivateIPs = append (allPrivateIPs , privateIPs ... )
414+ }
415+ }
350416 }
351417
418+ _ = d .Set ("private_ips" , allPrivateIPs )
352419 _ = d .Set ("public_network" , flattenPublicNetwork (cluster .Endpoints ))
353420
354421 if cluster .TLSEnabled {
@@ -370,7 +437,7 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
370437 _ = d .Set ("certificate" , "" )
371438 }
372439
373- return nil
440+ return diags
374441}
375442
376443func ResourceClusterUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments