Makes an AWS API call to the list of RDS instances in a particular
        region
488182
def get_rds_instances_by_region(self, region):
        '''  '''

        try:
            conn = self.connect_to_aws(rds, region)
            if conn:
                instances = conn.get_all_dbinstances()
                for instance in instances:
                    self.add_rds_instance(instance, region)
        except boto.exception.BotoServerError as e:
            error = e.reason

            if e.error_code == 'AuthFailure':
                error = self.get_auth_error_message()
            if not e.reason == "Forbidden":
                error = "Looks like AWS RDS is down:\n%s" % e.message
            self.fail_with_error(error, 'getting RDS instances')
616313
def list_elasticache(region, filter_by_kwargs):
    """"""
    conn = boto.elasticache.connect_to_region(region)
    req = conn.describe_cache_clusters()
    data = req["DescribeCacheClustersResponse"]["DescribeCacheClustersResult"]["CacheClusters"]
    if filter_by_kwargs:
        clusters = [x['CacheClusterId'] for x in data if x[filter_by_kwargs.keys()[0]] == filter_by_kwargs.values()[0]]
    else:
        clusters = [x['CacheClusterId'] for x in data]
    return clusters
1034727
def _find_usage_apis(self):
        
        api_ids = []
        logger.debug('Finding usage for APIs')
        regional_count = 0
        private_count = 0
        edge_count = 0
        paginator = self.conn.get_paginator('get_rest_apis')
        for resp in paginator.paginate():
            for api in resp['items']:
                api_ids.append(api['id'])
                epconf = api.get('endpointConfiguration', {}).get('types', [])
                if epconf == ['PRIVATE']:
                    private_count += 1
                elif epconf == ['EDGE']:
                    edge_count += 1
                else:
                    regional_count += 1
        self.limits['Regional APIs per account']._add_current_usage(
            regional_count, aws_type='AWS::ApiGateway::RestApi'
        )
        self.limits['Private APIs per account']._add_current_usage(
            private_count, aws_type='AWS::ApiGateway::RestApi'
        )
        self.limits['Edge APIs per account']._add_current_usage(
            edge_count, aws_type='AWS::ApiGateway::RestApi'
        )
        logger.debug('Found %d APIs', len(api_ids))
        # now the per-API limits...
        warn_stages_paginated = None
        logger.debug('Finding usage for per-API limits')
        for api_id in api_ids:
            res_count = 0
            paginator = self.conn.get_paginator('get_resources')
            for resp in paginator.paginate(restApiId=api_id):
                res_count += len(resp['items'])
            self.limits['Resources per API']._add_current_usage(
                res_count, resource_id=api_id,
                aws_type='AWS::ApiGateway::Resource'
            )
            doc_parts = paginate_dict(
                self.conn.get_documentation_parts,
                restApiId=api_id,
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            )
            self.limits['Documentation parts per API']._add_current_usage(
                len(doc_parts), resource_id=api_id,
                aws_type='AWS::ApiGateway::DocumentationPart'
            )
            # note that per the boto3 docs, there's no pagination of this...
            stages = self.conn.get_stages(restApiId=api_id)
            if len(set(stages.keys()) - set(['item', 'ResponseMetadata'])) > 0:
                warn_stages_paginated = stages.keys()
            self.limits['Stages per API']._add_current_usage(
                len(stages['item']), resource_id=api_id,
                aws_type='AWS::ApiGateway::Stage'
            )
            authorizers = paginate_dict(
                self.conn.get_authorizers,
                restApiId=api_id,
                alc_marker_path=['position'],
                alc_data_path=['items'],
                alc_marker_param='position'
            )
            self.limits['Custom authorizers per API']._add_current_usage(
                len(authorizers), resource_id=api_id,
                aws_type='AWS::ApiGateway::Authorizer'
            )
        if warn_stages_paginated is not None:
            logger.warning(
                'APIGateway get_stages returned more keys than present in '
                'boto3 docs: %s', sorted(warn_stages_paginated)
            )
960868
def add_rds_instance(self, instance, region):
        '''  '''

        # Only want available instances unless all_rds_instances is True
        if not self.all_rds_instances and instance.status != 'available':
            return

        # Select the best destination address
        dest = instance.endpoint[0]

        if not dest:
            # Skip instances we cannot address (e.g. private VPC subnet)
            return

        # Add to index
        self.index[dest] = [region, instance.id]

        # Inventory: Group by instance ID (always a group of 1)
        if self.group_by_instance_id:
            self.inventory[instance.id] = [dest]
            if self.nested_groups:
                self.push_group(self.inventory, 'instances', instance.id)

        # Inventory: Group by region
        if self.group_by_region:
            self.push(self.inventory, region, dest)
            if self.nested_groups:
                self.push_group(self.inventory, 'regions', region)

        # Inventory: Group by availability zone
        if self.group_by_availability_zone:
            self.push(self.inventory, instance.availability_zone, dest)
            if self.nested_groups:
                if self.group_by_region:
                    self.push_group(self.inventory, region, instance.availability_zone)
                self.push_group(self.inventory, 'zones', instance.availability_zone)

        # Inventory: Group by instance type
        if self.group_by_instance_type:
            type_name = self.to_safe('type_' + instance.instance_class)
            self.push(self.inventory, type_name, dest)
            if self.nested_groups:
                self.push_group(self.inventory, 'types', type_name)

        # Inventory: Group by VPC
        if self.group_by_vpc_id and instance.subnet_group and instance.subnet_group.vpc_id:
            vpc_id_name = self.to_safe('vpc_id_' + instance.subnet_group.vpc_id)
            self.push(self.inventory, vpc_id_name, dest)
            if self.nested_groups:
                self.push_group(self.inventory, 'vpcs', vpc_id_name)

        # Inventory: Group by security group
        if self.group_by_security_group:
            try:
                if instance.security_group:
                    key = self.to_safe("security_group_" + instance.security_group.name)
                    self.push(self.inventory, key, dest)
                    if self.nested_groups:
                        self.push_group(self.inventory, 'security_groups', key)

            except AttributeError:
                self.fail_with_error('\n'.join(['Package boto seems a bit older.', 
                                            'Please upgrade boto >= 2.3.0.']))


        # Inventory: Group by engine
        if self.group_by_rds_engine:
            self.push(self.inventory, self.to_safe("rds_" + instance.engine), dest)
            if self.nested_groups:
                self.push_group(self.inventory, 'rds_engines', self.to_safe("rds_" + instance.engine))

        # Inventory: Group by parameter group
        if self.group_by_rds_parameter_group:
            self.push(self.inventory, self.to_safe("rds_parameter_group_" + instance.parameter_group.name), dest)
            if self.nested_groups:
                self.push_group(self.inventory, 'rds_parameter_groups', self.to_safe("rds_parameter_group_" + instance.parameter_group.name))

        # Global Tag: all RDS instances
        self.push(self.inventory, 'rds', dest)

        self.inventory["_meta"]["hostvars"][dest] = self.get_host_info_dict_from_instance(instance)
441235
def get_s3_buckets(api_client, s3_info, s3_params):
    
    manage_dictionary(s3_info, 'buckets', {})
    buckets = api_client[get_s3_list_region(s3_params['selected_regions'])].list_buckets()['Buckets']
    targets = []
    for b in buckets:
        # Abort if bucket is not of interest
        if (b['Name'] in s3_params['skipped_buckets']) or (len(s3_params['checked_buckets']) and b['Name'] not in s3_params['checked_buckets']):
            continue
        targets.append(b)
    s3_info['buckets_count'] = len(targets)
    s3_params['api_clients'] = api_client
    s3_params['s3_info'] = s3_info
    thread_work(targets, get_s3_bucket, params = s3_params, num_threads = 30)
    show_status(s3_info)
    s3_info['buckets_count'] = len(s3_info['buckets'])
    return s3_info
1028595
def db_aws_list_instances(self):
        '''  '''
        instances = self.db_services.get_db_instances()
        if not instances:
            print("There are no DB instances associated with your AWS account " \
                "in region " + self.db_services.get_region())
        else:
            print("Here are the current DB instances associated with your AWS " \
                "account in region " + self.db_services.get_region())
            for dbinst in instances:
                print '\t'+'-'*20
                print "\tInstance ID: " + dbinst.id
                print "\tStatus: " + dbinst.status
580080
def avail_locations(call=None):
    '''
    
    '''
    if call == 'action':
        raise SaltCloudSystemExit(
            'The avail_locations function must be called with '
            '-f or --function, or with the --list-locations option'
        )

    params = {
        'action': 'DescribeZones',
    }
    items = query(params=params)

    result = {}
    for region in items['zone_set']:
        result[region['zone_id']] = {}
        for key in region:
            result[region['zone_id']][key] = six.text_type(region[key])

    return result
1151853
def get(self, instance_ids=None, filters=None):
        """"""
        params = {}
        if filters:
            params["filters"] = make_filters(filters)
        if instance_ids:
            params['InstanceIds'] = instance_ids
        reservations = self.call("DescribeInstances",
                                 response_data_key="Reservations",
                                 **params)
        if reservations:
            return list(chain(*(r["Instances"] for r in reservations)))
        return []
880487
function buildRDSSubnetGroup(numZones = 0, { name = 'RDSSubnetGroup' } = {}) {
  if (numZones < 1) {
    return {};
  }

  const subnetIds = [];
  for (let i = 1; i <= numZones; i += 1) {
    subnetIds.push({ Ref: `${DB_SUBNET}Subnet${i}` });
  }

  return {
    [name]: {
      Type: 'AWS::RDS::DBSubnetGroup',
      Properties: {
        DBSubnetGroupName: {
          Ref: 'AWS::StackName',
        },
        DBSubnetGroupDescription: {
          Ref: 'AWS::StackName',
        },
        SubnetIds: subnetIds,
      },
    },
  };
}
566464
def ls(ctx, name, list_formatted):
    """"""
    session = create_session(ctx.obj['AWS_PROFILE_NAME'])
    ec2 = session.resource('ec2')
    if name == '*':
        instances = ec2.instances.filter()
    else:
        condition = {'Name': 'tag:Name', 'Values': [name]}
        instances = ec2.instances.filter(Filters=[condition])
    out = format_output(instances, list_formatted)
    click.echo('\n'.join(out))
344627
def list_cloudfront(region, filter_by_kwargs):
    """"""
    conn = boto.connect_cloudfront()
    instances = conn.get_all_distributions()
    return lookup(instances, filter_by=filter_by_kwargs)
363642
def _set_bucket_dns(self):
        """"""
        # Different regions have different s3 endpoint formats
        dotformat_regions = ["eu-west-2", "eu-central-1", "ap-northeast-2", "ap-south-1", "ca-central-1", "us-east-2"]
        if self.region in dotformat_regions:
            s3_endpoint = "{0}.s3-website.{1}.amazonaws.com".format(self.bucket, self.region)
        else:
            s3_endpoint = "{0}.s3-website-{1}.amazonaws.com".format(self.bucket, self.region)

        zone_ids = get_dns_zone_ids(env=self.env, facing="public")
        dns_kwargs = {
            'dns_name': self.bucket,
            'dns_name_aws': s3_endpoint,
            'dns_ttl': self.properties['dns']['ttl']
        }

        for zone_id in zone_ids:
            LOG.debug('zone_id: %s', zone_id)
            update_dns_zone_record(self.env, zone_id, **dns_kwargs)
        LOG.info("Created DNS %s for Bucket", self.bucket)
1062843
def get_instance_aws_context(ec2_client):
  
  result = {}
  try:
    result["region"] = http_get_metadata("placement/availability-zone/")
    result["region"] = result["region"][:-1]
    result["instance_id"] = http_get_metadata('instance-id')
  except IOError as error:
    raise IOError("Error looking up metadata:availability-zone or instance-id: " + repr(error))
  try:
    instance_desc = ec2_client.describe_instances(InstanceIds=[result["instance_id"]])
  except Exception as error:
    raise IOError("Error calling describe_instances: " + repr(error))
  result["account"] = instance_desc["Reservations"][0]["OwnerId"]
  arn = instance_desc["Reservations"][0]["Instances"][0]["IamInstanceProfile"]["Arn"]
  result["role"] = arn.split(":")[5].split("/")[1]
  env = re.search("^(" + EFConfig.VALID_ENV_REGEX + ")-", result["role"])
  if not env:
    raise IOError("Did not find environment in role name: " + result["role"])
  result["env"] = env.group(1)
  result["env_short"] = result["env"].strip(".0123456789")
  result["service"] = "-".join(result["role"].split("-")[1:])
  return result
739049
def list_apis(awsclient):
    """"""
    client_api = awsclient.get_client('apigateway')

    apis = client_api.get_rest_apis()['items']

    for api in apis:
        print(json2table(api))
709772
def list_ec2(region, filter_by_kwargs):
    """"""
    conn = boto.ec2.connect_to_region(region)
    instances = conn.get_only_instances()
    return lookup(instances, filter_by=filter_by_kwargs)
328698
def get_instances_by_tag(self, tags):
        

        try:
            c = ec2.connect_to_region(self._region,
                                      aws_access_key_id=self._aws_access_key_id,
                                      aws_secret_access_key=self._aws_secret_access_key)

            # select instance by list of tags (OR used)
            reservations = c.get_all_reservations(filters=SergentSsh.tags_to_dict(tags))
            instances = list()

            for r in reservations:
                # for each instance launched by this reservation
                for instance in r.instances:
                    # we need only running instances
                    if instance.state.lower() == 'running':
                        instances.append(instance)

            return instances
        except NoAuthHandlerFound:
            raise SergentSshException('Boto said that you should check your credentials')
997654
def get_all_objects_in_bucket(
        aws_bucket_name,
        s3_client=None,
        max_keys=1000
):
    
    logger.debug("Retrieving bucket object list")

    if not s3_client:
        s3_client, s3_resource = get_s3_client()

    obj_dict = {}
    paginator = s3_client.get_paginator('list_objects')
    page_iterator = paginator.paginate(Bucket=aws_bucket_name)
    for page in page_iterator:
        key_list = page.get('Contents', [])
        logger.debug("Loading page with {} keys".format(len(key_list)))
        for obj in key_list:
            obj_dict[obj.get('Key')] = obj
    return obj_dict