update code for better use, addind delay and additional condition#150
update code for better use, addind delay and additional condition#150XeniaP wants to merge 1 commit intotrendmicro:masterfrom
Conversation
| 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.
This part could also use the headers variable you declared as well.
| st_call = http.request('GET', url , headers = {'Authorization': cloud_one_api_key, 'Api-Version': 'v1'}) | |
| st_call = http.request('GET', url, headers=headers) |
| 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.
Could you explain a bit more about the response error mentioned here? and why you choose to use 10 seconds as the delay interval?
| 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) |
There was a problem hiding this comment.
By moving the add_tag() and add_storage() inside the for loop means that every tag inside tag_status array that has a key != "FSSMonitored" will trigger the add_tag() and add_storage() once. I don't think this is what we wanted.
| 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) | |
| 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) |
| elif tags["Value"].lower() != "yes": | ||
| add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | ||
| break |
There was a problem hiding this comment.
Just curious, if the user gave an invalid FSSMonitored tag value like FSSMonitored: heehee, this plugin would still deploy the storage stack, right? In this case, should we also update their invalid FSSMonitored value to Yes instead of leaving it as heehee?
| print("tag_Status2: ", tag_status) | ||
| for tags in tag_status: | ||
| if tags["Key"] == "FSSMonitored": | ||
| print(tags["Value"].lower()) |
There was a problem hiding this comment.
Don't think we need this since the tag set logs will print out the detailed tag structure and values already.
| 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: | ||
| print("tag_Status2: ", tag_status) |
There was a problem hiding this comment.
Since we want to check the updated bucket tag after new tags were added, maybe consider adding the log at the end of add_tag() instead?
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)| 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: |
There was a problem hiding this comment.
We can simplify the code by taking out the if condition when tag_status == "does not have tags" and just directly execute the actions inside the exception section like so:
| 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: | |
| except ClientError: | |
| add_tag(s3_client, bucket_name, tag_list=[]) | |
| add_storage(cloud_one_api_key, bucket_name, ext_id, account_id, stack_id, kms_arn) | |
| return 0 |
| tag_status = tags | ||
| print("tag_Status1: ", tag_status) |
There was a problem hiding this comment.
Maybe consider logging sentences that are more readable:
| tag_status = tags | |
| print("tag_Status1: ", tag_status) | |
| print("Bucket tag set: ", tags) |
Update Code
Change Summary
Adding delay in Check_Status to avoid response error on FSS Api
Adding 1 condition to handle the tags of S3 Buckets
Setting else condition to avoid duplicate tags for FSSMonited on add_tags method
PR Checklist
Other Notes
Duplicate tags

API error response
