-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathmain.py
177 lines (123 loc) · 4.83 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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 google.cloud import dns
from google.cloud.exceptions import NotFound
# [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.
description=description,
)
zone.create()
return zone
# [END create_zone]
# [START get_zone]
def get_zone(project_id, name):
client = dns.Client(project=project_id)
zone = client.zone(name=name)
try:
zone.reload()
return zone
except NotFound:
return 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)
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)
records = zone.list_resource_record_sets()
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)
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(f"Zone {zone.name} added.")
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(f"Zone {args.name} deleted.")
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)