|
| 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) |
0 commit comments