-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update code for better use, addind delay and additional condition #150
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -151,6 +151,7 @@ Resources: | |||||||||||||||||||||||||
|
||||||||||||||||||||||||||
def lambda_handler(event, context): | ||||||||||||||||||||||||||
# get secret | ||||||||||||||||||||||||||
print(event) | ||||||||||||||||||||||||||
secrets = boto3.client('secretsmanager').get_secret_value(SecretId=fss_key) | ||||||||||||||||||||||||||
sm_data = json.loads(secrets["SecretString"]) | ||||||||||||||||||||||||||
new_api_format = sm_data["c1apikey"] | ||||||||||||||||||||||||||
|
@@ -215,15 +216,19 @@ Resources: | |||||||||||||||||||||||||
response = s3_client.get_bucket_tagging(Bucket=bucket_name) | ||||||||||||||||||||||||||
tags = response["TagSet"] | ||||||||||||||||||||||||||
tag_status = tags | ||||||||||||||||||||||||||
print("tag_Status1: ", tag_status) | ||||||||||||||||||||||||||
except ClientError: | ||||||||||||||||||||||||||
no_tags = "does not have tags" | ||||||||||||||||||||||||||
tag_status = no_tags | ||||||||||||||||||||||||||
if tag_status == "does not have tags": | ||||||||||||||||||||||||||
print("does not have tags") | ||||||||||||||||||||||||||
add_tag(s3_client, bucket_name, tag_list=[]) | ||||||||||||||||||||||||||
add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
Comment on lines
220
to
227
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can simplify the code by taking out the if condition when
Suggested change
|
||||||||||||||||||||||||||
print("tag_Status2: ", tag_status) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we want to check the updated bucket tag after new tags were added, maybe consider adding the log at the end of def add_tag(s3_client, bucket_name, tag_list):
tag_list.append({'Key':'FSSMonitored', 'Value': 'Yes'})
print(f"Bucket: {bucket_name} lacks an FSSMonitored tag; adding")
s3_client.put_bucket_tagging(
Bucket=bucket_name,
Tagging={"TagSet": tag_list},
)
print("Updated bucket tag set: ", tag_list) |
||||||||||||||||||||||||||
for tags in tag_status: | ||||||||||||||||||||||||||
if tags["Key"] == "FSSMonitored": | ||||||||||||||||||||||||||
print(tags["Value"].lower()) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think we need this since the tag set logs will print out the detailed tag structure and values already. |
||||||||||||||||||||||||||
if tags["Value"].lower() == "no": | ||||||||||||||||||||||||||
# if tag FSSMonitored is no; quit | ||||||||||||||||||||||||||
print( | ||||||||||||||||||||||||||
|
@@ -235,8 +240,12 @@ Resources: | |||||||||||||||||||||||||
elif tags["Value"].lower() != "yes": | ||||||||||||||||||||||||||
add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||
Comment on lines
240
to
242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, if the user gave an invalid |
||||||||||||||||||||||||||
add_tag(s3_client, bucket_name, tag_list=tag_status) | ||||||||||||||||||||||||||
add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||||||||||||||||||||||||||
elif tags["Value"].lower() == "yes": | ||||||||||||||||||||||||||
add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
add_tag(s3_client, bucket_name, tag_list=tag_status) #move this inside if to avoid duplicate tags | ||||||||||||||||||||||||||
add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||||||||||||||||||||||||||
Comment on lines
+246
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By moving the
Suggested change
|
||||||||||||||||||||||||||
def add_tag(s3_client, bucket_name, tag_list): | ||||||||||||||||||||||||||
tag_list.append({'Key':'FSSMonitored', 'Value': 'Yes'}) | ||||||||||||||||||||||||||
print(f"Bucket: {bucket_name} lacks an FSSMonitored tag; adding") | ||||||||||||||||||||||||||
|
@@ -310,10 +319,11 @@ Resources: | |||||||||||||||||||||||||
check_status(cloud_one_api_key, url) | ||||||||||||||||||||||||||
#check storage stack status | ||||||||||||||||||||||||||
def check_status(cloud_one_api_key, url): | ||||||||||||||||||||||||||
time.sleep(10) #Adding delay to avoid response error in API | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain a bit more about the response error mentioned here? and why you choose to use 10 seconds as the delay interval? |
||||||||||||||||||||||||||
#gather stack status | ||||||||||||||||||||||||||
st_call = http.request('GET', url , headers = {'Authorization': cloud_one_api_key, 'Api-Version': 'v1'}) | ||||||||||||||||||||||||||
headers = {'Authorization': cloud_one_api_key, 'Api-Version': 'v1'} | ||||||||||||||||||||||||||
st_call = http.request('GET', url, headers=headers) | ||||||||||||||||||||||||||
status = json.loads(st_call.data.decode('utf-8'))['status'] | ||||||||||||||||||||||||||
print("Status: " + status) | ||||||||||||||||||||||||||
while status == 'creating': | ||||||||||||||||||||||||||
st_call = http.request('GET', url , headers = {'Authorization': cloud_one_api_key, 'Api-Version': 'v1'}) | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part could also use the
Suggested change
|
||||||||||||||||||||||||||
status = json.loads(st_call.data.decode('utf-8'))['status'] | ||||||||||||||||||||||||||
|
@@ -322,6 +332,7 @@ Resources: | |||||||||||||||||||||||||
print('Deployed Successfully') | ||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||
print('Deployment Failed') | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
TracingConfig: | ||||||||||||||||||||||||||
Mode: Active | ||||||||||||||||||||||||||
MemorySize: 128 | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe consider logging sentences that are more readable: