Exemplo n.º 1
0
    def test_six_pt_five(self):
        x = column("x")
        self.assert_compile(select([x]).where(or_(x == 7, true())),
                "SELECT x WHERE true")

        self.assert_compile(select([x]).where(or_(x == 7, true())),
                "SELECT x WHERE 1 = 1",
                dialect=default.DefaultDialect(supports_native_boolean=False))
Exemplo n.º 2
0
 def test_eleven(self):
     c = column('x', Boolean)
     self.assert_compile(
         c.is_(true()),
         "x IS true",
         dialect=self._dialect(True)
     )
Exemplo n.º 3
0
def query_with_hooks(context, model):
    query = context.session.query(model)
    # define basic filter condition for model query
    query_filter = None
    if db_utils.model_query_scope_is_project(context, model):
        if hasattr(model, 'rbac_entries'):
            query = query.outerjoin(model.rbac_entries)
            rbac_model = model.rbac_entries.property.mapper.class_
            query_filter = (
                (model.tenant_id == context.tenant_id) |
                ((rbac_model.action == 'access_as_shared') &
                 ((rbac_model.target_tenant == context.tenant_id) |
                  (rbac_model.target_tenant == '*'))))
        elif hasattr(model, 'shared'):
            query_filter = ((model.tenant_id == context.tenant_id) |
                            (model.shared == sql.true()))
        else:
            query_filter = (model.tenant_id == context.tenant_id)
    # Execute query hooks registered from mixins and plugins
    for hook in get_hooks(model):
        query_hook = helpers.resolve_ref(hook.get('query'))
        if query_hook:
            query = query_hook(context, model, query)

        filter_hook = helpers.resolve_ref(hook.get('filter'))
        if filter_hook:
            query_filter = filter_hook(context, model, query_filter)

    # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
    # condition, raising an exception
    if query_filter is not None:
        query = query.filter(query_filter)
    return query
Exemplo n.º 4
0
    def get_catalog(self, user_id, tenant_id, metadata=None):
        substitutions = dict(six.iteritems(CONF))
        substitutions.update({'tenant_id': tenant_id, 'user_id': user_id})

        session = sql.get_session()
        endpoints = (session.query(Endpoint).
                     options(sql.joinedload(Endpoint.service)).
                     filter(Endpoint.enabled == true()).all())

        catalog = {}

        for endpoint in endpoints:
            if not endpoint.service['enabled']:
                continue
            try:
                url = core.format_url(endpoint['url'], substitutions)
            except exception.MalformedEndpoint:
                continue  # this failure is already logged in format_url()

            region = endpoint['region_id']
            service_type = endpoint.service['type']
            default_service = {
                'id': endpoint['id'],
                'name': endpoint.service.extra.get('name', ''),
                'publicURL': ''
            }
            catalog.setdefault(region, {})
            catalog[region].setdefault(service_type, default_service)
            interface_url = '%sURL' % endpoint['interface']
            catalog[region][service_type][interface_url] = url

        return catalog
Exemplo n.º 5
0
    def get_v3_catalog(self, user_id, tenant_id, metadata=None):
        d = dict(six.iteritems(CONF))
        d.update({'tenant_id': tenant_id,
                  'user_id': user_id})

        session = sql.get_session()
        services = (session.query(Service).filter(Service.enabled == true()).
                    options(sql.joinedload(Service.endpoints)).
                    all())

        def make_v3_endpoints(endpoints):
            for endpoint in (ep.to_dict() for ep in endpoints if ep.enabled):
                del endpoint['service_id']
                del endpoint['legacy_endpoint_id']
                del endpoint['enabled']
                endpoint['region'] = endpoint['region_id']
                try:
                    endpoint['url'] = core.format_url(endpoint['url'], d)
                except exception.MalformedEndpoint:
                    continue  # this failure is already logged in format_url()

                yield endpoint

        def make_v3_service(svc):
            eps = list(make_v3_endpoints(svc.endpoints))
            service = {'endpoints': eps, 'id': svc.id, 'type': svc.type}
            service['name'] = svc.extra.get('name', '')
            return service

        return [make_v3_service(svc) for svc in services]
Exemplo n.º 6
0
def get_network_ports(context, network_id):
    try:
        return (context.session.query(models_v2.Port).
                filter(models_v2.Port.network_id == network_id,
                       models_v2.Port.admin_state_up == sql.true()).all())
    except exc.NoResultFound:
        return
    def schedule_unscheduled_bgp_speakers(self, context, host):
        """Schedule unscheduled BgpSpeaker to a BgpDrAgent.
        """

        LOG.debug('Started auto-scheduling on host %s', host)
        with context.session.begin(subtransactions=True):
            query = context.session.query(agents_db.Agent)
            query = query.filter_by(
                agent_type=bgp_consts.AGENT_TYPE_BGP_ROUTING,
                host=host,
                admin_state_up=sql.true())
            try:
                bgp_dragent = query.one()
            except (exc.NoResultFound):
                LOG.debug('No enabled BgpDrAgent on host %s', host)
                return False

            if agents_db.AgentDbMixin.is_agent_down(
                    bgp_dragent.heartbeat_timestamp):
                LOG.warning(_LW('BgpDrAgent %s is down'), bgp_dragent.id)
                return False

            if self._is_bgp_speaker_hosted(context, bgp_dragent['id']):
                # One BgpDrAgent can only host one BGP speaker
                LOG.debug('BgpDrAgent already hosting a speaker on host %s. '
                          'Cannot schedule an another one', host)
                return False

            unscheduled_speakers = self._get_unscheduled_bgp_speakers(context)
            if not unscheduled_speakers:
                LOG.debug('Nothing to auto-schedule on host %s', host)
                return False

            self.bind(context, [bgp_dragent], unscheduled_speakers[0])
        return True
Exemplo n.º 8
0
    def _get_default_external_network(self, context):
        """Get the default external network for the deployment."""
        with context.session.begin(subtransactions=True):
            default_external_networks = (
                context.session.query(ext_net_models.ExternalNetwork)
                .filter_by(is_default=sql.true())
                .join(models_v2.Network)
                .join(standard_attr.StandardAttribute)
                .order_by(standard_attr.StandardAttribute.id)
                .all()
            )

        if not default_external_networks:
            LOG.error(
                _LE(
                    "Unable to find default external network "
                    "for deployment, please create/assign one to "
                    "allow auto-allocation to work correctly."
                )
            )
            raise exceptions.AutoAllocationFailure(reason=_("No default router:external network"))
        if len(default_external_networks) > 1:
            LOG.error(
                _LE("Multiple external default networks detected. " "Network %s is true 'default'."),
                default_external_networks[0]["network_id"],
            )
        return default_external_networks[0].network_id
Exemplo n.º 9
0
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        query_filter = None
        if self.model_query_scope(context, model):
            if hasattr(model, 'shared'):
                query_filter = ((model.tenant_id == context.tenant_id) |
                                (model.shared == sql.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)
        # Execute query hooks registered from mixins and plugins
        for _name, hooks in self._model_query_hooks.get(model,
                                                        {}).iteritems():
            query_hook = hooks.get('query')
            if isinstance(query_hook, six.string_types):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, six.string_types):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)
        return query
Exemplo n.º 10
0
    def has_property(cls, prop, when=None):
        # TODO Use joins
        property_granted_select = select(
            [null()],
            from_obj=[
                Property.__table__,
                PropertyGroup.__table__,
                Membership.__table__
            ]
        ).where(
            and_(
                Property.name == prop,
                Property.property_group_id == PropertyGroup.id,
                PropertyGroup.id == Membership.group_id,
                Membership.user_id == cls.id,
                Membership.active(when)
            )
        )
        #.cte("property_granted_select")
        return and_(
            not_(exists(
                property_granted_select.where(
                    Property.granted == false())

            )),
            exists(
                property_granted_select.where(
                    Property.granted == true()
                )
            )
        ).self_group().label("has_property_" + prop)
Exemplo n.º 11
0
    def has_property(self, prop):
        property_granted_select = select(
            [null()],
            from_obj=[
                Property.__table__,
                PropertyGroup.__table__,
                Membership.__table__
            ]
        ).where(
            and_(
                Property.name == prop,
                Property.property_group_id == PropertyGroup.id,
                PropertyGroup.id == Membership.group_id,
                Membership.user_id == self.id,
                Membership.active
            )
        )
        #.cte("property_granted_select")
        return and_(
            not_(exists(
                property_granted_select.where(
                    Property.granted == false())

            )),
            exists(
                property_granted_select.where(
                    Property.granted == true()
                )
            )
        )
def upgrade():
    op.create_table('trunks',
        sa.Column('admin_state_up', sa.Boolean(),
                  nullable=False, server_default=sql.true()),
        sa.Column('tenant_id', sa.String(length=255), nullable=True,
                  index=True),
        sa.Column('id', sa.String(length=36), nullable=False),
        sa.Column('name', sa.String(length=255), nullable=True),
        sa.Column('port_id', sa.String(length=36), nullable=False),
        sa.Column('status', sa.String(length=16),
                  nullable=False, server_default='ACTIVE'),
        sa.Column('standard_attr_id', sa.BigInteger(), nullable=False),
        sa.ForeignKeyConstraint(['port_id'], ['ports.id'],
                                ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['standard_attr_id'],
                                ['standardattributes.id'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('port_id'),
        sa.UniqueConstraint('standard_attr_id')
    )
    op.create_table('subports',
        sa.Column('port_id', sa.String(length=36)),
        sa.Column('trunk_id', sa.String(length=36), nullable=False),
        sa.Column('segmentation_type', sa.String(length=32), nullable=False),
        sa.Column('segmentation_id', sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(['port_id'], ['ports.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['trunk_id'], ['trunks.id'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('port_id'),
        sa.UniqueConstraint('trunk_id', 'segmentation_type', 'segmentation_id',
            name='uniq_subport0trunk_id0segmentation_type0segmentation_id')
    )
Exemplo n.º 13
0
    def get_v3_catalog(self, user_id, tenant_id, metadata=None):
        d = dict(itertools.chain(six.iteritems(CONF), six.iteritems(CONF.eventlet_server)))
        d.update({"tenant_id": tenant_id, "user_id": user_id})

        session = sql.get_session()
        services = (
            session.query(Service).filter(Service.enabled == true()).options(sql.joinedload(Service.endpoints)).all()
        )

        def make_v3_endpoints(endpoints):
            for endpoint in (ep.to_dict() for ep in endpoints if ep.enabled):
                del endpoint["service_id"]
                del endpoint["legacy_endpoint_id"]
                del endpoint["enabled"]
                endpoint["region"] = endpoint["region_id"]
                try:
                    endpoint["url"] = core.format_url(endpoint["url"], d)
                except exception.MalformedEndpoint:
                    continue  # this failure is already logged in format_url()

                yield endpoint

        def make_v3_service(svc):
            eps = list(make_v3_endpoints(svc.endpoints))
            service = {"endpoints": eps, "id": svc.id, "type": svc.type}
            service["name"] = svc.extra.get("name", "")
            return service

        return [make_v3_service(svc) for svc in services]
Exemplo n.º 14
0
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        # NOTE(jkoelker) non-admin queries are scoped to their tenant_id
        # NOTE(salvatore-orlando): unless the model allows for shared objects
        query_filter = None
        if not context.is_admin and hasattr(model, 'tenant_id'):
            if hasattr(model, 'shared'):
                query_filter = ((model.tenant_id == context.tenant_id) |
                                (model.shared == sql.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)
        # Execute query hooks registered from mixins and plugins
        for _name, hooks in self._model_query_hooks.get(model,
                                                        {}).iteritems():
            query_hook = hooks.get('query')
            if isinstance(query_hook, basestring):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, basestring):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)
        return query
Exemplo n.º 15
0
    def get_ha_routers_l3_agents_count(self, context):
        """Return a map between HA routers and how many agents every
        router is scheduled to.
        """

        # Postgres requires every column in the select to be present in
        # the group by statement when using an aggregate function.
        # One solution is to generate a subquery and join it with the desired
        # columns.
        binding_model = l3_sch_db.RouterL3AgentBinding
        sub_query = (context.session.query(
            binding_model.router_id,
            func.count(binding_model.router_id).label('count')).
            join(l3_attrs_db.RouterExtraAttributes,
                 binding_model.router_id ==
                 l3_attrs_db.RouterExtraAttributes.router_id).
            join(l3_db.Router).
            filter(l3_attrs_db.RouterExtraAttributes.ha == sql.true()).
            group_by(binding_model.router_id).subquery())

        query = (context.session.query(l3_db.Router, sub_query.c.count).
                 join(sub_query))

        return [(self._make_router_dict(router), agent_count)
                for router, agent_count in query]
Exemplo n.º 16
0
 def _get_by_disabled_from_db(context, disabled):
     if disabled:
         return context.session.query(api_models.CellMapping).filter_by(
             disabled=true()).order_by(asc(api_models.CellMapping.id)).all()
     else:
         return context.session.query(api_models.CellMapping).filter_by(
             disabled=false()).order_by(asc(
                 api_models.CellMapping.id)).all()
Exemplo n.º 17
0
 def test_true_false(self):
     self.assert_compile(
         sql.false(), "0"
     )
     self.assert_compile(
         sql.true(),
         "1"
     )
Exemplo n.º 18
0
    def get_catalog(self, user_id, tenant_id):
        """Retrieve and format the V2 service catalog.

        :param user_id: The id of the user who has been authenticated for
            creating service catalog.
        :param tenant_id: The id of the project. 'tenant_id' will be None
            in the case this being called to create a catalog to go in a
            domain scoped token. In this case, any endpoint that requires
            a tenant_id as part of their URL will be skipped (as would a whole
            service if, as a consequence, it has no valid endpoints).

        :returns: A nested dict representing the service catalog or an
                  empty dict.

        """
        substitutions = dict(
            itertools.chain(CONF.items(), CONF.eventlet_server.items()))
        substitutions.update({'user_id': user_id})
        silent_keyerror_failures = []
        if tenant_id:
            substitutions.update({'tenant_id': tenant_id})
        else:
            silent_keyerror_failures = ['tenant_id']

        session = sql.get_session()
        endpoints = (session.query(Endpoint).
                     options(sql.joinedload(Endpoint.service)).
                     filter(Endpoint.enabled == true()).all())

        catalog = {}

        for endpoint in endpoints:
            if not endpoint.service['enabled']:
                continue
            try:
                formatted_url = core.format_url(
                    endpoint['url'], substitutions,
                    silent_keyerror_failures=silent_keyerror_failures)
                if formatted_url is not None:
                    url = formatted_url
                else:
                    continue
            except exception.MalformedEndpoint:
                continue  # this failure is already logged in format_url()

            region = endpoint['region_id']
            service_type = endpoint.service['type']
            default_service = {
                'id': endpoint['id'],
                'name': endpoint.service.extra.get('name', ''),
                'publicURL': ''
            }
            catalog.setdefault(region, {})
            catalog[region].setdefault(service_type, default_service)
            interface_url = '%sURL' % endpoint['interface']
            catalog[region][service_type][interface_url] = url

        return catalog
Exemplo n.º 19
0
Arquivo: sql.py Projeto: dims/keystone
    def get_v3_catalog(self, user_id, tenant_id):
        """Retrieve and format the current V3 service catalog.

        :param user_id: The id of the user who has been authenticated for
            creating service catalog.
        :param tenant_id: The id of the project. 'tenant_id' will be None in
            the case this being called to create a catalog to go in a domain
            scoped token. In this case, any endpoint that requires a
            tenant_id as part of their URL will be skipped.

        :returns: A list representing the service catalog or an empty list

        """
        d = dict(
            itertools.chain(CONF.items(), CONF.eventlet_server.items()))
        d.update({'user_id': user_id})
        silent_keyerror_failures = []
        if tenant_id:
            d.update({
                'tenant_id': tenant_id,
                'project_id': tenant_id,
            })
        else:
            silent_keyerror_failures = ['tenant_id', 'project_id', ]

        session = sql.get_session()
        services = (session.query(Service).filter(Service.enabled == true()).
                    options(sql.joinedload(Service.endpoints)).
                    all())

        def make_v3_endpoints(endpoints):
            for endpoint in (ep.to_dict() for ep in endpoints if ep.enabled):
                del endpoint['service_id']
                del endpoint['legacy_endpoint_id']
                del endpoint['enabled']
                endpoint['region'] = endpoint['region_id']
                try:
                    formatted_url = core.format_url(
                        endpoint['url'], d,
                        silent_keyerror_failures=silent_keyerror_failures)
                    if formatted_url:
                        endpoint['url'] = formatted_url
                    else:
                        continue
                except exception.MalformedEndpoint:
                    continue  # this failure is already logged in format_url()

                yield endpoint

        # TODO(davechen): If there is service with no endpoints, we should skip
        # the service instead of keeping it in the catalog, see bug #1436704.
        def make_v3_service(svc):
            eps = list(make_v3_endpoints(svc.endpoints))
            service = {'endpoints': eps, 'id': svc.id, 'type': svc.type}
            service['name'] = svc.extra.get('name', '')
            return service

        return [make_v3_service(svc) for svc in services]
Exemplo n.º 20
0
 def test_twelve(self):
     c = column('x', Boolean)
     # I don't have a solution for this one yet,
     # other than adding some heavy-handed conditionals
     # into compiler
     self.assert_compile(
         c.is_(true()),
         "x IS 1",
         dialect=self._dialect(False)
     )
Exemplo n.º 21
0
Arquivo: flavor.py Projeto: sapcc/nova
 def _flavor_get_query_from_db(context):
     query = context.session.query(api_models.Flavors).\
             options(joinedload('extra_specs'))
     if not context.is_admin:
         the_filter = [api_models.Flavors.is_public == true()]
         the_filter.extend([
             api_models.Flavors.projects.any(project_id=context.project_id)
         ])
         query = query.filter(or_(*the_filter))
     return query
Exemplo n.º 22
0
def _flavor_get_query(context, session=None, read_deleted=None):
    query = model_query(context, models.InstanceTypes, session=session,
                       read_deleted=read_deleted)
    if not context.is_admin:
        the_filter = [models.InstanceTypes.is_public == true()]
        the_filter.extend([
            models.InstanceTypes.projects.any(project_id=context.project_id)
        ])
        query = query.filter(or_(*the_filter))
    return query
Exemplo n.º 23
0
def remove_expired_reservations(context, tenant_id=None):
    now = utcnow()
    resv_query = context.session.query(quota_models.Reservation)
    if tenant_id:
        tenant_expr = (quota_models.Reservation.tenant_id == tenant_id)
    else:
        tenant_expr = sql.true()
    resv_query = resv_query.filter(sa.and_(
        tenant_expr, quota_models.Reservation.expiration < now))
    return resv_query.delete()
Exemplo n.º 24
0
 def get_network_ports(self, session, network_id):
     with session.begin(subtransactions=True):
         query = session.query(ml2_models.PortBinding, agents_db.Agent)
         query = query.join(agents_db.Agent, agents_db.Agent.host == ml2_models.PortBinding.host)
         query = query.join(models_v2.Port)
         query = query.filter(
             models_v2.Port.network_id == network_id,
             models_v2.Port.admin_state_up == sql.true(),
             agents_db.Agent.agent_type.in_(l2_const.SUPPORTED_AGENT_TYPES),
         )
         return query
Exemplo n.º 25
0
 def get_for_webhook_rebuild(cls, copr_id, webhook_secret, clone_url, commits):
     packages = (models.Package.query.join(models.Copr)
                 .filter(models.Copr.webhook_secret == webhook_secret)
                 .filter(models.Package.copr_id == copr_id)
                 .filter(models.Package.webhook_rebuild == true())
                 .filter(models.Package.source_json.contains(clone_url)))
     result = []
     for package in packages:
         if cls.commits_belong_to_package(package, commits):
             result += [package]
     return result
Exemplo n.º 26
0
    def test_const_expr_role(self):
        t = true()
        is_(expect(roles.ConstExprRole, t), t)

        f = false()
        is_(expect(roles.ConstExprRole, f), f)

        is_instance_of(expect(roles.ConstExprRole, True), True_)

        is_instance_of(expect(roles.ConstExprRole, False), False_)

        is_instance_of(expect(roles.ConstExprRole, None), Null)
Exemplo n.º 27
0
 def delete_expired(cls, context, now, project_id):
     resv_query = context.session.query(models.Reservation)
     if project_id:
         project_expr = (models.Reservation.project_id == project_id)
     else:
         project_expr = sql.true()
     # TODO(manjeets) Fetch and delete objects using
     # object/db/api.py once comparison operations are
     # supported
     resv_query = resv_query.filter(sa.and_(
         project_expr, models.Reservation.expiration < now))
     return resv_query.delete()
Exemplo n.º 28
0
    def distributed_schedule_networks(self, context, **kargs):
        """Distributed schedule network to hosts who hosting vms belong to the network"""
        host = kargs.get('host')
        plugin = manager.NeutronManager.get_plugin()
        bindings_to_add = []
        LOG.debug('distributed_schedule_networks, host:%s', host)
        with context.session.begin(subtransactions=True):
            agent = plugin.get_enabled_agent_on_host(context, constants.AGENT_TYPE_DHCP, host)
            if not agent:
                return False
            nets = plugin.list_networks_on_dhcp_agent(context, agent.id)
            all_net_ids = set(n['id'] for n in nets['networks'])
            LOG.debug('distributed_schedule_networks, all_net_ids:%s', all_net_ids)

            fields = ['network_id', 'enable_dhcp']
            subnets = plugin.get_subnets(context, fields=fields)
            dhcp_net_ids = set(s['network_id'] for s in subnets if s['enable_dhcp'])
            LOG.debug('distributed_schedule_networks, dhcp_net_ids:%s', dhcp_net_ids)

            """get net-ids by vms' host-id in current host"""
            filters = {'binding:host_id': [agent.host]}
            fields = ['network_id']
            ports = plugin.get_ports(context, filters = filters, fields = fields)
            host_net_ids = set(p['network_id'] for p in ports)
            need_binding_net_ids = dhcp_net_ids & host_net_ids

            for net_id in all_net_ids - need_binding_net_ids:
                try:
                    plugin.remove_network_from_dhcp_agent(context, agent.id, net_id)
                except dhcpagentscheduler.NetworkNotHostedByDhcpAgent:
                    LOG.warning(_('NetworkNotHostedByDhcpAgent,netid:%s, host:%s'), net_id, agent.host)

            query = context.session.query(agents_db.Agent)
            query = query.filter(agents_db.Agent.agent_type ==
                                 constants.AGENT_TYPE_DHCP,
                                 agents_db.Agent.host == agent.host,
                                 agents_db.Agent.admin_state_up == sql.true())
            dhcp_agents = query.all()
            if dhcp_agents and len(dhcp_agents) == 1:
                dhcp_agent = dhcp_agents[0]
                if not agents_db.AgentDbMixin.is_agent_down(dhcp_agent.heartbeat_timestamp):
                    for net_id in need_binding_net_ids:
                        bindings_to_add.append((dhcp_agent, net_id))
                else:
                    LOG.error(_('DHCP agent on host %s is not active'), dhcp_agent.host)
            else:
                LOG.error(_('No DHCP agent for host %s', agent.host))

        for agent, net_id in bindings_to_add:
            self._schedule_bind_network(context, [agent], net_id)
        return True
Exemplo n.º 29
0
    def auto_schedule_networks(self, plugin, context, host):
        """Schedule non-hosted networks to the DHCP agent on
        the specified host.
        """
        agents_per_network = cfg.CONF.dhcp_agents_per_network
        dhcp_network_filter = cfg.CONF.dhcp_network_filter
        # a list of (agent, net_ids) tuples
        bindings_to_add = []
        with context.session.begin(subtransactions=True):
            fields = ['network_id', 'enable_dhcp']
            subnets = plugin.get_subnets(context, fields=fields)
            net_ids = set(s['network_id'] for s in subnets
                          if s['enable_dhcp'])

            #Modify by FSO, for supported FM.
            if dhcp_network_filter:
                filter_net_ids = set()
                for id in net_ids:
                    network = plugin.get_network(context, id)
                    if network['name'] in dhcp_network_filter:
                        filter_net_ids.add(id)
                net_ids = filter_net_ids

            if not net_ids:
                LOG.debug(_('No non-hosted networks'))
                return False
            query = context.session.query(agents_db.Agent)
            query = query.filter(agents_db.Agent.agent_type ==
                                 constants.AGENT_TYPE_DHCP,
                                 agents_db.Agent.host == host,
                                 agents_db.Agent.admin_state_up == sql.true())
            dhcp_agents = query.all()
            for dhcp_agent in dhcp_agents:
                if agents_db.AgentDbMixin.is_agent_down(
                    dhcp_agent.heartbeat_timestamp):
                    LOG.warn(_('DHCP agent %s is not active'), dhcp_agent.id)
                    continue
                for net_id in net_ids:
                    agents = plugin.get_dhcp_agents_hosting_networks(
                        context, [net_id], active=True)
                    if len(agents) >= agents_per_network:
                        continue
                    if any(dhcp_agent.id == agent.id for agent in agents):
                        continue
                    bindings_to_add.append((dhcp_agent, net_id))
        # do it outside transaction so particular scheduling results don't
        # make other to fail
        for agent, net_id in bindings_to_add:
            self._schedule_bind_network(context, [agent], net_id)
        return True
Exemplo n.º 30
0
    def flavor_get(self, context, flavor_uuid):
        query = model_query(context,
                            models.Flavors).filter_by(uuid=flavor_uuid)

        if not context.is_admin:
            query = query.filter_by(disabled=False)
            the_filter = [models.Flavors.is_public == true()]
            the_filter.extend(
                [models.Flavors.projects.has(project_id=context.project_id)])
            query = query.filter(or_(*the_filter))

        try:
            return query.one()
        except NoResultFound:
            raise exception.FlavorNotFound(flavor_id=flavor_uuid)
Exemplo n.º 31
0
 def get_dvr_network_ports(self, session, network_id):
     with session.begin(subtransactions=True):
         query = session.query(ml2_models.DVRPortBinding,
                               agents_db.Agent)
         query = query.join(agents_db.Agent,
                            agents_db.Agent.host ==
                            ml2_models.DVRPortBinding.host)
         query = query.join(models_v2.Port)
         query = query.filter(models_v2.Port.network_id == network_id,
                              models_v2.Port.admin_state_up == sql.true(),
                              models_v2.Port.device_owner ==
                              const.DEVICE_OWNER_DVR_INTERFACE,
                              agents_db.Agent.agent_type.in_(
                                  l2_const.SUPPORTED_AGENT_TYPES))
         return query
Exemplo n.º 32
0
 def get_dvr_network_ports(self, session, network_id):
     with session.begin(subtransactions=True):
         query = session.query(ml2_models.DVRPortBinding,
                               agents_db.Agent)
         query = query.join(agents_db.Agent,
                            agents_db.Agent.host ==
                            ml2_models.DVRPortBinding.host)
         query = query.join(models_v2.Port)
         query = query.filter(models_v2.Port.network_id == network_id,
                              models_v2.Port.admin_state_up == sql.true(),
                              models_v2.Port.device_owner ==
                              const.DEVICE_OWNER_DVR_INTERFACE,
                              agents_db.Agent.agent_type.in_(
                                  l2_const.SUPPORTED_AGENT_TYPES))
         return query
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        # NOTE(jkoelker) non-admin queries are scoped to their tenant_id
        # NOTE(salvatore-orlando): unless the model allows for shared objects

        # ###-------- alter by xm and zyk at 2015.9.23--------------
        # query_filter = None
        # if not context.is_admin and hasattr(model, 'tenant_id'):
        #     if hasattr(model, 'shared'):
        #         query_filter = ((model.tenant_id == context.tenant_id) |
        #                         (model.shared == sql.true()))
        #     else:
        #         query_filter = (model.tenant_id == context.tenant_id)
        ###--change to as follow (because of gcloud7 access controller:gc_resource_type=0/1 0isOwnerAndShared 1isAll)--:

        query_filter = None
        if context.gc_resource_type == '0' and hasattr(model, 'user_id'):
            if hasattr(model, 'shared'):
                query_filter = ((model.user_id == context.user_id) |
                                (model.shared == sql.true()) |
                                (model.user_id == None))
            else:
                query_filter = ((model.user_id == context.user_id) |
                                (model.user_id == None))
        #model.user_id ==None(can not change as model.user_id is None) added
        # because of such as network:dhcp Ports(which user_id is None)
        ###------------- end by xm and zyk--------------------------------

        # Execute query hooks registered from mixins and plugins
        for _name, hooks in self._model_query_hooks.get(model, {}).iteritems():
            query_hook = hooks.get('query')
            if isinstance(query_hook, basestring):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, basestring):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)
        return query
Exemplo n.º 34
0
def query_with_hooks(context, model, field=None):
    """Query with hooks using the said context and model.

    :param context: The context to use for the DB session.
    :param model: The model to query.
    :param field: The column.
    :returns: The query with hooks applied to it.
    """
    if field:
        if hasattr(model, field):
            field = getattr(model, field)
        else:
            msg = _("'%s' is not supported as field") % field
            raise n_exc.InvalidInput(error_message=msg)
        query = context.session.query(field)
    else:
        query = context.session.query(model)
    # define basic filter condition for model query
    query_filter = None
    if db_utils.model_query_scope_is_project(context, model):
        if hasattr(model, 'rbac_entries'):
            query = query.outerjoin(model.rbac_entries)
            rbac_model = model.rbac_entries.property.mapper.class_
            query_filter = (
                (model.tenant_id == context.tenant_id) |
                ((rbac_model.action == 'access_as_shared') &
                 ((rbac_model.target_tenant == context.tenant_id) |
                  (rbac_model.target_tenant == '*'))))
        elif hasattr(model, 'shared'):
            query_filter = ((model.tenant_id == context.tenant_id) |
                            (model.shared == sql.true()))
        else:
            query_filter = (model.tenant_id == context.tenant_id)
    # Execute query hooks registered from mixins and plugins
    for hook in get_hooks(model):
        query_hook = helpers.resolve_ref(hook.get('query'))
        if query_hook:
            query = query_hook(context, model, query)

        filter_hook = helpers.resolve_ref(hook.get('filter'))
        if filter_hook:
            query_filter = filter_hook(context, model, query_filter)

    # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
    # condition, raising an exception
    if query_filter is not None:
        query = query.filter(query_filter)
    return query
Exemplo n.º 35
0
class Vim(model_base.BASE, models_v1.HasId, models_v1.HasTenant,
          models_v1.Audit):
    type = sa.Column(sa.String(64), nullable=False)
    name = sa.Column(sa.String(255), nullable=False)
    description = sa.Column(sa.Text, nullable=True)
    placement_attr = sa.Column(types.Json, nullable=True)
    shared = sa.Column(sa.Boolean,
                       default=True,
                       server_default=sql.true(),
                       nullable=False)
    is_default = sa.Column(sa.Boolean,
                           default=False,
                           server_default=sql.false(),
                           nullable=False)
    vim_auth = orm.relationship('VimAuth')
    status = sa.Column(sa.String(255), nullable=False)
Exemplo n.º 36
0
class SecurityGroup(standard_attr.HasStandardAttributes, model_base.BASEV2,
                    model_base.HasId, model_base.HasProject):
    """Represents a v2 neutron security group."""

    name = sa.Column(sa.String(db_const.NAME_FIELD_SIZE))
    stateful = sa.Column(sa.Boolean,
                         default=True,
                         server_default=sql.true(),
                         nullable=False)
    rbac_entries = sa.orm.relationship(rbac_db_models.SecurityGroupRBAC,
                                       backref='security_group',
                                       lazy='subquery',
                                       cascade='all, delete, delete-orphan')
    api_collections = [sg.SECURITYGROUPS]
    collection_resource_map = {sg.SECURITYGROUPS: 'security_group'}
    tag_support = True
Exemplo n.º 37
0
 def get_enabled_agent_on_host(self, context, agent_type, host):
     """Return agent of agent_type for the specified host."""
     query = context.session.query(Agent)
     query = query.filter(Agent.agent_type == agent_type,
                          Agent.host == host,
                          Agent.admin_state_up == sql.true())
     try:
         agent = query.one()
     except exc.NoResultFound:
         LOG.debug('No enabled %(agent_type)s agent on host '
                   '%(host)s', {'agent_type': agent_type, 'host': host})
         return
     if self.is_agent_down(agent.heartbeat_timestamp):
         LOG.warning(_LW('%(agent_type)s agent %(agent_id)s is not active'),
                     {'agent_type': agent_type, 'agent_id': agent.id})
     return agent
Exemplo n.º 38
0
 def auto_schedule_networks(self, plugin, context, host):
     """Schedule non-hosted networks to the DHCP agent on the specified
        host.
     """
     agents_per_network = cfg.CONF.dhcp_agents_per_network
     # a list of (agent, net_ids) tuples
     bindings_to_add = []
     with context.session.begin(subtransactions=True):
         fields = ['network_id', 'enable_dhcp']
         subnets = plugin.get_subnets(context, fields=fields)
         net_ids = set(s['network_id'] for s in subnets
                       if s['enable_dhcp'])
         if not net_ids:
             LOG.debug('No non-hosted networks')
             return False
         query = context.session.query(agents_db.Agent)
         query = query.filter(agents_db.Agent.agent_type ==
                              constants.AGENT_TYPE_DHCP,
                              agents_db.Agent.host == host,
                              agents_db.Agent.admin_state_up == sql.true())
         dhcp_agents = query.all()
         for dhcp_agent in dhcp_agents:
             if agents_db.AgentDbMixin.is_agent_down(
                 dhcp_agent.heartbeat_timestamp):
                 LOG.warning(_LW('DHCP agent %s is not active'),
                             dhcp_agent.id)
                 continue
             for net_id in net_ids:
                 agents = plugin.get_dhcp_agents_hosting_networks(
                     context, [net_id])
                 if len(agents) >= agents_per_network:
                     continue
                 if any(dhcp_agent.id == agent.id for agent in agents):
                     continue
                 net = plugin.get_network(context, net_id)
                 az_hints = (net.get(az_ext.AZ_HINTS) or
                             cfg.CONF.default_availability_zones)
                 if (az_hints and
                     dhcp_agent['availability_zone'] not in az_hints):
                     continue
                 bindings_to_add.append((dhcp_agent, net_id))
     # do it outside transaction so particular scheduling results don't
     # make other to fail
     for agent, net_id in bindings_to_add:
         self.resource_filter.bind(context, [agent], net_id)
     return True
Exemplo n.º 39
0
class HeadlineModel(db.Model):
    __tablename__ = 'headline'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer(), nullable=False, index=True)
    title = db.Column(db.Unicode(512), nullable=False)
    url = db.Column(db.Unicode(512))
    is_display = db.Column(db.Boolean,
                           default=True,
                           server_default=sql.true(),
                           nullable=False,
                           index=True)
    date_created = db.Column(db.DateTime,
                             nullable=False,
                             index=True,
                             server_default=db.func.current_timestamp())

    def as_dict(self):
        return {
            'id': self.id,
            'title': self.title,
        }

    @staticmethod
    def generate_fake(count=100):
        from sqlalchemy.exc import IntegrityError
        from random import seed, randint
        import forgery_py

        seed()
        user_count = UserModel.query.count()
        if user_count == 0:
            UserModel.generate_fake()
            user_count = UserModel.query.count()
        for i in range(count):
            u = UserModel.query.offset(randint(0, user_count - 1)).first()
            h = HeadlineModel(user_id=u.id,
                              title=forgery_py.lorem_ipsum.title())
            db.session.add(h)
            try:
                db.session.commit()
            except IntegrityError:
                db.session.rollback()

    def __repr__(self):
        return '<Headline %r>' % self.title
Exemplo n.º 40
0
def stop_callback(update: Update, context: CallbackContext, chat_info: dict,
                  _: gettext):
    if chat_info["is_subscribed"]:
        Session().query(Chat).filter_by(id=update.message.chat_id).update(
            {"is_subscribed": false()})
        transaction.commit()

    update.message.reply_text(text=_(
        "You're unsubscribed. You always can subscribe again 👉 /start"))

    Session().query(Notification).filter_by(
        is_active=true(),
        chat_id=update.message.chat_id).update({"is_active": false()})

    transaction.commit()

    return ConversationHandler.END
Exemplo n.º 41
0
class Agent(model_base.BASEV2, model_base.HasId):
    """Represents agents running in neutron deployments."""

    __table_args__ = (sa.UniqueConstraint('agent_type',
                                          'host',
                                          name='uniq_agents0agent_type0host'),
                      model_base.BASEV2.__table_args__)

    # L3 agent, DHCP agent, OVS agent, LinuxBridge
    agent_type = sa.Column(sa.String(255), nullable=False)
    binary = sa.Column(sa.String(255), nullable=False)
    # TOPIC is a fanout exchange topic
    topic = sa.Column(sa.String(255), nullable=False)
    # TOPIC.host is a target topic
    host = sa.Column(sa.String(255), nullable=False, index=True)
    availability_zone = sa.Column(sa.String(255))
    admin_state_up = sa.Column(sa.Boolean,
                               default=True,
                               server_default=sql.true(),
                               nullable=False)
    # the time when first report came from agents
    created_at = sa.Column(sa.DateTime, nullable=False)
    # the time when first report came after agents start
    started_at = sa.Column(sa.DateTime, nullable=False)
    # updated when agents report
    heartbeat_timestamp = sa.Column(sa.DateTime, nullable=False)
    # description is note for admin user
    description = sa.Column(sa.String(db_const.DESCRIPTION_FIELD_SIZE))
    # configurations: a json dict string, I think 4095 is enough
    configurations = sa.Column(sa.String(4095), nullable=False)
    # resource_versions: json dict, 8191 allows for ~256 resource versions
    #                    assuming ~32byte length "'name': 'ver',"
    #                    the whole row limit is 65535 bytes in mysql
    resource_versions = sa.Column(sa.String(8191))
    # load - number of resources hosted by the agent
    load = sa.Column(sa.Integer, server_default='0', nullable=False)
    # resources_synced: nullable boolean, success of last sync to Placement
    resources_synced = sa.Column(sa.Boolean,
                                 default=None,
                                 server_default=None,
                                 nullable=True)

    @property
    def is_active(self):
        return not utils.is_agent_down(self.heartbeat_timestamp)
Exemplo n.º 42
0
def _finish_task_values(memo, elapsed_seconds=None):
    """
    Get values to update when finishing tasks.
    """
    values = {
        'is_processed': sa_sql.true(),
        'processed': sa_sql.func.now(),
        'locked_until': sa_sql.func.now(),
    }
    if elapsed_seconds is not None:
        # we store milliseconds here
        values['elapsed_seconds'] = elapsed_seconds * 1000
    if memo is not None:
        # Ensure the memo object is JSON serializable, and therefore won't
        # be rejected by SQLAlchemy, by first marshaling it using 'str' as
        # the default encoder, then unmarshalling it back to a Python value.
        values['memo'] = json.loads(json.dumps(memo, default=str))
    return values
Exemplo n.º 43
0
 def has_property(self, prop):
     property_granted_select = select(
         [null()],
         from_obj=[
             Property.__table__, PropertyGroup.__table__,
             Membership.__table__
         ]).where(
             and_(Property.name == prop,
                  Property.property_group_id == PropertyGroup.id,
                  PropertyGroup.id == Membership.group_id,
                  Membership.user_id == self.id, Membership.active))
     #.cte("property_granted_select")
     return and_(
         not_(
             exists(
                 property_granted_select.where(
                     Property.granted == false()))),
         exists(property_granted_select.where(Property.granted == true())))
Exemplo n.º 44
0
def _build_solves_query(extra_filters=(), admin_view=False):
    """Returns queries and data that that are used for showing an account's solves.
    It returns a tuple of
        - SQLAlchemy query with (challenge_id, solve_count_for_challenge_id)
        - Current user's solved challenge IDs
    """
    # This can return None (unauth) if visibility is set to public
    user = get_current_user()
    # We only set a condition for matching user solves if there is a user and
    # they have an account ID (user mode or in a team in teams mode)
    AccountModel = get_model()
    if user is not None and user.account_id is not None:
        user_solved_cond = Solves.account_id == user.account_id
    else:
        user_solved_cond = false()
    # We have to filter solves to exclude any made after the current freeze
    # time unless we're in an admin view as determined by the caller.
    freeze = get_config("freeze")
    if freeze and not admin_view:
        freeze_cond = Solves.date < unix_time_to_utc(freeze)
    else:
        freeze_cond = true()
    # Finally, we never count solves made by hidden or banned users/teams, even
    # if we are an admin. This is to match the challenge detail API.
    exclude_solves_cond = and_(
        AccountModel.banned == false(), AccountModel.hidden == false(),
    )
    # This query counts the number of solves per challenge, as well as the sum
    # of correct solves made by the current user per the condition above (which
    # should probably only be 0 or 1!)
    solves_q = (
        db.session.query(Solves.challenge_id, sa_func.count(Solves.challenge_id),)
        .join(AccountModel)
        .filter(*extra_filters, freeze_cond, exclude_solves_cond)
        .group_by(Solves.challenge_id)
    )
    # Also gather the user's solve items which can be different from above query
    # Even if we are a hidden user, we should see that we have solved the challenge
    # But as a hidden user we are not included in the count
    solve_ids = (
        Solves.query.with_entities(Solves.challenge_id).filter(user_solved_cond).all()
    )
    solve_ids = {value for value, in solve_ids}
    return solves_q, solve_ids
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        # NOTE(jkoelker) non-admin queries are scoped to their tenant_id
        # NOTE(salvatore-orlando): unless the model allows for shared objects
        query_filter = None
        if not context.is_admin and hasattr(model, 'tenant_id'):
            if hasattr(model, 'shared'):
                query_filter = ((model.tenant_id == context.tenant_id) |
                                (model.shared == sql.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)
        elif context.is_admin and hasattr(model, 'tenant_id'):
            # NOTE(Lijiale) One tenant can not access other tenant's Vim,
            # NOTE(Lijiale) even though this tenant has admin role.
            if hasattr(model, 'shared'):
                query_filter = (model.tenant_id == context.tenant_id)

        # Execute query hooks registered from mixins and plugins
        for _name, hooks in iteritems(self._model_query_hooks.get(model, {})):
            query_hook = hooks.get('query')
            if isinstance(query_hook, six.string_types):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, six.string_types):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)

        # Don't list the deleted entries
        if hasattr(model, 'deleted_at'):
            query = query.filter_by(deleted_at=datetime.min)

        return query
Exemplo n.º 46
0
Arquivo: user.py Projeto: JuKu/pycroft
 def has_property(cls, prop, when=None):
     # TODO Use joins
     property_granted_select = select(
         [null()],
         from_obj=[
             Property.__table__, PropertyGroup.__table__,
             Membership.__table__
         ]).where(
             and_(Property.name == prop,
                  Property.property_group_id == PropertyGroup.id,
                  PropertyGroup.id == Membership.group_id,
                  Membership.user_id == cls.id, Membership.active(when)))
     #.cte("property_granted_select")
     return and_(
         not_(
             exists(
                 property_granted_select.where(
                     Property.granted == false()))),
         exists(property_granted_select.where(Property.granted == true()))
     ).self_group().label("has_property_" + prop)
Exemplo n.º 47
0
    def get_packet_filters_for_port(self, context, port):
        """Retrieve packet filters on OFC on a given port.

        It returns a list of tuple (neutron filter_id, OFC id).
        """
        query = (context.session.query(nmodels.OFCFilterMapping)
                 .join(PacketFilter,
                       nmodels.OFCFilterMapping.neutron_id == PacketFilter.id)
                 .filter(PacketFilter.admin_state_up == sql.true()))

        network_id = port['network_id']
        net_pf_query = (query.filter(PacketFilter.network_id == network_id)
                        .filter(PacketFilter.in_port == sql.null()))
        net_filters = [(pf['neutron_id'], pf['ofc_id']) for pf in net_pf_query]

        port_pf_query = query.filter(PacketFilter.in_port == port['id'])
        port_filters = [(pf['neutron_id'], pf['ofc_id'])
                        for pf in port_pf_query]

        return net_filters + port_filters
Exemplo n.º 48
0
    def test_deletion_three_offline_nodes_and_one_online(self):
        cluster = self.env.create(
            nodes_kwargs=[
                {"online": False, "pending_deletion": True},
                {"online": False, "pending_deletion": True},
                {"online": False, "pending_deletion": True},
                {"online": True, "pending_deletion": True}
            ]
        )

        supertask = self.env.launch_deployment()
        self.db.flush()
        self.env.wait_ready(supertask, timeout=5)

        # this test is failing when whole test set is executing
        # apparently the main reason for that is delays in data
        # updating inside of fake threads so in order to make test
        # pass we have to wait for data to be present in db
        self.env.wait_for_true(self.check_node_presence, args=[1])

        # Offline nodes were deleted, online node came back
        self.assertEqual(
            self.db.query(models.Node).filter(
                models.Node.cluster_id == cluster['id']).count(),
            0
        )
        self.assertEqual(
            self.db.query(models.Node).filter(
                models.Node.cluster_id.is_(None)).count(),
            1
        )
        self.assertEqual(
            self.db.query(models.Node).filter(
                models.Node.status == NODE_STATUSES.discover).count(),
            1
        )
        self.assertEqual(
            self.db.query(models.Node).filter(
                models.Node.online == sql.true()).count(),
            1
        )
Exemplo n.º 49
0
def _check_untagged_builds(session, collection, package_map, build_infos):
    """
    Check whether some of the builds we have weren't untagged/deleted in the
    meantime. Only checks last builds of packages.
    """
    for info in build_infos:
        package = package_map.get(info['package_name'])
        # info contains the last build for the package that Koji knows
        if package and util.is_build_newer(info, package.last_build):
            # The last build (possibly more) we have is newer than last build in Koji.
            # That means it was untagged or deleted.
            # Get the last build we have that was not untagged.
            last_valid_build_query = (
                session.db.query(Build)
                .filter(
                    (Build.package_id == package.id) &
                    (Build.epoch == info['epoch']) &
                    (Build.version == info['version']) &
                    (Build.release == info['release'])
                )
                .order_by(Build.started.desc())
            )
            last_valid_build = last_valid_build_query.first()
            if not last_valid_build:
                # We don't have the build anymore, register it again
                register_real_builds(session, collection,
                                     [(package.id, info)])
                # Now, it must find it as we've just inserted it
                last_valid_build = last_valid_build_query.first()
            # set all following builds as untagged
            # last_build pointers get reset by the trigger
            (
                session.db.query(Build)
                .filter(Build.package_id == package.id)
                .filter(
                    Build.started > last_valid_build.started
                    if last_valid_build else true()
                )
                .update({'untagged': True})
            )
            session.log.info("{} is no longer tagged".format(package.last_build))
Exemplo n.º 50
0
    def filter_timestamp_column(self, ts_col,
                                cmp_op: callable) -> ColumnElement:
        """
        Filter timestamp column by comparing to this hour-of-day using the given
        comparison operator.

        Note that for the class `MissingHourAndMinutesTimestamp` this always
        returns TRUE, since a missing timestamp imposes no constraint.

        Parameters
        ----------
        ts_col : sqlalchemy column
            The timestamp column to filter.
        cmp_op : callable
            Comparison operator to use. For example: `operator.lt`, `operator.ge`.

        Returns
        -------
        sqlalchemy.sql.elements.True_
        """
        return true()
Exemplo n.º 51
0
class Vim(model_base.BASE,
          models_v1.HasId,
          models_v1.HasTenant,
          models_v1.Audit):
    type = sa.Column(sa.String(64), nullable=False)
    name = sa.Column(sa.String(255), nullable=False)
    description = sa.Column(sa.Text, nullable=True)
    placement_attr = sa.Column(types.Json, nullable=True)
    shared = sa.Column(sa.Boolean, default=True, server_default=sql.true(
    ), nullable=False)
    is_default = sa.Column(sa.Boolean, default=False, server_default=sql.false(
    ), nullable=False)
    vim_auth = orm.relationship('VimAuth')
    status = sa.Column(sa.String(255), nullable=False)

    __table_args__ = (
        schema.UniqueConstraint(
            "tenant_id",
            "name",
            name="uniq_vim0tenant_id0name"),
    )
Exemplo n.º 52
0
    def _get_default_external_network(self, context):
        """Get the default external network for the deployment."""
        with context.session.begin(subtransactions=True):
            default_external_networks = (context.session.query(
                external_net_db.ExternalNetwork).
                filter_by(is_default=sql.true()).
                join(models_v2.Network).
                join(model_base.StandardAttribute).
                order_by(model_base.StandardAttribute.id).all())

        if not default_external_networks:
            LOG.error(_LE("Unable to find default external network "
                          "for deployment, please create/assign one to "
                          "allow auto-allocation to work correctly."))
            raise exceptions.AutoAllocationFailure(
                reason=_("No default router:external network"))
        if len(default_external_networks) > 1:
            LOG.error(_LE("Multiple external default networks detected. "
                          "Network %s is true 'default'."),
                      default_external_networks[0]['network_id'])
        return default_external_networks[0]
Exemplo n.º 53
0
 def get_pending_build_tasks(cls, background=None):
     query = (models.BuildChroot.query.outerjoin(models.Build).outerjoin(
         models.CoprDir).outerjoin(
             models.Package,
             models.Package.id == models.Build.package_id).options(
                 joinedload('build').joinedload('copr_dir'),
                 joinedload('build').joinedload('package')
             ).filter(models.Build.canceled == false()).filter(
                 or_(
                     models.BuildChroot.status == StatusEnum("pending"),
                     and_(
                         models.BuildChroot.status == StatusEnum("running"),
                         models.BuildChroot.started_on <
                         int(time.time() - 1.1 * MAX_BUILD_TIMEOUT),
                         models.BuildChroot.ended_on.is_(None)))).order_by(
                             models.Build.is_background.asc(),
                             models.Build.id.asc()))
     if background is not None:
         query = query.filter(models.Build.is_background == (
             true() if background else false()))
     return query
Exemplo n.º 54
0
class Trunk(model_base.HasStandardAttributes, model_base.BASEV2,
            model_base.HasId, model_base.HasProject):

    admin_state_up = sa.Column(
        sa.Boolean(), nullable=False, server_default=sql.true())
    name = sa.Column(sa.String(attributes.NAME_MAX_LEN))
    port_id = sa.Column(sa.String(36),
                        sa.ForeignKey('ports.id',
                                      ondelete='CASCADE'),
                        nullable=False,
                        unique=True)
    status = sa.Column(
        sa.String(16), nullable=False, server_default=constants.ACTIVE_STATUS)

    port = sa.orm.relationship(
        models_v2.Port,
        backref=sa.orm.backref('trunk_port', lazy='joined', uselist=False,
                               cascade='delete'))

    sub_ports = sa.orm.relationship(
        'SubPort', lazy='joined', uselist=True, cascade="all, delete-orphan")
Exemplo n.º 55
0
    def get_ha_routers_l3_agents_count(self, context):
        """Return a map between HA routers and how many agents every
        router is scheduled to.
        """

        # Postgres requires every column in the select to be present in
        # the group by statement when using an aggregate function.
        # One solution is to generate a subquery and join it with the desired
        # columns.
        binding_model = l3_sch_db.RouterL3AgentBinding
        sub_query = (context.session.query(
            binding_model.router_id,
            func.count(binding_model.router_id).label('count')).join(
                l3_attrs_db.RouterExtraAttributes,
                binding_model.router_id == l3_attrs_db.RouterExtraAttributes.
                router_id).join(l3_db.Router).filter(
                    l3_attrs_db.RouterExtraAttributes.ha ==
                    sql.true()).group_by(binding_model.router_id).subquery())

        query = (context.session.query(l3_db.Router.id, l3_db.Router.tenant_id,
                                       sub_query.c.count).join(sub_query))
        return query
Exemplo n.º 56
0
def upgrade():
    op.create_table(
        'trunks',
        sa.Column('admin_state_up',
                  sa.Boolean(),
                  nullable=False,
                  server_default=sql.true()),
        sa.Column('tenant_id',
                  sa.String(length=255),
                  nullable=True,
                  index=True),
        sa.Column('id', sa.String(length=36), nullable=False),
        sa.Column('name', sa.String(length=255), nullable=True),
        sa.Column('port_id', sa.String(length=36), nullable=False),
        sa.Column('status',
                  sa.String(length=16),
                  nullable=False,
                  server_default='ACTIVE'),
        sa.Column('standard_attr_id', sa.BigInteger(), nullable=False),
        sa.ForeignKeyConstraint(['port_id'], ['ports.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['standard_attr_id'],
                                ['standardattributes.id'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('port_id'),
        sa.UniqueConstraint('standard_attr_id'))
    op.create_table(
        'subports', sa.Column('port_id', sa.String(length=36)),
        sa.Column('trunk_id', sa.String(length=36), nullable=False),
        sa.Column('segmentation_type', sa.String(length=32), nullable=False),
        sa.Column('segmentation_id', sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(['port_id'], ['ports.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['trunk_id'], ['trunks.id'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('port_id'),
        sa.UniqueConstraint(
            'trunk_id',
            'segmentation_type',
            'segmentation_id',
            name='uniq_subport0trunk_id0segmentation_type0segmentation_id'))
Exemplo n.º 57
0
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        query_filter = None
        if self.model_query_scope(context, model):  # ★这个权限范围判断很重要 ★
            if hasattr(model, 'rbac_entries'):  # model有没有rbac_entries这个属性
                query = query.outerjoin(model.rbac_entries)
                rbac_model = model.rbac_entries.property.mapper.class_
                query_filter = (
                    (model.tenant_id == context.tenant_id) |
                    ((rbac_model.action == 'access_as_shared') &
                     ((rbac_model.target_tenant == context.tenant_id) |
                      (rbac_model.target_tenant == '*'))))
            elif hasattr(model, 'shared'):  # 有没有shared这个属性
                query_filter = ((model.tenant_id == context.tenant_id) |  # 条件就是:同租户或者shared为true
                                (model.shared == sql.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)  # 条件为:同租户
        # Execute query hooks registered from mixins and plugins
        for _name, hooks in six.iteritems(self._model_query_hooks.get(model,  # model专属的过滤钩子
                                                                      {})):
            query_hook = hooks.get('query')
            if isinstance(query_hook, six.string_types):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, six.string_types):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)  # 没有这个query_filter的话就加进去
        return query
Exemplo n.º 58
0
    def _model_query(self, context, model):
        query = context.session.query(model)
        # define basic filter condition for model query
        query_filter = None
        if self.model_query_scope(context, model):
            if hasattr(model, 'rbac_entries'):
                rbac_model, join_params = self._get_rbac_query_params(model)
                query = query.outerjoin(*join_params)
                query_filter = (
                    (model.tenant_id == context.tenant_id) |
                    ((rbac_model.action == 'access_as_shared') &
                     ((rbac_model.target_tenant == context.tenant_id) |
                      (rbac_model.target_tenant == '*'))))
            elif hasattr(model, 'shared'):
                query_filter = ((model.tenant_id == context.tenant_id) |
                                (model.shared == sql.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)
        # Execute query hooks registered from mixins and plugins
        for _name, hooks in six.iteritems(self._model_query_hooks.get(model,
                                                                      {})):
            query_hook = hooks.get('query')
            if isinstance(query_hook, six.string_types):
                query_hook = getattr(self, query_hook, None)
            if query_hook:
                query = query_hook(context, model, query)

            filter_hook = hooks.get('filter')
            if isinstance(filter_hook, six.string_types):
                filter_hook = getattr(self, filter_hook, None)
            if filter_hook:
                query_filter = filter_hook(context, model, query_filter)

        # NOTE(salvatore-orlando): 'if query_filter' will try to evaluate the
        # condition, raising an exception
        if query_filter is not None:
            query = query.filter(query_filter)
        return query
Exemplo n.º 59
0
class PermissionGroup(db.Base):
    __tablename__ = 'permission_groups'
    __table_args__ = {'extend_existing': True}

    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    trade = Column(Boolean, server_default=sql.false())
    deposit = Column(Boolean, server_default=sql.false())
    withdraw = Column(Boolean, server_default=sql.false())
    login = Column(Boolean, server_default=sql.true())
    full_ui = Column(Boolean, server_default=sql.false())

    def __init__(self, name, permissions):
        """

        :param name:
        :type name: str
        """
        self.name = name
        self.trade = 'trade' in permissions
        self.withdraw = 'withdraw' in permissions
        self.deposit = 'deposit' in permissions
        self.login = '******' in permissions
        self.full_ui = 'full_ui' in permissions

    @property
    def dict(self):
        return {'name': self.name,
                'trade': self.trade,
                'deposit': self.deposit,
                'withdraw': self.withdraw,
                'login': self.login,
                'full_ui': self.full_ui
        }

    def __repr__(self):
        return "<PermissionGroup('%s')>" % self.dict
Exemplo n.º 60
0
class Agent(model_base.BASEV2, models_v2.HasId):
    """Represents agents running in neutron deployments."""

    __table_args__ = (sa.UniqueConstraint('agent_type',
                                          'host',
                                          name='uniq_agents0agent_type0host'),
                      model_base.BASEV2.__table_args__)

    # L3 agent, DHCP agent, OVS agent, LinuxBridge
    agent_type = sa.Column(sa.String(255), nullable=False)
    binary = sa.Column(sa.String(255), nullable=False)
    # TOPIC is a fanout exchange topic
    topic = sa.Column(sa.String(255), nullable=False)
    # TOPIC.host is a target topic
    host = sa.Column(sa.String(255), nullable=False)
    availability_zone = sa.Column(sa.String(255))
    admin_state_up = sa.Column(sa.Boolean,
                               default=True,
                               server_default=sql.true(),
                               nullable=False)
    # the time when first report came from agents
    created_at = sa.Column(sa.DateTime, nullable=False)
    # the time when first report came after agents start
    started_at = sa.Column(sa.DateTime, nullable=False)
    # updated when agents report
    heartbeat_timestamp = sa.Column(sa.DateTime, nullable=False)
    # description is note for admin user
    description = sa.Column(sa.String(255))
    # configurations: a json dict string, I think 4095 is enough
    configurations = sa.Column(sa.String(4095), nullable=False)
    # load - number of resources hosted by the agent
    load = sa.Column(sa.Integer, server_default='0', nullable=False)

    @property
    def is_active(self):
        return not AgentDbMixin.is_agent_down(self.heartbeat_timestamp)