-
Notifications
You must be signed in to change notification settings - Fork 365
/
resource_metastore_assignment.go
119 lines (113 loc) · 3.81 KB
/
resource_metastore_assignment.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
package catalog
import (
"context"
"strconv"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func ResourceMetastoreAssignment() common.Resource {
s := common.StructToSchema(catalog.MetastoreAssignment{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["default_catalog_name"].Default = "hive_metastore"
m["workspace_id"].ForceNew = true
m["metastore_id"].ForceNew = true
return m
})
pi := common.NewPairID("workspace_id", "metastore_id").Schema(
func(m map[string]*schema.Schema) map[string]*schema.Schema {
return s
})
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
workspaceId := int64(d.Get("workspace_id").(int))
metastoreId := d.Get("metastore_id").(string)
var create catalog.CreateMetastoreAssignment
common.DataToStructPointer(d, s, &create)
create.WorkspaceId = workspaceId
return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
err := acc.MetastoreAssignments.Create(ctx,
catalog.AccountsCreateMetastoreAssignment{
WorkspaceId: workspaceId,
MetastoreId: metastoreId,
MetastoreAssignment: &create,
})
if err != nil {
return err
}
pi.Pack(d)
return nil
}, func(w *databricks.WorkspaceClient) error {
err := w.Metastores.Assign(ctx, create)
if err != nil {
return err
}
pi.Pack(d)
return nil
})
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
first, _, err := pi.Unpack(d)
if err != nil {
return err
}
workspaceId, err := strconv.ParseInt(first, 10, 64)
if err != nil {
return err
}
return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
ma, err := acc.MetastoreAssignments.GetByWorkspaceId(ctx, workspaceId)
if err != nil {
return err
}
return common.StructToData(ma, s, d)
}, func(w *databricks.WorkspaceClient) error {
//this only works when managing the metastore assigned to the current workspace.
//plus we don't know the workspace we're logged into.
ma, err := w.Metastores.Current(ctx)
if err != nil {
return err
}
d.Set("metastore_id", ma.MetastoreId)
return nil
})
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
workspaceId := int64(d.Get("workspace_id").(int))
metastoreId := d.Get("metastore_id").(string)
var update catalog.UpdateMetastoreAssignment
common.DataToStructPointer(d, s, &update)
update.WorkspaceId = workspaceId
return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
return acc.MetastoreAssignments.Update(ctx,
catalog.AccountsUpdateMetastoreAssignment{
WorkspaceId: workspaceId,
MetastoreId: metastoreId,
MetastoreAssignment: &update,
})
}, func(w *databricks.WorkspaceClient) error {
return w.Metastores.UpdateAssignment(ctx, update)
})
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
first, metastoreId, err := pi.Unpack(d)
if err != nil {
return err
}
workspaceId, err := strconv.ParseInt(first, 10, 64)
if err != nil {
return err
}
return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
return acc.MetastoreAssignments.DeleteByWorkspaceIdAndMetastoreId(ctx, workspaceId, metastoreId)
}, func(w *databricks.WorkspaceClient) error {
return w.Metastores.Unassign(ctx, catalog.UnassignRequest{
MetastoreId: metastoreId,
WorkspaceId: workspaceId,
})
})
},
}
}