Skip to content
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

Adding dns samples #251

Merged
merged 1 commit into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include =
blog/*
cloud_logging/*
compute/*
dns/*
datastore/*
managed_vms/*
monitoring/*
Expand Down
26 changes: 26 additions & 0 deletions dns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Google Cloud DNS Samples

This section contains samples for [Google Cloud DNS](https://cloud.google.com/dns).

## Running the samples

1. Your environment must be setup with [authentication
information](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork). If you're running in your local development environment and you have the [Google Cloud SDK](https://cloud.google.com/sdk/) installed, you can do this easily by running:

$ gcloud init

2. Install dependencies from `requirements.txt`:

$ pip install -r requirements.txt

3. Depending on the sample, you may also need to create resources on the [Google Developers Console](https://console.developers.google.com). Refer to the sample description and associated documentation page.

## Additional resources

For more information on Cloud Storage you can visit:

> https://cloud.google.com/dns

For information on the Python Cloud Client Library visit:

> https://googlecloudplatform.github.io/gcloud-python
4 changes: 4 additions & 0 deletions dns/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Cloud DNS API Samples

<!-- auto-doc-link -->
<!-- end-auto-doc-link -->
163 changes: 163 additions & 0 deletions dns/api/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Copyright 2016, 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 argparse

from gcloud import dns


# [START create_zone]
def create_zone(project_id, name, dns_name, description):
client = dns.Client(project=project_id)
zone = client.zone(
name, # examplezonename
dns_name=dns_name) # example.com.
zone.description = description
zone.create()
return zone
# [END create_zone]


# [START get_zone]
def get_zone(project_id, name):
client = dns.Client(project=project_id)
zones, _ = client.list_zones()
zone = list(filter(lambda zone: zone.name == name, zones))
return zone.pop() if zone else None
# [END get_zone]


# [START list_zones]
def list_zones(project_id):
client = dns.Client(project=project_id)
zones, _ = client.list_zones()
return [zone.name for zone in zones]
# [END list_zones]


# [START delete_zone]
def delete_zone(project_id, name):
client = dns.Client(project=project_id)
zone = client.zone(name, None)
zone.delete()
# [END delete_zone]


# [START list_resource_records]
def list_resource_records(project_id, zone_name):
client = dns.Client(project=project_id)
zone = client.zone(zone_name, None)

records, page_token = zone.list_resource_record_sets()
while page_token is not None:
next_batch, page_token = zone.list_resource_record_sets(
page_token=page_token)
records.extend(next_batch)

return [(record.name, record.record_type, record.ttl, record.rrdatas)
for record in records]
# [END list_resource_records]


# [START changes]
def list_changes(project_id, zone_name):
client = dns.Client(project=project_id)
zone = client.zone(zone_name, None)

changes, _ = zone.list_changes()

return [(change.started, change.status) for change in changes]
# [END changes]


def create_command(args):
"""Adds a zone with the given name, DNS name, and description."""
zone = create_zone(
args.project_id, args.name, args.dns_name, args.description)
print('Zone {} added.'.format(zone.name))


def get_command(args):
"""Gets a zone by name."""
zone = get_zone(args.project_id, args.name)
if not zone:
print('Zone not found.')
else:
print('Zone: {}, {}, {}'.format(
zone.name, zone.dns_name, zone.description))


def list_command(args):
"""Lists all zones."""
print(list_zones(args.project_id))


def delete_command(args):
"""Deletes a zone."""
delete_zone(args.project_id, args.name)
print('Zone {} deleted.'.format(args.name))


def list_resource_records_command(args):
"""List all resource records for a zone."""
records = list_resource_records(args.project_id, args.name)
for record in records:
print(record)


def changes_command(args):
"""List all changes records for a zone."""
changes = list_changes(args.project_id, args.name)
for change in changes:
print(change)


if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()

parser.add_argument('--project-id', help='Your cloud project ID.')

create_parser = subparsers.add_parser(
'create', help=create_command.__doc__)
create_parser.set_defaults(func=create_command)
create_parser.add_argument('name', help='New zone name, e.g. "azonename".')
create_parser.add_argument(
'dns_name', help='New zone dns name, e.g. "example.com."')
create_parser.add_argument('description', help='New zone description.')

get_parser = subparsers.add_parser('get', help=get_command.__doc__)
get_parser.add_argument('name', help='Zone name, e.g. "azonename".')
get_parser.set_defaults(func=get_command)

list_parser = subparsers.add_parser('list', help=list_command.__doc__)
list_parser.set_defaults(func=list_command)

delete_parser = subparsers.add_parser(
'delete', help=delete_command.__doc__)
delete_parser.add_argument('name', help='Zone name, e.g. "azonename".')
delete_parser.set_defaults(func=delete_command)

list_rr_parser = subparsers.add_parser(
'list-resource-records', help=list_resource_records_command.__doc__)
list_rr_parser.add_argument('name', help='Zone name, e.g. "azonename".')
list_rr_parser.set_defaults(func=list_resource_records_command)

changes_parser = subparsers.add_parser(
'changes', help=changes_command.__doc__)
changes_parser.add_argument('name', help='Zone name, e.g. "azonename".')
changes_parser.set_defaults(func=changes_command)

args = parser.parse_args()

args.func(args)
93 changes: 93 additions & 0 deletions dns/api/main_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 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.

from gcloud import dns
from gcp.testing.flaky import flaky
import pytest
import main


TEST_ZONE_NAME = 'test-zone'
TEST_ZONE_DNS_NAME = 'theadora.is.'
TEST_ZONE_DESCRIPTION = 'Test zone'


@pytest.yield_fixture
def client(cloud_config):
client = dns.Client(cloud_config.project)

yield client

# Delete anything created during the test.
for zone in client.list_zones()[0]:
zone.delete()


@pytest.yield_fixture
def zone(client, cloud_config):
zone = client.zone(TEST_ZONE_NAME, TEST_ZONE_DNS_NAME)
zone.description = TEST_ZONE_DESCRIPTION
zone.create()

yield zone

if zone.exists():
zone.delete()


@flaky
def test_create_zone(client, cloud_config):
zone = main.create_zone(
cloud_config.project,
TEST_ZONE_NAME,
TEST_ZONE_DNS_NAME,
TEST_ZONE_DESCRIPTION)

assert zone.name == TEST_ZONE_NAME
assert zone.dns_name == TEST_ZONE_DNS_NAME
assert zone.description == TEST_ZONE_DESCRIPTION


@flaky
def test_get_zone(client, cloud_config, zone):
zone = main.get_zone(cloud_config.project, TEST_ZONE_NAME)

assert zone.name == TEST_ZONE_NAME
assert zone.dns_name == TEST_ZONE_DNS_NAME
assert zone.description == TEST_ZONE_DESCRIPTION


@flaky
def test_list_zones(client, cloud_config, zone):
zones = main.list_zones(cloud_config.project)

assert TEST_ZONE_NAME in zones


@flaky
def test_delete_zone(client, cloud_config, zone):
main.delete_zone(cloud_config.project, TEST_ZONE_NAME)


@flaky
def test_list_resource_records(client, cloud_config, zone):
records = main.list_resource_records(cloud_config.project, TEST_ZONE_NAME)

assert records


@flaky
def test_list_changes(client, cloud_config, zone):
changes = main.list_changes(cloud_config.project, TEST_ZONE_NAME)

assert changes
1 change: 1 addition & 0 deletions dns/api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcloud==0.12