Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#59 from GoogleCloudPlatform/qu…
Browse files Browse the repository at this point in the history
…ickstart

Add quick start sample.
  • Loading branch information
jerjou committed Jul 27, 2015
2 parents 2fe2289 + e9f8cb5 commit b453dc5
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
105 changes: 105 additions & 0 deletions bigquery/samples/list_datasets_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
# Copyright 2015, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Command-line skeleton application for BigQuery API.
This is the sample for this page:
https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
In order to run it, your environment must be setup with authentication
information [1]. If you're running it in your local development environment and
you have the Google Cloud SDK [2] installed, you can do this easily by running:
$ gcloud auth login
Usage:
$ python list_datasets_projects.py <project-id>
where <project-id> is the id of the developers console [3] project you'd like
to list the bigquery datasets and projects for.
[1] https://developers.google.com/identity/protocols/application-default-credentials#howtheywork
[2] https://cloud.google.com/sdk/
[3] https://console.developers.google.com
""" # NOQA

import argparse
from pprint import pprint

from urllib2 import HTTPError

from apiclient import discovery

from oauth2client.client import GoogleCredentials


# [START list_datasets]
def list_datasets(service, project):
try:
datasets = service.datasets()
list_reply = datasets.list(projectId=project).execute()
print('Dataset list:')
pprint(list_reply)

except HTTPError as err:
print('Error in list_datasets: %s' % err.content)
# [END list_datasets]


# [START list_projects]
def list_projects(service):
try:
# Start training on a data set
projects = service.projects()
list_reply = projects.list().execute()

print('Project list:')
pprint(list_reply)

except HTTPError as err:
print('Error in list_projects: %s' % err.content)
# [END list_projects]


def main(project_id):
credentials = GoogleCredentials.get_application_default()
# Construct the service object for the interacting with the BigQuery API.
service = discovery.build('bigquery', 'v2', credentials=credentials)

list_datasets(service, project_id)
list_projects(service)


# For more information on the BigQuery API you can visit:
#
# https://developers.google.com/bigquery/docs/overview
#
# For more information on the BigQuery API Python library surface you
# can visit:
#
# https://developers.google.com/resources/api-libraries/documentation/bigquery/v2/python/latest/
#
# For information on the Python Client Library visit:
#
# https://developers.google.com/api-client-library/python/start/get_started
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Lists BigQuery datasets and projects.')
parser.add_argument('project_id', help='the project id to list.')

args = parser.parse_args()

main(args.project_id)
35 changes: 35 additions & 0 deletions bigquery/tests/test_list_datasets_projects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2015, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
import unittest

from bigquery.samples import list_datasets_projects

import tests


class TestListDatasetsProjects(tests.CloudBaseTest):

def test_main(self):
with tests.capture_stdout() as mock_stdout:
list_datasets_projects.main(self.constants['projectId'])
stdout = mock_stdout.getvalue()
self.assertRegexpMatches(stdout, re.compile(
r'Project list:.*bigquery#projectList.*projects', re.DOTALL))
self.assertRegexpMatches(stdout, re.compile(
r'Dataset list:.*datasets.*datasetId', re.DOTALL))


if __name__ == '__main__':
unittest.main()

0 comments on commit b453dc5

Please sign in to comment.