Skip to content

Commit 713c990

Browse files
committed
fix endpoints
1 parent 15598f3 commit 713c990

File tree

4 files changed

+499
-306
lines changed

4 files changed

+499
-306
lines changed

internal/services/inference/deployment.go

Lines changed: 105 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package inference
22

33
import (
44
"context"
5-
65
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
76
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
87
inference "github.com/scaleway/scaleway-sdk-go/api/inference/v1beta1"
@@ -88,36 +87,6 @@ func ResourceDeployment() *schema.Resource {
8887
Computed: true,
8988
Description: "The status of the deployment",
9089
},
91-
"endpoint_public_url": {
92-
Type: schema.TypeString,
93-
Computed: true,
94-
Description: "The endpoint public URL",
95-
},
96-
"endpoint_private_url": {
97-
Type: schema.TypeString,
98-
Computed: true,
99-
Description: "The endpoint private URL",
100-
},
101-
"disable_auth_private": {
102-
Type: schema.TypeBool,
103-
Computed: true,
104-
Description: "Whether or not the authentication on the private endpoint is disabled.",
105-
},
106-
"endpoint_public_id": {
107-
Type: schema.TypeString,
108-
Computed: true,
109-
Description: "The endpoint public ID",
110-
},
111-
"endpoint_private_id": {
112-
Type: schema.TypeString,
113-
Computed: true,
114-
Description: "The endpoint private ID",
115-
},
116-
"disable_auth_public": {
117-
Type: schema.TypeBool,
118-
Computed: true,
119-
Description: "Whether or not the authentication on the public endpoint is disabled.",
120-
},
12190
"created_at": {
12291
Type: schema.TypeString,
12392
Computed: true,
@@ -128,16 +97,19 @@ func ResourceDeployment() *schema.Resource {
12897
Computed: true,
12998
Description: "The date and time of the last update of the deployment",
13099
},
131-
"endpoints": {
132-
Type: schema.TypeList,
133-
Required: true,
134-
Description: "List of endpoints",
100+
101+
"private_endpoint": {
102+
Type: schema.TypeList,
103+
Optional: true,
104+
MaxItems: 1,
105+
AtLeastOneOf: []string{"public_endpoint"},
106+
Description: "List of endpoints",
135107
Elem: &schema.Resource{
136108
Schema: map[string]*schema.Schema{
137-
"public_endpoint": {
138-
Type: schema.TypeBool,
139-
Description: "Set the endpoint as public",
140-
Optional: true,
109+
"id": {
110+
Type: schema.TypeString,
111+
Description: "The id of the private endpoint",
112+
Computed: true,
141113
},
142114
"private_endpoint_id": {
143115
Type: schema.TypeString,
@@ -150,6 +122,44 @@ func ResourceDeployment() *schema.Resource {
150122
Optional: true,
151123
Default: false,
152124
},
125+
"url": {
126+
Type: schema.TypeString,
127+
Description: "The URL of the endpoint.",
128+
Computed: true,
129+
},
130+
},
131+
},
132+
},
133+
134+
"public_endpoint": {
135+
Type: schema.TypeList,
136+
Optional: true,
137+
AtLeastOneOf: []string{"private_endpoint"},
138+
Description: "Public endpoints",
139+
MaxItems: 1,
140+
Elem: &schema.Resource{
141+
Schema: map[string]*schema.Schema{
142+
"id": {
143+
Type: schema.TypeString,
144+
Description: "The id of the public endpoint",
145+
Computed: true,
146+
},
147+
"is_enabled": {
148+
Type: schema.TypeBool,
149+
Description: "Set the endpoint as public",
150+
Optional: true,
151+
},
152+
"disable_auth": {
153+
Type: schema.TypeBool,
154+
Description: "Disable the authentication on the endpoint.",
155+
Optional: true,
156+
Default: false,
157+
},
158+
"url": {
159+
Type: schema.TypeString,
160+
Description: "The URL of the endpoint.",
161+
Computed: true,
162+
},
153163
},
154164
},
155165
},
@@ -170,27 +180,9 @@ func ResourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, m int
170180
NodeType: d.Get("node_type").(string),
171181
ModelName: d.Get("model_name").(string),
172182
Tags: types.ExpandStrings(d.Get("tags")),
183+
Endpoints: buildEndpoints(d),
173184
}
174185

175-
endpoint := inference.EndpointSpec{
176-
Public: nil,
177-
PrivateNetwork: nil,
178-
DisableAuth: false,
179-
}
180-
181-
if _, isEndpoint := d.GetOk("endpoints"); isEndpoint {
182-
if publicEndpoint := d.Get("endpoints.0.public_endpoint"); publicEndpoint != nil && publicEndpoint.(bool) {
183-
endpoint.Public = &inference.EndpointSpecPublic{}
184-
}
185-
if privateEndpoint := d.Get("endpoints.0.private_endpoint_id"); privateEndpoint != "" {
186-
endpoint.PrivateNetwork = &inference.EndpointSpecPrivateNetwork{
187-
PrivateNetworkID: regional.ExpandID(privateEndpoint.(string)).ID,
188-
}
189-
}
190-
}
191-
192-
req.Endpoints = []*inference.EndpointSpec{&endpoint}
193-
194186
if isAcceptingEula, ok := d.GetOk("accept_eula"); ok {
195187
req.AcceptEula = scw.BoolPtr(isAcceptingEula.(bool))
196188
}
@@ -210,6 +202,36 @@ func ResourceDeploymentCreate(ctx context.Context, d *schema.ResourceData, m int
210202
return ResourceDeploymentRead(ctx, d, m)
211203
}
212204

205+
func buildEndpoints(d *schema.ResourceData) []*inference.EndpointSpec {
206+
var endpoints []*inference.EndpointSpec
207+
208+
if publicEndpoint, ok := d.GetOk("public_endpoint"); ok {
209+
publicEndpointMap := publicEndpoint.([]interface{})[0].(map[string]interface{})
210+
if publicEndpointMap["is_enabled"].(bool) {
211+
publicEp := inference.EndpointSpec{
212+
Public: &inference.EndpointSpecPublic{},
213+
DisableAuth: publicEndpointMap["disable_auth"].(bool),
214+
}
215+
endpoints = append(endpoints, &publicEp)
216+
}
217+
}
218+
219+
if privateEndpoint, ok := d.GetOk("private_endpoint"); ok {
220+
privateEndpointMap := privateEndpoint.([]interface{})[0].(map[string]interface{})
221+
if privateID, exists := privateEndpointMap["private_endpoint_id"]; exists {
222+
privateEp := inference.EndpointSpec{
223+
PrivateNetwork: &inference.EndpointSpecPrivateNetwork{
224+
PrivateNetworkID: regional.ExpandID(privateID.(string)).ID,
225+
},
226+
DisableAuth: privateEndpointMap["disable_auth"].(bool),
227+
}
228+
endpoints = append(endpoints, &privateEp)
229+
}
230+
}
231+
232+
return endpoints
233+
}
234+
213235
func ResourceDeploymentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
214236
api, region, id, err := NewAPIWithRegionAndID(m, d.Id())
215237
if err != nil {
@@ -237,17 +259,35 @@ func ResourceDeploymentRead(ctx context.Context, d *schema.ResourceData, m inter
237259
_ = d.Set("model_id", deployment.ModelID)
238260
_ = d.Set("created_at", types.FlattenTime(deployment.CreatedAt))
239261
_ = d.Set("updated_at", types.FlattenTime(deployment.UpdatedAt))
262+
var privateEndpoints []map[string]interface{}
263+
var publicEndpoints []map[string]interface{}
240264

241265
for _, endpoint := range deployment.Endpoints {
242266
if endpoint.PrivateNetwork != nil {
243-
_ = d.Set("endpoint_private_url", endpoint.URL)
244-
_ = d.Set("endpoint_private_id", endpoint.ID)
245-
_ = d.Set("disable_auth_private", endpoint.DisableAuth)
246-
} else {
247-
_ = d.Set("endpoint_public_url", endpoint.URL)
248-
_ = d.Set("endpoint_public_id", endpoint.ID)
249-
_ = d.Set("disable_auth_public", endpoint.DisableAuth)
267+
privateEndpointSpec := map[string]interface{}{
268+
"id": endpoint.ID,
269+
"private_endpoint_id": regional.NewID(deployment.Region, endpoint.PrivateNetwork.PrivateNetworkID).String(),
270+
"disable_auth": endpoint.DisableAuth,
271+
"url": endpoint.URL,
272+
}
273+
privateEndpoints = append(privateEndpoints, privateEndpointSpec)
250274
}
275+
if endpoint.PublicAccess != nil {
276+
publicEndpointSpec := map[string]interface{}{
277+
"id": endpoint.ID,
278+
"is_enabled": true,
279+
"disable_auth": endpoint.DisableAuth,
280+
"url": endpoint.URL,
281+
}
282+
publicEndpoints = append(publicEndpoints, publicEndpointSpec)
283+
}
284+
}
285+
286+
if len(privateEndpoints) > 0 {
287+
_ = d.Set("private_endpoint", privateEndpoints)
288+
}
289+
if len(publicEndpoints) > 0 {
290+
_ = d.Set("public_endpoint", publicEndpoints)
251291
}
252292
return nil
253293
}

internal/services/inference/deployment_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func TestAccDeployment_Basic(t *testing.T) {
2727
name = "test-inference-deployment-basic"
2828
node_type = "L4"
2929
model_name = "meta/llama-3.1-8b-instruct:fp8"
30-
endpoints {
31-
public_endpoint = true
32-
}
30+
public_endpoint {
31+
is_enabled = true
32+
}
3333
accept_eula = true
3434
}
3535
`,
@@ -60,7 +60,7 @@ func TestAccDeployment_Endpoint(t *testing.T) {
6060
name = "test-inference-deployment-endpoint-private"
6161
node_type = "L4"
6262
model_name = "meta/llama-3.1-8b-instruct:fp8"
63-
endpoints {
63+
private_endpoint {
6464
private_endpoint_id = "${scaleway_vpc_private_network.pn01.id}"
6565
}
6666
accept_eula = true
@@ -70,28 +70,32 @@ func TestAccDeployment_Endpoint(t *testing.T) {
7070
testAccCheckDeploymentExists(tt, "scaleway_inference_deployment.main"),
7171
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-endpoint-private"),
7272
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "node_type", "L4"),
73+
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_endpoint_id", "scaleway_vpc_private_network.pn01", "id"),
7374
),
7475
},
7576
{
7677
Config: `
7778
resource "scaleway_vpc_private_network" "pn01" {
78-
name = "private-network-test-inference"
79+
name = "private-network-test-inference-public"
7980
}
8081
resource "scaleway_inference_deployment" "main" {
8182
name = "test-inference-deployment-basic-endpoints-private-public"
8283
node_type = "L4"
8384
model_name = "meta/llama-3.1-8b-instruct:fp8"
84-
endpoints {
85+
private_endpoint {
8586
private_endpoint_id = "${scaleway_vpc_private_network.pn01.id}"
86-
public_endpoint = true
87+
}
88+
public_endpoint {
89+
is_enabled = true
8790
}
8891
accept_eula = true
8992
}
9093
`,
9194
Check: resource.ComposeTestCheckFunc(
9295
testAccCheckDeploymentExists(tt, "scaleway_inference_deployment.main"),
9396
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "name", "test-inference-deployment-basic-endpoints-private-public"),
94-
resource.TestCheckResourceAttrSet("scaleway_inference_deployment.main", "endpoints.0.public_endpoint"),
97+
resource.TestCheckResourceAttr("scaleway_inference_deployment.main", "public_endpoint.0.is_enabled", "true"),
98+
resource.TestCheckResourceAttrPair("scaleway_inference_deployment.main", "private_endpoint.0.private_endpoint_id", "scaleway_vpc_private_network.pn01", "id"),
9599
),
96100
},
97101
},

0 commit comments

Comments
 (0)