@@ -77,28 +77,29 @@ def _get_routine_reference(self, routine_id):
7777class AccessEntry (object ):
7878"""Represents grant of an access role to an entity.
7979
80- An entry must have exactly one of the allowed :attr:`ENTITY_TYPES`. If
81- anything but ``view`` or ``routine`` are set, a ``role`` is also required.
82- ``role ``is omitted for ``view ``and ``routine``, because they are always
83- read-only.
80+ An entry must have exactly one of the allowed
81+ :class:`google.cloud.bigquery.enums.EntityTypes`. If anything but ``view``, ``routine``,
82+ or ``dataset ``are set, a ``role ``is also required. ``role`` is omitted for ``view``,
83+ ``routine``, ``dataset``, because they are always read-only.
8484
8585 See https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets.
8686
8787 Args:
8888 role (str):
8989 Role granted to the entity. The following string values are
9090 supported: `'READER'`, `'WRITER'`, `'OWNER'`. It may also be
91- :data:`None` if the ``entity_type`` is ``view`` or ``routine ``.
91+ :data:`None` if the ``entity_type`` is ``view``, ``routine``, or ``dataset ``.
9292
9393 entity_type (str):
94- Type of entity being granted the role. One of :attr:`ENTITY_TYPES`.
94+ Type of entity being granted the role. See
95+ :class:`google.cloud.bigquery.enums.EntityTypes` for supported types.
9596
9697 entity_id (Union[str, Dict[str, str]]):
97- If the ``entity_type`` is not 'view' or 'routine ', the ``entity_id``
98- is the ``str`` ID of the entity being granted the role. If the
99- ``entity_type`` is 'view' or 'routine', the ``entity_id`` is a ``dict``
100- representing the view or routine from a different dataset to grant
101- access to in the following format for views::
98+ If the ``entity_type`` is not 'view', 'routine', or 'dataset ', the
99+ ``entity_id`` is the ``str`` ID of the entity being granted the role. If
100+ the ``entity_type`` is 'view' or 'routine', the ``entity_id`` is a ``dict``
101+ representing the view or routine from a different dataset to grant access
102+ to in the following format for views::
102103
103104 {
104105 'projectId': string,
@@ -114,11 +115,22 @@ class AccessEntry(object):
114115 'routineId': string
115116 }
116117
118+ If the ``entity_type`` is 'dataset', the ``entity_id`` is a ``dict`` that includes
119+ a 'dataset' field with a ``dict`` representing the dataset and a 'target_types'
120+ field with a ``str`` value of the dataset's resource type::
121+
122+ {
123+ 'dataset': {
124+ 'projectId': string,
125+ 'datasetId': string,
126+ },
127+ 'target_types: 'VIEWS'
128+ }
129+
117130 Raises:
118131 ValueError:
119- If the ``entity_type`` is not among :attr:`ENTITY_TYPES`, or if a
120- ``view`` or a ``routine`` has ``role`` set, or a non ``view`` and
121- non ``routine`` **does not** have a ``role`` set.
132+ If a ``view``, ``routine``, or ``dataset`` has ``role`` set, or a non ``view``,
133+ non ``routine``, and non ``dataset`` **does not** have a ``role`` set.
122134
123135 Examples:
124136 >>> entry = AccessEntry('OWNER', 'userByEmail', 'user@example.com')
@@ -131,27 +143,9 @@ class AccessEntry(object):
131143 >>> entry = AccessEntry(None, 'view', view)
132144 """
133145
134- ENTITY_TYPES = frozenset (
135- [
136- "userByEmail" ,
137- "groupByEmail" ,
138- "domain" ,
139- "specialGroup" ,
140- "view" ,
141- "iamMember" ,
142- "routine" ,
143- ]
144- )
145- """Allowed entity types."""
146-
147- def __init__ (self ,role ,entity_type ,entity_id ):
148- if entity_type not in self .ENTITY_TYPES :
149- message = "Entity type %r not among: %s" % (
150- entity_type ,
151- ", " .join (self .ENTITY_TYPES ),
152- )
153- raise ValueError (message )
154- if entity_type in ("view" ,"routine" ):
146+ def __init__ (self ,role = None ,entity_type = None ,entity_id = None ):
147+ self ._properties = {}
148+ if entity_type in ("view" ,"routine" ,"dataset" ):
155149if role is not None :
156150raise ValueError (
157151"Role must be None for a %r. Received "
@@ -162,7 +156,6 @@ def __init__(self, role, entity_type, entity_id):
162156raise ValueError (
163157"Role must be set for entity " "type %r" % (entity_type ,)
164158 )
165-
166159self ._role = role
167160self ._entity_type = entity_type
168161self ._entity_id = entity_id
@@ -214,7 +207,8 @@ def to_api_repr(self):
214207 Returns:
215208 Dict[str, object]: Access entry represented as an API resource
216209 """
217- resource = {self ._entity_type :self ._entity_id }
210+ resource = copy .deepcopy (self ._properties )
211+ resource [self ._entity_type ]= self ._entity_id
218212if self ._role is not None :
219213resource ["role" ]= self ._role
220214return resource
@@ -241,7 +235,10 @@ def from_api_repr(cls, resource: dict) -> "AccessEntry":
241235entity_type ,entity_id = entry .popitem ()
242236if len (entry )!= 0 :
243237raise ValueError ("Entry has unexpected keys remaining." ,entry )
244- return cls (role ,entity_type ,entity_id )
238+
239+ config = cls (role ,entity_type ,entity_id )
240+ config ._properties = copy .deepcopy (resource )
241+ return config
245242
246243
247244class DatasetReference (object ):