Skip to content

Commit

Permalink
grant_access no longer common and takes in grant_target_dict param
Browse files Browse the repository at this point in the history
  • Loading branch information
azhard committed Jun 10, 2020
1 parent 78cf843 commit 8422c3a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
8 changes: 0 additions & 8 deletions core/dbt/include/global_project/macros/adapters/common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,3 @@
{{ config.set('sql_header', caller()) }}
{%- endmacro %}

{% macro grant_access_to(entity, entity_type, role, relation) -%}
{{ return(adapter_macro('grant_access_to', entity, entity_type, role, relation)) }}
{% endmacro %}

{% macro default__grant_access_to(entity, entity_type, role, relation) -%}
{{ exceptions.raise_not_implemented(
'grant_access_to macro not implemented for adapter '+adapter.type()) }}
{% endmacro %}
18 changes: 13 additions & 5 deletions plugins/bigquery/dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ def parse(cls, raw_partition_by) -> Optional['PartitionConfig']:
)


@dataclass
class GrantTarget(JsonSchemaMixin):
dataset: str
project: str

def render(self):
return f'{self.project}.{self.dataset}'


def _stub_relation(*args, **kwargs):
return BigQueryRelation.create(
database='',
Expand All @@ -94,7 +103,7 @@ class BigqueryConfig(AdapterConfig):
kms_key_name: Optional[str] = None
labels: Optional[Dict[str, str]] = None
partitions: Optional[List[str]] = None
grant_access_to: Optional[List[BigQueryRelation]] = None
grant_access_to: Optional[List[Dict[str, str]]] = None


class BigQueryAdapter(BaseAdapter):
Expand Down Expand Up @@ -677,17 +686,16 @@ def get_table_options(
return opts

@available.parse_none
def grant_access_to(self, entity, entity_type, role, relation):
def grant_access_to(self, entity, entity_type, role, grant_target_dict):
"""
Given an entity, grants it access to a permissioned dataset.
"""
conn = self.connections.get_thread_connection()
client = conn.handle

grant_target = GrantTarget.from_dict(grant_target_dict)
dataset = client.get_dataset(
self.connections.dataset_from_id(
f'{relation.database}.{relation.schema}'
)
self.connections.dataset_from_id(grant_target.render())
)

if entity_type == 'view':
Expand Down
4 changes: 0 additions & 4 deletions plugins/bigquery/dbt/include/bigquery/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,3 @@
{% macro bigquery__alter_column_comment(relation, column_dict) -%}
{% do adapter.update_column_descriptions(relation, column_dict) %}
{% endmacro %}

{% macro bigquery__grant_access_to(entity, entity_type, role, relation) -%}
{% do adapter.grant_access_to(entity, entity_type, role, relation) %}
{% endmacro %}
5 changes: 4 additions & 1 deletion plugins/bigquery/dbt/include/bigquery/macros/etc.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

{% macro date_sharded_table(base_name) %}
{{ return(base_name ~ "[DBT__PARTITION_DATE]") }}
{% endmacro %}

{% macro grant_access_to(entity, entity_type, role, grant_target_dict) -%}
{% do adapter.grant_access_to(entity, entity_type, role, grant_target_dict) %}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
{% do persist_docs(target_relation, model) %}

{% if config.get('grant_access_to') %}
{% for relation in config.get('grant_access_to') %}
{% do adapter.grant_access_to(this, 'view', None, relation) %}
{% for grant_target_dict in config.get('grant_access_to') %}
{% do adapter.grant_access_to(this, 'view', None, grant_target_dict) %}
{% endfor %}
{% endif %}

Expand Down

0 comments on commit 8422c3a

Please sign in to comment.