@@ -7,10 +7,12 @@ import (
7
7
"strings"
8
8
"time"
9
9
10
+ "github.com/hashicorp/go-cty/cty"
10
11
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
15
+ ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
14
16
"github.com/scaleway/scaleway-sdk-go/api/redis/v1"
15
17
"github.com/scaleway/scaleway-sdk-go/scw"
16
18
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -19,6 +21,7 @@ import (
19
21
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
20
22
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
21
23
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
24
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
22
25
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
23
26
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
24
27
)
@@ -193,6 +196,25 @@ func ResourceCluster() *schema.Resource {
193
196
},
194
197
},
195
198
},
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
+ },
196
218
"certificate" : {
197
219
Type : schema .TypeString ,
198
220
Computed : true ,
@@ -344,11 +366,56 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
344
366
}
345
367
346
368
// set endpoints
369
+ allPrivateIPs := []map [string ]interface {}(nil )
370
+ diags := diag.Diagnostics {}
371
+
347
372
pnI , pnExists := flattenPrivateNetwork (cluster .Endpoints )
348
373
if pnExists {
349
374
_ = 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
+ }
350
416
}
351
417
418
+ _ = d .Set ("private_ips" , allPrivateIPs )
352
419
_ = d .Set ("public_network" , flattenPublicNetwork (cluster .Endpoints ))
353
420
354
421
if cluster .TLSEnabled {
@@ -370,7 +437,7 @@ func ResourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac
370
437
_ = d .Set ("certificate" , "" )
371
438
}
372
439
373
- return nil
440
+ return diags
374
441
}
375
442
376
443
func ResourceClusterUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments