Skip to content

Commit

Permalink
[Internal] Simplify databricks_storage_credential code
Browse files Browse the repository at this point in the history
Previously, the `DatabricksGcpServiceAccount` type was defined as `any` and the led to the
problems sending the Create request. Now this type is declared as struct, so we don't need
the workaround anymore.
  • Loading branch information
alexott committed Dec 7, 2024
1 parent 2f4b570 commit 77a2cde
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
7 changes: 1 addition & 6 deletions catalog/resource_metastore_data_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ func ResourceMetastoreDataAccess() common.Resource {
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
metastoreId := d.Get("metastore_id").(string)

tmpSchema := removeGcpSaField(dacSchema)
var create catalog.CreateStorageCredential
common.DataToStructPointer(d, tmpSchema, &create)
//manually add empty struct back for databricks_gcp_service_account
if _, ok := d.GetOk("databricks_gcp_service_account"); ok {
create.DatabricksGcpServiceAccount = &catalog.DatabricksGcpServiceAccountRequest{}
}
common.DataToStructPointer(d, dacSchema, &create)

return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
dac, err := acc.StorageCredentials.Create(ctx,
Expand Down
22 changes: 4 additions & 18 deletions catalog/resource_storage_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,14 @@ type StorageCredentialInfo struct {
IsolationMode string `json:"isolation_mode,omitempty" tf:"computed"`
}

func removeGcpSaField(originalSchema map[string]*schema.Schema) map[string]*schema.Schema {
//common.DataToStructPointer(d, s, &create) will error out because of DatabricksGcpServiceAccount any
tmpSchema := make(map[string]*schema.Schema)
for k, v := range originalSchema {
tmpSchema[k] = v
}
delete(tmpSchema, "databricks_gcp_service_account")
return tmpSchema
}

var storageCredentialSchema = common.StructToSchema(StorageCredentialInfo{},
func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["storage_credential_id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
common.MustSchemaPath(m, "databricks_gcp_service_account", "email").Computed = true
common.MustSchemaPath(m, "databricks_gcp_service_account", "credential_id").Computed = true
return adjustDataAccessSchema(m)
})

Expand All @@ -50,19 +42,13 @@ func ResourceStorageCredential() common.Resource {
Schema: storageCredentialSchema,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
metastoreId := d.Get("metastore_id").(string)
tmpSchema := removeGcpSaField(storageCredentialSchema)

var create catalog.CreateStorageCredential
var update catalog.UpdateStorageCredential
common.DataToStructPointer(d, tmpSchema, &create)
common.DataToStructPointer(d, tmpSchema, &update)
common.DataToStructPointer(d, storageCredentialSchema, &create)
common.DataToStructPointer(d, storageCredentialSchema, &update)
update.Name = d.Get("name").(string)

//manually add empty struct back for databricks_gcp_service_account
if _, ok := d.GetOk("databricks_gcp_service_account"); ok {
create.DatabricksGcpServiceAccount = &catalog.DatabricksGcpServiceAccountRequest{}
}

return c.AccountOrWorkspaceRequest(func(acc *databricks.AccountClient) error {
storageCredential, err := acc.StorageCredentials.Create(ctx,
catalog.AccountsCreateStorageCredential{
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/storage_credential.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ resource "databricks_storage_credential" "external_mi" {
}
resource "databricks_grants" "external_creds" {
storage_credential = databricks_storage_credential.external.id
storage_credential = databricks_storage_credential.external_mi.id
grant {
principal = "Data Engineers"
privileges = ["CREATE_EXTERNAL_TABLE"]
Expand Down

0 comments on commit 77a2cde

Please sign in to comment.