-
Notifications
You must be signed in to change notification settings - Fork 367
/
data_metastore.go
67 lines (64 loc) · 2.26 KB
/
data_metastore.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
package catalog
import (
"context"
"fmt"
"strings"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
)
func DataSourceMetastore() common.Resource {
type AccountMetastoreByID struct {
Id string `json:"id,omitempty" tf:"computed"`
MetastoreId string `json:"metastore_id,omitempty" tf:"computed"`
Name string `json:"name,omitempty" tf:"computed"`
Region string `json:"region,omitempty" tf:"computed"`
Metastore *catalog.MetastoreInfo `json:"metastore_info,omitempty" tf:"computed" `
}
return common.AccountData(func(ctx context.Context, data *AccountMetastoreByID, acc *databricks.AccountClient) error {
if data.MetastoreId == "" && data.Name == "" && data.Region == "" {
return fmt.Errorf("one of metastore_id, name or region must be provided")
}
if (data.MetastoreId != "" && data.Name != "") || (data.Region != "" && data.MetastoreId != "") || (data.Region != "" && data.Name != "") {
return fmt.Errorf("only one of metastore_id, name or region must be provided")
}
if data.MetastoreId != "" {
minfo, err := acc.Metastores.GetByMetastoreId(ctx, data.MetastoreId)
if err != nil {
return err
}
data.Metastore = minfo.MetastoreInfo
} else {
metastores, err := acc.Metastores.ListAll(ctx)
if err != nil {
return err
}
minfos := []catalog.MetastoreInfo{}
if data.Name != "" {
for _, v := range metastores {
if strings.EqualFold(v.Name, data.Name) {
minfos = append(minfos, v)
}
}
} else {
for _, v := range metastores {
if strings.EqualFold(v.Region, data.Region) {
minfos = append(minfos, v)
}
}
}
if len(minfos) == 0 {
return fmt.Errorf("a metastore with name '%s' or in region '%s' is not found", data.Name, data.Region)
}
if len(minfos) > 1 {
return fmt.Errorf("there are %d metastores with name '%s' in region '%s'", len(minfos), data.Name, data.Region)
}
data.Metastore = &minfos[0]
}
data.Id = data.Metastore.MetastoreId
data.MetastoreId = data.Metastore.MetastoreId
data.Name = data.Metastore.Name
data.Region = data.Metastore.Region
return nil
})
}