Exemplo n.º 1
0
    def create(self, context, listener):
        _l = listener['listener']
        tenant_id = self._get_tenant_id_for_create(context, _l)
        project = self._project_read(project_id=tenant_id)

        if _l['loadbalancer_id']:
            try:
                lb = self._api.loadbalancer_read(id=_l['loadbalancer_id'])
            except NoIdError:
                raise loadbalancerv2.EntityNotFound(name='Loadbalancer',
                                                    id=_l['loadbalancer_id'])
            project_id = lb.parent_uuid
            if str(uuid.UUID(tenant_id)) != project_id:
                raise NotAuthorized()
        else:
            lb = None

        obj_uuid = uuidutils.generate_uuid()
        name = self._get_resource_name('loadbalancer-listener', project,
                                       _l['name'], obj_uuid)
        id_perms = IdPermsType(enable=True, description=_l['description'])
        ll = LoadbalancerListener(name,
                                  project,
                                  id_perms=id_perms,
                                  display_name=_l['name'])
        ll.uuid = obj_uuid

        if lb:
            ll.set_loadbalancer(lb)

        props = self.make_properties(_l)
        ll.set_loadbalancer_listener_properties(props)

        self._api.loadbalancer_listener_create(ll)
        return self.make_dict(ll)
def _raise_contrail_error(info, obj_name):
    exc_name = info.get('exception')

    if exc_name:
        LOG.exception(str(exc_name) + str(info) + str(obj_name))
        if str(exc_name) == 'OverQuota':
            info['exception'] = str(info['exception'])
            if 'msg' in info:
                info['msg'] = str(info['msg'])
            if 'overs' in info:
                info['overs'] = [str(info['overs'][0])]
        if exc_name == 'BadRequest' and 'resource' not in info:
            info['resource'] = obj_name
        if exc_name == 'VirtualRouterNotFound':
            raise HttpResponseError(info)
        if exc_name == 'NotAuthorized':
            raise NotAuthorized(**info)
        if hasattr(neutron_exc, exc_name):
            raise getattr(neutron_exc, exc_name)(**info)
        if hasattr(l3, exc_name):
            raise getattr(l3, exc_name)(**info)
        if hasattr(securitygroup, exc_name):
            raise getattr(securitygroup, exc_name)(**info)
        if hasattr(allowedaddresspairs, exc_name):
            raise getattr(allowedaddresspairs, exc_name)(**info)
        if neutron_lib_exc and hasattr(neutron_lib_exc, exc_name):
            raise getattr(neutron_lib_exc, exc_name)(**info)
        if neutron_lib_l3_exc and hasattr(neutron_lib_l3_exc, exc_name):
            raise getattr(neutron_lib_l3_exc, exc_name)(**info)
    raise NeutronException(**info)
Exemplo n.º 3
0
    def create(self, context, pool_id, member):
        """
        Create a loadbalancer_member object.
        """
        m = member['member']
        try:
            pool = self._api.loadbalancer_pool_read(id=pool_id)
        except NoIdError:
            raise loadbalancerv2.EntityNotFound(name='Pool', id=pool_id)

        tenant_id = self._get_tenant_id_for_create(context, m)
        if str(uuid.UUID(tenant_id)) != pool.parent_uuid:
            raise NotAuthorized()

        obj_uuid = uuidutils.generate_uuid()
        props = self.make_properties(m)
        id_perms = IdPermsType(enable=True)

        member_db = LoadbalancerMember(obj_uuid,
                                       pool,
                                       loadbalancer_member_properties=props,
                                       id_perms=id_perms)
        member_db.uuid = obj_uuid

        self._api.loadbalancer_member_create(member_db)
        return self.make_dict(member_db)
    def create_pool_health_monitor(self, context, health_monitor, pool_id):
        """ Associate an health monitor with a pool.
        """
        m = health_monitor['health_monitor']
        try:
            pool = self.api.loadbalancer_pool_read(id=pool_id)
        except vnc_exc.NoIdError:
            raise loadbalancer.PoolNotFound(pool_id=pool_id)

        try:
            monitor = self.api.loadbalancer_healthmonitor_read(id=m['id'])
        except vnc_exc.NoIdError:
            raise loadbalancer.HealthMonitorNotFound(monitor_id=m['id'])

        if not context.is_admin:
            tenant_id = str(uuid.UUID(context.tenant_id))
            if tenant_id != pool.parent_uuid or \
                    tenant_id != monitor.parent_uuid:
                raise NotAuthorized()

        pool_refs = monitor.get_loadbalancer_pool_back_refs()
        if pool_refs is not None:
            for ref in pool_refs:
                if ref['uuid'] == pool_id:
                    raise loadbalancer.PoolMonitorAssociationExists(
                        monitor_id=m['id'], pool_id=pool_id)

        pool.add_loadbalancer_healthmonitor(monitor)
        self.api.loadbalancer_pool_update(pool)

        res = {
            'id': monitor.uuid,
            'tenant_id': monitor.parent_uuid.replace('-', '')
        }
        return res
    def update_object(self, vip_db, id, v):
        if 'pool_id' in v and self._get_vip_pool_id(vip_db) != v['pool_id']:
            try:
                pool = self._api.loadbalancer_pool_read(id=v['pool_id'])
            except NoIdError:
                raise loadbalancer.PoolNotFound(pool_id=v['pool_id'])
            if vip_db.parent_uuid != pool.parent_uuid:
                raise NotAuthorized()

            # check that the pool has no vip configured
            if pool.get_virtual_ip_back_refs():
                raise loadbalancer.VipExists(pool_id=pool.uuid)

            # check that the protocol matches
            pool_props = pool.get_loadbalancer_pool_properties()
            vip_props = vip_db.get_virtual_ip_properties()
            if pool_props.get_protocol() != vip_props.get_protocol():
                raise loadbalancer.ProtocolMismatch(
                    vip_proto=vip_props.get_protocol(),
                    pool_proto=pool_props.get_protocol())

            # update vip
            vip_db.set_loadbalancer_pool(pool)
            return True

        return False
    def create(self, context, pool):
        """
        Create a loadbalancer_pool object.
        """
        p = pool['pool']
        tenant_id = self._get_tenant_id_for_create(context, p)
        project = self._project_read(project_id=tenant_id)

        if p['listener_id']:
            try:
                ll = self._api.loadbalancer_listener_read(id=p['listener_id'])
            except NoIdError:
                raise loadbalancerv2.EntityNotFound(name='Listener',
                                                    id=p['listener_id'])
            project_id = ll.parent_uuid
            if str(uuid.UUID(tenant_id)) != project_id:
                raise NotAuthorized()
        else:
            ll = None

        pool_uuid = uuidutils.generate_uuid()
        name = self._get_resource_name('loadbalancer-pool', project,
                                       p['name'], pool_uuid)
        props = self.make_properties(p)
        id_perms = IdPermsType(enable=True,
                               description=p['description'])
        pool = LoadbalancerPool(name, project, uuid=pool_uuid,
                                loadbalancer_pool_properties=props,
                                id_perms=id_perms, display_name=p['name'])

        if ll:
            pool_exists = ll.get_loadbalancer_pool_back_refs()
            if pool_exists is not None:
                raise loadbalancerv2.OnePoolPerListener(
                    listener_id=p['listener_id'],
                    pool_id=pool_exists[0]['uuid'])
            pool.set_loadbalancer_listener(ll)

        # Custom attributes
        if p['custom_attributes'] != ATTR_NOT_SPECIFIED:
            custom_attributes = KeyValuePairs()
            self.create_update_custom_attributes(p['custom_attributes'],
                                                 custom_attributes)
            pool.set_loadbalancer_pool_custom_attributes(custom_attributes)

        self._api.loadbalancer_pool_create(pool)
        return self.make_dict(pool)
    def create(self, context, vip):
        """
        Create a VIP.
        """
        v = vip['vip']
        tenant_id = self._get_tenant_id_for_create(context, v)

        project = self._project_read(project_id=tenant_id)

        if v['pool_id']:
            try:
                pool = self._api.loadbalancer_pool_read(id=v['pool_id'])
            except NoIdError:
                raise loadbalancer.PoolNotFound(pool_id=v['pool_id'])
            project_id = pool.parent_uuid
            if str(uuid.UUID(tenant_id)) != project_id:
                raise NotAuthorized()
            protocol = pool.get_loadbalancer_pool_properties().get_protocol()
            if protocol != v['protocol']:
                raise loadbalancer.ProtocolMismatch(
                    vip_proto=v['protocol'], pool_proto=protocol)
            if pool.get_virtual_ip_back_refs():
                raise loadbalancer.VipExists(pool_id=v['pool_id'])
        else:
            pool = None

        obj_uuid = uuidutils.generate_uuid()
        name = self._get_resource_name('virtual-ip', project, v['name'], obj_uuid)
        id_perms = IdPermsType(enable=True,
                               description=v['description'])
        vip = VirtualIp(name, project, id_perms=id_perms,
                        display_name=v['name'])
        vip.uuid = obj_uuid

        if pool:
            vip.set_loadbalancer_pool(pool)

        vmi, vip_address = self._create_virtual_interface(project,
            obj_uuid, v['subnet_id'], v.get('address'))
        vip.set_virtual_machine_interface(vmi)

        props = self.make_properties(v)
        props.set_address(vip_address)
        vip.set_virtual_ip_properties(props)

        self._api.virtual_ip_create(vip)
        return self.make_dict(vip)
Exemplo n.º 8
0
    def delete(self, context, id):
        if not context.is_admin:
            try:
                obj = self.resource_read(id=id)
            except NoIdError:
                raise self.get_exception_notfound(id=id)
            tenant_id = str(uuid.UUID(context.tenant_id))
            project_id = self._get_object_tenant_id(obj)
            if tenant_id != project_id:
                raise NotAuthorized()

        try:
            self.resource_delete(id=id)
        except NoIdError:
            raise self.get_exception_notfound(id=id)
        except RefsExistError:
            raise self.get_exception_inuse(id=id)
Exemplo n.º 9
0
def _raise_contrail_error(info, obj_name):
    exc_name = info.get('exception')
    if exc_name:
        if exc_name == 'BadRequest' and 'resource' not in info:
            info['resource'] = obj_name
        if exc_name == 'VirtualRouterNotFound':
            raise HttpResponseError(info)
        if exc_name == 'NotAuthorized':
            raise NotAuthorized(**info)
        if hasattr(neutron_exc, exc_name):
            raise getattr(neutron_exc, exc_name)(**info)
        if hasattr(l3, exc_name):
            raise getattr(l3, exc_name)(**info)
        if hasattr(securitygroup, exc_name):
            raise getattr(securitygroup, exc_name)(**info)
        if hasattr(allowedaddresspairs, exc_name):
            raise getattr(allowedaddresspairs, exc_name)(**info)
        if neutron_lib_exc and hasattr(neutron_lib_exc, exc_name):
            raise getattr(neutron_lib_exc, exc_name)(**info)
    raise NeutronException(**info)
def _raise_contrail_error(info, obj_name):
    exc_name = info.get('exception', 'No exception name provided')

    if exc_name == 'VirtualRouterNotFound':
        raise HttpResponseError(info)
    elif exc_name == 'NotAuthorized':
        raise NotAuthorized(**info)
    elif str(exc_name) == 'OverQuota':
        info['exception'] = str(info['exception'])
        if 'msg' in info:
            info['msg'] = str(info['msg'])
        if 'overs' in info:
            info['overs'] = [str(info['overs'][0])]
    elif exc_name == 'BadRequest' and 'resource' not in info:
        info['resource'] = obj_name

    if hasattr(neutron_exc, exc_name):
        raise getattr(neutron_exc, exc_name)(**info)
    elif hasattr(l3, exc_name):
        raise getattr(l3, exc_name)(**info)
    elif hasattr(securitygroup, exc_name):
        raise getattr(securitygroup, exc_name)(**info)
    elif hasattr(allowedaddresspairs, exc_name):
        raise getattr(allowedaddresspairs, exc_name)(**info)
    elif hasattr(port_security_extn, exc_name):
        raise getattr(port_security_extn, exc_name)(**info)
    elif neutron_lib_exc and hasattr(neutron_lib_exc, exc_name):
        raise getattr(neutron_lib_exc, exc_name)(**info)
    elif neutron_lib_l3_exc and hasattr(neutron_lib_l3_exc, exc_name):
        raise getattr(neutron_lib_l3_exc, exc_name)(**info)
    elif firewall_v2_exc and hasattr(firewall_v2_exc, exc_name):
        raise getattr(firewall_v2_exc, exc_name)(**info)
    elif neutron_trunk_exc and hasattr(neutron_trunk_exc, exc_name):
        raise getattr(neutron_trunk_exc, exc_name)(**info)
    else:
        try:
            raise NeutronException(**info)
        except Exception:
            LOG.exception(
                "Contrail raised unknown exception '%s' with args: "
                "%s", exc_name, info)