|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + |
| 10 | + "github.com/grafana/machine-learning-go-client/mlapi" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + machineLearningJob = &schema.Resource{ |
| 15 | + |
| 16 | + Description: ` |
| 17 | +A job defines the queries and model parameters for a machine learning task. |
| 18 | +`, |
| 19 | + |
| 20 | + CreateContext: resourceMachineLearningJobCreate, |
| 21 | + ReadContext: resourceMachineLearningJobRead, |
| 22 | + UpdateContext: resourceMachineLearningJobUpdate, |
| 23 | + DeleteContext: resourceMachineLearningJobDelete, |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + StateContext: schema.ImportStatePassthroughContext, |
| 26 | + }, |
| 27 | + |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "id": { |
| 30 | + Description: "The ID of the job.", |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "name": { |
| 35 | + Description: "The name of the job.", |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + }, |
| 39 | + "metric": { |
| 40 | + Description: "The metric used to query the job results.", |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + "description": { |
| 45 | + Description: "A description of the job.", |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + }, |
| 49 | + "datasource_id": { |
| 50 | + Description: "The id of the datasource to query.", |
| 51 | + Type: schema.TypeInt, |
| 52 | + Required: true, |
| 53 | + }, |
| 54 | + "datasource_type": { |
| 55 | + Description: "The type of datasource being queried. Currently allowed values are prometheus, graphite, loki, postgres, and datadog.", |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + }, |
| 59 | + "query_params": { |
| 60 | + Description: "An object representing the query params to query Grafana with.", |
| 61 | + Type: schema.TypeMap, |
| 62 | + Required: true, |
| 63 | + }, |
| 64 | + "interval": { |
| 65 | + Description: "The data interval in seconds to train the data on.", |
| 66 | + Type: schema.TypeInt, |
| 67 | + Optional: true, |
| 68 | + Default: 300, |
| 69 | + }, |
| 70 | + "hyper_params": { |
| 71 | + Description: "The hyperparameters used to fine tune the algorithm. See https://grafana.com/docs/grafana-cloud/machine-learning/models/ for the full list of available hyperparameters.", |
| 72 | + Type: schema.TypeMap, |
| 73 | + Optional: true, |
| 74 | + Default: map[string]interface{}{}, |
| 75 | + }, |
| 76 | + "training_window": { |
| 77 | + Description: "The data interval in seconds to train the data on.", |
| 78 | + Type: schema.TypeInt, |
| 79 | + Optional: true, |
| 80 | + Default: int(90 * 24 * time.Hour / time.Second), |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +) |
| 85 | + |
| 86 | +func resourceMachineLearningJob() *schema.Resource { |
| 87 | + return machineLearningJob |
| 88 | +} |
| 89 | + |
| 90 | +func resourceMachineLearningJobCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 91 | + c := meta.(*client).mlapi |
| 92 | + job := makeMLJob(d, meta) |
| 93 | + job, err := c.NewJob(ctx, job) |
| 94 | + if err != nil { |
| 95 | + return diag.FromErr(err) |
| 96 | + } |
| 97 | + d.SetId(job.ID) |
| 98 | + return resourceMachineLearningJobRead(ctx, d, meta) |
| 99 | +} |
| 100 | + |
| 101 | +func resourceMachineLearningJobRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 102 | + c := meta.(*client).mlapi |
| 103 | + job, err := c.Job(ctx, d.Id()) |
| 104 | + if err != nil { |
| 105 | + return diag.FromErr(err) |
| 106 | + } |
| 107 | + |
| 108 | + d.Set("name", job.Name) |
| 109 | + d.Set("metric", job.Metric) |
| 110 | + d.Set("description", job.Description) |
| 111 | + d.Set("datasource_id", job.DatasourceID) |
| 112 | + d.Set("datasource_type", job.DatasourceType) |
| 113 | + d.Set("query_params", job.QueryParams) |
| 114 | + d.Set("interval", job.Interval) |
| 115 | + d.Set("hyper_params", job.HyperParams) |
| 116 | + d.Set("training_window", job.TrainingWindow) |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func resourceMachineLearningJobUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 122 | + c := meta.(*client).mlapi |
| 123 | + j := makeMLJob(d, meta) |
| 124 | + _, err := c.UpdateJob(ctx, j) |
| 125 | + if err != nil { |
| 126 | + return diag.FromErr(err) |
| 127 | + } |
| 128 | + return resourceMachineLearningJobRead(ctx, d, meta) |
| 129 | +} |
| 130 | + |
| 131 | +func resourceMachineLearningJobDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 132 | + c := meta.(*client).mlapi |
| 133 | + err := c.DeleteJob(ctx, d.Id()) |
| 134 | + if err != nil { |
| 135 | + return diag.FromErr(err) |
| 136 | + } |
| 137 | + d.SetId("") |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func makeMLJob(d *schema.ResourceData, meta interface{}) mlapi.Job { |
| 142 | + return mlapi.Job{ |
| 143 | + ID: d.Id(), |
| 144 | + Name: d.Get("name").(string), |
| 145 | + Metric: d.Get("metric").(string), |
| 146 | + Description: d.Get("description").(string), |
| 147 | + GrafanaURL: meta.(*client).url, |
| 148 | + DatasourceID: uint(d.Get("datasource_id").(int)), |
| 149 | + DatasourceType: d.Get("datasource_type").(string), |
| 150 | + QueryParams: d.Get("query_params").(map[string]interface{}), |
| 151 | + Interval: uint(d.Get("interval").(int)), |
| 152 | + Algorithm: "Prophet", |
| 153 | + HyperParams: d.Get("hyper_params").(map[string]interface{}), |
| 154 | + TrainingWindow: uint(d.Get("training_window").(int)), |
| 155 | + TrainingFrequency: uint(24 * time.Hour / time.Second), |
| 156 | + } |
| 157 | +} |
0 commit comments