Skip to content

Update export FHIR store to use GCS bucket. #1803

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 6 commits into from
Nov 21, 2018
Merged
Changes from all commits
Commits
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
69 changes: 31 additions & 38 deletions healthcare/api-client/fhir/fhir_stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ def patch_fhir_store(
# [END healthcare_patch_fhir_store]


# [START healthcare_export_fhir_resource]
def export_fhir_resource(
# [START healthcare_export_fhir_store_gcs]
def export_fhir_store_gcs(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id,
fhir_store_id,
uri_prefix):
gcs_uri):
"""Export resources to a Google Cloud Storage bucket by copying
them from the FHIR store."""
client = get_client(service_account_json, api_key)
Expand All @@ -220,12 +220,9 @@ def export_fhir_resource(
fhir_store_parent, fhir_store_id)

body = {
"outputConfig":
"gcsDestinationLocation":
{
"gcsDestination":
{
"uriPrefix": 'gs://{}'.format(uri_prefix)
}
"gcsUri": 'gs://{}'.format(gcs_uri)
}
}

Expand All @@ -234,23 +231,23 @@ def export_fhir_resource(

try:
response = request.execute()
print('Exported FHIR resources to bucket: gs://{}'.format(uri_prefix))
print('Exported FHIR resources to bucket: gs://{}'.format(gcs_uri))
return response
except HttpError as e:
print('Error, FHIR resources not exported: {}'.format(e))
return ""
# [END healthcare_export_fhir_resource]
# [END healthcare_export_fhir_store_gcs]


# [START healthcare_import_fhir_resource]
def import_fhir_resource(
# [START healthcare_import_fhir_store]
def import_fhir_store(
service_account_json,
api_key,
project_id,
cloud_region,
dataset_id,
fhir_store_id,
content_uri):
gcs_uri):
"""Import resources into the FHIR store by copying them from the
specified source.
"""
Expand All @@ -261,12 +258,13 @@ def import_fhir_resource(
fhir_store_parent, fhir_store_id)

body = {
"inputConfig":
"gcsSourceLocation":
{
"gcsSource":
{
"contentUri": 'gs://{}'.format(content_uri)
}
"gcsUri": 'gs://{}'.format(gcs_uri)
},
"gcsErrorLocation":
{
"gcsUri": 'gs://{}/errors'.format(gcs_uri)
}
}

Expand All @@ -277,12 +275,12 @@ def import_fhir_resource(

try:
response = request.execute()
print('Imported FHIR resources: {}'.format(content_uri))
print('Imported FHIR resources: {}'.format(gcs_uri))
return response
except HttpError as e:
print('Error, FHIR resources not imported: {}'.format(e))
return ""
# [END healthcare_import_fhir_resource]
# [END healthcare_import_fhir_store]


def parse_command_line_args():
Expand Down Expand Up @@ -329,16 +327,11 @@ def parse_command_line_args():
'are published')

parser.add_argument(
'--uri_prefix',
default=None,
help='URI for a Google Cloud Storage directory to which result files'
'should be written (e.g., "bucket-id/path/to/destination/dir").')

parser.add_argument(
'--content_uri',
'--gcs_uri',
default=None,
help='URI for a Google Cloud Storage directory from which files'
'should be imported (e.g., "bucket-id/path/to/destination/dir").')
'should be import or to which result files'
'should be written (e.g., "bucket-id/path/to/destination/dir").')

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

Expand All @@ -348,11 +341,11 @@ def parse_command_line_args():
command.add_parser('list-fhir-stores', help=list_fhir_stores.__doc__)
command.add_parser('patch-fhir-store', help=patch_fhir_store.__doc__)
command.add_parser(
'export-fhir-resource',
help=import_fhir_resource.__doc__)
'import-fhir-store',
help=import_fhir_store.__doc__)
command.add_parser(
'import-fhir-resource',
help=export_fhir_resource.__doc__)
'export-fhir-store-gcs',
help=export_fhir_store_gcs.__doc__)

return parser.parse_args()

Expand Down Expand Up @@ -409,25 +402,25 @@ def run_command(args):
args.fhir_store_id,
args.pubsub_topic)

elif args.command == 'export-fhir-resource':
patch_fhir_store(
elif args.command == 'export-fhir-store-gcs':
export_fhir_store_gcs(
args.service_account_json,
args.api_key,
args.project_id,
args.cloud_region,
args.dataset_id,
args.fhir_store_id,
args.uri_prefix)
args.gcs_uri)

elif args.command == 'import-fhir-resource':
patch_fhir_store(
elif args.command == 'import-fhir-store':
import_fhir_store(
args.service_account_json,
args.api_key,
args.project_id,
args.cloud_region,
args.dataset_id,
args.fhir_store_id,
args.content_uri)
args.gcs_uri)


def main():
Expand Down