def detach_nic_from_backend_pool(**_):
    '''
        Detaches a Network Interface Card's IPConfigurations
        from a Load Balancer Backend Pool
    '''
    # Get the ID of the Backend Pool
    be_pool_id = utils.get_full_id_reference(
        BackendAddressPool, _ctx=ctx.target)
    # Get an interface to the Network Interface Card
    nic_iface = NetworkInterfaceCard(_ctx=ctx.source)
    # Get the existing NIC IPConfigurations
    nic_data = nic_iface.get(utils.get_resource_name(ctx.source))
    nic_ip_cfgs = nic_data.get('properties', dict()).get(
        'ipConfigurations', list())
    # Remove the Backend Pool from the NIC IPConfigurations
    for ip_idx, _ in enumerate(nic_ip_cfgs):
        nic_pools = nic_ip_cfgs[ip_idx].get(
            'properties', dict()).get(
                LB_ADDRPOOLS_KEY, list())
        for pool_idx, nic_pool in enumerate(nic_pools):
            if nic_pool != be_pool_id:
                continue
            del nic_pools[pool_idx]
            nic_ip_cfgs[ip_idx]['properties'][LB_ADDRPOOLS_KEY] = nic_pools
    # Update the NIC IPConfigurations
    utils.task_resource_update(
        nic_iface,
        {
            'properties': {
                'ipConfigurations': nic_ip_cfgs
            }
        }, _ctx=ctx.source)
Ejemplo n.º 2
0
def detach_nic_from_backend_pool(**_):
    '''
        Detaches a Network Interface Card's IPConfigurations
        from a Load Balancer Backend Pool
    '''
    # Get the ID of the Backend Pool
    be_pool_id = utils.get_full_id_reference(BackendAddressPool,
                                             _ctx=ctx.target)
    # Get an interface to the Network Interface Card
    nic_iface = NetworkInterfaceCard(_ctx=ctx.source)
    # Get the existing NIC IPConfigurations
    nic_data = nic_iface.get(utils.get_resource_name(ctx.source))
    nic_ip_cfgs = nic_data.get('properties',
                               dict()).get('ipConfigurations', list())
    # Remove the Backend Pool from the NIC IPConfigurations
    for ip_idx, _ in enumerate(nic_ip_cfgs):
        nic_pools = nic_ip_cfgs[ip_idx].get('properties', dict()).get(
            LB_ADDRPOOLS_KEY, list())
        for pool_idx, nic_pool in enumerate(nic_pools):
            if nic_pool != be_pool_id:
                continue
            del nic_pools[pool_idx]
            nic_ip_cfgs[ip_idx]['properties'][LB_ADDRPOOLS_KEY] = nic_pools
    # Update the NIC IPConfigurations
    utils.task_resource_update(
        nic_iface, {'properties': {
            'ipConfigurations': nic_ip_cfgs
        }},
        _ctx=ctx.source)
Ejemplo n.º 3
0
def configure(command_to_execute, file_uris, type_handler_version='v2.0', **_):
    '''Configures the resource'''
    os_family = ctx.node.properties.get('os_family', '').lower()
    if os_family == 'windows':
        utils.task_resource_create(
            VirtualMachineExtension(virtual_machine=utils.get_resource_name()),
            {
                'location': ctx.node.properties.get('location'),
                'tags': ctx.node.properties.get('tags'),
                'properties': {
                    'publisher': 'Microsoft.Compute',
                    'type': 'CustomScriptExtension',
                    'typeHandlerVersion': type_handler_version,
                    'settings': {
                        'fileUris': file_uris,
                        'commandToExecute': command_to_execute
                    }
                }
            })

    # Write the IP address to runtime properties for the agent
    # Get a reference to the NIC
    rel_nic = utils.get_relationship_by_type(ctx.instance.relationships,
                                             constants.REL_CONNECTED_TO_NIC)
    # No NIC? Exit and hope the user doesn't plan to install an agent
    if not rel_nic:
        return
    # Get the NIC data from the API directly (because of IPConfiguration)
    nic = NetworkInterfaceCard(_ctx=rel_nic.target)
    nic_data = nic.get(utils.get_resource_name(rel_nic.target))
    # Iterate over each IPConfiguration entry
    for ip_cfg in nic_data.get('properties',
                               dict()).get('ipConfigurations', list()):
        # Get the Private IP Address endpoint
        ctx.instance.runtime_properties['ip'] = \
            ip_cfg.get('properties', dict()).get('privateIPAddress')
        # Get the Public IP Address endpoint
        pubip_id = ip_cfg.get('properties',
                              dict()).get('publicIPAddress', dict()).get('id')
        if isinstance(pubip_id, basestring):
            # use the ID to get the data on the public ip
            pubip = PublicIPAddress(_ctx=rel_nic.target)
            pubip.endpoint = '{0}{1}'.format(constants.CONN_API_ENDPOINT,
                                             pubip_id)
            pubip_data = pubip.get()
            if isinstance(pubip_data, dict):
                ctx.instance.runtime_properties['public_ip'] = \
                    pubip_data.get('properties', dict()).get('ipAddress')
    # See if the user wants to use the public IP as primary IP
    if ctx.node.properties.get('use_public_ip') and \
            ctx.instance.runtime_properties.get('public_ip'):
        ctx.instance.runtime_properties['ip'] = \
            ctx.instance.runtime_properties.get('public_ip')
    ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
        ctx.instance.id, 'ip', ctx.instance.runtime_properties.get('ip')))
    ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
        ctx.instance.id, 'public_ip',
        ctx.instance.runtime_properties.get('public_ip')))
def configure(command_to_execute, file_uris, **_):
    '''Configures the resource'''
    os_family = ctx.node.properties.get('os_family', '').lower()
    if os_family == 'windows':
        # By default, this should enable WinRM HTTP (unencrypted)
        # This entire function can be overridden from the plugin
        utils.task_resource_create(
            VirtualMachineExtension(
                virtual_machine=utils.get_resource_name()
            ),
            {
                'location': ctx.node.properties.get('location'),
                'tags': ctx.node.properties.get('tags'),
                'properties': {
                    'publisher': 'Microsoft.Compute',
                    'type': 'CustomScriptExtension',
                    'typeHandlerVersion': '1.4',
                    'settings': {
                        'fileUris': file_uris,
                        'commandToExecute': command_to_execute
                    }
                }
            })

    # Write the IP address to runtime properties for the agent
    # Get a reference to the NIC
    rel_nic = utils.get_relationship_by_type(
        ctx.instance.relationships,
        constants.REL_CONNECTED_TO_NIC)
    # No NIC? Exit and hope the user doesn't plan to install an agent
    if not rel_nic:
        return
    # Get the NIC data from the API directly (because of IPConfiguration)
    nic = NetworkInterfaceCard(_ctx=rel_nic.target)
    nic_data = nic.get(utils.get_resource_name(rel_nic.target))
    # Iterate over each IPConfiguration entry
    for ip_cfg in nic_data.get(
            'properties', dict()).get(
                'ipConfigurations', list()):
        # Get the Private IP Address endpoint
        ctx.instance.runtime_properties['ip'] = \
            ip_cfg.get('properties', dict()).get('privateIPAddress')
        # Get the Public IP Address endpoint
        pubip_id = ip_cfg.get(
            'properties', dict()).get(
                'publicIPAddress', dict()).get('id')
        if isinstance(pubip_id, basestring):
            # use the ID to get the data on the public ip
            pubip = PublicIPAddress(_ctx=rel_nic.target)
            pubip.endpoint = '{0}{1}'.format(
                constants.CONN_API_ENDPOINT, pubip_id)
            pubip_data = pubip.get()
            if isinstance(pubip_data, dict):
                ctx.instance.runtime_properties['public_ip'] = \
                    pubip_data.get('properties', dict()).get('ipAddress')
    # See if the user wants to use the public IP as primary IP
    if ctx.node.properties.get('use_public_ip') and \
            ctx.instance.runtime_properties.get('public_ip'):
        ctx.instance.runtime_properties['ip'] = \
            ctx.instance.runtime_properties.get('public_ip')
    ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
        ctx.instance.id,
        'ip',
        ctx.instance.runtime_properties.get('ip')))
    ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
        ctx.instance.id,
        'public_ip',
        ctx.instance.runtime_properties.get('public_ip')))
def configure(command_to_execute, file_uris, type_handler_version='v2.0', **_):
    '''Configures the resource'''
    os_family = ctx.node.properties.get('os_family', '').lower()
    if os_family == 'windows':
        utils.task_resource_create(
            VirtualMachineExtension(virtual_machine=utils.get_resource_name(),
                                    api_version=ctx.node.properties.get(
                                        'api_version',
                                        constants.API_VER_COMPUTE)),
            {
                'location': ctx.node.properties.get('location'),
                'tags': ctx.node.properties.get('tags'),
                'properties': {
                    'publisher': 'Microsoft.Compute',
                    'type': 'CustomScriptExtension',
                    'typeHandlerVersion': type_handler_version,
                    'settings': {
                        'fileUris': file_uris,
                        'commandToExecute': command_to_execute
                    }
                }
            })

    virtual_machine_name = ctx.instance.runtime_properties.get('name')
    virtual_machine_iface = \
        VirtualMachine(
            api_version=ctx.node.properties.get(
                'api_version',
                constants.API_VER_COMPUTE)).get(
                    name=virtual_machine_name)

    # Write the IP address to runtime properties for the agent
    # Get a reference to the NIC
    rel_nics = utils.get_relationships_by_type(ctx.instance.relationships,
                                               constants.REL_CONNECTED_TO_NIC)

    # No NIC? Exit and hope the user doesn't plan to install an agent
    if not rel_nics:
        return

    for rel_nic in rel_nics:
        # Get the NIC data from the API directly (because of IPConfiguration)
        nic_iface = NetworkInterfaceCard(
            _ctx=rel_nic.target,
            api_version=rel_nic.target.node.properties.get(
                'api_version', constants.API_VER_NETWORK))
        nic_name = utils.get_resource_name(rel_nic.target)
        nic_data = nic_iface.get(nic_name)
        nic_virtual_machine_id = nic_data.get('properties', dict()).get(
            'virtualMachine', dict()).get('id')

        if virtual_machine_name not in nic_virtual_machine_id:
            nic_data['properties'] = \
                utils.dict_update(
                    nic_data.get('properties', {}),
                    {
                        'virtualMachine': {
                            'id': virtual_machine_iface.get('id')
                        }
                    }
                )
            utils.task_resource_update(nic_iface,
                                       nic_data,
                                       _ctx=rel_nic.target)
            nic_data = nic_iface.get(nic_name)
            if virtual_machine_name not in nic_data.get(
                    'properties', dict()).get('virtualMachine',
                                              dict()).get('id', str()):
                return ctx.operation.retry(message='Waiting for NIC {0} to '
                                           'attach to VM {1}..'.format(
                                               nic_name, virtual_machine_name),
                                           retry_after=10)

        # Iterate over each IPConfiguration entry
        creds = utils.get_credentials(_ctx=ctx)
        for ip_cfg in nic_data.get('properties',
                                   dict()).get('ipConfigurations', list()):

            # Get the Private IP Address endpoint
            ctx.instance.runtime_properties['ip'] = \
                ip_cfg.get('properties', dict()).get('privateIPAddress')

            # Get the Public IP Address endpoint
            pubip_id = ip_cfg.get('properties',
                                  dict()).get('publicIPAddress',
                                              dict()).get('id')
            if isinstance(pubip_id, basestring):
                # use the ID to get the data on the public ip
                pubip = PublicIPAddress(
                    _ctx=rel_nic.target,
                    api_version=rel_nic.target.node.properties.get(
                        'api_version', constants.API_VER_NETWORK))
                pubip.endpoint = '{0}{1}'.format(
                    creds.endpoints_resource_manager, pubip_id)
                pubip_data = pubip.get()
                if isinstance(pubip_data, dict):
                    public_ip = \
                        pubip_data.get('properties', dict()).get('ipAddress')
                    # Maintained for backwards compatibility.
                    ctx.instance.runtime_properties['public_ip'] = \
                        public_ip
                    # For consistency with other plugins.
                    ctx.instance.runtime_properties[PUBLIC_IP_PROPERTY] = \
                        public_ip
                    # We should also consider that maybe there will be many
                    # public ip addresses.
                    public_ip_addresses = \
                        ctx.instance.runtime_properties.get(
                            PUBLIC_IP_PROPERTY, [])
                    if public_ip not in public_ip_addresses:
                        public_ip_addresses.append(public_ip)
                    ctx.instance.runtime_properties['public_ip_addresses'] = \
                        public_ip_addresses

        # See if the user wants to use the public IP as primary IP
        if ctx.node.properties.get('use_public_ip') and \
                ctx.instance.runtime_properties.get('public_ip'):
            ctx.instance.runtime_properties['ip'] = \
                ctx.instance.runtime_properties.get('public_ip')
        ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
            ctx.instance.id, 'ip', ctx.instance.runtime_properties.get('ip')))
        ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
            ctx.instance.id, 'public_ip',
            ctx.instance.runtime_properties.get('public_ip')))
def configure(command_to_execute, file_uris, type_handler_version='v2.0', **_):
    '''Configures the resource'''
    os_family = ctx.node.properties.get('os_family', '').lower()
    if os_family == 'windows':
        utils.task_resource_create(
            VirtualMachineExtension(
                virtual_machine=utils.get_resource_name(),
                api_version=ctx.node.properties.get('api_version',
                                                    constants.API_VER_COMPUTE)
            ),
            {
                'location': ctx.node.properties.get('location'),
                'tags': ctx.node.properties.get('tags'),
                'properties': {
                    'publisher': 'Microsoft.Compute',
                    'type': 'CustomScriptExtension',
                    'typeHandlerVersion': type_handler_version,
                    'settings': {
                        'fileUris': file_uris,
                        'commandToExecute': command_to_execute
                    }
                }
            })

    virtual_machine_name = ctx.instance.runtime_properties.get('name')
    virtual_machine_iface = \
        VirtualMachine(
            api_version=ctx.node.properties.get(
                'api_version',
                constants.API_VER_COMPUTE)).get(
                    name=virtual_machine_name)

    # Write the IP address to runtime properties for the agent
    # Get a reference to the NIC
    rel_nics = utils.get_relationships_by_type(
        ctx.instance.relationships,
        constants.REL_CONNECTED_TO_NIC)

    # No NIC? Exit and hope the user doesn't plan to install an agent
    if not rel_nics:
        return

    for rel_nic in rel_nics:
        # Get the NIC data from the API directly (because of IPConfiguration)
        nic_iface = NetworkInterfaceCard(
            _ctx=rel_nic.target,
            api_version=rel_nic.target.node.properties.get(
                'api_version',
                constants.API_VER_NETWORK))
        nic_name = utils.get_resource_name(rel_nic.target)
        nic_data = nic_iface.get(nic_name)
        nic_virtual_machine_id = nic_data.get(
            'properties', dict()).get(
                'virtualMachine', dict()).get('id')

        if virtual_machine_name not in nic_virtual_machine_id:
            nic_data['properties'] = \
                utils.dict_update(
                    nic_data.get('properties', {}),
                    {
                        'virtualMachine': {
                            'id': virtual_machine_iface.get('id')
                        }
                    }
                )
            utils.task_resource_update(
                nic_iface, nic_data, _ctx=rel_nic.target)
            nic_data = nic_iface.get(nic_name)
            if virtual_machine_name not in nic_data.get(
                    'properties', dict()).get(
                        'virtualMachine', dict()).get('id', str()):
                return ctx.operation.retry(
                    message='Waiting for NIC {0} to '
                            'attach to VM {1}..'
                            .format(nic_name,
                                    virtual_machine_name),
                    retry_after=10)

        # Iterate over each IPConfiguration entry
        creds = utils.get_credentials(_ctx=ctx)
        for ip_cfg in nic_data.get(
                'properties', dict()).get(
                    'ipConfigurations', list()):

            # Get the Private IP Address endpoint
            ctx.instance.runtime_properties['ip'] = \
                ip_cfg.get('properties', dict()).get('privateIPAddress')

            # Get the Public IP Address endpoint
            pubip_id = ip_cfg.get(
                'properties', dict()).get(
                    'publicIPAddress', dict()).get('id')
            if isinstance(pubip_id, basestring):
                # use the ID to get the data on the public ip
                pubip = PublicIPAddress(
                    _ctx=rel_nic.target,
                    api_version=rel_nic.target.node.properties.get(
                        'api_version',
                        constants.API_VER_NETWORK))
                pubip.endpoint = '{0}{1}'.format(
                    creds.endpoints_resource_manager, pubip_id)
                pubip_data = pubip.get()
                if isinstance(pubip_data, dict):
                    public_ip = \
                        pubip_data.get('properties', dict()).get('ipAddress')
                    # Maintained for backwards compatibility.
                    ctx.instance.runtime_properties['public_ip'] = \
                        public_ip
                    # For consistency with other plugins.
                    ctx.instance.runtime_properties[PUBLIC_IP_PROPERTY] = \
                        public_ip
                    # We should also consider that maybe there will be many
                    # public ip addresses.
                    public_ip_addresses = \
                        ctx.instance.runtime_properties.get(
                            PUBLIC_IP_PROPERTY, [])
                    if public_ip not in public_ip_addresses:
                        public_ip_addresses.append(public_ip)
                    ctx.instance.runtime_properties['public_ip_addresses'] = \
                        public_ip_addresses

        # See if the user wants to use the public IP as primary IP
        if ctx.node.properties.get('use_public_ip') and \
                ctx.instance.runtime_properties.get('public_ip'):
            ctx.instance.runtime_properties['ip'] = \
                ctx.instance.runtime_properties.get('public_ip')
        ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
            ctx.instance.id,
            'ip',
            ctx.instance.runtime_properties.get('ip')))
        ctx.logger.info('OUTPUT {0}.{1} = "{2}"'.format(
            ctx.instance.id,
            'public_ip',
            ctx.instance.runtime_properties.get('public_ip')))