def parse_lb(self, global_params, region, lb): """ :param global_params: :param region: :param source: :return: """ lb['arn'] = lb.pop('LoadBalancerArn') lb['name'] = lb.pop('LoadBalancerName') vpc_id = lb.pop( 'VpcId') if 'VpcId' in lb and lb['VpcId'] else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) lb['security_groups'] = [] for sg in lb['SecurityGroups']: lb['security_groups'].append({'GroupId': sg}) lb.pop('SecurityGroups') lb['listeners'] = {} # Get listeners listeners = handle_truncated_response( api_clients[region].describe_listeners, {'LoadBalancerArn': lb['arn']}, ['Listeners'])['Listeners'] for listener in listeners: listener.pop('ListenerArn') listener.pop('LoadBalancerArn') port = listener.pop('Port') lb['listeners'][port] = listener # Get attributes lb['attributes'] = api_clients[ region].describe_load_balancer_attributes( LoadBalancerArn=lb['arn'])['Attributes'] self.vpcs[vpc_id].lbs[self.get_non_aws_id(lb['name'])] = lb
def parse_cluster(self, global_params, region, cluster): """ Parse a single ElastiCache cluster :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param cluster: ElastiCache cluster """ cluster_name = cluster.pop('CacheClusterId') cluster['name'] = cluster_name # Must fetch info about the subnet group to retrieve the VPC ID... if 'CacheSubnetGroupName' in cluster: subnet_group = api_clients[region].describe_cache_subnet_groups( CacheSubnetGroupName=cluster['CacheSubnetGroupName'] )['CacheSubnetGroups'][0] vpc_id = subnet_group['VpcId'] else: vpc_id = ec2_classic subnet_group = None manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) self.vpcs[vpc_id].clusters[cluster_name] = cluster if subnet_group: self.vpcs[vpc_id].subnet_groups[ subnet_group['CacheSubnetGroupName']] = subnet_group
def parse_instance(self, global_params, region, dbi): """ Parse a single RDS instance :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param instance: Instance """ vpc_id = dbi['DBSubnetGroup'][ 'VpcId'] if 'DBSubnetGroup' in dbi and 'VpcId' in dbi[ 'DBSubnetGroup'] and dbi['DBSubnetGroup'][ 'VpcId'] else ec2_classic instance = {} instance['name'] = dbi.pop('DBInstanceIdentifier') for key in [ 'InstanceCreateTime', 'Engine', 'DBInstanceStatus', 'AutoMinorVersionUpgrade', 'DBInstanceClass', 'MultiAZ', 'Endpoint', 'BackupRetentionPeriod', 'PubliclyAccessible', 'StorageEncrypted', 'VpcSecurityGroups', 'DBSecurityGroups', 'DBParameterGroups', 'EnhancedMonitoringResourceArn', 'StorageEncrypted' ]: # parameter_groups , security_groups, vpc_security_groups instance[key] = dbi[key] if key in dbi else None # If part of a cluster, multi AZ information is only available via cluster information if 'DBClusterIdentifier' in dbi: api_client = api_clients[region] cluster = api_client.describe_db_clusters( DBClusterIdentifier=dbi['DBClusterIdentifier'] )['DBClusters'][0] instance['MultiAZ'] = cluster['MultiAZ'] # Save manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) self.vpcs[vpc_id].instances[instance['name']] = instance
def parse_snapshot(self, global_params, region, dbs): """ :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param dbs: Snapshot :return: """ vpc_id = dbs['VpcId'] if 'VpcId' in dbs else ec2_classic snapshot_id = dbs.pop('DBSnapshotIdentifier') snapshot = { 'arn': dbs.pop('DBSnapshotArn'), 'id': snapshot_id, 'name': snapshot_id, 'vpc_id': vpc_id } attributes = [ 'DBInstanceIdentifier', 'SnapshotCreateTime', 'Encrypted', 'OptionGroupName' ] for attribute in attributes: snapshot[attribute] = dbs[attribute] if attribute in dbs else None api_client = api_clients[region] attributes = api_client.describe_db_snapshot_attributes( DBSnapshotIdentifier=snapshot_id)['DBSnapshotAttributesResult'] snapshot['attributes'] = attributes['DBSnapshotAttributes'] if 'DBSnapshotAttributes' in attributes else {} # Save manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) self.vpcs[vpc_id].snapshots[snapshot_id] = snapshot
def parse_security_group(self, global_params, region, group): """ Parse a single Redsfhit security group :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param security)_group: Security group """ vpc_id = group[ 'VpcId'] if 'VpcId' in group and group['VpcId'] else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) security_group = {} security_group['name'] = group['GroupName'] security_group['id'] = group['GroupId'] security_group['description'] = group['Description'] security_group['owner_id'] = group['OwnerId'] security_group['rules'] = {'ingress': {}, 'egress': {}} security_group['rules']['ingress']['protocols'], security_group[ 'rules']['ingress']['count'] = self.__parse_security_group_rules( group['IpPermissions']) security_group['rules']['egress']['protocols'], security_group[ 'rules']['egress']['count'] = self.__parse_security_group_rules( group['IpPermissionsEgress']) self.vpcs[vpc_id].security_groups[group['GroupId']] = security_group
def parse_instance(self, global_params, region, reservation): """ Parse a single EC2 instance :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param instance: Cluster """ for i in reservation['Instances']: instance = {} vpc_id = i['VpcId'] if 'VpcId' in i and i['VpcId'] else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) instance['reservation_id'] = reservation['ReservationId'] instance['id'] = i['InstanceId'] get_name(i, instance, 'InstanceId') get_keys(i, instance, [ 'KeyName', 'LaunchTime', 'InstanceType', 'State', 'IamInstanceProfile', 'SubnetId' ]) # Network interfaces & security groups manage_dictionary(instance, 'network_interfaces', {}) for eni in i['NetworkInterfaces']: nic = {} get_keys(eni, nic, [ 'Association', 'Groups', 'PrivateIpAddresses', 'SubnetId' ]) instance['network_interfaces'][eni['NetworkInterfaceId']] = nic self.vpcs[vpc_id].instances[i['InstanceId']] = instance
def parse_security_group(self, global_params, region, security_group): """ Parse a single Redsfhit security group :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param security)_group: Security group """ vpc_id = security_group.pop( 'VpcId') if 'VpcId' in security_group else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) security_group['arn'] = security_group.pop('DBSecurityGroupArn') security_group['name'] = security_group.pop('DBSecurityGroupName') # Save manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) self.vpcs[vpc_id].security_groups['name'] = security_group
def store_target(self, global_params, region, target): target_type = target.pop('scout2_target_type') if 'VpcId' in target: vpc_id = target.pop('VpcId') manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) tmp = getattr(self, 'vpcs')[vpc_id] target_dict = getattr(tmp, target_type) else: target_dict = getattr(self, target_type) target_id = target[resource_id_map[target_type]] get_name(target, target, resource_id_map[target_type]) target_dict[target_id] = target
def parse_cluster(self, global_params, region, cluster): """ Parse a single Redshift cluster :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param cluster: Cluster """ vpc_id = cluster.pop('VpcId') if 'VpcId' in cluster else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig()) name = cluster.pop('ClusterIdentifier') cluster['name'] = name self.vpcs[vpc_id].clusters[name] = cluster
def parse_cluster(self, global_params, region, cluster): """ Parse a single EMR cluster :param global_params: Parameters shared for all regions :param region: Name of the AWS region :param cluster: EMR cluster """ cluster_id = cluster['Id'] cluster = api_clients[region].describe_cluster(ClusterId = cluster_id)['Cluster'] cluster['id'] = cluster.pop('Id') cluster['name'] = cluster.pop('Name') vpc_id = 'TODO' # The EMR API won't disclose the VPC ID, so wait until all configs have been fetch and look up the VPC based on the subnet ID manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) self.vpcs[vpc_id].clusters[cluster_id] = cluster
def parse_elb(self, global_params, region, lb): """ :param global_params: :param region: :param elb: :return: """ elb = {} elb['name'] = lb.pop('LoadBalancerName') vpc_id = lb['VPCId'] if 'VPCId' in lb and lb['VPCId'] else ec2_classic manage_dictionary(self.vpcs, vpc_id, VPCConfig(self.vpc_resource_types)) get_keys(lb, elb, [ 'DNSName', 'CreatedTime', 'AvailabilityZones', 'Subnets', 'Scheme' ]) elb['security_groups'] = [] for sg in lb['SecurityGroups']: elb['security_groups'].append({'GroupId': sg}) manage_dictionary(elb, 'listeners', {}) policy_names = [] for l in lb['ListenerDescriptions']: listener = l['Listener'] manage_dictionary(listener, 'policies', []) for policy_name in l['PolicyNames']: policy_id = self.get_non_aws_id(policy_name) listener['policies'].append(policy_id) if policy_id not in self.elb_policies: policy_names.append(policy_name) elb['listeners'][l['Listener']['LoadBalancerPort']] = listener # Fetch LB policies here. This is not ideal, but the alternative is to download all policies and clean up after... if len(policy_names): policies = api_clients[region].describe_load_balancer_policies( LoadBalancerName=elb['name'], PolicyNames=policy_names)['PolicyDescriptions'] for policy in policies: policy['name'] = policy.pop('PolicyName') policy_id = self.get_non_aws_id(policy['name']) self.elb_policies[policy_id] = policy manage_dictionary(elb, 'instances', []) for i in lb['Instances']: elb['instances'].append(i['InstanceId']) self.vpcs[vpc_id].elbs[self.get_non_aws_id(elb['name'])] = elb