def _update_floating_ip_network(floating_ip_resource, allow_multiple=False):
    """
    This method will try to update floating ip config with network
    configurations using the relationships connected with floating ip node
    :param dict floating_ip_resource: Instance of openstack floating ip
    resource
    :param boolean allow_multiple: This flag to set if it is allowed to have
    networks configuration from multiple resources relationships + node
    properties
    """

    # Check to see if the floating network id is provided on the floating ip
    # config properties
    floating_ip_config = floating_ip_resource.config
    floating_network_id = floating_ip_config.get('floating_network_id')

    # Check if floating_network_name is provided
    floating_network_name = floating_ip_config.get('floating_network_name')

    # Get the floating network id from relationship if it is existed
    rel_floating_network_id = \
        _get_floating_network_id_from_relationship(NETWORK_OPENSTACK_TYPE)

    if (floating_network_id and floating_network_name or
        (floating_network_id and rel_floating_network_id) or
        (floating_network_name and rel_floating_network_id))\
            and not allow_multiple:
        raise NonRecoverableError('Floating ip can\'t have the '
                                  '"floating network properties and be '
                                  'connected to a network via a '
                                  'relationship at the same time')

    # Check if floating network name is provided or not
    if floating_network_name:
        # Create network instance to get the network id
        network = OpenstackNetwork(
            client_config=floating_ip_resource.client_config,
            logger=ctx.logger)
        # Set the network name provided in "resource_config"
        network.name = floating_network_name
        # Lookup remote network
        remote_network = network.find_network()
        if not remote_network:
            raise NonRecoverableError('Floating IP network {0} not found'
                                      ''.format(floating_network_name))
        # Set "floating_network_id" to the remote network id
        floating_network_id = remote_network.id
        # Clean "floating_network_name" from floating_ip_config since it is
        # not part of the payload request for creating floating ip
        del floating_ip_config['floating_network_name']

    # Set the final "floating_network_id" value based on the computation above
    floating_ip_config['floating_network_id'] = \
        floating_network_id or rel_floating_network_id
def _connect_router_to_external_network(router_resource):
    """
    This method will update router config with external network by checking
    if it is provided using node property "resource_config" or via
    relationship and we should only connect router to external network from
    one source
    :param router_resource: Instance of openstack router resource
    """
    if not router_resource or router_resource and not router_resource.config:
        return

    network_resource = \
        OpenstackNetwork(client_config=router_resource.client_config,
                         logger=ctx.logger)
    # Get network id from "resource_config" which represent "router_config"
    ext_net_id = \
        _get_external_network_id(
            router_resource.config.get('external_gateway_info'), 'network_id')

    # Get network name from "resource_config" which represent "router_config"
    ext_net_name = \
        _get_external_network_id(
            router_resource.config.get(
                'external_gateway_info'), 'network_name')

    # Get the network name from node property "external_network"
    ext_net_by_property = ctx.node.properties.get('external_network')
    ext_net = None
    if ext_net_by_property:
        ext_net = network_resource.find_network(ext_net_by_property)
    elif ext_net_name:
        del router_resource.config['external_gateway_info']['network_name']
        ext_net = network_resource.find_network(ext_net_name)

    if ext_net:
        ext_net_id = ext_net.id

    # Get network id id from relationship connected to router
    rel_ext_net_id = \
        _get_connected_external_network_from_relationship(network_resource)

    if ext_net_id and rel_ext_net_id:
        raise NonRecoverableError('Router can\'t an'
                                  ' external network connected by both a '
                                  'relationship and by a network name/id')

    if 'external_gateway_info' not in router_resource.config:
        router_resource.config['external_gateway_info'] = {}

    network_id = ext_net_id or rel_ext_net_id
    if network_id:
        router_resource.config['external_gateway_info']['network_id'] = \
            network_id
def _connect_router_to_external_network(router_resource):
    """
    This method will update router config with external network by checking
    if it is provided using node property "resource_config" or via
    relationship and we should only connect router to external network from
    one source
    :param router_resource: Instance of openstack router resource
    """
    if not router_resource or router_resource and not router_resource.config:
        return

    network_resource = \
        OpenstackNetwork(client_config=router_resource.client_config,
                         logger=ctx.logger)
    # Get network id from "resource_config" which represent "router_config"
    ext_net_id = \
        _get_external_network_id(
            router_resource.config.get('external_gateway_info'))

    # Get network id id from relationship connected to router
    rel_ext_net_id = \
        _get_connected_external_network_from_relationship(network_resource)

    if ext_net_id and rel_ext_net_id:
        raise NonRecoverableError('Router can\'t both have the '
                                  '"external_gateway_info" property and be '
                                  'connected to a network via a '
                                  'relationship at the same time')

    if 'external_gateway_info' not in router_resource.config:
        router_resource.config['external_gateway_info'] = {}

    router_resource.config['external_gateway_info']['network_id'] = \
        ext_net_id or rel_ext_net_id
Example #4
0
def _disable_dhcp_for_subnets(client_config, resource_id):
    """
    Disable dhcp for subnets associated with network so that rbac policy can
    be removed
    :param client_config: Openstack config required to make API calls
    :param resource_id:  resource_id: Resource id of the target object
    """

    network = OpenstackNetwork(client_config, logger=ctx.logger)
    network.resource_id = resource_id
    network_item = network.get()
    # Disable dhcp option for all attached subnets associated with
    # current network
    for subnet_id in network_item.subnet_ids:
        subnet = OpenstackSubnet(client_config, logger=ctx.logger)
        subnet.resource_id = subnet_id
        subnet_item = subnet.get()
        # Disable dhcp for subnets if its already enabled, since this
        # will prevent rbac policy from deletion
        if subnet_item.is_dhcp_enabled:
            subnet.update(new_config={'enable_dhcp': False})
def _handle_external_router_resource(openstack_resource):
    """
    This method is to do a validation for external router resource when it
    is connected to external network node resource
    :param openstack_resource: Instance of openstack router resource
    """
    remote_router = openstack_resource.get()
    network_resource = \
        OpenstackNetwork(client_config=openstack_resource.client_config,
                         logger=ctx.logger)
    rel_network_id = \
        _get_connected_external_network_from_relationship(network_resource)
    ext_network_id = \
        _get_external_network_id(remote_router.external_gateway_info,
                                 'network_id')
    if rel_network_id and ext_network_id != rel_network_id:
        raise NonRecoverableError(
            'Expected external resources subnet {0} and network'
            ' {1} to be connected'.format(rel_network_id, ext_network_id))