def _create_eni_if_necessary(interface, vm_):
    '''
    Create an Elastic Interface if necessary and return a Network Interface Specification
    '''
    if 'NetworkInterfaceId' in interface and interface['NetworkInterfaceId'] is not None:
        return {'DeviceIndex': interface['DeviceIndex'],
                'NetworkInterfaceId': interface['NetworkInterfaceId']}

    params = {'Action': 'DescribeSubnets'}
    subnet_query = aws.query(params,
                             return_root=True,
                             location=get_location(vm_),
                             provider=get_provider(),
                             opts=__opts__,
                             sigver='4')

    if 'SecurityGroupId' not in interface and 'securitygroupname' in interface:
        interface['SecurityGroupId'] = _get_securitygroupname_id(interface['securitygroupname'])
    if 'SubnetId' not in interface and 'subnetname' in interface:
        interface['SubnetId'] = _get_subnetname_id(interface['subnetname'])

    subnet_id = _get_subnet_id_for_interface(subnet_query, interface)
    if not subnet_id:
        raise SaltCloudConfigError(
            'No such subnet <{0}>'.format(interface.get('SubnetId'))
        )
    params = {'SubnetId': subnet_id}

    for k in 'Description', 'PrivateIpAddress', 'SecondaryPrivateIpAddressCount':
        if k in interface:
            params[k] = interface[k]

    for k in 'PrivateIpAddresses', 'SecurityGroupId':
        if k in interface:
            params.update(_param_from_config(k, interface[k]))

    if 'AssociatePublicIpAddress' in interface:
        # Associating a public address in a VPC only works when the interface is not
        # created beforehand, but as a part of the machine creation request.
        for k in ('DeviceIndex', 'AssociatePublicIpAddress', 'NetworkInterfaceId'):
            if k in interface:
                params[k] = interface[k]
        params['DeleteOnTermination'] = interface.get('delete_interface_on_terminate', True)
        return params

    params['Action'] = 'CreateNetworkInterface'

    result = aws.query(params,
                       return_root=True,
                       location=get_location(vm_),
                       provider=get_provider(),
                       opts=__opts__,
                       sigver='4')

    eni_desc = result[1]
    if not eni_desc or not eni_desc.get('networkInterfaceId'):
        raise SaltCloudException('Failed to create interface: {0}'.format(result))

    eni_id = eni_desc.get('networkInterfaceId')
    log.debug(
        'Created network interface %s inst %s',
        eni_id, interface['DeviceIndex']
    )

    associate_public_ip = interface.get('AssociatePublicIpAddress', False)
    if isinstance(associate_public_ip, six.string_types):
        # Assume id of EIP as value
        _associate_eip_with_interface(eni_id, associate_public_ip, vm_=vm_)

    if interface.get('associate_eip'):
        _associate_eip_with_interface(eni_id, interface.get('associate_eip'), vm_=vm_)
    elif interface.get('allocate_new_eip'):
        _new_eip = _request_eip(interface, vm_)
        _associate_eip_with_interface(eni_id, _new_eip, vm_=vm_)
    elif interface.get('allocate_new_eips'):
        addr_list = _list_interface_private_addrs(eni_desc)
        eip_list = []
        for idx, addr in enumerate(addr_list):
            eip_list.append(_request_eip(interface, vm_))
        for idx, addr in enumerate(addr_list):
            _associate_eip_with_interface(eni_id, eip_list[idx], addr, vm_=vm_)

    if 'Name' in interface:
        tag_params = {'Action': 'CreateTags',
                      'ResourceId.0': eni_id,
                      'Tag.0.Key': 'Name',
                      'Tag.0.Value': interface['Name']}
        tag_response = aws.query(tag_params,
                                 return_root=True,
                                 location=get_location(vm_),
                                 provider=get_provider(),
                                 opts=__opts__,
                                 sigver='4')
        if 'error' in tag_response:
            log.error('Failed to set name of interface {0}')

    return {'DeviceIndex': interface['DeviceIndex'],
            'NetworkInterfaceId': eni_id}
def _create_eni_if_necessary ( interface , vm_ ) : if 'NetworkInterfaceId' in interface and interface [ 'NetworkInterfaceId' ] is not None : return { 'DeviceIndex' : interface [ 'DeviceIndex' ] , 'NetworkInterfaceId' : interface [ 'NetworkInterfaceId' ] } params = { 'Action' : 'DescribeSubnets' } subnet_query = aws . query ( params , return_root = True , location = get_location ( vm_ ) , provider = get_provider ( ) , opts = __opts__ , sigver = '4' ) if 'SecurityGroupId' not in interface and 'securitygroupname' in interface : interface [ 'SecurityGroupId' ] = _get_securitygroupname_id ( interface [ 'securitygroupname' ] ) if 'SubnetId' not in interface and 'subnetname' in interface : interface [ 'SubnetId' ] = _get_subnetname_id ( interface [ 'subnetname' ] ) subnet_id = _get_subnet_id_for_interface ( subnet_query , interface ) if not subnet_id : raise SaltCloudConfigError ( 'No such subnet <{0}>' . format ( interface . get ( 'SubnetId' ) ) ) params = { 'SubnetId' : subnet_id } for k in 'Description' , 'PrivateIpAddress' , 'SecondaryPrivateIpAddressCount' : if k in interface : params [ k ] = interface [ k ] for k in 'PrivateIpAddresses' , 'SecurityGroupId' : if k in interface : params . update ( _param_from_config ( k , interface [ k ] ) ) if 'AssociatePublicIpAddress' in interface : for k in ( 'DeviceIndex' , 'AssociatePublicIpAddress' , 'NetworkInterfaceId' ) : if k in interface : params [ k ] = interface [ k ] params [ 'DeleteOnTermination' ] = interface . get ( 'delete_interface_on_terminate' , True ) return params params [ 'Action' ] = 'CreateNetworkInterface' result = aws . query ( params , return_root = True , location = get_location ( vm_ ) , provider = get_provider ( ) , opts = __opts__ , sigver = '4' ) eni_desc = result [ 1 ] if not eni_desc or not eni_desc . get ( 'networkInterfaceId' ) : raise SaltCloudException ( 'Failed to create interface: {0}' . format ( result ) ) eni_id = eni_desc . get ( 'networkInterfaceId' ) log . debug ( 'Created network interface %s inst %s' , eni_id , interface [ 'DeviceIndex' ] ) associate_public_ip = interface . get ( 'AssociatePublicIpAddress' , False ) if isinstance ( associate_public_ip , six . string_types ) : _associate_eip_with_interface ( eni_id , associate_public_ip , vm_ = vm_ ) if interface . get ( 'associate_eip' ) : _associate_eip_with_interface ( eni_id , interface . get ( 'associate_eip' ) , vm_ = vm_ ) elif interface . get ( 'allocate_new_eip' ) : _new_eip = _request_eip ( interface , vm_ ) _associate_eip_with_interface ( eni_id , _new_eip , vm_ = vm_ ) elif interface . get ( 'allocate_new_eips' ) : addr_list = _list_interface_private_addrs ( eni_desc ) eip_list = [ ] for idx , addr in enumerate ( addr_list ) : eip_list . append ( _request_eip ( interface , vm_ ) ) for idx , addr in enumerate ( addr_list ) : _associate_eip_with_interface ( eni_id , eip_list [ idx ] , addr , vm_ = vm_ ) if 'Name' in interface : tag_params = { 'Action' : 'CreateTags' , 'ResourceId.0' : eni_id , 'Tag.0.Key' : 'Name' , 'Tag.0.Value' : interface [ 'Name' ] } tag_response = aws . query ( tag_params , return_root = True , location = get_location ( vm_ ) , provider = get_provider ( ) , opts = __opts__ , sigver = '4' ) if 'error' in tag_response : log . error ( 'Failed to set name of interface {0}' ) return { 'DeviceIndex' : interface [ 'DeviceIndex' ] , 'NetworkInterfaceId' : eni_id }
Create an Elastic Interface if necessary and return a Network Interface Specification