设置交叉点工作,但是打印所有输出而不是差异
我获得了使用set.difference
标记的AWS资源列表,仅给我重复。但是,当打印而不是仅获得差异时,我会得到所有结果。
我的脚本在哪里出错?
我的代码:
def extract_tags():
session = boto3.Session(profile_name='dev-apps-read-only')
client = session.client('resourcegroupstaggingapi',
region_name='ap-southeast-1')
paginator = client.get_paginator('get_resources')
page_iterator = paginator.paginate()
for page in page_iterator:
for resource in page['ResourceTagMappingList']:
resource_arn = resource['ResourceARN']
tags_list = []
for key_value_pair in resource['Tags']:
key_name = key_value_pair['Key']
key_value = key_value_pair['Value']
tags_list.append(key_name)
# list to check against the tag output from AWS
mandatory_tags_list = [
"phx.tfe_workspace",
"aws.region",
"phx.bounded_context",
"phx.owner",
"phx.service_tier"
]
# tag list from AWS. Variable declared in main loop above
tags_list
non_compliant_tags = ('Non-Compliant Tags:',
(set(mandatory_tags_list).difference(tags_list)))
count = len(set(mandatory_tags_list) & set(tags_list))
print(json.dumps(f'resource ARN: {resource_arn}'))
print(json.dumps(f'{non_compliant_tags}', indent=2))
print(count)
extract_tags()
我的输出为我提供了投诉资源和不合格的资源。我只想要不合规的资源:
Wrong:
"resource ARN:0a895d1c83fdbd76a"
"('Non-Compliant Tags:', {'phx.service_tier', 'phx.bounded_context', 'phx.tfe_workspace', 'phx.owner', 'aws.region'})"
Correct:
"resource ARN:e5b5ccfa-db6e-e027-9fab-0415d4958c30"
"('Non-Compliant Tags:', {'phx.service_tier', 'phx.tfe_workspace'})"
3
I am getting the list of AWS resources that are tagged using set.difference
to give me only the duplicates. This works, however when printing instead of getting only the differences I am getting all results.
Where am I going wrong with my script?
My code:
def extract_tags():
session = boto3.Session(profile_name='dev-apps-read-only')
client = session.client('resourcegroupstaggingapi',
region_name='ap-southeast-1')
paginator = client.get_paginator('get_resources')
page_iterator = paginator.paginate()
for page in page_iterator:
for resource in page['ResourceTagMappingList']:
resource_arn = resource['ResourceARN']
tags_list = []
for key_value_pair in resource['Tags']:
key_name = key_value_pair['Key']
key_value = key_value_pair['Value']
tags_list.append(key_name)
# list to check against the tag output from AWS
mandatory_tags_list = [
"phx.tfe_workspace",
"aws.region",
"phx.bounded_context",
"phx.owner",
"phx.service_tier"
]
# tag list from AWS. Variable declared in main loop above
tags_list
non_compliant_tags = ('Non-Compliant Tags:',
(set(mandatory_tags_list).difference(tags_list)))
count = len(set(mandatory_tags_list) & set(tags_list))
print(json.dumps(f'resource ARN: {resource_arn}'))
print(json.dumps(f'{non_compliant_tags}', indent=2))
print(count)
extract_tags()
My output gives me complaint resources and non-compliant resources. I only would like non-compliant resources:
Wrong:
"resource ARN:0a895d1c83fdbd76a"
"('Non-Compliant Tags:', {'phx.service_tier', 'phx.bounded_context', 'phx.tfe_workspace', 'phx.owner', 'aws.region'})"
Correct:
"resource ARN:e5b5ccfa-db6e-e027-9fab-0415d4958c30"
"('Non-Compliant Tags:', {'phx.service_tier', 'phx.tfe_workspace'})"
3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论