Skip to content

Commit 035f711

Browse files
authored
Merge pull request #141 from SumoLogic/aws_observability_account_alias
AWS Observability Account Alias Function.
2 parents a43e76a + 656d34d commit 035f711

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

sumologic-app-utils/src/sumoresource.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import tempfile
44
import time
5+
import csv
56
from abc import abstractmethod
67
from datetime import datetime
78
from random import uniform
@@ -1261,6 +1262,55 @@ def extract_params(self, event):
12611262
props = event.get("ResourceProperties")
12621263
return props
12631264

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+
}
12641314

12651315
class AlertsMonitor(SumoResource):
12661316

0 commit comments

Comments
 (0)