-
Notifications
You must be signed in to change notification settings - Fork 389
/
resource_provider.go
91 lines (79 loc) · 2.88 KB
/
resource_provider.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
package sharing
import (
"context"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
type ProvidersAPI struct {
client *common.DatabricksClient
context context.Context
}
func NewProvidersAPI(ctx context.Context, m interface{}) ProvidersAPI {
return ProvidersAPI{m.(*common.DatabricksClient), ctx}
}
type ProviderInfo struct {
Name string `json:"name" tf:"force_new"`
Comment string `json:"comment,omitempty"`
AuthenticationType string `json:"authentication_type"`
RecipientProfileStr string `json:"recipient_profile_str" tf:"sensitive"`
}
type Providers struct {
Providers []ProviderInfo `json:"providers"`
}
func (a ProvidersAPI) createProvider(ci *ProviderInfo) error {
return a.client.Post(a.context, "/unity-catalog/providers", ci, ci)
}
func (a ProvidersAPI) getProvider(name string) (ci ProviderInfo, err error) {
err = a.client.Get(a.context, "/unity-catalog/providers/"+name, nil, &ci)
return
}
func (a ProvidersAPI) deleteProvider(name string) error {
return a.client.Delete(a.context, "/unity-catalog/providers/"+name, nil)
}
func (a ProvidersAPI) updateProvider(ci *ProviderInfo) error {
patch := struct {
Comment string `json:"comment"`
}{
Comment: ci.Comment,
}
return a.client.Patch(a.context, "/unity-catalog/providers/"+ci.Name, patch)
}
func ResourceProvider() common.Resource {
providerSchema := common.StructToSchema(ProviderInfo{}, func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["authentication_type"].ValidateFunc = validation.StringInSlice([]string{"TOKEN"}, false)
return m
})
providerSchemaForRead := map[string]*schema.Schema{
"name": providerSchema["name"],
"comment": providerSchema["comment"],
"authentication_type": providerSchema["authentication_type"],
}
return common.Resource{
Schema: providerSchema,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var ri ProviderInfo
common.DataToStructPointer(d, providerSchema, &ri)
if err := NewProvidersAPI(ctx, c).createProvider(&ri); err != nil {
return err
}
d.SetId(ri.Name)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
ri, err := NewProvidersAPI(ctx, c).getProvider(d.Id())
if err != nil {
return err
}
return common.StructToData(ri, providerSchemaForRead, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var ri ProviderInfo
common.DataToStructPointer(d, providerSchema, &ri)
return NewProvidersAPI(ctx, c).updateProvider(&ri)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
return NewProvidersAPI(ctx, c).deleteProvider(d.Id())
},
}
}