Skip to content

Commit 77939e4

Browse files
authored
feat(inference): read deployment private ip (#3114)
* feat(inference): read deployment private ip * fix
1 parent eab54f0 commit 77939e4

File tree

4 files changed

+909
-439
lines changed

4 files changed

+909
-439
lines changed

docs/resources/inference_deployment.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ In addition to all arguments above, the following attributes are exported:
6060
- `private_endpoint` - Private endpoint's attributes.
6161
- `id` - (Optional) The id of the private endpoint.
6262
- `url` - (Optional) The URL of the endpoint.
63+
- `private_ip` - The private IPv4 address associated with the deployment.
64+
- `id` - The ID of the IPv4 address resource.
65+
- `address` - The private IPv4 address.
6366
- `public_endpoint` - (Optional) Public endpoint's attributes.
6467
- `id` - (Optional) The id of the public endpoint.
6568
- `url` - (Optional) The URL of the endpoint.

internal/services/inference/deployment.go

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package inference
22

33
import (
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

331402
func ResourceDeploymentUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {

internal/services/inference/deployment_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func TestAccDeployment_Endpoint(t *testing.T) {
8686
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-endpoint-private"),
8787
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "node_type", "L4"),
8888
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_network_id", "scaleway_vpc_private_network.pn01", "id"),
89+
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.id"),
90+
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.address"),
8991
),
9092
},
9193
{
@@ -116,6 +118,8 @@ func TestAccDeployment_Endpoint(t *testing.T) {
116118
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-basic-endpoints-private-public"),
117119
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "public_endpoint.0.is_enabled", "true"),
118120
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_network_id", "scaleway_vpc_private_network.pn01", "id"),
121+
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.id"),
122+
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.address"),
119123
),
120124
},
121125
},

internal/services/inference/testdata/deployment-endpoint.cassette.yaml

Lines changed: 828 additions & 436 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)