Skip to content

Commit c7c60e2

Browse files
stikkireddynfx
andauthored
Add experimental resources (#1008)
```hcl resource "databricks_storage_credential" "this" { name = "abc" aws_iam_role { role_arn = aws_iam_role.this.arn } comment = "Managed by TF" } ``` Co-authored-by: Serge Smertin <[email protected]>
1 parent d3b16ac commit c7c60e2

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Version changelog
22

3+
## 0.4.3
4+
5+
* Improved documentation with regards to public subnets in AWS quick start ([#1005](https://github.com/databrickslabs/terraform-provider-databricks/pull/1005)).
6+
* Added `databricks_mount` code genration for [exporter](https://registry.terraform.io/providers/databrickslabs/databricks/latest/docs/guides/experimental-exporter) tooling ([#1006](https://github.com/databrickslabs/terraform-provider-databricks/pull/1006)).
7+
* Increase dependency check frequency ([#1007](https://github.com/databrickslabs/terraform-provider-databricks/pull/1007)).
8+
* Added experimental resources.
9+
310
## 0.4.2
411

512
* Added optional `auth_type` provider conf to enforce specific auth type to be used in very rare cases, where a single Terraform state manages Databricks workspaces on more than one cloud and `More than one authorization method configured` error is a false positive. Valid values are `pat`, `basic`, `azure-client-secret`, `azure-msi`, `azure-cli`, and `databricks-cli` ([#1000](https://github.com/databrickslabs/terraform-provider-databricks/pull/1000)).
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package catalog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databrickslabs/terraform-provider-databricks/qa"
7+
)
8+
9+
func TestStorageCredentialsCornerCases(t *testing.T) {
10+
qa.ResourceCornerCases(t, ResourceStorageCredential())
11+
}
12+
13+
func TestCreateStorageCredentials(t *testing.T) {
14+
qa.ResourceFixture{
15+
Fixtures: []qa.HTTPFixture{
16+
{
17+
Method: "POST",
18+
Resource: "/api/2.0/unity-catalog/storage-credentials",
19+
ExpectedRequest: StorageCredentialInfo{
20+
Name: "a",
21+
Aws: &AwsIamRole{
22+
RoleARN: "def",
23+
},
24+
Comment: "c",
25+
},
26+
Response: StorageCredentialInfo{
27+
Name: "a",
28+
},
29+
},
30+
{
31+
Method: "GET",
32+
Resource: "/api/2.0/unity-catalog/storage-credentials/a",
33+
Response: StorageCredentialInfo{
34+
Name: "a",
35+
Aws: &AwsIamRole{
36+
RoleARN: "def",
37+
},
38+
MetastoreID: "d",
39+
},
40+
},
41+
},
42+
Resource: ResourceStorageCredential(),
43+
Create: true,
44+
HCL: `
45+
name = "a"
46+
aws_iam_role {
47+
role_arn = "def"
48+
}
49+
comment = "c"
50+
`,
51+
}.ApplyNoError(t)
52+
}
53+
54+
func TestUpdateStorageCredentials(t *testing.T) {
55+
qa.ResourceFixture{
56+
Fixtures: []qa.HTTPFixture{
57+
{
58+
Method: "PATCH",
59+
Resource: "/api/2.0/unity-catalog/storage-credentials/a",
60+
ExpectedRequest: StorageCredentialInfo{
61+
Name: "a",
62+
Aws: &AwsIamRole{
63+
RoleARN: "CHANGED",
64+
},
65+
},
66+
},
67+
{
68+
Method: "GET",
69+
Resource: "/api/2.0/unity-catalog/storage-credentials/a",
70+
Response: StorageCredentialInfo{
71+
Name: "a",
72+
Aws: &AwsIamRole{
73+
RoleARN: "CHANGED",
74+
},
75+
MetastoreID: "d",
76+
},
77+
},
78+
},
79+
Resource: ResourceStorageCredential(),
80+
Update: true,
81+
ID: "a",
82+
InstanceState: map[string]string{
83+
"name": "a",
84+
"comment": "c",
85+
},
86+
HCL: `
87+
name = "a"
88+
aws_iam_role {
89+
role_arn = "CHANGED"
90+
}
91+
comment = "c"
92+
`,
93+
}.ApplyNoError(t)
94+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func DatabricksProvider() *schema.Provider {
101101
"databricks_sql_query": sqlanalytics.ResourceQuery(),
102102
"databricks_sql_visualization": sqlanalytics.ResourceVisualization(),
103103
"databricks_sql_widget": sqlanalytics.ResourceWidget(),
104+
"databricks_storage_credential": catalog.ResourceStorageCredential(),
104105
"databricks_token": tokens.ResourceToken(),
105106
"databricks_user": scim.ResourceUser(),
106107
"databricks_user_instance_profile": aws.ResourceUserInstanceProfile(),

0 commit comments

Comments
 (0)