forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix googleapis#77 - Initialized gcloud dns.
Cleaned up dns. Addressed comments on dns. Added function to create changes. Changed project_id to project and addressed comments.
- Loading branch information
Showing
8 changed files
with
257 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
__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): | ||
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_name, project, client_email, private_key_path): | ||
connection = get_connection(project, client_email, private_key_path) | ||
return connection.get_zone(zone_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import json | ||
|
||
from gcloud import connection | ||
from gcloud.dns.zone import Zone | ||
|
||
|
||
class Connection(connection.JsonConnection): | ||
API_VERSION = 'v1beta1' | ||
"""The version of the API, used in building the API call's URL.""" | ||
|
||
API_URL_TEMPLATE = ('{api_base_url}/dns/{api_version}/projects/{path}') | ||
"""A template used to craft the URL pointing toward a particular API call.""" | ||
|
||
_EMPTY = object() | ||
"""A pointer to represent an empty value for default arguments.""" | ||
|
||
def __init__(self, project=None, *args, **kwargs): | ||
|
||
super(Connection, self).__init__(*args, **kwargs) | ||
|
||
self.project = project | ||
|
||
def create_zone(self, data): | ||
zone = self.new_zone(data['name']) | ||
response = self.api_request(method='POST', path=zone.path, | ||
data=data) | ||
return Zone.from_dict(response, connection=self) | ||
|
||
def delete_zone(self, zone): | ||
zone = self.new_zone(zone) | ||
self.api_request(method='DELETE', path=zone.path + | ||
zone.name) | ||
return True | ||
|
||
def get_zone(self, zone): | ||
zone = self.new_zone(zone) | ||
response = self.api_request(method='GET', path=zone.path) | ||
return Zone.from_dict(response['managedZones'][0], | ||
connection=self) | ||
|
||
def list_zones(self): | ||
zone = self.new_zone('test') | ||
response = self.api_request(method='GET', path=zone.path) | ||
print json.dumps(response, indent=2) | ||
|
||
def new_zone(self, zone): | ||
if isinstance(zone, Zone): | ||
return zone | ||
|
||
# Support Python 2 and 3. | ||
try: | ||
string_type = basestring | ||
except NameError: | ||
string_type = str | ||
|
||
if isinstance(zone, string_type): | ||
return Zone(connection=self, name=zone) | ||
|
||
def create_changes(self, zone, data): | ||
zone = self.new_zone(zone) | ||
self.api_request(method='POST', path=zone.path + zone.name + '/changes', | ||
data=data) | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
from gcloud import dns | ||
|
||
|
||
__all__ = ['get_connection', 'get_zone' 'CLIENT_EMAIL', 'PRIVATE_KEY_PATH', | ||
'PROJECT'] | ||
|
||
|
||
CLIENT_EMAIL = '' | ||
PRIVATE_KEY_PATH = os.path.join(os.path.dirname(__file__), 'demo.key') | ||
PROJECT = '' | ||
|
||
|
||
def get_connection(): | ||
return dns.get_connection(PROJECT, CLIENT_EMAIL, PRIVATE_KEY_PATH) | ||
|
||
|
||
def get_zone(zone_name): | ||
return dns.get_zone(zone_name, PROJECT, CLIENT_EMAIL, PRIVATE_KEY_PATH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Welcome to the gCloud DNS Demo! (hit enter) | ||
|
||
# We're going to walk through some of the basics..., | ||
# Don't worry though. You don't need to do anything, just keep hitting enter... | ||
|
||
# Let's start by importing the demo module and getting a connection: | ||
from gcloud.dns import demo | ||
connection = demo.get_connection() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from gcloud.exceptions import gcloudError | ||
# TODO: Make these super useful. | ||
|
||
|
||
class DNSError(gcloudError): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Zone(object): | ||
|
||
def __init__(self, connection=None, creation_time=None, description=None, | ||
dns_name=None, id=None, kind=None, name=None, | ||
name_servers=None): | ||
self.connection = connection | ||
self.creation_time = creation_time | ||
self.description = description | ||
self.dns_name = dns_name | ||
self.id = id | ||
self.kind = kind | ||
self.name = name | ||
self.name_servers = name_servers | ||
|
||
@classmethod | ||
def from_dict(cls, zone_dict, connection=None): | ||
|
||
return cls(connection=connection, | ||
creation_time=zone_dict['creationTime'], | ||
description=zone_dict['description'], | ||
dns_name=zone_dict['dnsName'], id=zone_dict['id'], | ||
kind=zone_dict['kind'], name=zone_dict['name'], | ||
name_servers=zone_dict['nameServers']) | ||
|
||
@property | ||
def path(self): | ||
"""The URL path to this zone.""" | ||
|
||
if not self.connection.project: | ||
raise ValueError('Cannot determine path without project name.') | ||
|
||
return self.connection.project + '/managedZones/' | ||
|
||
def delete(self): | ||
return self.connection.delete_zone(self) | ||
|
||
def get(self): | ||
return self.connection.get_zone(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# TODO: Make these super useful. | ||
|
||
|
||
class gcloudError(Exception): | ||
pass | ||
|
||
|
||
class ConnectionError(gcloudError): | ||
|
||
def __init__(self, response, content): | ||
message = str(response) + content | ||
super(ConnectionError, self).__init__(message) | ||
|
||
|
||
class NotFoundError(gcloudError): | ||
|
||
def __init__(self, response, content): | ||
self.message = 'GET %s returned a 404.' % (response.url) |