Skip to content

New healthcare #2089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
623ce7c
dicom_stores tests pass (when discovery_url is active)
engelke Mar 15, 2019
52ce620
Get/Set IAM policies for fhir_stores
engelke Mar 20, 2019
c8b6a23
Updated test
engelke Mar 20, 2019
43f13b6
IAM operations on fhir_stores complete
engelke Mar 20, 2019
1055a41
IAM policy get/set for hl7v2 stores
engelke Mar 20, 2019
032ff93
IAM get/set for dicom stores
engelke Mar 20, 2019
8978635
IAM get/set policy for datasets
engelke Mar 20, 2019
72a4255
Added list_resource_history
engelke Mar 22, 2019
673e0f4
Added get_resource_history
engelke Mar 22, 2019
ba514e0
Added delete_resource_purge
engelke Mar 22, 2019
2396490
Added conditional_update_resource
engelke Mar 22, 2019
fa79fb9
Added conditional_delete_resource
engelke Mar 22, 2019
980357c
Added conditional_patch_resource
engelke Mar 22, 2019
ec7b731
Added fhir resource import and export
engelke Mar 22, 2019
11c5315
Added execute_bundle
engelke Mar 22, 2019
14136bb
Dicomweb search instances
engelke Mar 22, 2019
2d70b36
Cleaning up FHIR resources
engelke Mar 26, 2019
87834b6
All dicom tests running and passing
engelke Apr 3, 2019
ec7b6c0
No longer skipping datasets tests
engelke Apr 3, 2019
a18101e
All fhir tests running and passing
engelke Apr 3, 2019
bdb04c6
All hl7v2 tests running and passing
engelke Apr 3, 2019
e274bf5
Fix lint issues
engelke Apr 4, 2019
ccd8ce2
More lint issues
engelke Apr 4, 2019
724f254
Removed trailing whitespace.
engelke Apr 4, 2019
9085f06
Fixing lint errors
engelke Apr 4, 2019
3a9a350
Fixed API version
engelke Apr 4, 2019
565e02f
A new lint error appears
engelke Apr 4, 2019
7c6472b
Lint
engelke Apr 4, 2019
2c1c3f6
Merge branch 'master' into new_healthcare
gguuss Apr 4, 2019
767d17c
Fixed API version
engelke Apr 4, 2019
11dcd7a
Merge branch 'new_healthcare' of github.com:GoogleCloudPlatform/pytho…
engelke Apr 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 107 additions & 2 deletions healthcare/api-client/datasets/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def get_client(service_account_json, api_key):
service_account_json)
scoped_credentials = credentials.with_scopes(api_scopes)

discovery_url = '{}?labels=CHC_ALPHA&version={}&key={}'.format(
discovery_url = '{}?labels=CHC_BETA&version={}&key={}'.format(
discovery_api, api_version, api_key)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. trailing whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just fixed the two lint errors. I'll remove the trailing whitespace ASAP.

return discovery.build(
service_name,
api_version,
Expand Down Expand Up @@ -237,6 +237,80 @@ def deidentify_dataset(
# [END healthcare_deidentify_dataset]


# [START healthcare_dataset_get_iam_policy]
def get_dataset_iam_policy(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id):
"""Gets the IAM policy for the specified dataset."""
client = get_client(service_account_json, api_key)
dataset_name = 'projects/{}/locations/{}/datasets/{}'.format(
project_id, cloud_region, dataset_id)

request = client.projects().locations().datasets().getIamPolicy(
resource=dataset_name)
response = request.execute()

print('etag: {}'.format(response.get('name')))
return response
# [END healthcare_dataset_get_iam_policy]


# [START healthcare_dataset_set_iam_policy]
def set_dataset_iam_policy(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id,
member,
role,
etag = None):
"""Sets the IAM policy for the specified dataset.

A single member will be assigned a single role. A member can be any of:

- allUsers, that is, anyone
- allAuthenticatedUsers, anyone authenticated with a Google account
- user:email, as in 'user:[email protected]'
- group:email, as in 'group:[email protected]'
- domain:domainname, as in 'domain:example.com'
- serviceAccount:email,
as in 'serviceAccount:[email protected]'

A role can be any IAM role, such as 'roles/viewer', 'roles/owner',
or 'roles/editor'
"""
client = get_client(service_account_json, api_key)
dataset_name = 'projects/{}/locations/{}/datasets/{}'.format(
project_id, cloud_region, dataset_id)

policy = {
"bindings": [
{
"role": role,
"members": [
member
]
}
]
}

if etag is not None:
policy['etag'] = etag

request = client.projects().locations().datasets().setIamPolicy(
resource=dataset_name, body={'policy': policy})
response = request.execute()

print('etag: {}'.format(response.get('name')))
print('bindings: {}'.format(response.get('bindings')))
return response
# [END healthcare_dataset_set_iam_policy]


def parse_command_line_args():
"""Parses command line arguments."""

Expand Down Expand Up @@ -286,13 +360,26 @@ def parse_command_line_args():
help='The data to keeplist, for example "PatientID" '
'or "StudyInstanceUID"')

parser.add_argument(
'--member',
default=None,
help='Member to add to IAM policy (e.g. "domain:example.com")')

parser.add_argument(
'--role',
default=None,
help='IAM Role to give to member (e.g. "roles/viewer")')


command = parser.add_subparsers(dest='command')

command.add_parser('create-dataset', help=create_dataset.__doc__)
command.add_parser('delete-dataset', help=delete_dataset.__doc__)
command.add_parser('get-dataset', help=get_dataset.__doc__)
command.add_parser('list-datasets', help=list_datasets.__doc__)
command.add_parser('patch-dataset', help=patch_dataset.__doc__)
command.add_parser('get_iam_policy', help=get_dataset_iam_policy.__doc__)
command.add_parser('set_iam_policy', help=set_dataset_iam_policy.__doc__)

command.add_parser('deidentify-dataset', help=deidentify_dataset.__doc__)

Expand Down Expand Up @@ -356,6 +443,24 @@ def run_command(args):
args.destination_dataset_id,
args.keeplist_tags)

elif args.command == 'get_iam_policy':
get_dataset_iam_policy(
args.service_account_json,
args.api_key,
args.project_id,
args.cloud_region,
args.dataset_id)

elif args.command == 'set_iam_policy':
set_dataset_iam_policy(
args.service_account_json,
args.api_key,
args.project_id,
args.cloud_region,
args.dataset_id,
args.member,
args.role)


def main():
args = parse_command_line_args()
Expand Down
44 changes: 41 additions & 3 deletions healthcare/api-client/datasets/datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
time_zone = 'UTC'


@pytest.mark.skip(reason='disable until API whitelisted / enabled')
def test_CRUD_dataset(capsys):
datasets.create_dataset(
service_account_json,
Expand Down Expand Up @@ -57,7 +56,6 @@ def test_CRUD_dataset(capsys):
assert 'Deleted dataset' in out


@pytest.mark.skip(reason='disable until API whitelisted / enabled')
def test_patch_dataset(capsys):
datasets.create_dataset(
service_account_json,
Expand All @@ -84,7 +82,6 @@ def test_patch_dataset(capsys):
assert 'UTC' in out


@pytest.mark.skip(reason='disable until API whitelisted / enabled')
def test_deidentify_dataset(capsys):
datasets.create_dataset(
service_account_json,
Expand Down Expand Up @@ -116,3 +113,44 @@ def test_deidentify_dataset(capsys):

# Check that de-identify worked
assert 'De-identified data written to' in out


def test_get_set_dataset_iam_policy(capsys):
datasets.create_dataset(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id)

get_response = datasets.get_dataset_iam_policy(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id)

set_response = datasets.set_dataset_iam_policy(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id,
'serviceAccount:[email protected]',
'roles/viewer')

# Clean up
datasets.delete_dataset(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id)

out, _ = capsys.readouterr()

assert 'etag' in get_response
assert 'bindings' in set_response
assert len(set_response['bindings']) == 1
assert 'python-docs-samples-tests' in str(set_response['bindings'])
assert 'roles/viewer' in str(set_response['bindings'])
Loading