Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix #77 - Initialized cloud dns.#78
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
3472e2286b886e9654c3b2cf60d46ce841aad1f0f76c999c070069c54ea692bfbdff58f32f10ef1c5491f7150706e196cf21ec3c9b22da80be94dff87c3e734dd8f67af92e74c021a0dFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| language: python | ||
| python: | ||
| - "2.6" | ||
| - "2.7" | ||
| # command to install dependencies | ||
| install: "pip install . unittest2" | ||
| # command to run tests | ||
| script: nosetests |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import httplib2 | ||
| import json | ||
| import urllib | ||
| from gcloud import exceptions | ||
| class Connection(object): | ||
| @@ -42,3 +46,83 @@ def http(self): | ||
| self._http = self._credentials.authorize(self._http) | ||
| return self._http | ||
| class JsonConnection(Connection): | ||
| API_BASE_URL = 'https://www.googleapis.com' | ||
| """The base of the API call URL.""" | ||
| _EMPTY = object() | ||
| """A pointer to represent an empty value for default arguments.""" | ||
| def __init__(self, project=None, *args, **kwargs): | ||
| super(JsonConnection, self).__init__(*args, **kwargs) | ||
| self.project = project | ||
| def build_api_url(self, path, query_params=None, api_base_url=None, | ||
| api_version=None): | ||
| url = self.API_URL_TEMPLATE.format( | ||
| api_base_url=(api_base_url or self.API_BASE_URL), | ||
| api_version=(api_version or self.API_VERSION), | ||
| path=path) | ||
| query_params = query_params or {} | ||
| query_params.update({'project': self.project}) | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| url += '?' + urllib.urlencode(query_params) | ||
| return url | ||
| def make_request(self, method, url, data=None, content_type=None, | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| headers=None): | ||
| headers = headers or {} | ||
| headers['Accept-Encoding'] = 'gzip' | ||
| if data: | ||
| content_length = len(str(data)) | ||
| else: | ||
| content_length = 0 | ||
| headers['Content-Length'] = content_length | ||
| if content_type: | ||
| headers['Content-Type'] = content_type | ||
| return self.http.request(uri=url, method=method, headers=headers, | ||
| body=data) | ||
| def api_request(self, method, path=None, query_params=None, | ||
| data=None, content_type=None, | ||
| api_base_url=None, api_version=None, | ||
| expect_json=True): | ||
| url = self.build_api_url(path=path, query_params=query_params, | ||
| api_base_url=api_base_url, | ||
| api_version=api_version) | ||
| # Making the executive decision that any dictionary | ||
| # data will be sent properly as JSON. | ||
| if data and isinstance(data, dict): | ||
| data = json.dumps(data) | ||
| content_type = 'application/json' | ||
| response, content = self.make_request( | ||
| method=method, url=url, data=data, content_type=content_type) | ||
| # TODO: Add better error handling. | ||
| if response.status == 404: | ||
| raise exceptions.NotFoundError(response, content) | ||
| elif not 200 <= response.status < 300: | ||
| raise exceptions.ConnectionError(response, content) | ||
| if content and expect_json: | ||
| # TODO: Better checking on this header for JSON. | ||
| content_type = response.get('content-type', '') | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| if not content_type.startswith('application/json'): | ||
| raise TypeError('Expected JSON, got %s' % content_type) | ||
| return json.loads(content) | ||
| return content | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Shortcut methods for getting set up with Google Cloud DNS. | ||
| You'll typically use these to get started with the API: | ||
| >>> import gcloud.dns | ||
| >>> zone = gcloud.dns.get_zone('zone-name-here', | ||
| 'long-email@googleapis.com', | ||
| '/path/to/private.key') | ||
| The main concepts with this API are: | ||
| - :class:`gcloud.dns.connection.Connection` | ||
| which represents a connection between your machine | ||
| and the Cloud DNS API. | ||
| - :class:`gcloud.dns.zone.Zone` | ||
| which represents a particular zone. | ||
| """ | ||
| __version__ = '0.1' | ||
| # TODO: Allow specific scopes and authorization levels. | ||
| SCOPE = ('https://www.googleapis.com/auth/cloud-platform', | ||
| 'https://www.googleapis.com/auth/ndev.clouddns.readonly', | ||
| 'https://www.googleapis.com/auth/ndev.clouddns.readwrite') | ||
| """The scope required for authenticating as a Cloud DNS consumer.""" | ||
| def get_connection(project, client_email, private_key_path): | ||
| """Shortcut method to establish a connection to Cloud DNS. | ||
| Use this if you are going to access several zones | ||
| with the same set of credentials: | ||
| >>> from gcloud import dns | ||
| >>> connection = dns.get_connection(project, email, key_path) | ||
| >>> zone1 = connection.get_zone('zone1') | ||
| >>> zone2 = connection.get_zone('zone2') | ||
| :type project: string | ||
| :param project: The name of the project to connect to. | ||
| :type client_email: string | ||
| :param client_email: The e-mail attached to the service account. | ||
| :type private_key_path: string | ||
| :param private_key_path: The path to a private key file (this file was | ||
| given to you when you created the service | ||
| account). | ||
| :rtype: :class:`gcloud.dns.connection.Connection` | ||
| :returns: A connection defined with the proper credentials. | ||
| """ | ||
| from gcloud.credentials import Credentials | ||
| from gcloud.dns.connection import Connection | ||
| credentials = Credentials.get_for_service_account( | ||
| client_email, private_key_path, scope=SCOPE) | ||
| return Connection(project=project, credentials=credentials) | ||
| def get_zone(zone, project, client_email, private_key_path): | ||
| """Shortcut method to establish a connection to a particular zone. | ||
| You'll generally use this as the first call to working with the API: | ||
| >>> from gcloud import dns | ||
| >>> zone = dns.get_zone(zone, project, email, key_path) | ||
| :type zone: string | ||
| :param zone: The id of the zone you want to use. | ||
| This is akin to a disk name on a file system. | ||
| :type project: string | ||
| :param project: The name of the project to connect to. | ||
| :type client_email: string | ||
| :param client_email: The e-mail attached to the service account. | ||
| :type private_key_path: string | ||
| :param private_key_path: The path to a private key file (this file was | ||
| given to you when you created the service | ||
| account). | ||
| :rtype: :class:`gcloud.dns.zone.Zone` | ||
| :returns: A zone with a connection using the provided credentials. | ||
| """ | ||
| connection = get_connection(project, client_email, private_key_path) | ||
| return connection.get_zone(zone) |
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.