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