|
| 1 | +package catalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/url" |
| 6 | + |
| 7 | + "github.com/databrickslabs/terraform-provider-databricks/common" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +type ExternalLocationsAPI struct { |
| 12 | + client *common.DatabricksClient |
| 13 | + context context.Context |
| 14 | +} |
| 15 | + |
| 16 | +func NewExternalLocationsAPI(ctx context.Context, m interface{}) ExternalLocationsAPI { |
| 17 | + return ExternalLocationsAPI{m.(*common.DatabricksClient), ctx} |
| 18 | +} |
| 19 | + |
| 20 | +type ExternalLocationInfo struct { |
| 21 | + Name string `json:"name" tf:"force_new"` |
| 22 | + URL string `json:"url"` |
| 23 | + CredentialName string `json:"credential_name"` |
| 24 | + Comment string `json:"comment,omitempty"` |
| 25 | + Owner string `json:"owner,omitempty" tf:"computed"` |
| 26 | + MetastoreID string `json:"metastore_id,omitempty" tf:"computed"` |
| 27 | +} |
| 28 | + |
| 29 | +func (a ExternalLocationsAPI) create(el *ExternalLocationInfo) error { |
| 30 | + return a.client.Post(a.context, "/unity-catalog/external-locations", el, &el) |
| 31 | +} |
| 32 | + |
| 33 | +func (a ExternalLocationsAPI) get(name string) (el ExternalLocationInfo, err error) { |
| 34 | + err = a.client.Get(a.context, "/unity-catalog/external-locations/"+url.PathEscape(name), nil, &el) |
| 35 | + return |
| 36 | +} |
| 37 | + |
| 38 | +func (a ExternalLocationsAPI) update(name string, el ExternalLocationInfo) error { |
| 39 | + return a.client.Patch(a.context, "/unity-catalog/external-locations/"+url.PathEscape(name), el) |
| 40 | +} |
| 41 | + |
| 42 | +func (a ExternalLocationsAPI) delete(name string) error { |
| 43 | + return a.client.Delete(a.context, "/unity-catalog/external-locations/"+url.PathEscape(name), nil) |
| 44 | +} |
| 45 | + |
| 46 | +func ResourceExternalLocation() *schema.Resource { |
| 47 | + s := common.StructToSchema(ExternalLocationInfo{}, |
| 48 | + func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 49 | + return m |
| 50 | + }) |
| 51 | + return common.Resource{ |
| 52 | + Schema: s, |
| 53 | + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 54 | + var el ExternalLocationInfo |
| 55 | + common.DataToStructPointer(d, s, &el) |
| 56 | + err := NewExternalLocationsAPI(ctx, c).create(&el) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + d.SetId(el.Name) |
| 61 | + return nil |
| 62 | + }, |
| 63 | + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 64 | + el, err := NewExternalLocationsAPI(ctx, c).get(d.Id()) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + return common.StructToData(el, s, d) |
| 69 | + }, |
| 70 | + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 71 | + var el ExternalLocationInfo |
| 72 | + common.DataToStructPointer(d, s, &el) |
| 73 | + return NewExternalLocationsAPI(ctx, c).update(d.Id(), ExternalLocationInfo{ |
| 74 | + Name: d.Id(), |
| 75 | + URL: el.URL, |
| 76 | + CredentialName: el.CredentialName, |
| 77 | + Comment: el.Comment, |
| 78 | + Owner: el.Owner, |
| 79 | + }) |
| 80 | + }, |
| 81 | + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 82 | + return NewExternalLocationsAPI(ctx, c).delete(d.Id()) |
| 83 | + }, |
| 84 | + }.ToResource() |
| 85 | +} |
0 commit comments