@@ -3,11 +3,14 @@ package mongodb
33import (
44 "context"
55 "errors"
6+ "fmt"
67 "time"
78
9+ "github.com/hashicorp/go-cty/cty"
810 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
911 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1012 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13+ ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1114 mongodb "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1alpha1"
1215 "github.com/scaleway/scaleway-sdk-go/scw"
1316 "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
@@ -16,6 +19,7 @@ import (
1619 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1720 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1821 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
22+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1923 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
2024 "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
2125)
@@ -152,6 +156,26 @@ func ResourceInstance() *schema.Resource {
152156 },
153157 },
154158 // 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+ },
155179 "public_network" : {
156180 Type : schema .TypeList ,
157181 Optional : true ,
@@ -362,10 +386,59 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
362386 _ = d .Set ("public_network" , publicNetworkEndpoint )
363387 }
364388
389+ diags := diag.Diagnostics {}
390+ privateIPs := []map [string ]interface {}(nil )
391+ authorized := true
392+
365393 privateNetworkEndpoint , privateNetworkExists := flattenPrivateNetwork (instance .Endpoints )
366394
367395 if privateNetworkExists {
368396 _ = 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 )
369442 }
370443
371444 if len (instance .Settings ) > 0 {
@@ -377,7 +450,7 @@ func ResourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
377450 _ = d .Set ("settings" , settingsMap )
378451 }
379452
380- return nil
453+ return diags
381454}
382455
383456func ResourceInstanceUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments