-
Notifications
You must be signed in to change notification settings - Fork 365
/
resource_external_location.go
150 lines (141 loc) · 4.83 KB
/
resource_external_location.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package catalog
import (
"context"
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// This structure contains the fields of both catalog.UpdateExternalLocation and catalog.CreateExternalLocation
type ExternalLocationInfo struct {
Name string `json:"name" tf:"force_new"`
URL string `json:"url"`
CredentialName string `json:"credential_name"`
Comment string `json:"comment,omitempty"`
SkipValidation bool `json:"skip_validation,omitempty"`
Owner string `json:"owner,omitempty" tf:"computed"`
MetastoreID string `json:"metastore_id,omitempty" tf:"computed"`
ReadOnly bool `json:"read_only,omitempty"`
AccessPoint string `json:"access_point,omitempty"`
EncDetails *catalog.EncryptionDetails `json:"encryption_details,omitempty"`
}
func ResourceExternalLocation() common.Resource {
s := common.StructToSchema(ExternalLocationInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["force_destroy"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
}
m["force_update"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
}
m["skip_validation"].DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
return old == "false" && new == "true"
}
m["url"].DiffSuppressFunc = ucDirectoryPathSlashOnlySuppressDiff
m["name"].DiffSuppressFunc = common.EqualFoldDiffSuppress
return m
})
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
var createExternalLocationRequest catalog.CreateExternalLocation
common.DataToStructPointer(d, s, &createExternalLocationRequest)
el, err := w.ExternalLocations.Create(ctx, createExternalLocationRequest)
if err != nil {
return err
}
d.SetId(el.Name)
// Don't update owner if it is not provided
if d.Get("owner") == "" {
return nil
}
var updateExternalLocationRequest catalog.UpdateExternalLocation
common.DataToStructPointer(d, s, &updateExternalLocationRequest)
updateExternalLocationRequest.Name = d.Id()
_, err = w.ExternalLocations.Update(ctx, updateExternalLocationRequest)
if err != nil {
return err
}
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
w, err := c.WorkspaceClient()
if err != nil {
return err
}
el, err := w.ExternalLocations.GetByName(ctx, d.Id())
if err != nil {
return err
}
return common.StructToData(el, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
force := d.Get("force_update").(bool)
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
var updateExternalLocationRequest catalog.UpdateExternalLocation
common.DataToStructPointer(d, s, &updateExternalLocationRequest)
updateExternalLocationRequest.Name = d.Id()
updateExternalLocationRequest.Force = force
if d.HasChange("owner") {
_, err = w.ExternalLocations.Update(ctx, catalog.UpdateExternalLocation{
Name: updateExternalLocationRequest.Name,
Owner: updateExternalLocationRequest.Owner,
})
if err != nil {
return err
}
}
if !d.HasChangeExcept("owner") {
return nil
}
updateExternalLocationRequest.Owner = ""
_, err = w.ExternalLocations.Update(ctx, updateExternalLocationRequest)
if err != nil {
if d.HasChange("owner") {
// Rollback
old, new := d.GetChange("owner")
_, rollbackErr := w.ExternalLocations.Update(ctx, catalog.UpdateExternalLocation{
Name: updateExternalLocationRequest.Name,
Owner: old.(string),
})
if rollbackErr != nil {
return common.OwnerRollbackError(err, rollbackErr, old.(string), new.(string))
}
}
return err
}
return nil
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
force := d.Get("force_destroy").(bool)
w, err := c.WorkspaceClient()
if err != nil {
return err
}
err = validateMetastoreId(ctx, w, d.Get("metastore_id").(string))
if err != nil {
return err
}
return w.ExternalLocations.Delete(ctx, catalog.DeleteExternalLocationRequest{
Name: d.Id(),
Force: force,
})
},
}
}