Retrieve information for the specified VPC.

    If no VPC ID was specified then just pick the first VPC we find.

    Returns a dict with the VPC's zones, subnets, route tables and
    instances.
8895
def get_vpc_overview(con, vpc_id, region_name):
    
    logging.debug("Retrieving information for VPC '%s'" % vpc_id)
    d = {}
    d['zones'] = con.get_all_zones()

    # Find the specified VPC, or just use the first one
    all_vpcs    = con.get_all_vpcs()
    if not all_vpcs:
        raise VpcRouteSetError("Cannot find any VPCs.")

    if not vpc_id:
        # Just grab the first available VPC and use it, if no VPC specified
        vpc = all_vpcs[0]
        vpc_id = vpc.id
    else:
        # Search through the list of VPCs for the one with the specified ID
        vpc = None
        for v in all_vpcs:
            if v.id == vpc_id:
                vpc = v
                break
        if not vpc:
            raise VpcRouteSetError("Cannot find specified VPC '%s' "
                                   "in region '%s'." % (vpc_id, region_name))
    d['vpc'] = vpc

    vpc_filter = {"vpc-id" : vpc_id}  # Will use this filter expression a lot

    # Now find the subnets, route tables and instances within this VPC
    d['subnets']      = con.get_all_subnets(filters=vpc_filter)
    d['route_tables'] = con.get_all_route_tables(filters=vpc_filter)

    # Route tables are associated with subnets. Maintain a lookup table from
    # RT ID to (list of) subnets. This is necessary later on, because
    # we only want to set a route in an RT if at least one of the ENIs we are
    # dealing with is associated with the RT. That way, we don't have to set
    # the route in all tables all the time.
    d['rt_subnet_lookup'] = {}
    for rt in d['route_tables']:
        for assoc in rt.associations:
            if hasattr(assoc, 'subnet_id'):
                subnet_id = assoc.subnet_id
                if subnet_id:
                    d['rt_subnet_lookup'].setdefault(rt.id, []). \
                                                            append(subnet_id)

    # Get all the cluster instances (all EC2 instances associated with this
    # VPC).
    reservations   = con.get_all_reservations(filters=vpc_filter)
    d['instances'] = []
    for r in reservations:  # a reservation may have multiple instances
        d['instances'].extend(r.instances)

    # Add reverse lookup from the instances private IP addresses to the
    # subnets they are associated with. More info in the doc for that function.
    _make_ip_subnet_lookup(d)

    # Maintain a quick instance lookup for convenience
    d['instance_by_id'] = {}
    for i in d['instances']:
        d['instance_by_id'][i.id] = i

    return d
556163
def _get_group(conn=None, name=None, vpc_id=None, vpc_name=None, group_id=None,
               region=None, key=None, keyid=None, profile=None):  # pylint: disable=W0613
    '''
    
    '''
    if vpc_name and vpc_id:
        raise SaltInvocationError('The params \'vpc_id\' and \'vpc_name\' '
                                  'are mutually exclusive.')
    if vpc_name:
        try:
            vpc_id = _vpc_name_to_id(vpc_id=vpc_id, vpc_name=vpc_name, region=region,
                                key=key, keyid=keyid, profile=profile)
        except boto.exception.BotoServerError as e:
            log.debug(e)
            return None
    if name:
        if vpc_id is None:
            log.debug('getting group for %s', name)
            group_filter = {'group-name': name}
            filtered_groups = conn.get_all_security_groups(filters=group_filter)
            # security groups can have the same name if groups exist in both
            # EC2-Classic and EC2-VPC
            # iterate through groups to ensure we return the EC2-Classic
            # security group
            for group in filtered_groups:
                # a group in EC2-Classic will have vpc_id set to None
                if group.vpc_id is None:
                    return group
            # If there are more security groups, and no vpc_id, we can't know which one to choose.
            if len(filtered_groups) > 1:
                raise CommandExecutionError('Security group belongs to more VPCs, specify the VPC ID!')
            elif len(filtered_groups) == 1:
                return filtered_groups[0]
            return None
        elif vpc_id:
            log.debug('getting group for %s in vpc_id %s', name, vpc_id)
            group_filter = {'group-name': name, 'vpc_id': vpc_id}
            filtered_groups = conn.get_all_security_groups(filters=group_filter)
            if len(filtered_groups) == 1:
                return filtered_groups[0]
            else:
                return None
        else:
            return None
    elif group_id:
        try:
            groups = conn.get_all_security_groups(group_ids=[group_id])
        except boto.exception.BotoServerError as e:
            log.debug(e)
            return None
        if len(groups) == 1:
            return groups[0]
        else:
            return None
    else:
        return None
1055147
def retrieve_dcnm_subnet_info(self, tenant_id, direc):
        
        serv_obj = self.get_service_obj(tenant_id)
        subnet_dict = serv_obj.get_dcnm_subnet_dict(direc)
        return subnet_dict
937097
def get_ips(self, instance_id):
        
        self._init_az_api()
        cluster_name, node_name = instance_id
        # XXX: keep in sync with contents of `vm_deployment_template`
        ip_name = ('{node_name}-public-ip'.format(node_name=node_name))
        ip = self._network_client.public_ip_addresses.get(cluster_name, ip_name)
        if (ip.provisioning_state == 'Succeeded' and ip.ip_address):
            return [ip.ip_address]
        else:
            return []
1339668
def get_config(name=None, group_id=None, region=None, key=None, keyid=None,
               profile=None, vpc_id=None, vpc_name=None):
    '''
    
    '''
    conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)

    sg = _get_group(conn, name=name, vpc_id=vpc_id, vpc_name=vpc_name,
                    group_id=group_id, region=region, key=key, keyid=keyid,
                    profile=profile)
    if sg:
        ret = odict.OrderedDict()
        ret['name'] = sg.name
        # TODO: add support for vpc_id in return
        # ret['vpc_id'] = sg.vpc_id
        ret['group_id'] = sg.id
        ret['owner_id'] = sg.owner_id
        ret['description'] = sg.description
        ret['tags'] = sg.tags
        _rules = _parse_rules(sg, sg.rules)
        _rules_egress = _parse_rules(sg, sg.rules_egress)
        ret['rules'] = _split_rules(_rules)
        ret['rules_egress'] = _split_rules(_rules_egress)
        return ret
    else:
        return None
670713
def get_dhcp_options(dhcp_options_name=None, dhcp_options_id=None,
                     region=None, key=None, keyid=None, profile=None):
    '''
    
    '''
    if not any((dhcp_options_name, dhcp_options_id)):
        raise SaltInvocationError('At least one of the following must be specified: '
                                  'dhcp_options_name, dhcp_options_id.')

    if not dhcp_options_id and dhcp_options_name:
        dhcp_options_id = _get_resource_id('dhcp_options', dhcp_options_name,
                                            region=region, key=key,
                                            keyid=keyid, profile=profile)
    if not dhcp_options_id:
        return {'dhcp_options': {}}

    try:
        conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
        r = conn.get_all_dhcp_options(dhcp_options_ids=[dhcp_options_id])
    except BotoServerError as e:
        return {'error': __utils__['boto.get_error'](e)}

    if not r:
        return {'dhcp_options': None}

    keys = ('domain_name', 'domain_name_servers', 'ntp_servers',
            'netbios_name_servers', 'netbios_node_type')

    return {'dhcp_options': dict((k, r[0].options.get(k)) for k in keys)}
1243799
def first_ipv4(self) -> Optional[AddressInfo]:
        ''''''
        for info in self._address_infos:
            if info.family == socket.AF_INET:
                return info
501766
def list_security_groups_in_vpc(self, vpc_id=None):
        
        log = logging.getLogger(self.cls_logger + '.list_security_groups_in_vpc')
        if vpc_id is None and self.vpc_id is not None:
            vpc_id = self.vpc_id
        else:
            msg = 'Unable to determine VPC ID to use to create the Security Group'
            log.error(msg)
            raise EC2UtilError(msg)

        # Create a filter on the VPC ID
        filters = [
            {
                'Name': 'vpc-id',
                'Values': [vpc_id]
            }
        ]

        # Get a list of security groups in the VPC
        log.info('Querying for a list of security groups in VPC ID: {v}'.format(v=vpc_id))
        try:
            security_groups = self.client.describe_security_groups(DryRun=False, Filters=filters)
        except ClientError:
            _, ex, trace = sys.exc_info()
            msg = 'Unable to query AWS for a list of security groups in VPC ID: {v}\n{e}'.format(
                v=vpc_id, e=str(ex))
            raise AWSAPIError, msg, trace
        return security_groups
682811
def __get_subnetwork(vm_):
    '''
    
    '''
    ex_subnetwork = config.get_cloud_config_value(
        'subnetwork', vm_, __opts__,
        search_global=False)

    return ex_subnetwork
117210
def get_vpn6_table(self):
        
        vpn_table = self._global_tables.get(RF_IPv6_VPN)
        # Lazy initialize the table.
        if not vpn_table:
            vpn_table = Vpnv6Table(self._core_service, self._signal_bus)
            self._global_tables[RF_IPv6_VPN] = vpn_table
            self._tables[(None, RF_IPv6_VPN)] = vpn_table

        return vpn_table
1345667
def get_next_ip(self, tenant_id, direc):
        
        # TODO(padkrish) Put in a common functionality for services.
        if direc == 'in':
            subnet_dict = self.get_in_ip_addr(tenant_id)
        else:
            subnet_dict = self.get_out_ip_addr(tenant_id)
        if subnet_dict:
            return subnet_dict
        if direc == 'in':
            # ip_next = self.service_in_ip.allocate_subnet()
            ip_next = self.check_allocate_ip(self.service_in_ip, "in")
        else:
            # ip_next = self.service_out_ip.allocate_subnet()
            ip_next = self.check_allocate_ip(self.service_out_ip, "out")
        return {'subnet': ip_next, 'start': self.get_start_ip(ip_next),
                'end': self.get_end_ip(ip_next),
                'gateway': self.get_gateway(ip_next),
                'sec_gateway': self.get_secondary_gateway(ip_next)}
80582
def _find_nat_gateways(nat_gateway_id=None, subnet_id=None, subnet_name=None, vpc_id=None, vpc_name=None,
                       states=('pending', 'available'),
                       region=None, key=None, keyid=None, profile=None):
    '''
    
    '''

    if not any((nat_gateway_id, subnet_id, subnet_name, vpc_id, vpc_name)):
        raise SaltInvocationError('At least one of the following must be '
                                  'provided: nat_gateway_id, subnet_id, '
                                  'subnet_name, vpc_id, or vpc_name.')
    filter_parameters = {'Filter': []}

    if nat_gateway_id:
        filter_parameters['NatGatewayIds'] = [nat_gateway_id]

    if subnet_name:
        subnet_id = _get_resource_id('subnet', subnet_name,
                                     region=region, key=key,
                                     keyid=keyid, profile=profile)
        if not subnet_id:
            return False

    if subnet_id:
        filter_parameters['Filter'].append({'Name': 'subnet-id', 'Values': [subnet_id]})

    if vpc_name:
        vpc_id = _get_resource_id('vpc', vpc_name,
                                     region=region, key=key,
                                     keyid=keyid, profile=profile)
        if not vpc_id:
            return False

    if vpc_id:
        filter_parameters['Filter'].append({'Name': 'vpc-id', 'Values': [vpc_id]})

    conn3 = _get_conn3(region=region, key=key, keyid=keyid, profile=profile)
    nat_gateways = []
    for ret in __utils__['boto3.paged_call'](conn3.describe_nat_gateways,
                                           marker_flag='NextToken', marker_arg='NextToken',
                                           **filter_parameters):
        for gw in ret.get('NatGateways', []):
            if gw.get('State') in states:
                nat_gateways.append(gw)
    log.debug('The filters criteria %s matched the following nat gateways: %s',
              filter_parameters, nat_gateways)

    if nat_gateways:
        return nat_gateways
    else:
        return False
1119816
def get_ec2_info(connection,
                 instance_id,
                 region,
                 username=None):
    
    instance = connection.get_only_instances(
        filters={'instance_id': instance_id}
        )[0]

    data = instance.__dict__
    data['state'] = instance.state
    data['cloud_type'] = 'ec2'

    try:
        volume = connection.get_all_volumes(
            filters={'attachment.instance-id': instance.id}
        )[0].id
        data['volume'] = volume
    except:
        data['volume'] = ''
    return data
539587
def get_data(conn_objs, providers):
    """"""
    cld_svc_map = {"aws": nodes_aws,
                   "azure": nodes_az,
                   "gcp": nodes_gcp,
                   "alicloud": nodes_ali}
    sys.stdout.write("\rCollecting Info:  ")
    sys.stdout.flush()
    busy_obj = busy_disp_on()
    collec_fn = [[cld_svc_map[x.rstrip('1234567890')], conn_objs[x]]
                 for x in providers]
    ngroup = Group()
    node_list = []
    node_list = ngroup.map(get_nodes, collec_fn)
    ngroup.join()
    busy_disp_off(dobj=busy_obj)
    sys.stdout.write("\r                                                 \r")
    sys.stdout.write("\033[?25h")  # cursor back on
    sys.stdout.flush()
    return node_list
462892
def parse_instance(self, global_params, region, reservation):
        
        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', 'Ipv6Addresses'])
                instance['network_interfaces'][eni['NetworkInterfaceId']] = nic
            self.vpcs[vpc_id].instances[i['InstanceId']] = instance
1316489
def get_host_info_dict_from_describe_dict(self, describe_dict):
        '''  '''

        # I really don't agree with prefixing everything with 'ec2'
        # because EC2, RDS and ElastiCache are different services.
        # I'm just following the pattern used until now to not break any
        # compatibility.

        host_info = {}
        for key in describe_dict:
            value = describe_dict[key]
            key = self.to_safe('ec2_' + self.uncammelize(key))

            # Handle complex types

            # Target: Memcached Cache Clusters
            if key == 'ec2_configuration_endpoint' and value:
                host_info['ec2_configuration_endpoint_address'] = value['Address']
                host_info['ec2_configuration_endpoint_port'] = value['Port']

            # Target: Cache Nodes and Redis Cache Clusters (single node)
            if key == 'ec2_endpoint' and value:
                host_info['ec2_endpoint_address'] = value['Address']
                host_info['ec2_endpoint_port'] = value['Port']

            # Target: Redis Replication Groups
            if key == 'ec2_node_groups' and value:
                host_info['ec2_endpoint_address'] = value[0]['PrimaryEndpoint']['Address']
                host_info['ec2_endpoint_port'] = value[0]['PrimaryEndpoint']['Port']
                replica_count = 0
                for node in value[0]['NodeGroupMembers']:
                    if node['CurrentRole'] == 'primary':
                        host_info['ec2_primary_cluster_address'] = node['ReadEndpoint']['Address']
                        host_info['ec2_primary_cluster_port'] = node['ReadEndpoint']['Port']
                        host_info['ec2_primary_cluster_id'] = node['CacheClusterId']
                    elif node['CurrentRole'] == 'replica':
                        host_info['ec2_replica_cluster_address_'+ str(replica_count)] = node['ReadEndpoint']['Address']
                        host_info['ec2_replica_cluster_port_'+ str(replica_count)] = node['ReadEndpoint']['Port']
                        host_info['ec2_replica_cluster_id_'+ str(replica_count)] = node['CacheClusterId']
                        replica_count += 1

            # Target: Redis Replication Groups
            if key == 'ec2_member_clusters' and value:
                host_info['ec2_member_clusters'] = ','.join([str(i) for i in value])

            # Target: All Cache Clusters
            elif key == 'ec2_cache_parameter_group':
                host_info["ec2_cache_node_ids_to_reboot"] = ','.join([str(i) for i in value['CacheNodeIdsToReboot']])
                host_info['ec2_cache_parameter_group_name'] = value['CacheParameterGroupName']
                host_info['ec2_cache_parameter_apply_status'] = value['ParameterApplyStatus']

            # Target: Almost everything
            elif key == 'ec2_security_groups':

                # Skip if SecurityGroups is None
                # (it is possible to have the key defined but no value in it).
                if value is not None:
                    sg_ids = []
                    for sg in value:
                        sg_ids.append(sg['SecurityGroupId'])
                    host_info["ec2_security_group_ids"] = ','.join([str(i) for i in sg_ids])

            # Target: Everything
            # Preserve booleans and integers
            elif type(value) in [int, bool]:
                host_info[key] = value

            # Target: Everything
            # Sanitize string values
            elif isinstance(value, six.string_types):
                host_info[key] = value.strip()

            # Target: Everything
            # Replace None by an empty string
            elif type(value) == type(None):
                host_info[key] = ''

            else:
                # Remove non-processed complex types
                pass

        return host_info
15322
def get_vpc_config(self, vpc_config_override=vpc_utils.VPC_CONFIG_DEFAULT):
        
        if vpc_config_override is vpc_utils.VPC_CONFIG_DEFAULT:
            return vpc_utils.to_dict(self.subnets, self.security_group_ids)
        else:
            return vpc_utils.sanitize(vpc_config_override)