def test_ctor_explicit(self): from google.cloud.bigquery.dataset import AccessGrant phred = AccessGrant('OWNER', 'userByEmail', '*****@*****.**') bharney = AccessGrant('OWNER', 'userByEmail', '*****@*****.**') grants = [phred, bharney] OTHER_PROJECT = 'foo-bar-123' client = _Client(self.PROJECT) dataset = self._make_one(self.DS_NAME, client, access_grants=grants, project=OTHER_PROJECT) self.assertEqual(dataset.name, self.DS_NAME) self.assertIs(dataset._client, client) self.assertEqual(dataset.project, OTHER_PROJECT) self.assertEqual( dataset.path, '/projects/%s/datasets/%s' % (OTHER_PROJECT, self.DS_NAME)) self.assertEqual(dataset.access_grants, grants) self.assertIsNone(dataset.created) self.assertIsNone(dataset.dataset_id) self.assertIsNone(dataset.etag) self.assertIsNone(dataset.modified) self.assertIsNone(dataset.self_link) self.assertIsNone(dataset.default_table_expiration_ms) self.assertIsNone(dataset.description) self.assertIsNone(dataset.friendly_name) self.assertIsNone(dataset.location)
def test_access_grants_setter(self): from google.cloud.bigquery.dataset import AccessGrant client = _Client(self.PROJECT) dataset = self._make_one(self.DS_NAME, client) phred = AccessGrant('OWNER', 'userByEmail', '*****@*****.**') bharney = AccessGrant('OWNER', 'userByEmail', '*****@*****.**') dataset.access_grants = [phred, bharney] self.assertEqual(dataset.access_grants, [phred, bharney])
def test_access_grants_setter_invalid_field(self): from google.cloud.bigquery.dataset import AccessGrant client = _Client(self.PROJECT) dataset = self._make_one(self.DS_NAME, client) phred = AccessGrant('OWNER', 'userByEmail', '*****@*****.**') with self.assertRaises(ValueError): dataset.access_grants = [phred, object()]
def test_create_w_alternate_client(self): from google.cloud.bigquery.dataset import AccessGrant PATH = 'projects/%s/datasets' % self.PROJECT USER_EMAIL = '*****@*****.**' GROUP_EMAIL = '*****@*****.**' DESCRIPTION = 'DESCRIPTION' TITLE = 'TITLE' RESOURCE = self._makeResource() RESOURCE['description'] = DESCRIPTION RESOURCE['friendlyName'] = TITLE conn1 = _Connection() CLIENT1 = _Client(project=self.PROJECT, connection=conn1) conn2 = _Connection(RESOURCE) CLIENT2 = _Client(project=self.PROJECT, connection=conn2) dataset = self._makeOne(self.DS_NAME, client=CLIENT1) dataset.friendly_name = TITLE dataset.description = DESCRIPTION VIEW = { 'projectId': 'my-proj', 'datasetId': 'starry-skies', 'tableId': 'northern-hemisphere', } dataset.access_grants = [ AccessGrant('OWNER', 'userByEmail', USER_EMAIL), AccessGrant('OWNER', 'groupByEmail', GROUP_EMAIL), AccessGrant('READER', 'domain', 'foo.com'), AccessGrant('READER', 'specialGroup', 'projectReaders'), AccessGrant('WRITER', 'specialGroup', 'projectWriters'), AccessGrant(None, 'view', VIEW), ] dataset.create(client=CLIENT2) self.assertEqual(len(conn1._requested), 0) self.assertEqual(len(conn2._requested), 1) req = conn2._requested[0] self.assertEqual(req['method'], 'POST') self.assertEqual(req['path'], '/%s' % PATH) SENT = { 'datasetReference': { 'projectId': self.PROJECT, 'datasetId': self.DS_NAME, }, 'description': DESCRIPTION, 'friendlyName': TITLE, 'access': [ {'role': 'OWNER', 'userByEmail': USER_EMAIL}, {'role': 'OWNER', 'groupByEmail': GROUP_EMAIL}, {'role': 'READER', 'domain': 'foo.com'}, {'role': 'READER', 'specialGroup': 'projectReaders'}, {'role': 'WRITER', 'specialGroup': 'projectWriters'}, {'view': VIEW}, ], } self.assertEqual(req['data'], SENT) self._verifyResourceProperties(dataset, RESOURCE)
def _sink_bigquery_setup(client): from google.cloud import bigquery DATASET_NAME = 'sink_bigquery_%d' % (_millis(), ) client = bigquery.Client() dataset = client.dataset(DATASET_NAME) dataset.create() dataset.reload() # [START sink_dataset_permissions] from google.cloud.bigquery.dataset import AccessGrant grants = dataset.access_grants grants.append( AccessGrant('WRITER', 'groupByEmail', '*****@*****.**')) dataset.access_grants = grants dataset.update() # API call # [END sink_dataset_permissions] return dataset
def _init_bigquery_dataset(self): from google.cloud import bigquery from google.cloud.bigquery.dataset import AccessGrant DATASET_NAME = ( 'system_testing_dataset' + _RESOURCE_ID).replace('-', '_') DATASET_URI = 'bigquery.googleapis.com/projects/%s/datasets/%s' % ( Config.CLIENT.project, DATASET_NAME,) # Create the destination dataset, and set up the ACL to allow # Stackdriver Logging to write into it. bigquery_client = bigquery.Client() dataset = bigquery_client.dataset(DATASET_NAME) dataset.create() self.to_delete.append(dataset) dataset.reload() grants = dataset.access_grants grants.append(AccessGrant( 'WRITER', 'groupByEmail', '*****@*****.**')) dataset.access_grants = grants dataset.update() return DATASET_URI