Skip to content

Commit d15fecb

Browse files
authored
Added azure_managed_identity block to databricks_storage_credential and databricks_metastore_data_access resources (#1354)
1 parent f7c8d38 commit d15fecb

File tree

6 files changed

+146
-8
lines changed

6 files changed

+146
-8
lines changed

catalog/resource_metastore_data_access.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ type AzureServicePrincipal struct {
2727
ClientSecret string `json:"client_secret"`
2828
}
2929

30+
type AzureManagedIdentity struct {
31+
AccessConnectorID string `json:"access_connector_id"`
32+
}
33+
3034
type DataAccessConfiguration struct {
3135
ID string `json:"id,omitempty" tf:"computed"`
3236
Name string `json:"name"`
3337
ConfigurationType string `json:"configuration_type,omitempty" tf:"computed"`
3438
Aws *AwsIamRole `json:"aws_iam_role,omitempty" tf:"group:access"`
3539
Azure *AzureServicePrincipal `json:"azure_service_principal,omitempty" tf:"group:access"`
40+
AzMI *AzureManagedIdentity `json:"azure_managed_identity,omitempty" tf:"group:access"`
3641
}
3742

3843
func (a DataAccessConfigurationsAPI) Create(metastoreID string, dac *DataAccessConfiguration) error {
@@ -65,9 +70,10 @@ func ResourceDataAccessConfiguration() *schema.Resource {
6570
Type: schema.TypeBool,
6671
Optional: true,
6772
}
68-
alof := []string{"aws_iam_role", "azure_service_principal"}
73+
alof := []string{"aws_iam_role", "azure_service_principal", "azure_managed_identity"}
6974
m["aws_iam_role"].AtLeastOneOf = alof
7075
m["azure_service_principal"].AtLeastOneOf = alof
76+
m["azure_managed_identity"].AtLeastOneOf = alof
7177
return m
7278
})
7379
p := common.NewPairID("metastore_id", "id")

catalog/resource_metastore_data_access_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,57 @@ func TestCreateDac(t *testing.T) {
6464
`,
6565
}.ApplyNoError(t)
6666
}
67+
68+
func TestCreateDacWithAzMI(t *testing.T) {
69+
qa.ResourceFixture{
70+
Fixtures: []qa.HTTPFixture{
71+
{
72+
Method: "POST",
73+
Resource: "/api/2.0/unity-catalog/metastores/abc/data-access-configurations",
74+
ExpectedRequest: DataAccessConfiguration{
75+
Name: "bcd",
76+
AzMI: &AzureManagedIdentity{
77+
AccessConnectorID: "def",
78+
},
79+
},
80+
Response: DataAccessConfiguration{
81+
ID: "efg",
82+
},
83+
},
84+
{
85+
Method: "PATCH",
86+
Resource: "/api/2.0/unity-catalog/metastores/abc",
87+
ExpectedRequest: map[string]interface{}{
88+
"default_data_access_config_id": "efg",
89+
},
90+
},
91+
{
92+
Method: "GET",
93+
Resource: "/api/2.0/unity-catalog/metastores/abc/data-access-configurations/efg",
94+
Response: DataAccessConfiguration{
95+
Name: "bcd",
96+
AzMI: &AzureManagedIdentity{
97+
AccessConnectorID: "def",
98+
},
99+
},
100+
},
101+
{
102+
Method: "GET",
103+
Resource: "/api/2.0/unity-catalog/metastores/abc",
104+
Response: MetastoreInfo{
105+
DefaultDacID: "efg",
106+
},
107+
},
108+
},
109+
Create: true,
110+
Resource: ResourceDataAccessConfiguration(),
111+
HCL: `
112+
metastore_id = "abc"
113+
name = "bcd"
114+
is_default = true
115+
azure_managed_identity {
116+
access_connector_id = "def"
117+
}
118+
`,
119+
}.ApplyNoError(t)
120+
}

catalog/resource_storage_credentials.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type StorageCredentialInfo struct {
2222
Comment string `json:"comment,omitempty"`
2323
Aws *AwsIamRole `json:"aws_iam_role,omitempty" tf:"group:access"`
2424
Azure *AzureServicePrincipal `json:"azure_service_principal,omitempty" tf:"group:access"`
25+
AzMI *AzureManagedIdentity `json:"azure_managed_identity,omitempty" tf:"group:access"`
2526
MetastoreID string `json:"metastore_id,omitempty" tf:"computed"`
2627
}
2728

@@ -41,9 +42,10 @@ func (a StorageCredentialsAPI) delete(id string) error {
4142
func ResourceStorageCredential() *schema.Resource {
4243
s := common.StructToSchema(StorageCredentialInfo{},
4344
func(m map[string]*schema.Schema) map[string]*schema.Schema {
44-
alof := []string{"aws_iam_role", "azure_service_principal"}
45+
alof := []string{"aws_iam_role", "azure_service_principal", "azure_managed_identity"}
4546
m["aws_iam_role"].AtLeastOneOf = alof
4647
m["azure_service_principal"].AtLeastOneOf = alof
48+
m["azure_managed_identity"].AtLeastOneOf = alof
4749
return m
4850
})
4951
update := updateFunctionFactory("/unity-catalog/storage-credentials", []string{"owner", "comment", "aws_iam_role", "azure_service_principal"})

catalog/resource_storage_credentials_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,44 @@ func TestUpdateStorageCredentials(t *testing.T) {
137137
`,
138138
}.ApplyNoError(t)
139139
}
140+
141+
func TestCreateStorageCredentialWithAzMI(t *testing.T) {
142+
qa.ResourceFixture{
143+
Fixtures: []qa.HTTPFixture{
144+
{
145+
Method: "POST",
146+
Resource: "/api/2.0/unity-catalog/storage-credentials",
147+
ExpectedRequest: StorageCredentialInfo{
148+
Name: "a",
149+
AzMI: &AzureManagedIdentity{
150+
AccessConnectorID: "def",
151+
},
152+
Comment: "c",
153+
},
154+
Response: StorageCredentialInfo{
155+
Name: "a",
156+
},
157+
},
158+
{
159+
Method: "GET",
160+
Resource: "/api/2.0/unity-catalog/storage-credentials/a",
161+
Response: StorageCredentialInfo{
162+
Name: "a",
163+
AzMI: &AzureManagedIdentity{
164+
AccessConnectorID: "def",
165+
},
166+
MetastoreID: "d",
167+
},
168+
},
169+
},
170+
Resource: ResourceStorageCredential(),
171+
Create: true,
172+
HCL: `
173+
name = "a"
174+
azure_managed_identity {
175+
access_connector_id = "def"
176+
}
177+
comment = "c"
178+
`,
179+
}.ApplyNoError(t)
180+
}

docs/resources/metastore_data_access.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "databricks_metastore_data_access" "this" {
2929
}
3030
```
3131

32-
For Azure
32+
For Azure using service principal as credential
3333

3434
```hcl
3535
resource "databricks_metastore" "this" {
@@ -43,7 +43,7 @@ resource "databricks_metastore" "this" {
4343
4444
resource "databricks_metastore_data_access" "this" {
4545
metastore_id = databricks_metastore.this.id
46-
name = aws_iam_role.metastore_data_access.name
46+
name = "sp_dac"
4747
azure_service_principal {
4848
directory_id = var.tenant_id
4949
application_id = azuread_application.unity_catalog.application_id
@@ -53,6 +53,28 @@ resource "databricks_metastore_data_access" "this" {
5353
}
5454
```
5555

56+
For Azure using managed identity as credential (Private Preview)
57+
58+
```hcl
59+
resource "databricks_metastore" "this" {
60+
name = "primary"
61+
storage_root = format("abfss://%s@%s.dfs.core.windows.net/",
62+
azurerm_storage_account.unity_catalog.name,
63+
azurerm_storage_container.unity_catalog.name)
64+
owner = "uc admins"
65+
force_destroy = true
66+
}
67+
68+
resource "databricks_metastore_data_access" "this" {
69+
metastore_id = databricks_metastore.this.id
70+
name = "mi_dac"
71+
azure_managed_identity {
72+
access_connector_id = var.access_connector_id
73+
}
74+
is_default = true
75+
}
76+
```
77+
5678
## Argument Reference
5779

5880
The following arguments are required:
@@ -68,6 +90,9 @@ The following arguments are required:
6890
* `application_id` - The application ID of the application registration within the referenced AAD tenant
6991
* `client_secret` - The client secret generated for the above app ID in AAD. **This field is redacted on output**
7092

93+
`azure_managed_identity` optional configuration block for using managed identity as credential details for Azure:
94+
* `access_connector_id` - The Resource ID of the Azure Databricks Access Connector resource, of the form `/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-name/providers/Microsoft.Databricks/accessConnectors/connector-name`
95+
7196
## Import
7297

7398
-> **Note** Importing this resource is not currently supported.

docs/resources/storage_credential.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,22 @@ resource "databricks_grants" "external_creds" {
3434
For Azure
3535

3636
```hcl
37-
resource "databricks_storage_credential" "external" {
37+
resource "databricks_storage_credential" "external_sp" {
3838
name = azuread_application.ext_cred.display_name
3939
azure_service_principal {
4040
directory_id = var.tenant_id
4141
application_id = azuread_application.ext_cred.application_id
4242
client_secret = azuread_application_password.ext_cred.value
4343
}
44-
comment = "Managed by TF"
44+
comment = "SP credential managed by TF"
45+
}
46+
47+
resource "databricks_storage_credential" "external_mi" {
48+
name = "mi_credential"
49+
azure_managed_identity {
50+
access_connector_id = var.access_connector_id
51+
}
52+
comment = "Managed identity credential managed by TF"
4553
}
4654
4755
resource "databricks_grants" "external_creds" {
@@ -62,11 +70,13 @@ The following arguments are required:
6270
`aws_iam_role` optional configuration block for credential details for AWS:
6371
* `role_arn` - The Amazon Resource Name (ARN) of the AWS IAM role for S3 data access, of the form `arn:aws:iam::1234567890:role/MyRole-AJJHDSKSDF`
6472

65-
`azure_service_principal` optional configuration block for credential details for Azure:
73+
`azure_service_principal` optional configuration block to use service principal as credential details for Azure:
6674
* `directory_id` - The directory ID corresponding to the Azure Active Directory (AAD) tenant of the application
6775
* `application_id` - The application ID of the application registration within the referenced AAD tenant
6876
* `client_secret` - The client secret generated for the above app ID in AAD. **This field is redacted on output**
69-
* `owner` - (Optional) Username/groupname/sp application_id storage credential owner.
77+
78+
`azure_managed_identity` optional configuration block for using managed identity as credential details for Azure:
79+
* `access_connector_id` - The Resource ID of the Azure Databricks Access Connector resource, of the form `/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-name/providers/Microsoft.Databricks/accessConnectors/connector-name`
7080

7181
## Import
7282

0 commit comments

Comments
 (0)