Skip to content

Commit 20e80ec

Browse files
committed
add support model data-source
1 parent 0435457 commit 20e80ec

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ func Provider(config *Config) plugin.ProviderFunc {
274274
"scaleway_iam_ssh_key": iam.DataSourceSSHKey(),
275275
"scaleway_iam_user": iam.DataSourceUser(),
276276
"scaleway_iam_api_key": iam.DataSourceAPIKey(),
277+
"scaleway_inference_model": inference.DataSourceModel(),
277278
"scaleway_instance_image": instance.DataSourceImage(),
278279
"scaleway_instance_ip": instance.DataSourceIP(),
279280
"scaleway_instance_placement_group": instance.DataSourcePlacementGroup(),

internal/services/inference/deployment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func ResourceDeployment() *schema.Resource {
4545
"node_type": {
4646
Type: schema.TypeString,
4747
Required: true,
48+
ForceNew: true,
4849
Description: "The node type to use for the deployment",
4950
},
5051
"model_name": {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package inference
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/scaleway-sdk-go/api/inference/v1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
13+
)
14+
15+
func DataSourceModel() *schema.Resource {
16+
dsSchema := datasource.SchemaFromResourceSchema(ResourceModel().Schema)
17+
18+
//datasource.FixDatasourceSchemaFlags(dsSchema, true, "name")
19+
datasource.AddOptionalFieldsToSchema(dsSchema, "url", "name")
20+
dsSchema["name"].ConflictsWith = []string{"model_id"}
21+
dsSchema["model_id"] = &schema.Schema{
22+
Type: schema.TypeString,
23+
Optional: true,
24+
Description: "The ID of the model",
25+
ValidateDiagFunc: verify.IsUUIDWithLocality(),
26+
ConflictsWith: []string{"name"},
27+
}
28+
29+
return &schema.Resource{
30+
ReadContext: DataSourceModelRead,
31+
Schema: dsSchema,
32+
}
33+
}
34+
35+
func DataSourceModelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
36+
api, region, err := NewAPIWithRegion(d, m)
37+
if err != nil {
38+
return diag.FromErr(err)
39+
}
40+
41+
modelID, ok := d.GetOk("model_id")
42+
if !ok {
43+
modelName := d.Get("name").(string)
44+
modelList, err := api.ListModels(&inference.ListModelsRequest{
45+
Region: region,
46+
Name: scw.StringPtr(modelName),
47+
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
48+
}, scw.WithContext(ctx))
49+
if err != nil {
50+
return diag.FromErr(err)
51+
}
52+
53+
foundModel, err := datasource.FindExact(
54+
modelList.Models,
55+
func(model *inference.Model) bool {
56+
return model.Name == modelName
57+
},
58+
modelName,
59+
)
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
modelID = foundModel.ID
65+
}
66+
67+
err = d.Set("model_id", modelID)
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
diags := ResourceModelRead(ctx, d, m)
73+
if diags != nil {
74+
return diags
75+
}
76+
77+
if d.Id() == "" {
78+
return diag.FromErr(fmt.Errorf("model_id is empty"))
79+
}
80+
81+
return nil
82+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package inference

0 commit comments

Comments
 (0)