Skip to content

feat(inference): read deployment private ip #3114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/resources/inference_deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ In addition to all arguments above, the following attributes are exported:
- `private_endpoint` - Private endpoint's attributes.
- `id` - (Optional) The id of the private endpoint.
- `url` - (Optional) The URL of the endpoint.
- `private_ip` - The private IPv4 address associated with the deployment.
- `id` - The ID of the IPv4 address resource.
- `address` - The private IPv4 address.
- `public_endpoint` - (Optional) Public endpoint's attributes.
- `id` - (Optional) The id of the public endpoint.
- `url` - (Optional) The URL of the endpoint.
Expand Down
77 changes: 74 additions & 3 deletions internal/services/inference/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package inference

import (
"context"
"fmt"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/api/inference/v1"
ipamAPI "github.com/scaleway/scaleway-sdk-go/api/ipam/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/ipam"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)

Expand Down Expand Up @@ -111,7 +115,6 @@ func ResourceDeployment() *schema.Resource {
Computed: true,
Description: "The date and time of the last update of the deployment",
},

"private_endpoint": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -144,7 +147,6 @@ func ResourceDeployment() *schema.Resource {
},
},
},

"public_endpoint": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -177,6 +179,26 @@ func ResourceDeployment() *schema.Resource {
},
},
},
"private_ip": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "The private IPv4 address associated with the deployment",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the IPv4 address resource",
},
"address": {
Type: schema.TypeString,
Computed: true,
Description: "The private IPv4 address",
},
},
},
},
},
}
}
Expand Down Expand Up @@ -317,15 +339,64 @@ func ResourceDeploymentRead(ctx context.Context, d *schema.ResourceData, m inter
}
}

diags := diag.Diagnostics{}
privateIPs := []map[string]interface{}(nil)
authorized := true

if privateEndpoints != nil {
_ = d.Set("private_endpoint", privateEndpoints)

for _, endpoint := range deployment.Endpoints {
if endpoint.PrivateNetwork == nil {
continue
}

resourceType := ipamAPI.ResourceTypeLlmDeployment
opts := &ipam.GetResourcePrivateIPsOptions{
ResourceID: &deployment.ID,
ResourceType: &resourceType,
PrivateNetworkID: &endpoint.PrivateNetwork.PrivateNetworkID,
ProjectID: &deployment.ProjectID,
}

endpointPrivateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)

switch {
case err == nil:
privateIPs = append(privateIPs, endpointPrivateIPs...)
case httperrors.Is403(err):
authorized = false

diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Unauthorized to read deployment's private IP, please check your IAM permissions",
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ip"),
})
default:
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Unable to get private IP for deployment %q", deployment.Name),
Detail: err.Error(),
AttributePath: cty.GetAttrPath("private_ip"),
})
}

if !authorized {
break
}
}
}

if authorized {
_ = d.Set("private_ip", privateIPs)
}

if publicEndpoints != nil {
_ = d.Set("public_endpoint", publicEndpoints)
}

return nil
return diags
}

func ResourceDeploymentUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
4 changes: 4 additions & 0 deletions internal/services/inference/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func TestAccDeployment_Endpoint(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-endpoint-private"),
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "node_type", "L4"),
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_network_id", "scaleway_vpc_private_network.pn01", "id"),
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.address"),
),
},
{
Expand Down Expand Up @@ -116,6 +118,8 @@ func TestAccDeployment_Endpoint(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-basic-endpoints-private-public"),
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "public_endpoint.0.is_enabled", "true"),
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_network_id", "scaleway_vpc_private_network.pn01", "id"),
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.id"),
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "private_ip.0.address"),
),
},
},
Expand Down
1,264 changes: 828 additions & 436 deletions internal/services/inference/testdata/deployment-endpoint.cassette.yaml

Large diffs are not rendered by default.

Loading