Skip to content

Commit e96c13a

Browse files
author
Jon Wayne Parrott
committed
Adding dns samples
Change-Id: I6b14c5bc28679f783de26121d55d723677b4f450
1 parent 891e302 commit e96c13a

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ include =
55
blog/*
66
cloud_logging/*
77
compute/*
8+
dns/*
89
datastore/*
910
managed_vms/*
1011
monitoring/*

dns/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Google Cloud DNS Samples
2+
3+
This section contains samples for [Google Cloud DNS](https://cloud.google.com/dns).
4+
5+
## Running the samples
6+
7+
1. Your environment must be setup with [authentication
8+
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:
9+
10+
$ gcloud init
11+
12+
2. Install dependencies from `requirements.txt`:
13+
14+
$ pip install -r requirements.txt
15+
16+
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.
17+
18+
## Additional resources
19+
20+
For more information on Cloud Storage you can visit:
21+
22+
> https://cloud.google.com/dns
23+
24+
For information on the Python Cloud Client Library visit:
25+
26+
> https://googlecloudplatform.github.io/gcloud-python

dns/api/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Cloud DNS API Samples
2+
3+
<!-- auto-doc-link -->
4+
<!-- end-auto-doc-link -->

dns/api/main.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import argparse
15+
16+
from gcloud import dns
17+
18+
19+
# [START create_zone]
20+
def create_zone(project_id, name, dns_name, description):
21+
client = dns.Client(project=project_id)
22+
zone = client.zone(
23+
name, # examplezonename
24+
dns_name=dns_name) # example.com.
25+
zone.description = description
26+
zone.create()
27+
return zone
28+
# [END create_zone]
29+
30+
31+
# [START get_zone]
32+
def get_zone(project_id, name):
33+
client = dns.Client(project=project_id)
34+
zones, _ = client.list_zones()
35+
zone = list(filter(lambda zone: zone.name == name, zones))
36+
return zone.pop() if zone else None
37+
# [END get_zone]
38+
39+
40+
# [START list_zones]
41+
def list_zones(project_id):
42+
client = dns.Client(project=project_id)
43+
zones, _ = client.list_zones()
44+
return [zone.name for zone in zones]
45+
# [END list_zones]
46+
47+
48+
# [START delete_zone]
49+
def delete_zone(project_id, name):
50+
client = dns.Client(project=project_id)
51+
zone = client.zone(name, None)
52+
zone.delete()
53+
# [END delete_zone]
54+
55+
56+
# [START list_resource_records]
57+
def list_resource_records(project_id, zone_name):
58+
client = dns.Client(project=project_id)
59+
zone = client.zone(zone_name, None)
60+
61+
records, page_token = zone.list_resource_record_sets()
62+
while page_token is not None:
63+
next_batch, page_token = zone.list_resource_record_sets(
64+
page_token=page_token)
65+
records.extend(next_batch)
66+
67+
return [(record.name, record.record_type, record.ttl, record.rrdatas)
68+
for record in records]
69+
# [END list_resource_records]
70+
71+
72+
# [START changes]
73+
def list_changes(project_id, zone_name):
74+
client = dns.Client(project=project_id)
75+
zone = client.zone(zone_name, None)
76+
77+
changes, _ = zone.list_changes()
78+
79+
return [(change.started, change.status) for change in changes]
80+
# [END changes]
81+
82+
83+
def create_command(args):
84+
"""Adds a zone with the given name, DNS name, and description."""
85+
zone = create_zone(
86+
args.project_id, args.name, args.dns_name, args.description)
87+
print('Zone {} added.'.format(zone.name))
88+
89+
90+
def get_command(args):
91+
"""Gets a zone by name."""
92+
zone = get_zone(args.project_id, args.name)
93+
if not zone:
94+
print('Zone not found.')
95+
else:
96+
print('Zone: {}, {}, {}'.format(
97+
zone.name, zone.dns_name, zone.description))
98+
99+
100+
def list_command(args):
101+
"""Lists all zones."""
102+
print(list_zones(args.project_id))
103+
104+
105+
def delete_command(args):
106+
"""Deletes a zone."""
107+
delete_zone(args.project_id, args.name)
108+
print('Zone {} deleted.'.format(args.name))
109+
110+
111+
def list_resource_records_command(args):
112+
"""List all resource records for a zone."""
113+
records = list_resource_records(args.project_id, args.name)
114+
for record in records:
115+
print(record)
116+
117+
118+
def changes_command(args):
119+
"""List all changes records for a zone."""
120+
changes = list_changes(args.project_id, args.name)
121+
for change in changes:
122+
print(change)
123+
124+
125+
if __name__ == '__main__':
126+
parser = argparse.ArgumentParser()
127+
subparsers = parser.add_subparsers()
128+
129+
parser.add_argument('--project-id', help='Your cloud project ID.')
130+
131+
create_parser = subparsers.add_parser(
132+
'create', help=create_command.__doc__)
133+
create_parser.set_defaults(func=create_command)
134+
create_parser.add_argument('name', help='New zone name, e.g. "azonename".')
135+
create_parser.add_argument(
136+
'dns_name', help='New zone dns name, e.g. "example.com."')
137+
create_parser.add_argument('description', help='New zone description.')
138+
139+
get_parser = subparsers.add_parser('get', help=get_command.__doc__)
140+
get_parser.add_argument('name', help='Zone name, e.g. "azonename".')
141+
get_parser.set_defaults(func=get_command)
142+
143+
list_parser = subparsers.add_parser('list', help=list_command.__doc__)
144+
list_parser.set_defaults(func=list_command)
145+
146+
delete_parser = subparsers.add_parser(
147+
'delete', help=delete_command.__doc__)
148+
delete_parser.add_argument('name', help='Zone name, e.g. "azonename".')
149+
delete_parser.set_defaults(func=delete_command)
150+
151+
list_rr_parser = subparsers.add_parser(
152+
'list-resource-records', help=list_resource_records_command.__doc__)
153+
list_rr_parser.add_argument('name', help='Zone name, e.g. "azonename".')
154+
list_rr_parser.set_defaults(func=list_resource_records_command)
155+
156+
changes_parser = subparsers.add_parser(
157+
'changes', help=changes_command.__doc__)
158+
changes_parser.add_argument('name', help='Zone name, e.g. "azonename".')
159+
changes_parser.set_defaults(func=changes_command)
160+
161+
args = parser.parse_args()
162+
163+
args.func(args)

dns/api/main_test.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from gcloud import dns
15+
from gcp.testing.flaky import flaky
16+
import pytest
17+
import main
18+
19+
20+
TEST_ZONE_NAME = 'test-zone'
21+
TEST_ZONE_DNS_NAME = 'theadora.is.'
22+
TEST_ZONE_DESCRIPTION = 'Test zone'
23+
24+
25+
@pytest.yield_fixture
26+
def client(cloud_config):
27+
client = dns.Client(cloud_config.project)
28+
29+
yield client
30+
31+
# Delete anything created during the test.
32+
for zone in client.list_zones()[0]:
33+
zone.delete()
34+
35+
36+
@pytest.yield_fixture
37+
def zone(client, cloud_config):
38+
zone = client.zone(TEST_ZONE_NAME, TEST_ZONE_DNS_NAME)
39+
zone.description = TEST_ZONE_DESCRIPTION
40+
zone.create()
41+
42+
yield zone
43+
44+
if zone.exists():
45+
zone.delete()
46+
47+
48+
@flaky
49+
def test_create_zone(client, cloud_config):
50+
zone = main.create_zone(
51+
cloud_config.project,
52+
TEST_ZONE_NAME,
53+
TEST_ZONE_DNS_NAME,
54+
TEST_ZONE_DESCRIPTION)
55+
56+
assert zone.name == TEST_ZONE_NAME
57+
assert zone.dns_name == TEST_ZONE_DNS_NAME
58+
assert zone.description == TEST_ZONE_DESCRIPTION
59+
60+
61+
@flaky
62+
def test_get_zone(client, cloud_config, zone):
63+
zone = main.get_zone(cloud_config.project, TEST_ZONE_NAME)
64+
65+
assert zone.name == TEST_ZONE_NAME
66+
assert zone.dns_name == TEST_ZONE_DNS_NAME
67+
assert zone.description == TEST_ZONE_DESCRIPTION
68+
69+
70+
@flaky
71+
def test_list_zones(client, cloud_config, zone):
72+
zones = main.list_zones(cloud_config.project)
73+
74+
assert TEST_ZONE_NAME in zones
75+
76+
77+
@flaky
78+
def test_delete_zone(client, cloud_config, zone):
79+
main.delete_zone(cloud_config.project, TEST_ZONE_NAME)
80+
81+
82+
@flaky
83+
def test_list_resource_records(client, cloud_config, zone):
84+
records = main.list_resource_records(cloud_config.project, TEST_ZONE_NAME)
85+
86+
assert records
87+
88+
89+
@flaky
90+
def test_list_changes(client, cloud_config, zone):
91+
changes = main.list_changes(cloud_config.project, TEST_ZONE_NAME)
92+
93+
assert changes

dns/api/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gcloud==0.12

0 commit comments

Comments
 (0)