|
| 1 | +package catalog |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/databricks/terraform-provider-databricks/common" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 8 | +) |
| 9 | + |
| 10 | +type RecipientsAPI struct { |
| 11 | + client *common.DatabricksClient |
| 12 | + context context.Context |
| 13 | +} |
| 14 | + |
| 15 | +func NewRecipientsAPI(ctx context.Context, m interface{}) RecipientsAPI { |
| 16 | + return RecipientsAPI{m.(*common.DatabricksClient), ctx} |
| 17 | +} |
| 18 | + |
| 19 | +type Token struct { |
| 20 | + Id string `json:"id,omitempty" tf:"computed"` |
| 21 | + CreatedAt int64 `json:"created_at,omitempty" tf:"computed"` |
| 22 | + CreatedBy string `json:"created_by,omitempty" tf:"computed"` |
| 23 | + ActivationUrl string `json:"activation_url,omitempty" tf:"computed"` |
| 24 | + ExpirationTime int64 `json:"expiration_time,omitempty" tf:"computed"` |
| 25 | + UpdatedAt int64 `json:"updated_at,omitempty" tf:"computed"` |
| 26 | + UpdatedBy string `json:"updated_by,omitempty" tf:"computed"` |
| 27 | +} |
| 28 | + |
| 29 | +type IpAccessList struct { |
| 30 | + AllowedIpAddresses []string `json:"allowed_ip_addresses"` |
| 31 | +} |
| 32 | + |
| 33 | +type RecipientInfo struct { |
| 34 | + Name string `json:"name" tf:"force_new"` |
| 35 | + Comment string `json:"comment,omitempty"` |
| 36 | + SharingCode string `json:"sharing_code,omitempty" tf:"sensitive,force_new,suppress_diff"` |
| 37 | + AuthenticationType string `json:"authentication_type" tf:"force_new"` |
| 38 | + Tokens []Token `json:"tokens,omitempty" tf:"computed"` |
| 39 | + DataRecipientGlobalMetastoreId string `json:"data_recipient_global_metastore_id,omitempty" tf:"force_new,conflicts:ip_access_list"` |
| 40 | + IpAccessList *IpAccessList `json:"ip_access_list,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +type Recipients struct { |
| 44 | + Recipients []RecipientInfo `json:"recipients"` |
| 45 | +} |
| 46 | + |
| 47 | +func (a RecipientsAPI) createRecipient(ci *RecipientInfo) error { |
| 48 | + return a.client.Post(a.context, "/unity-catalog/recipients", ci, ci) |
| 49 | +} |
| 50 | + |
| 51 | +func (a RecipientsAPI) getRecipient(name string) (ci RecipientInfo, err error) { |
| 52 | + err = a.client.Get(a.context, "/unity-catalog/recipients/"+name, nil, &ci) |
| 53 | + return |
| 54 | +} |
| 55 | + |
| 56 | +func (a RecipientsAPI) deleteRecipient(name string) error { |
| 57 | + return a.client.Delete(a.context, "/unity-catalog/recipients/"+name, nil) |
| 58 | +} |
| 59 | + |
| 60 | +func (a RecipientsAPI) updateRecipient(ci *RecipientInfo) error { |
| 61 | + patch := map[string]interface{}{"comment": ci.Comment, "ip_access_list": ci.IpAccessList} |
| 62 | + |
| 63 | + return a.client.Patch(a.context, "/unity-catalog/recipients/"+ci.Name, patch) |
| 64 | +} |
| 65 | + |
| 66 | +func ResourceRecipient() *schema.Resource { |
| 67 | + recipientSchema := common.StructToSchema(RecipientInfo{}, func(m map[string]*schema.Schema) map[string]*schema.Schema { |
| 68 | + m["authentication_type"].ValidateFunc = validation.StringInSlice([]string{"TOKEN", "DATABRICKS"}, false) |
| 69 | + return m |
| 70 | + }) |
| 71 | + return common.Resource{ |
| 72 | + Schema: recipientSchema, |
| 73 | + Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 74 | + var ri RecipientInfo |
| 75 | + common.DataToStructPointer(d, recipientSchema, &ri) |
| 76 | + if err := NewRecipientsAPI(ctx, c).createRecipient(&ri); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + d.SetId(ri.Name) |
| 80 | + return nil |
| 81 | + }, |
| 82 | + Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 83 | + ri, err := NewRecipientsAPI(ctx, c).getRecipient(d.Id()) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + return common.StructToData(ri, recipientSchema, d) |
| 88 | + }, |
| 89 | + Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 90 | + var ri RecipientInfo |
| 91 | + common.DataToStructPointer(d, recipientSchema, &ri) |
| 92 | + return NewRecipientsAPI(ctx, c).updateRecipient(&ri) |
| 93 | + }, |
| 94 | + Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { |
| 95 | + return NewRecipientsAPI(ctx, c).deleteRecipient(d.Id()) |
| 96 | + }, |
| 97 | + }.ToResource() |
| 98 | +} |
0 commit comments