|
| 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/httperrors" |
| 11 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceCustomModel() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + CreateContext: ResourceCustomModelCreate, |
| 19 | + ReadContext: ResourceCustomModelRead, |
| 20 | + DeleteContext: ResourceCustomModelDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + StateContext: schema.ImportStatePassthroughContext, |
| 23 | + }, |
| 24 | + Timeouts: &schema.ResourceTimeout{ |
| 25 | + Default: schema.DefaultTimeout(defaultCustomModelTimeout), |
| 26 | + Create: schema.DefaultTimeout(defaultCustomModelTimeout), |
| 27 | + Update: schema.DefaultTimeout(defaultCustomModelTimeout), |
| 28 | + Delete: schema.DefaultTimeout(defaultCustomModelTimeout), |
| 29 | + }, |
| 30 | + SchemaVersion: 0, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + Description: "The name of the model", |
| 37 | + }, |
| 38 | + "url": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + Description: "The URL of the model", |
| 43 | + }, |
| 44 | + "secret": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + Sensitive: true, |
| 48 | + ForceNew: true, |
| 49 | + Description: "The secret to pull a model", |
| 50 | + }, |
| 51 | + "tags": { |
| 52 | + Type: schema.TypeList, |
| 53 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 54 | + Computed: true, |
| 55 | + Description: "The tags associated with the deployment", |
| 56 | + }, |
| 57 | + "project_id": account.ProjectIDSchema(), |
| 58 | + "status": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "The status of the model", |
| 62 | + }, |
| 63 | + "description": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "The description of the model", |
| 67 | + }, |
| 68 | + "error_message": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Displays information if your model is in error state", |
| 72 | + }, |
| 73 | + "created_at": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "The date and time of the creation of the model", |
| 77 | + }, |
| 78 | + "updated_at": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "The date and time of the last update of the model", |
| 82 | + }, |
| 83 | + "has_eula": { |
| 84 | + Type: schema.TypeBool, |
| 85 | + Computed: true, |
| 86 | + Description: "Defines whether the model has an end user license agreement", |
| 87 | + }, |
| 88 | + "nodes_support": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Computed: true, |
| 91 | + Description: "Supported node types with quantization options and context lengths.", |
| 92 | + Elem: &schema.Resource{ |
| 93 | + Schema: map[string]*schema.Schema{ |
| 94 | + "node_type_name": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Supported node type.", |
| 98 | + }, |
| 99 | + "quantization": { |
| 100 | + Type: schema.TypeList, |
| 101 | + Computed: true, |
| 102 | + Description: "Supported quantization options.", |
| 103 | + Elem: &schema.Resource{ |
| 104 | + Schema: map[string]*schema.Schema{ |
| 105 | + "quantization_bits": { |
| 106 | + Type: schema.TypeInt, |
| 107 | + Computed: true, |
| 108 | + Description: "Number of bits used for quantization.", |
| 109 | + }, |
| 110 | + "allowed": { |
| 111 | + Type: schema.TypeBool, |
| 112 | + Computed: true, |
| 113 | + Description: "Whether this quantization is allowed for the model.", |
| 114 | + }, |
| 115 | + "max_context_size": { |
| 116 | + Type: schema.TypeInt, |
| 117 | + Computed: true, |
| 118 | + Description: "Maximum inference context size for this quantization and node type.", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + "parameter_size_bits": { |
| 127 | + Type: schema.TypeInt, |
| 128 | + Computed: true, |
| 129 | + Description: "Size, in bits, of the model parameters", |
| 130 | + }, |
| 131 | + "size_bits": { |
| 132 | + Type: schema.TypeInt, |
| 133 | + Computed: true, |
| 134 | + Description: "Total size, in bytes, of the model files", |
| 135 | + }, |
| 136 | + "region": regional.Schema(), |
| 137 | + }, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func ResourceCustomModelCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 142 | + api, region, err := NewAPIWithRegion(d, m) |
| 143 | + if err != nil { |
| 144 | + return diag.FromErr(err) |
| 145 | + } |
| 146 | + |
| 147 | + modelSource := &inference.ModelSource{ |
| 148 | + URL: d.Get("url").(string), |
| 149 | + } |
| 150 | + if secret, ok := d.GetOk("secret"); ok { |
| 151 | + secretStr := secret.(string) |
| 152 | + modelSource.Secret = &secretStr |
| 153 | + } |
| 154 | + |
| 155 | + reqCreateModel := &inference.CreateModelRequest{ |
| 156 | + Region: region, |
| 157 | + Name: d.Get("name").(string), |
| 158 | + ProjectID: d.Get("project_id").(string), |
| 159 | + Source: modelSource, |
| 160 | + } |
| 161 | + |
| 162 | + model, err := api.CreateModel(reqCreateModel) |
| 163 | + if err != nil { |
| 164 | + return diag.FromErr(err) |
| 165 | + } |
| 166 | + |
| 167 | + d.SetId(regional.NewIDString(region, model.ID)) |
| 168 | + |
| 169 | + model, err = waitForModel(ctx, api, region, model.ID, d.Timeout(schema.TimeoutCreate)) |
| 170 | + if err != nil { |
| 171 | + return diag.FromErr(err) |
| 172 | + } |
| 173 | + |
| 174 | + if model.Status == inference.ModelStatusError { |
| 175 | + errMsg := *model.ErrorMessage |
| 176 | + return diag.FromErr(fmt.Errorf("model '%s' is in status '%s'", model.ID, errMsg)) |
| 177 | + } |
| 178 | + |
| 179 | + return ResourceCustomModelRead(ctx, d, m) |
| 180 | +} |
| 181 | + |
| 182 | +func ResourceCustomModelRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 183 | + api, region, id, err := NewAPIWithRegionAndID(m, d.Id()) |
| 184 | + if err != nil { |
| 185 | + return diag.FromErr(err) |
| 186 | + } |
| 187 | + |
| 188 | + model, err := waitForModel(ctx, api, region, id, d.Timeout(schema.TimeoutRead)) |
| 189 | + if err != nil { |
| 190 | + if httperrors.Is404(err) { |
| 191 | + d.SetId("") |
| 192 | + |
| 193 | + return nil |
| 194 | + } |
| 195 | + |
| 196 | + return diag.FromErr(err) |
| 197 | + } |
| 198 | + |
| 199 | + _ = d.Set("parameter_size_bits", model.ParameterSizeBits) |
| 200 | + _ = d.Set("size_bits", model.SizeBytes) |
| 201 | + _ = d.Set("name", model.Name) |
| 202 | + _ = d.Set("status", model.Status) |
| 203 | + _ = d.Set("description", model.Description) |
| 204 | + _ = d.Set("tags", types.ExpandUpdatedStringsPtr(model.Tags)) |
| 205 | + _ = d.Set("created_at", types.FlattenTime(model.CreatedAt)) |
| 206 | + _ = d.Set("updated_at", types.FlattenTime(model.UpdatedAt)) |
| 207 | + _ = d.Set("has_eula", model.HasEula) |
| 208 | + _ = d.Set("nodes_support", flattenNodeSupport(model.NodesSupport)) |
| 209 | + _ = d.Set("error_message", model.ErrorMessage) |
| 210 | + |
| 211 | + return nil |
| 212 | +} |
| 213 | + |
| 214 | +func ResourceCustomModelDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 215 | + api, region, id, err := NewAPIWithRegionAndID(m, d.Id()) |
| 216 | + if err != nil { |
| 217 | + return diag.FromErr(err) |
| 218 | + } |
| 219 | + |
| 220 | + _, err = waitForModel(ctx, api, region, id, d.Timeout(schema.TimeoutDelete)) |
| 221 | + if err != nil { |
| 222 | + return diag.FromErr(err) |
| 223 | + } |
| 224 | + |
| 225 | + err = api.DeleteModel(&inference.DeleteModelRequest{ |
| 226 | + Region: region, |
| 227 | + ModelID: id, |
| 228 | + }, scw.WithContext(ctx)) |
| 229 | + if err != nil { |
| 230 | + return diag.FromErr(err) |
| 231 | + } |
| 232 | + |
| 233 | + return nil |
| 234 | +} |
0 commit comments