-
Notifications
You must be signed in to change notification settings - Fork 73
/
aws_limit_increases_for_service.py
executable file
·279 lines (249 loc) · 10.4 KB
/
aws_limit_increases_for_service.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
"""
Script using boto3 to show all Limit Increase support tickets for a
specified service.
Requirements
------------
``pip install boto3 python-dateutil pytz``
CHANGELOG
---------
2017-09-22 Jason Antman <[email protected]>:
- initial version of script
"""
import sys
import argparse
import logging
import boto3
from datetime import datetime
from pytz import utc
from dateutil.parser import parse
import re
FORMAT = "[%(levelname)s %(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
logging.basicConfig(level=logging.WARNING, format=FORMAT)
logger = logging.getLogger(__name__)
# suppress boto3 internal logging below WARNING level
boto3_log = logging.getLogger("boto3")
boto3_log.setLevel(logging.WARNING)
boto3_log.propagate = True
# suppress botocore internal logging below WARNING level
botocore_log = logging.getLogger("botocore")
botocore_log.setLevel(logging.WARNING)
botocore_log.propagate = True
SERVICE_CODE = 'service-limit-increase'
# dict that maps the AwsLimitChecker service names to the category "name"
# returned by the Support API's DescribeServices action
SERVICE_NAMES = {
'IAM': 'IAM Groups and Users',
'S3': 'Simple Storage Service (S3)'
}
REQUEST_SEP_RE = re.compile(r'^-+$')
REQUEST_HEADER_RE = re.compile(r'^Limit increase request (\d+)$')
class LimitIncreaseFinder(object):
"""Find existing support tickets for service limit increases"""
def __init__(self, dry_run=False):
""" init method, run at class creation """
self.support = boto3.client('support', region_name='us-east-1')
self.category_codes = self.get_category_codes()
logger.debug("category_codes: %s", self.category_codes)
def list_categories(self):
""" just list the categories """
for catname, catcode in sorted(self.category_codes.items()):
print('%s (%s)' % (catname, catcode))
def get_category_codes(self):
"""get the Support case category codes for limit increases; return a
dict of category code to name"""
codes = self.support.describe_services(
serviceCodeList=[SERVICE_CODE], language='en'
)
res = {}
for d in codes['services'][0]['categories']:
res[d['code']] = d['name']
return res
def first_communication_in_case(self, case):
"""parse communications in a case and return the first one"""
if len(case['recentCommunications']['communications']) == 0:
raise RuntimeError("ERROR: case %s (%s) has no communications" %
(case['displayId'], case['caseId']))
elif len(case['recentCommunications']['communications']) == 1:
return case['recentCommunications']['communications'][0]
oldest_dt = datetime.utcnow().replace(tzinfo=utc)
oldest_comm = None
for comm in case['recentCommunications']['communications']:
dt = parse(comm['timeCreated'])
if dt < oldest_dt:
oldest_dt = dt
oldest_comm = comm
return oldest_comm
def limit_requests_in_case(self, case):
"""parse a case and return the limit requests in it"""
comm = self.first_communication_in_case(case)
limits = self.parse_limits_from_communication(comm['body'])
return limits
def parse_limits_from_communication(self, body):
"""given the body of a communication, parse it and return a list of
dicts describing the limit increase requests in it"""
logger.debug("Parsing communication body:\n%s", body)
lines = body.split("\n")
requests = []
tmp = {}
in_request = False
for line in lines:
line = line.strip()
if line == '':
continue
if REQUEST_SEP_RE.match(line):
# end of request
logger.debug("End of request: %s", tmp)
if 'New limit value' not in tmp or 'Service' not in tmp or \
'Limit name' not in tmp:
logger.error("Error: fields missing from record: %s", body)
else:
requests.append(tmp)
tmp = {}
in_request = False
continue
if not in_request:
m = REQUEST_HEADER_RE.match(line)
if m is None:
logger.debug("Not in request but no match for header: %s",
line)
continue
in_request = True
logger.debug("Beginning to parse limit request %s", m.group(1))
tmp['request_num'] = int(m.group(1))
else:
# in request
parts = line.split(':', 1)
if parts[0] == 'New limit value':
tmp[parts[0]] = int(parts[1].strip())
else:
tmp[parts[0]] = parts[1].strip()
return requests
def show_cases_for_service(self, svc_name, include_resolved=True):
"""
Print out all cases for the given service name and then return.
:param svc_name: the service name to search for. This is a key in the
``self.category_codes`` dictionary, as returned by
:py:meth:`~.get_category_codes`
:type svc_name: str
"""
if svc_name not in self.category_codes.keys():
raise RuntimeError(
"ERROR: '%s' is not a valid support case category code; please "
"run with '-l' option to list valid category codes." % svc_name
)
cases = self.get_cases(
includeResolvedCases=include_resolved, maxResults=100,
language='en', includeCommunications=True
)
for case in cases:
if case['serviceCode'] != SERVICE_CODE:
logger.debug('Skipping case %s (%s) with serviceCode %s',
case['displayId'], case['caseId'],
case['serviceCode'])
continue
if case['categoryCode'] != svc_name:
logger.debug('Skipping case %s (%s) with categoryCode %s',
case['displayId'], case['caseId'],
case['categoryCode'])
continue
# right service and category; need to parse it and check limits
try:
lim_requests = self.limit_requests_in_case(case)
except RuntimeError:
lim_requests = []
logger.debug("Parsing found %d limit request(s) in case %s",
len(lim_requests), case['displayId'])
if len(lim_requests) < 1:
continue
self.show_case(case, lim_requests)
return None
def show_case(self, case, lim_requests):
"""
Display (print) a support case
:param case:
:param lim_requests:
:return:
"""
# case metadata
print('#' * 60)
print('Case %s (ID: %s) Severity: %s' % (
case['displayId'], case['caseId'], case['severityCode']
))
print('Status: %s' % case['status'])
print('Subject: %s' % case['subject'])
print('\tCategory: %s Service: %s' % (case['categoryCode'], case['serviceCode']))
print('\tSubmitted By %s at %s (cc: %s)' % (
case['submittedBy'], case['timeCreated'], case['ccEmailAddresses']
))
print("\n")
# communications
print("### Communications:\n")
comms = case['recentCommunications']['communications']
for comm in sorted(comms, key=lambda k: k['timeCreated']):
print("=> %s from %s" % (comm['timeCreated'], comm['submittedBy']))
print(comm['body'] + "\n")
print("\n")
# limit requests
print('### Limit Requests:')
for lr in lim_requests:
print('%d) Service: "%s" Limit: "%s" Region: "%s" New Value: %d' % (
lr['request_num'], lr['Service'], lr['Limit name'],
lr['Region'], lr['New limit value']
))
def get_cases(self, **kwargs):
"""
Wrapper around boto3 support.describe_cases to handle pagination. Calls
`boto3.Support.Client.describe_cases <http://boto3.readthedocs.io/en/
latest/reference/services/support.html#Support.Client.describe_cases>`_
with the specified ``kwargs``, and combines results if ``nextToken`` is
present.
:param kwargs: kwargs to call ``describe_cases`` with
:type kwargs: dict
:return: list of support cases
:rtype: list
"""
all_cases = []
logger.debug('Beginning cases query')
while True:
cases = self.support.describe_cases(**kwargs)
all_cases += cases['cases']
logger.debug('Got %d cases' % len(cases['cases']))
if 'nextToken' not in cases:
logger.debug('Reached end of paginated results')
break
logger.debug('Paginating with nextToken=%s', cases['nextToken'])
kwargs['nextToken'] = cases['nextToken']
logger.debug('Found a total of %d cases', len(all_cases))
return all_cases
def parse_args(argv):
"""
parse arguments/options
this uses the new argparse module instead of optparse
see: <https://docs.python.org/2/library/argparse.html>
"""
p = argparse.ArgumentParser(
description='show AWS limit increase cases for a specific service'
)
p.add_argument('-v', '--verbose', dest='verbose', action='count', default=1,
help='verbose output. specify twice for debug-level output.')
p.add_argument('-l', '--list-categories', dest='list_categories',
action='store_true', default=False,
help='List limit increase request categories and exit')
p.add_argument(
'CATEGORY', action='store', type=str, default=None, nargs='?',
help='Category (service) name to list cases for'
)
args = p.parse_args(argv)
return args
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
if args.verbose > 1:
logger.setLevel(logging.DEBUG)
elif args.verbose > 0:
logger.setLevel(logging.INFO)
script = LimitIncreaseFinder()
if args.list_categories or args.CATEGORY is None:
script.list_categories()
raise SystemExit(0)
script.show_cases_for_service(args.CATEGORY)