-
Notifications
You must be signed in to change notification settings - Fork 365
/
resource_library.go
120 lines (114 loc) · 3.32 KB
/
resource_library.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package clusters
import (
"context"
"fmt"
"strings"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/libraries"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
type LibraryResource struct {
compute.Library
}
func (LibraryResource) CustomizeSchemaResourceSpecific(s *common.CustomizableSchema) *common.CustomizableSchema {
s.AddNewField("cluster_id", &schema.Schema{
Type: schema.TypeString,
Required: true,
})
return s
}
func (LibraryResource) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {
return s
}
func ResourceLibrary() common.Resource {
libraySdkSchema := common.StructToSchema(LibraryResource{}, nil)
parseId := func(id string) (string, string) {
split := strings.SplitN(id, "/", 2)
if len(split) != 2 {
return "unknown", "unknown"
}
return split[0], split[1]
}
return common.Resource{
Schema: libraySdkSchema,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
clusterID := d.Get("cluster_id").(string)
_, err = StartClusterAndGetInfo(ctx, w, clusterID)
if err != nil {
return err
}
var lib compute.Library
common.DataToStructPointer(d, libraySdkSchema, &lib)
err = w.Libraries.Install(ctx, compute.InstallLibraries{
ClusterId: clusterID,
Libraries: []compute.Library{lib},
})
if err != nil {
return err
}
_, err = libraries.WaitForLibrariesInstalledSdk(ctx, w, compute.Wait{
ClusterID: clusterID,
IsRunning: true,
}, d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%s/%s", clusterID, lib.String()))
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
clusterID, libraryRep := parseId(d.Id())
w, err := c.WorkspaceClient()
if err != nil {
return err
}
cll, err := libraries.WaitForLibrariesInstalledSdk(ctx, w, compute.Wait{
ClusterID: clusterID,
IsRefresh: true,
}, d.Timeout(schema.TimeoutRead))
if err != nil {
return err
}
for _, v := range cll.LibraryStatuses {
thisRep := v.Library.String()
if thisRep == libraryRep {
common.StructToData(v.Library, libraySdkSchema, d)
d.Set("cluster_id", clusterID)
return nil
}
}
return apierr.NotFound(fmt.Sprintf("cannot find %s on %s", libraryRep, clusterID))
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
clusterID, libraryRep := parseId(d.Id())
w, err := c.WorkspaceClient()
if err != nil {
return err
}
_, err = StartClusterAndGetInfo(ctx, w, clusterID)
if err != nil {
return err
}
cll, err := w.Libraries.ClusterStatusByClusterId(ctx, clusterID)
if err != nil {
return err
}
for _, v := range cll.LibraryStatuses {
if v.Library.String() != libraryRep {
continue
}
return w.Libraries.Uninstall(ctx, compute.UninstallLibraries{
ClusterId: clusterID,
Libraries: []compute.Library{*v.Library},
})
}
return apierr.NotFound(fmt.Sprintf("cannot find %s on %s", libraryRep, clusterID))
},
}
}