Added elasticsearch and autoscaling for in-use check

This commit is contained in:
AJ Bahnken 2018-02-12 13:20:14 -08:00
Родитель ed4f8e27bd
Коммит d64ea41b2d
3 изменённых файлов: 41 добавлений и 22 удалений

Просмотреть файл

@ -0,0 +1,10 @@
from conftest import botocore_client
def autoscaling_launch_configurations():
"http://botocore.readthedocs.io/en/latest/reference/services/autoscaling.html#AutoScaling.Client.describe_launch_configurations"
return botocore_client.get(
'autoscaling', 'describe_launch_configurations', [], {})\
.extract_key('LaunchConfigurations')\
.flatten()\
.values()

Просмотреть файл

@ -2,6 +2,7 @@ from collections import defaultdict
from conftest import botocore_client from conftest import botocore_client
from aws.autoscaling.resources import autoscaling_launch_configurations
from aws.elasticache.resources import elasticache_clusters from aws.elasticache.resources import elasticache_clusters
from aws.elb.resources import ( from aws.elb.resources import (
elbs, elbs,
@ -44,26 +45,30 @@ def ec2_ebs_volumes():
def ec2_security_groups_with_in_use_flag(): def ec2_security_groups_with_in_use_flag():
"""Returns security groups with an additional "InUse" key, """Returns security groups with an additional "InUse" key,
which is True if it is associated with at least one EC2 which is True if it is associated with at least one resource.
instance.
Possible resources:
- EC2
- ELBs (v1 and v2)
- RDS
- Redshift
- ElasticCache
- ElasticSearchService
- AutoScaling
""" """
sec_groups = ec2_security_groups() sec_groups = ec2_security_groups()
resources = sum([ec2_instances(), elbs(), elbs_v2(), elasticache_clusters()], []) resources = sum([
ec2_instances(),
elbs(),
elbs_v2(),
elasticache_clusters(),
autoscaling_launch_configurations()
], [])
vpc_namespaced_resources = sum([rds_db_instances(), redshift_clusters()], []) vpc_namespaced_resources = sum([rds_db_instances(), redshift_clusters()], [])
# Included:
# - ELBs (v1 and v2?)
# - RDS
# - Redshift
# - ElasticCache
# TODO: # TODO:
# Need to include: # Need to include:
# - AutoScaling (describe_launch_configurations)
#
# - ElasticSearchService
# - Are these just ec2 instances?
# - EMR? # - EMR?
# - Are these just ec2 instances? # - Are these just ec2 instances?
@ -82,8 +87,9 @@ def ec2_security_groups_with_in_use_flag():
in_use_sec_group_ids[attached_sec_group['VpcSecurityGroupId']] += 1 in_use_sec_group_ids[attached_sec_group['VpcSecurityGroupId']] += 1
for domain in elasticsearch_domains(): for domain in elasticsearch_domains():
for attached_sec_group in domain['VPCOptions']['SecurityGroupIds']: if 'VPCOptions' in domain:
in_use_sec_group_ids[attached_sec_group] += 1 for attached_sec_group in domain['VPCOptions']['SecurityGroupIds']:
in_use_sec_group_ids[attached_sec_group] += 1
for sec_group in sec_groups: for sec_group in sec_groups:
if sec_group["GroupId"] in in_use_sec_group_ids.keys(): if sec_group["GroupId"] in in_use_sec_group_ids.keys():

Просмотреть файл

@ -5,13 +5,16 @@ def elasticsearch_domains():
""" """
http://botocore.readthedocs.io/en/latest/reference/services/es.html#ElasticsearchService.Client.describe_elasticsearch_domains http://botocore.readthedocs.io/en/latest/reference/services/es.html#ElasticsearchService.Client.describe_elasticsearch_domains
""" """
domains = list_elasticsearch_domains() # You can only get 5 at a time.
print(domains) domains_list = list_elasticsearch_domains()
return botocore_client.get( domains = []
'es', 'describe_elasticsearch_domains', [], {'DomainNames': domains})\ for i in range(0, len(domains_list), 5):
.extract_key('DomainStatusList')\ domains += botocore_client.get(
.flatten()\ 'es', 'describe_elasticsearch_domains', [], {'DomainNames': domains_list[i:i+5]})\
.values() .extract_key('DomainStatusList')\
.flatten()\
.values()
return domains
def list_elasticsearch_domains(): def list_elasticsearch_domains():