|
2 | 2 | import re |
3 | 3 | import tempfile |
4 | 4 | import time |
| 5 | +import csv |
5 | 6 | from abc import abstractmethod |
6 | 7 | from datetime import datetime |
7 | 8 | from random import uniform |
@@ -1261,6 +1262,55 @@ def extract_params(self, event): |
1261 | 1262 | props = event.get("ResourceProperties") |
1262 | 1263 | return props |
1263 | 1264 |
|
| 1265 | +class AccountAlias(SumoResource): |
| 1266 | + |
| 1267 | + def get_account_alias(self, account_id, accountaliasmappings3url, accountalias): |
| 1268 | + if accountaliasmappings3url: |
| 1269 | + try: |
| 1270 | + with requests.get(accountaliasmappings3url, stream=True) as r: |
| 1271 | + with tempfile.NamedTemporaryFile() as fp: |
| 1272 | + for chunk in r.iter_content(chunk_size=8192): |
| 1273 | + if chunk: |
| 1274 | + fp.write(chunk) |
| 1275 | + fp.flush() |
| 1276 | + fp.seek(0) |
| 1277 | + with open(fp.name, 'r') as csv_file: |
| 1278 | + csv_reader = csv.reader(csv_file, delimiter=',') |
| 1279 | + for row in csv_reader: |
| 1280 | + if row[0] == account_id and row[1] is not None: |
| 1281 | + if len(str(row[1])) <= 30 and re.search(re.compile(r'[a-z0-9]+$'), row[1]): |
| 1282 | + return {"ACCOUNT_ALIAS": row[1]}, row[1] |
| 1283 | + except Exception as e: |
| 1284 | + print("Exception while trying to read the csv file") |
| 1285 | + print(e) |
| 1286 | + if accountalias: |
| 1287 | + return {"ACCOUNT_ALIAS": accountalias}, accountalias |
| 1288 | + else: |
| 1289 | + return {"ACCOUNT_ALIAS": account_id}, account_id |
| 1290 | + else: |
| 1291 | + if accountalias: |
| 1292 | + return {"ACCOUNT_ALIAS": accountalias}, accountalias |
| 1293 | + else: |
| 1294 | + return {"ACCOUNT_ALIAS": account_id}, account_id |
| 1295 | + |
| 1296 | + return {"ACCOUNT_ALIAS": account_id}, account_id |
| 1297 | + |
| 1298 | + def create(self, account_id, accountaliasmappings3url, accountalias, *args, **kwargs): |
| 1299 | + return self.get_account_alias(account_id, accountaliasmappings3url, accountalias) |
| 1300 | + |
| 1301 | + def update(self, account_id, accountaliasmappings3url, accountalias, *args, **kwargs): |
| 1302 | + return self.get_account_alias( account_id, accountaliasmappings3url, accountalias) |
| 1303 | + |
| 1304 | + def delete(self, account_id, accountaliasmappings3url, accountalias, *args, **kwargs): |
| 1305 | + print("In Delete method for Account Alias") |
| 1306 | + |
| 1307 | + def extract_params(self, event): |
| 1308 | + props = event.get("ResourceProperties") |
| 1309 | + return { |
| 1310 | + "account_id": props.get("AccountID"), |
| 1311 | + "accountaliasmappings3url": props.get("AccountAliasMappingS3Url"), |
| 1312 | + "accountalias": props.get("AccountAlias") |
| 1313 | + } |
1264 | 1314 |
|
1265 | 1315 | class AlertsMonitor(SumoResource): |
1266 | 1316 |
|
|
0 commit comments