I have to validate Tags with Rules for AWS resource.
Input contains list of dictionaries. [{"keyName": "KeyValue", "valueName": "ValueValue"}, {"keyName": "KeyValue", "valueName": "ValueValue"}, {"keyName": "KeyValue", "valueName": "ValueValue"}, ...]
There are following rules for validation:
- Tag key must be non empty.
- No Duplicate tags
- No tag have key from reserved keys list
- 30 tags allowed only.
- Insert Reserved tags in Replication Tags.
- Key Length should be less than 127.
- aws: prefix in tag names should not be allowed
- Value Length should be less than 255.
Code:
class ReplicationSettings:
def __init__(self):
pass
def replication_tag_validation(self, replication_tags):
""" Validate Replication Tags"""
# Tag key must be non empty.
# No Duplicate tags
# No tag have key from reserved keys list
# 30 tags allowed only.
# Insert Reserved tags in Replication Tags.
reserved_tags = ["C01_USAGE"]
unique_tags = []
new_replication_tags = []
try:
for tag in replication_tags:
key_str = tag["key"].strip()
value_str = tag["value"].strip()
if not key_str:
msg = f"Tag key should not empty."
print(msg)
return (False, {"message": msg})
if key_str in reserved_tags:
msg = f"Tag with key {key_str} is not allowed."
print(msg)
return (False, {"message": msg})
if key_str in unique_tags:
msg = f"Tag key {key_str} should be unique."
print(msg)
return (False, {"message": msg})
(status, message) = self.validate_key(key_str)
if status is False:
return (False, message)
(status, message) = self.validate_value(value_str)
if status is False:
return (False, message)
new_replication_tags.append(
{"key": key_str, "value": value_str})
unique_tags.append(key_str)
if len(new_replication_tags) > 29:
msg = "Allow maximum of 30 tags in replication settings."
print(msg)
return (False, {"message": msg})
new_replication_tags.insert(0, {"key": "C01_USAGE",
"value": "INTERNAL"})
return (True, {"replicationTags": new_replication_tags})
except Exception as err:
print("Unknown error while validating replication tags: {}"
"".format(err))
return (500, {"message": "Unable to validate Replication tags"})
def validate_key(self, key_str):
""" Validate key string. """
# Length should be less than 127.
# aws: prefix in tag names should not be allowed.
if len(key_str) > 127:
msg = f"Length of Tag key f{key_str} must be less than " \
f"127 characters"
return False, {"message": msg}
if key_str.lower().startswith("aws"):
msg = f"Tag key f{key_str} should not start with 'aws:' prefix"
return False, {"message": msg}
return True, {"message": "successful"}
def validate_value(self, value_str):
""" Validate value string. """
# Length should be less than 255.
# aws: prefix in tag values should not be allowed.
if len(value_str) > 255:
msg = f"Length of Tag value f{value_str} must be less than" \
f" 255 characters"
return False, {"message": msg}
if value_str.lower().startswith("aws"):
msg = f"Tag value f{value_str} should not start with 'aws:' prefix"
return False, {"message": msg}
return True, {"message": "successful"}
input_tags = [
{"key": "Key01", "value": "value01"},
{"key": "Key02", "value": "value01"},
{"key": "Key03", "value": "value01"},
{"key": "Key04", "value": "value01"},
{"key": "Key05", "value": "value01"},
{"key": "Key06", "value": "value01"},
{"key": "Key07", "value": "value01"},
{"key": "Key08", "value": "value01"}
]
obj = ReplicationSettings()
(status, message) = obj.replication_tag_validation(input_tags)
Above code is working. but it is not Pythonic code means not using p?ython features. Can you help me to write in more Pythonic way?
Thanks in advance.