-
Notifications
You must be signed in to change notification settings - Fork 365
/
resource_mlflow_experiment.go
106 lines (93 loc) · 3.11 KB
/
resource_mlflow_experiment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package mlflow
import (
"context"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// Experiment defines the response object from the API
type Experiment struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExperimentId string `json:"experiment_id,omitempty" tf:"computed"`
ArtifactLocation string `json:"artifact_location,omitempty" tf:"force_new,suppress_diff"`
LifecycleStage string `json:"lifecycle_stage,omitempty" tf:"computed"`
LastUpdateTime int64 `json:"last_update_time,omitempty" tf:"computed"`
CreationTime int64 `json:"creation_time,omitempty" tf:"computed"`
}
type experimentUpdate struct {
ExperimentId string `json:"experiment_id"`
NewName string `json:"new_name"`
}
type experimentWrapper struct {
Experiment Experiment `json:"experiment"`
}
// ExperimentsAPI ...
type ExperimentsAPI struct {
client *common.DatabricksClient
context context.Context
}
// NewExperimentsAPI ...
func NewExperimentsAPI(ctx context.Context, m any) ExperimentsAPI {
return ExperimentsAPI{m.(*common.DatabricksClient), ctx}
}
// Create ...
func (a ExperimentsAPI) Create(d *Experiment) error {
return a.client.Post(a.context, "/mlflow/experiments/create", d, &d)
}
// Read ...
func (a ExperimentsAPI) Read(experimentId string) (*Experiment, error) {
var d experimentWrapper
err := a.client.Get(a.context, "/mlflow/experiments/get", map[string]string{
"experiment_id": experimentId,
}, &d)
if err != nil {
return nil, err
}
return &d.Experiment, nil
}
// Update ...
func (a ExperimentsAPI) Update(e *experimentUpdate) error {
return a.client.Post(a.context, "/mlflow/experiments/update", e, &e)
}
// Delete ...
func (a ExperimentsAPI) Delete(id string) error {
return a.client.Post(a.context, "/mlflow/experiments/delete", map[string]string{
"experiment_id": id,
}, nil)
}
func ResourceMlflowExperiment() common.Resource {
s := common.StructToSchema(
Experiment{},
common.NoCustomize)
return common.Resource{
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var e Experiment
common.DataToStructPointer(d, s, &e)
if err := NewExperimentsAPI(ctx, c).Create(&e); err != nil {
return err
}
d.SetId(e.ExperimentId)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
e, err := NewExperimentsAPI(ctx, c).Read(d.Id())
if err != nil {
return err
}
return common.StructToData(*e, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var e Experiment
common.DataToStructPointer(d, s, &e)
updateDoc := experimentUpdate{ExperimentId: d.Id(), NewName: e.Name}
return NewExperimentsAPI(ctx, c).Update(&updateDoc)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
return NewExperimentsAPI(ctx, c).Delete(d.Id())
},
StateUpgraders: []schema.StateUpgrader{},
Schema: s,
SchemaVersion: 0,
Timeouts: &schema.ResourceTimeout{},
}
}