-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Initialized gcloud dns. #71
Closed
+276
−0
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
650df07
Initialized gcloud dns.
kleyow 7d8086d
Cleaned up dns.
kleyow 415556f
Addressed comments on dns.
kleyow 500dc6e
Added function to create changes.
kleyow bada594
Changed project_id to project and addressed comments.
kleyow 9f661b8
Added some functions.
kleyow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,82 @@ | ||
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 | ||
|
||
def new_resource(self, name, type, ttl, rrdatas): | ||
return {'kind': 'dns#resourceRecordSet', 'name': name, 'type': type, | ||
'ttl': ttl, 'rrdatas': rrdatas} | ||
|
||
def new_change(self): | ||
return {'kind': "dns#change", 'additions': [], 'deletions': []} | ||
|
||
def create_record(self, zone, data): | ||
zone = self.new_zone(zone) | ||
self.api_request(method='POST', path=zone.path + zone.name + '/changes', | ||
data=data) | ||
return True | ||
|
||
def list_records(self, zone): | ||
zone = self.new_zone(zone) | ||
response = self.api_request(method='GET', | ||
path=zone.path + zone.name + '/changes') | ||
print json.dumps(response, indent=2) |
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.