|
| 1 | +resource "azurerm_storage_data_lake_gen2_filesystem" "this" { |
| 2 | + count = var.create_metastore ? 1 : 0 |
| 3 | + |
| 4 | + name = "meta-${var.project}-${var.env}" |
| 5 | + storage_account_id = var.storage_account_id |
| 6 | + |
| 7 | + lifecycle { |
| 8 | + precondition { |
| 9 | + condition = alltrue([ |
| 10 | + for variable in [var.storage_account_id, var.access_connector_id, var.storage_account_name] : false if length(variable) == 0 |
| 11 | + ]) |
| 12 | + error_message = "To create Metastore in a Region it is required to provide proper values for these variables: access_connector_id, storage_account_id, storage_account_name" |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +resource "databricks_metastore" "this" { |
| 18 | + count = var.create_metastore ? 1 : 0 |
| 19 | + |
| 20 | + name = "meta-${var.project}-${var.env}-${var.location}${local.suffix}" |
| 21 | + storage_root = format("abfss://%s@%s.dfs.core.windows.net/", azurerm_storage_data_lake_gen2_filesystem.this[0].name, var.storage_account_name) |
| 22 | + force_destroy = true |
| 23 | +} |
| 24 | + |
| 25 | +resource "databricks_grants" "metastore" { |
| 26 | + for_each = !var.create_metastore && length(var.external_metastore_id) == 0 ? {} : { |
| 27 | + for k, v in var.metastore_grants : k => v |
| 28 | + if v != null |
| 29 | + } |
| 30 | + |
| 31 | + metastore = length(var.external_metastore_id) == 0 ? databricks_metastore.this[0].id : var.external_metastore_id |
| 32 | + grant { |
| 33 | + principal = each.key |
| 34 | + privileges = each.value |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +resource "databricks_metastore_data_access" "this" { |
| 39 | + count = var.create_metastore ? 1 : 0 |
| 40 | + |
| 41 | + metastore_id = databricks_metastore.this[0].id |
| 42 | + name = "data-access-${var.project}-${var.env}-${var.location}${local.suffix}" |
| 43 | + azure_managed_identity { |
| 44 | + access_connector_id = var.access_connector_id |
| 45 | + } |
| 46 | + is_default = true |
| 47 | +} |
| 48 | + |
| 49 | +resource "databricks_metastore_assignment" "this" { |
| 50 | + count = !var.create_metastore && length(var.external_metastore_id) == 0 ? 0 : 1 |
| 51 | + |
| 52 | + workspace_id = var.workspace_id |
| 53 | + metastore_id = length(var.external_metastore_id) == 0 ? databricks_metastore.this[0].id : var.external_metastore_id |
| 54 | + default_catalog_name = "hive_metastore" |
| 55 | +} |
| 56 | + |
| 57 | +# Catalog |
| 58 | +resource "databricks_catalog" "this" { |
| 59 | + for_each = !var.create_metastore && length(var.external_metastore_id) == 0 ? {} : var.catalog |
| 60 | + |
| 61 | + metastore_id = length(var.external_metastore_id) == 0 ? databricks_metastore.this[0].id : var.external_metastore_id |
| 62 | + name = each.key |
| 63 | + comment = lookup(each.value, "catalog_comment", "default comment") |
| 64 | + properties = merge(lookup(each.value, "catalog_properties", {}), { env = var.env }) |
| 65 | + force_destroy = true |
| 66 | + |
| 67 | + depends_on = [databricks_metastore_assignment.this[0]] |
| 68 | +} |
| 69 | + |
| 70 | +# Catalog grants |
| 71 | +resource "databricks_grants" "catalog" { |
| 72 | + for_each = !var.create_metastore && length(var.external_metastore_id) == 0 ? {} : { |
| 73 | + for name, params in var.catalog : name => params.catalog_grants |
| 74 | + if params.catalog_grants != null |
| 75 | + } |
| 76 | + |
| 77 | + catalog = databricks_catalog.this[each.key].name |
| 78 | + dynamic "grant" { |
| 79 | + for_each = each.value |
| 80 | + content { |
| 81 | + principal = grant.key |
| 82 | + privileges = grant.value |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +# Schema |
| 88 | +locals { |
| 89 | + schema = flatten([ |
| 90 | + for catalog, params in var.catalog : [ |
| 91 | + for schema in params.schema_name : { |
| 92 | + catalog = catalog, |
| 93 | + schema = schema, |
| 94 | + comment = lookup(params, "schema_comment", "default comment"), |
| 95 | + properties = lookup(params, "schema_properties", {}) |
| 96 | + } |
| 97 | + ] if params.schema_name != null |
| 98 | + ]) |
| 99 | +} |
| 100 | + |
| 101 | +resource "databricks_schema" "this" { |
| 102 | + for_each = !var.create_metastore && length(var.external_metastore_id) == 0 ? {} : { |
| 103 | + for entry in local.schema : "${entry.catalog}.${entry.schema}" => entry |
| 104 | + } |
| 105 | + |
| 106 | + catalog_name = databricks_catalog.this[each.value.catalog].name |
| 107 | + name = each.value.schema |
| 108 | + comment = each.value.comment |
| 109 | + properties = merge(each.value.properties, { env = var.env }) |
| 110 | + force_destroy = true |
| 111 | +} |
| 112 | + |
| 113 | +# Schema grants |
| 114 | +locals { |
| 115 | + schema_grants = flatten([ |
| 116 | + for catalog, params in var.catalog : [for schema in params.schema_name : [for principal in flatten(keys(params.schema_grants)) : { |
| 117 | + catalog = catalog, |
| 118 | + schema = schema, |
| 119 | + principal = principal, |
| 120 | + permission = flatten(values(params.schema_grants)), |
| 121 | + }]] if params.schema_grants != null |
| 122 | + ]) |
| 123 | +} |
| 124 | + |
| 125 | +resource "databricks_grants" "schema" { |
| 126 | + for_each = !var.create_metastore && length(var.external_metastore_id) == 0 ? {} : { |
| 127 | + for entry in local.schema_grants : "${entry.catalog}.${entry.schema}.${entry.principal}" => entry |
| 128 | + } |
| 129 | + |
| 130 | + schema = databricks_schema.this["${each.value.catalog}.${each.value.schema}"].id |
| 131 | + grant { |
| 132 | + principal = each.value.principal |
| 133 | + privileges = each.value.permission |
| 134 | + } |
| 135 | +} |
0 commit comments