Esempio n. 1
0
def _prevent_segment_delete_with_port_bound(resource,
                                            event,
                                            trigger,
                                            payload=None):
    """Raise exception if there are any ports bound with segment_id."""
    if payload.metadata.get(seg_db.FOR_NET_DELETE):
        # don't check for network deletes
        return

    with db_api.CONTEXT_READER.using(payload.context):
        auto_delete_port_ids, proper_port_count = port_obj.Port.\
            get_auto_deletable_port_ids_and_proper_port_count_by_segment(
                payload.context, segment_id=payload.resource_id)

    if proper_port_count:
        reason = (_("The segment is still bound with %s port(s)") %
                  (proper_port_count + len(auto_delete_port_ids)))
        raise seg_exc.SegmentInUse(segment_id=payload.resource_id,
                                   reason=reason)

    if auto_delete_port_ids:
        LOG.debug("Auto-deleting dhcp port(s) on segment %s: %s",
                  payload.resource_id, ", ".join(auto_delete_port_ids))
        plugin = directory.get_plugin()
    for port_id in auto_delete_port_ids:
        try:
            plugin.delete_port(payload.context.elevated(), port_id)
        except nlib_exc.PortNotFound:
            # Don't raise if something else concurrently deleted the port
            LOG.debug(
                "Ignoring PortNotFound when deleting port '%s'. "
                "The port has already been deleted.", port_id)
Esempio n. 2
0
def _prevent_segment_delete_with_port_bound(resource,
                                            event,
                                            trigger,
                                            context,
                                            segment,
                                            for_net_delete=False):
    """Raise exception if there are any ports bound with segment_id."""
    if for_net_delete:
        # don't check for network deletes
        return

    with db_api.context_manager.reader.using(context):
        segment_id = segment['id']
        query = context.session.query(models_v2.Port.id)
        query = query.join(
            models.PortBindingLevel,
            models.PortBindingLevel.port_id == models_v2.Port.id)
        query = query.filter(models.PortBindingLevel.segment_id == segment_id)
        port_ids = [p.id for p in query]

    # There are still some ports in the segment, segment should not be deleted
    # TODO(xiaohhui): Should we delete the dhcp port automatically here?
    if port_ids:
        reason = _("The segment is still bound with port(s) "
                   "%s") % ", ".join(port_ids)
        raise seg_exc.SegmentInUse(segment_id=segment_id, reason=reason)
Esempio n. 3
0
 def _prevent_segment_delete_with_subnet_associated(
         self, resource, event, trigger, context, segment):
     """Raise exception if there are any subnets associated with segment."""
     segment_id = segment['id']
     query = context.session.query(models_v2.Subnet.id)
     query = query.filter(models_v2.Subnet.segment_id == segment_id)
     subnet_ids = [s[0] for s in query]
     if subnet_ids:
         reason = _("The segment is still associated with subnet(s) "
                    "%s") % ", ".join(subnet_ids)
         raise exceptions.SegmentInUse(segment_id=segment_id, reason=reason)
Esempio n. 4
0
    def _prevent_segment_delete_with_subnet_associated(
            self, resource, event, trigger, payload=None):
        """Raise exception if there are any subnets associated with segment."""
        if payload.metadata.get('for_net_delete'):
            # don't check if this is a part of a network delete operation
            return
        segment_id = payload.resource_id
        subnets = subnet_obj.Subnet.get_objects(payload.context,
                                                segment_id=segment_id)
        subnet_ids = [s.id for s in subnets]

        if subnet_ids:
            reason = _("The segment is still associated with subnet(s) "
                       "%s") % ", ".join(subnet_ids)
            raise exceptions.SegmentInUse(segment_id=segment_id,
                                          reason=reason)
Esempio n. 5
0
def _prevent_segment_delete_with_port_bound(resource, event, trigger,
                                            payload=None):
    """Raise exception if there are any ports bound with segment_id."""
    if payload.metadata.get('for_net_delete'):
        # don't check for network deletes
        return

    with db_api.CONTEXT_READER.using(payload.context):
        port_ids = port_obj.Port.get_port_ids_filter_by_segment_id(
            payload.context, segment_id=payload.resource_id)

    # There are still some ports in the segment, segment should not be deleted
    # TODO(xiaohhui): Should we delete the dhcp port automatically here?
    if port_ids:
        reason = _("The segment is still bound with port(s) "
                   "%s") % ", ".join(port_ids)
        raise seg_exc.SegmentInUse(segment_id=payload.resource_id,
                                   reason=reason)
Esempio n. 6
0
    def _prevent_segment_delete_with_subnet_associated(
            self, resource, event, trigger, context, segment,
            for_net_delete=False):
        """Raise exception if there are any subnets associated with segment."""
        if for_net_delete:
            # don't check if this is a part of a network delete operation
            return
        with db_api.context_manager.reader.using(context):
            segment_id = segment['id']
            query = context.session.query(models_v2.Subnet.id)
            query = query.filter(models_v2.Subnet.segment_id == segment_id)
            subnet_ids = [s[0] for s in query]

        if subnet_ids:
            reason = _("The segment is still associated with subnet(s) "
                       "%s") % ", ".join(subnet_ids)
            raise exceptions.SegmentInUse(segment_id=segment_id,
                                          reason=reason)
Esempio n. 7
0
def _prevent_segment_delete_with_port_bound(resource,
                                            event,
                                            trigger,
                                            context,
                                            segment,
                                            for_net_delete=False):
    """Raise exception if there are any ports bound with segment_id."""
    if for_net_delete:
        # don't check for network deletes
        return

    with db_api.context_manager.reader.using(context):
        port_ids = port_obj.Port.get_port_ids_filter_by_segment_id(
            context, segment_id=segment['id'])

    # There are still some ports in the segment, segment should not be deleted
    # TODO(xiaohhui): Should we delete the dhcp port automatically here?
    if port_ids:
        reason = _("The segment is still bound with port(s) "
                   "%s") % ", ".join(port_ids)
        raise seg_exc.SegmentInUse(segment_id=segment['id'], reason=reason)