From e96c13a5f97363299aee7b0c8ae85ae45e4a8735 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Tue, 12 Apr 2016 13:07:57 -0700 Subject: [PATCH] Adding dns samples Change-Id: I6b14c5bc28679f783de26121d55d723677b4f450 --- .coveragerc | 1 + dns/README.md | 26 +++++++ dns/api/README.md | 4 + dns/api/main.py | 163 +++++++++++++++++++++++++++++++++++++++ dns/api/main_test.py | 93 ++++++++++++++++++++++ dns/api/requirements.txt | 1 + 6 files changed, 288 insertions(+) create mode 100644 dns/README.md create mode 100644 dns/api/README.md create mode 100644 dns/api/main.py create mode 100644 dns/api/main_test.py create mode 100644 dns/api/requirements.txt diff --git a/.coveragerc b/.coveragerc index 45ede645eb7a..5053cd951f23 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,6 +5,7 @@ include = blog/* cloud_logging/* compute/* + dns/* datastore/* managed_vms/* monitoring/* diff --git a/dns/README.md b/dns/README.md new file mode 100644 index 000000000000..526f21ee3e44 --- /dev/null +++ b/dns/README.md @@ -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 diff --git a/dns/api/README.md b/dns/api/README.md new file mode 100644 index 000000000000..5022309442be --- /dev/null +++ b/dns/api/README.md @@ -0,0 +1,4 @@ +# Cloud DNS API Samples + + + diff --git a/dns/api/main.py b/dns/api/main.py new file mode 100644 index 000000000000..b1bd4e849187 --- /dev/null +++ b/dns/api/main.py @@ -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) diff --git a/dns/api/main_test.py b/dns/api/main_test.py new file mode 100644 index 000000000000..24e0b68461c8 --- /dev/null +++ b/dns/api/main_test.py @@ -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 diff --git a/dns/api/requirements.txt b/dns/api/requirements.txt new file mode 100644 index 000000000000..d72fe79a356a --- /dev/null +++ b/dns/api/requirements.txt @@ -0,0 +1 @@ +gcloud==0.12