-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_terminate_route53.py
executable file
·84 lines (61 loc) · 2.14 KB
/
run_terminate_route53.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
#!/usr/bin/env python3
import json
import re
from argparse import ArgumentParser
from run_common import AWSCli
from run_common import _confirm_phase
from run_common import print_session
def parse_args():
parser = ArgumentParser()
parser.add_argument('-f', '--force', action='store_true', help='pass confirm')
parser.add_argument('-hn', '--hosted_zone_name', type=str, required=True, help='name of hosted zone')
parser.add_argument('-n', '--name', type=str, required=True, help='acm arn')
args = parser.parse_args()
if not args.force:
_confirm_phase()
return args
def find_host_zone_id(host_zone_name):
print_session('find_host_zone_id')
aws_cli = AWSCli()
cmd = ['route53', 'list-hosted-zones-by-name']
cmd += ['--dns-name', host_zone_name]
rr = aws_cli.run(cmd)
if not (len(rr['HostedZones']) == 1):
raise Exception('wrong host zone')
return rr['HostedZones'][0]['Id']
def delete_route53(name, host_zone_name):
id = find_host_zone_id(host_zone_name)
aws_cli = AWSCli()
cmd = ['route53', 'list-resource-record-sets']
cmd += ['--hosted-zone-id', id]
rr = aws_cli.run(cmd)
for rrs in rr['ResourceRecordSets']:
if rrs['Name'] == name + '.':
dd = dict()
dd['Changes'] = [
{
"Action": "DELETE",
"ResourceRecordSet": rrs
}
]
aws_cli = AWSCli()
cmd = ['route53', 'change-resource-record-sets']
id = find_host_zone_id(host_zone_name)
cmd += ['--hosted-zone-id', id]
cmd += ['--change-batch', json.dumps(dd)]
rr = aws_cli.run(cmd)
print(rr)
################################################################################
#
# start
#
################################################################################
if __name__ == "__main__":
args = parse_args()
hosted_zone_name = args.hosted_zone_name
name = args.name
cc = re.split('[-.]', name)
if 'dv' not in cc:
print('only can delete dv')
exit(1)
delete_route53(name, hosted_zone_name)