@@ -2,17 +2,21 @@ package applesilicon
22
33import (
44 "context"
5+ "fmt"
56 "time"
67
8+ "github.com/hashicorp/go-cty/cty"
79 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
810 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
911 applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1"
12+ ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
1013 "github.com/scaleway/scaleway-sdk-go/scw"
1114 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1215 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1316 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
1417 "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1518 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
19+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
1620 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1721 "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1822)
@@ -112,6 +116,26 @@ func ResourceServer() *schema.Resource {
112116 Description : "IPv4 address of the server" ,
113117 Computed : true ,
114118 },
119+ "private_ips" : {
120+ Type : schema .TypeList ,
121+ Computed : true ,
122+ Optional : true ,
123+ Description : "List of private IPv4 and IPv6 addresses associated with the server" ,
124+ Elem : & schema.Resource {
125+ Schema : map [string ]* schema.Schema {
126+ "id" : {
127+ Type : schema .TypeString ,
128+ Computed : true ,
129+ Description : "The ID of the IP address resource" ,
130+ },
131+ "address" : {
132+ Type : schema .TypeString ,
133+ Computed : true ,
134+ Description : "The private IP address" ,
135+ },
136+ },
137+ },
138+ },
115139 "vnc_url" : {
116140 Type : schema .TypeString ,
117141 Description : "VNC url use to connect remotely to the desktop GUI" ,
@@ -262,7 +286,56 @@ func ResourceAppleSiliconServerRead(ctx context.Context, d *schema.ResourceData,
262286
263287 _ = d .Set ("private_network" , flattenPrivateNetworks (pnRegion , listPrivateNetworks .ServerPrivateNetworks ))
264288
265- return nil
289+ privateNetworkIDs := make ([]string , 0 , listPrivateNetworks .TotalCount )
290+ for _ , pn := range listPrivateNetworks .ServerPrivateNetworks {
291+ privateNetworkIDs = append (privateNetworkIDs , pn .PrivateNetworkID )
292+ }
293+
294+ diags := diag.Diagnostics {}
295+ allPrivateIPs := make ([]map [string ]interface {}, 0 , listPrivateNetworks .TotalCount )
296+ authorized := true
297+
298+ for _ , privateNetworkID := range privateNetworkIDs {
299+ resourceType := ipamAPI .ResourceTypeAppleSiliconPrivateNic
300+ opts := & ipam.GetResourcePrivateIPsOptions {
301+ ResourceType : & resourceType ,
302+ PrivateNetworkID : & privateNetworkID ,
303+ ProjectID : & res .ProjectID ,
304+ }
305+
306+ privateIPs , err := ipam .GetResourcePrivateIPs (ctx , m , pnRegion , opts )
307+
308+ switch {
309+ case err == nil :
310+ allPrivateIPs = append (allPrivateIPs , privateIPs ... )
311+ case httperrors .Is403 (err ):
312+ authorized = false
313+
314+ diags = append (diags , diag.Diagnostic {
315+ Severity : diag .Warning ,
316+ Summary : "Unauthorized to read server's private IPs, please check your IAM permissions" ,
317+ Detail : err .Error (),
318+ AttributePath : cty .GetAttrPath ("private_ips" ),
319+ })
320+ default :
321+ diags = append (diags , diag.Diagnostic {
322+ Severity : diag .Warning ,
323+ Summary : fmt .Sprintf ("Unable to get private IP for server %q" , res .Name ),
324+ Detail : err .Error (),
325+ AttributePath : cty .GetAttrPath ("private_ips" ),
326+ })
327+ }
328+
329+ if ! authorized {
330+ break
331+ }
332+ }
333+
334+ if authorized {
335+ _ = d .Set ("private_ips" , allPrivateIPs )
336+ }
337+
338+ return diags
266339}
267340
268341func ResourceAppleSiliconServerUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments