@@ -2,17 +2,21 @@ package inference
22
33import (
44 "context"
5+ "fmt"
56
7+ "github.com/hashicorp/go-cty/cty"
68 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
911 "github.com/scaleway/scaleway-sdk-go/api/inference/v1"
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/dsf"
1215 "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1316 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1417 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
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)
1822
@@ -111,7 +115,6 @@ func ResourceDeployment() *schema.Resource {
111115 Computed : true ,
112116 Description : "The date and time of the last update of the deployment" ,
113117 },
114-
115118 "private_endpoint" : {
116119 Type : schema .TypeList ,
117120 Optional : true ,
@@ -144,7 +147,6 @@ func ResourceDeployment() *schema.Resource {
144147 },
145148 },
146149 },
147-
148150 "public_endpoint" : {
149151 Type : schema .TypeList ,
150152 Optional : true ,
@@ -177,6 +179,26 @@ func ResourceDeployment() *schema.Resource {
177179 },
178180 },
179181 },
182+ "private_ip" : {
183+ Type : schema .TypeList ,
184+ Computed : true ,
185+ Optional : true ,
186+ Description : "The private IPv4 address associated with the deployment" ,
187+ Elem : & schema.Resource {
188+ Schema : map [string ]* schema.Schema {
189+ "id" : {
190+ Type : schema .TypeString ,
191+ Computed : true ,
192+ Description : "The ID of the IPv4 address resource" ,
193+ },
194+ "address" : {
195+ Type : schema .TypeString ,
196+ Computed : true ,
197+ Description : "The private IPv4 address" ,
198+ },
199+ },
200+ },
201+ },
180202 },
181203 }
182204}
@@ -317,15 +339,64 @@ func ResourceDeploymentRead(ctx context.Context, d *schema.ResourceData, m inter
317339 }
318340 }
319341
342+ diags := diag.Diagnostics {}
343+ privateIPs := []map [string ]interface {}(nil )
344+ authorized := true
345+
320346 if privateEndpoints != nil {
321347 _ = d .Set ("private_endpoint" , privateEndpoints )
348+
349+ for _ , endpoint := range deployment .Endpoints {
350+ if endpoint .PrivateNetwork == nil {
351+ continue
352+ }
353+
354+ resourceType := ipamAPI .ResourceTypeLlmDeployment
355+ opts := & ipam.GetResourcePrivateIPsOptions {
356+ ResourceID : & deployment .ID ,
357+ ResourceType : & resourceType ,
358+ PrivateNetworkID : & endpoint .PrivateNetwork .PrivateNetworkID ,
359+ ProjectID : & deployment .ProjectID ,
360+ }
361+
362+ endpointPrivateIPs , err := ipam .GetResourcePrivateIPs (ctx , m , region , opts )
363+
364+ switch {
365+ case err == nil :
366+ privateIPs = append (privateIPs , endpointPrivateIPs ... )
367+ case httperrors .Is403 (err ):
368+ authorized = false
369+
370+ diags = append (diags , diag.Diagnostic {
371+ Severity : diag .Warning ,
372+ Summary : "Unauthorized to read deployment's private IP, please check your IAM permissions" ,
373+ Detail : err .Error (),
374+ AttributePath : cty .GetAttrPath ("private_ip" ),
375+ })
376+ default :
377+ diags = append (diags , diag.Diagnostic {
378+ Severity : diag .Warning ,
379+ Summary : fmt .Sprintf ("Unable to get private IP for deployment %q" , deployment .Name ),
380+ Detail : err .Error (),
381+ AttributePath : cty .GetAttrPath ("private_ip" ),
382+ })
383+ }
384+
385+ if ! authorized {
386+ break
387+ }
388+ }
389+ }
390+
391+ if authorized {
392+ _ = d .Set ("private_ip" , privateIPs )
322393 }
323394
324395 if publicEndpoints != nil {
325396 _ = d .Set ("public_endpoint" , publicEndpoints )
326397 }
327398
328- return nil
399+ return diags
329400}
330401
331402func ResourceDeploymentUpdate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments