简体   繁体   English

使用 boto3 检索 ECS 标签

[英]Retrieving ECS tags using boto3

I have below boto3 which gives list of dicts with key, value pairs .我在 boto3 下面给出list of dicts with key, value pairs

service_paginator = ecs_client.get_paginator('list_services')
for page in service_paginator.paginate(cluster=cluster_name,
                                       launchType='FARGATE'):
    # print(page)
    for service in page['serviceArns']:
        response = ecs_client.list_tags_for_resource(resourceArn=service)['tags']

This dict has multiple keys, value pairs.这个字典有多个键值对。 in the below sample format:在以下示例格式中:

Row-1:[{'key': 'Platform', 'value': 'XX'}, {'key': 'StackVersion', 'value': '1.0.1'},{'key': 'ResourceOwner', 'value': 'TeamA'}, {'key': 'Stackname', 'value': 'myfargate-1'}, {'key': 'Service', 'value': 'Processing'}, {'key': 'Name', 'value': 'someName'},{'key': 'deploy_date', 'value': '2021-07-12'}, {'key': 'Source', 'value': 'somesource'}]
Row-2:[{'key': 'Platform', 'value': 'XX'}, {'key': 'StackVersion', 'value': '1.0.1'},{'key': 'ResourceOwner', 'value': 'TeamA'}, {'key': 'Stackname', 'value': 'myfargate-1'}, {'key': 'Service', 'value': 'Processing'}, {'key': 'Name', 'value': 'someName'},{'key': 'deploy_date', 'value': '2021-07-12'}]
Row-3:[{'key': 'Platform', 'value': 'XXY'}, {'key': 'StackVersion', 'value': '1.0.1'},{'key': 'ResourceOwner', 'value': 'TeamA'}, {'key': 'Stackname', 'value': 'myfargate-1'}, {'key': 'Service', 'value': 'Processing'}, {'key': 'Name', 'value': 'someName'},{'key': 'deploy_date', 'value': '2021-07-12'}, {'key': 'Source', 'value': 'somesource'}]

From this lists, I would like to print the service, where in the dict 'key' == 'Platform' and 'key' == 'Source' present.从这个列表中,我想打印服务,其中 dict 'key' == 'Platform' and 'key' == 'Source'存在。 So output should be Row-1 and Row-3 , as ROw-2 doesn't have key called source .所以输出应该是 Row-1 和 Row-3 ,因为 ROw-2 没有名为source键。 for one key it's ok, but if I have to check multiple keys then it gives me ZERO count.对于一个键,它可以,但是如果我必须检查多个键,那么它会给我零计数。

Is there any pythonic way to do it for more than one key?是否有任何 pythonic 方法可以为多个键执行此操作?

I will answer this to myself, but I personally don't like this solution, if anyone has any better solution, please post it here.我会自己回答这个问题,但我个人不喜欢这个解决方案,如果有人有更好的解决方案,请在这里发布。

service_paginator = ecs_client.get_paginator('list_services')
for page in service_paginator.paginate(cluster=cluster_name,
                                       launchType='FARGATE'):
    # print(page)
    for service in page['serviceArns']:
        response = ecs_client.list_tags_for_resource(resourceArn=service)['tags']
        tags = {}
        for item in response:
            tags[item['key']] = item['value']

            if 'Platform' in tags and 'Source' in tags:
                platform_name.append(tags['Platform'])

print(set(platform_name)) # to print the unique platform_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM