@@ -3,11 +3,14 @@ package mongodb
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"time"
7
8
9
+ "github.com/hashicorp/go-cty/cty"
8
10
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
+ ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
11
14
mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
12
15
"github.com/scaleway/scaleway-sdk-go/scw"
13
16
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
@@ -16,6 +19,7 @@ import (
16
19
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
17
20
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
18
21
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
22
+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
19
23
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
20
24
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
21
25
)
@@ -152,6 +156,26 @@ func ResourceInstance() *schema.Resource {
152
156
},
153
157
},
154
158
// 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
+ },
155
179
"public_network" : {
156
180
Type : schema .TypeList ,
157
181
Optional : true ,
@@ -362,10 +386,59 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
362
386
_ = d .Set ("public_network" , publicNetworkEndpoint )
363
387
}
364
388
389
+ diags := diag.Diagnostics {}
390
+ privateIPs := []map [string ]interface {}(nil )
391
+ authorized := true
392
+
365
393
privateNetworkEndpoint , privateNetworkExists := flattenPrivateNetwork (instance .Endpoints )
366
394
367
395
if privateNetworkExists {
368
396
_ = 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 )
369
442
}
370
443
371
444
if len (instance .Settings ) > 0 {
@@ -377,7 +450,7 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
377
450
_ = d .Set ("settings" , settingsMap )
378
451
}
379
452
380
- return nil
453
+ return diags
381
454
}
382
455
383
456
func ResourceInstanceUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments