Python oslo_utils.strutils.is_int_like() Examples

The following are 12 code examples of oslo_utils.strutils.is_int_like(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_utils.strutils , or try the search function .
Example #1
Source File: api.py    From zun with Apache License 2.0 6 votes vote down vote up
def add_identity_filter(query, value):
    """Adds an identity filter to a query.

    Filters results by ID, if supplied value is a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param value: Value for filtering results by.
    :return: Modified query.
    """
    if strutils.is_int_like(value):
        return query.filter_by(id=value)
    elif uuidutils.is_uuid_like(value):
        return query.filter_by(uuid=value)
    else:
        raise exception.InvalidIdentity(identity=value) 
Example #2
Source File: api.py    From cyborg with Apache License 2.0 6 votes vote down vote up
def add_identity_filter(query, value):
    """Adds an identity filter to a query.

    Filters results by ID, if supplied value is a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param value: Value for filtering results by.
    :return: Modified query.
    """
    if strutils.is_int_like(value):
        return query.filter_by(id=value)
    elif uuidutils.is_uuid_like(value):
        return query.filter_by(uuid=value)
    else:
        raise exception.InvalidIdentity(identity=value) 
Example #3
Source File: test_strutils.py    From oslo.utils with Apache License 2.0 6 votes vote down vote up
def test_is_int_like_false(self):
        self.assertFalse(strutils.is_int_like(1.1))
        self.assertFalse(strutils.is_int_like("1.1"))
        self.assertFalse(strutils.is_int_like("1.1.1"))
        self.assertFalse(strutils.is_int_like(None))
        self.assertFalse(strutils.is_int_like("0."))
        self.assertFalse(strutils.is_int_like("aaaaaa"))
        self.assertFalse(strutils.is_int_like("...."))
        self.assertFalse(strutils.is_int_like("1g"))
        self.assertFalse(
            strutils.is_int_like("0cc3346e-9fef-4445-abe6-5d2b2690ec64"))
        self.assertFalse(strutils.is_int_like("a1"))
        # NOTE(viktors): 12e3 - is a float number
        self.assertFalse(strutils.is_int_like("12e3"))
        # NOTE(viktors): Check integer numbers with base not 10
        self.assertFalse(strutils.is_int_like("0o51"))
        self.assertFalse(strutils.is_int_like("0xDEADBEEF")) 
Example #4
Source File: connector.py    From designate with Apache License 2.0 5 votes vote down vote up
def __init__(self, options):
        """Initialize a new Infoblox object instance

        Args:
            options (dict): Target options dictionary
        """
        config = CONF[CFG_GROUP_NAME]

        reqd_opts = ['wapi_url', 'username', 'password', 'ns_group']
        other_opts = ['sslverify', 'network_view', 'dns_view', 'multi_tenant']

        for opt in reqd_opts + other_opts:
            if opt == 'sslverify' or opt == 'multi_tenant':
                # NOTE(selvakumar): This check is for sslverify option.
                # type of sslverify is unicode string from designate DB
                # if the value is 0 getattr called for setting default values.
                # to avoid setting default values we use oslo strutils
                if not strutils.is_int_like(options.get(opt)):
                    option_value = options.get(opt)
                else:
                    option_value = strutils.bool_from_string(options.get(opt),
                                                             default=True)
                setattr(self, opt, option_value)
                continue
            setattr(self, opt, options.get(opt) or getattr(config, opt))

        for opt in reqd_opts:
            LOG.debug("self.%s = %s", opt, getattr(self, opt))
            if not getattr(self, opt):
                raise exc.InfobloxIsMisconfigured(option=opt)

        self.session = requests.Session()
        adapter = requests.adapters.HTTPAdapter(
            pool_connections=config.http_pool_connections,
            pool_maxsize=config.http_pool_maxsize)
        self.session.mount('http://', adapter)
        self.session.mount('https://', adapter)
        self.session.auth = (self.username, self.password)
        self.session.verify = self.sslverify 
Example #5
Source File: cluster.py    From magnum with Apache License 2.0 5 votes vote down vote up
def get(cls, context, cluster_id):
        """Find a cluster based on its id or uuid and return a Cluster object.

        :param cluster_id: the id *or* uuid of a cluster.
        :param context: Security context
        :returns: a :class:`Cluster` object.
        """
        if strutils.is_int_like(cluster_id):
            return cls.get_by_id(context, cluster_id)
        elif uuidutils.is_uuid_like(cluster_id):
            return cls.get_by_uuid(context, cluster_id)
        else:
            raise exception.InvalidIdentity(identity=cluster_id) 
Example #6
Source File: nodegroup.py    From magnum with Apache License 2.0 5 votes vote down vote up
def get(cls, context, cluster_id, nodegroup_id):
        """Find a nodegroup based on its id or uuid and return a NodeGroup.

        :param cluster_id: the of id a cluster.
        :param nodegroup_id: the id of a nodegroup.
        :param context: Security context
        :returns: a :class:`NodeGroup` object.
        """
        if strutils.is_int_like(nodegroup_id):
            return cls.get_by_id(context, cluster_id, nodegroup_id)
        elif uuidutils.is_uuid_like(nodegroup_id):
            return cls.get_by_uuid(context, cluster_id, nodegroup_id)
        else:
            return cls.get_by_name(context, cluster_id, nodegroup_id) 
Example #7
Source File: cluster_template.py    From magnum with Apache License 2.0 5 votes vote down vote up
def get(cls, context, cluster_template_id):
        """Find and return ClusterTemplate object based on its id or uuid.

        :param cluster_template_id: the id *or* uuid of a ClusterTemplate.
        :param context: Security context
        :returns: a :class:`ClusterTemplate` object.
        """
        if strutils.is_int_like(cluster_template_id):
            return cls.get_by_id(context, cluster_template_id)
        elif uuidutils.is_uuid_like(cluster_template_id):
            return cls.get_by_uuid(context, cluster_template_id)
        else:
            raise exception.InvalidIdentity(identity=cluster_template_id) 
Example #8
Source File: x509keypair.py    From magnum with Apache License 2.0 5 votes vote down vote up
def get(cls, context, x509keypair_id):
        """Find a X509KeyPair based on its id or uuid.

        Find X509KeyPair by id or uuid and return a X509KeyPair object.

        :param x509keypair_id: the id *or* uuid of a x509keypair.
        :param context: Security context
        :returns: a :class:`X509KeyPair` object.
        """
        if strutils.is_int_like(x509keypair_id):
            return cls.get_by_id(context, x509keypair_id)
        elif uuidutils.is_uuid_like(x509keypair_id):
            return cls.get_by_uuid(context, x509keypair_id)
        else:
            raise exception.InvalidIdentity(identity=x509keypair_id) 
Example #9
Source File: federation.py    From magnum with Apache License 2.0 5 votes vote down vote up
def get(cls, context, federation_id):
        """Find a federation based on its id or uuid and return it.

        :param federation_id: the id *or* uuid of a federation.
        :param context: Security context
        :returns: a :class:`Federation` object.
        """
        if strutils.is_int_like(federation_id):
            return cls.get_by_id(context, federation_id)
        elif uuidutils.is_uuid_like(federation_id):
            return cls.get_by_uuid(context, federation_id)
        else:
            raise exception.InvalidIdentity(identity=federation_id) 
Example #10
Source File: utils.py    From senlin with Apache License 2.0 5 votes vote down vote up
def get_positive_int(v):
    """Util function converting/checking a value of positive integer.

    :param v: A value to be checked.
    :returns: (b, v) where v is (converted) value if bool is True.
              b is False if the value fails validation.
    """
    if strutils.is_int_like(v):
        count = int(v)
        if count > 0:
            return True, count
    return False, 0 
Example #11
Source File: test_strutils.py    From oslo.utils with Apache License 2.0 5 votes vote down vote up
def test_is_int_like_true(self):
        self.assertTrue(strutils.is_int_like(1))
        self.assertTrue(strutils.is_int_like("1"))
        self.assertTrue(strutils.is_int_like("514"))
        self.assertTrue(strutils.is_int_like("0")) 
Example #12
Source File: _filters.py    From tacker with Apache License 2.0 4 votes vote down vote up
def _validate_data_type(self, target):
        if not self.values:
            msg = ("Rule '%(rule)s' contains empty value")
            raise exception.ValidationError(msg % {"rule": self})

        special_attribute = self._attribute_special_field(target)
        if special_attribute:
            attribute_info = target.get(special_attribute)
        else:
            attribute_info = target.get(self.attribute)

        for value in self.values:
            error = False
            if attribute_info[1] == 'string' and not isinstance(value,
                    six.string_types):
                error = True
            elif attribute_info[1] == 'number':
                if not strutils.is_int_like(value):
                    error = True
            elif attribute_info[1] == 'uuid':
                if not uuidutils.is_uuid_like(value):
                    error = True
            elif attribute_info[1] == 'datetime':
                try:
                    timeutils.parse_isotime(value)
                except ValueError:
                    error = True
            elif attribute_info[1] == 'enum':
                if value not in attribute_info[3]:
                    msg = ("Rule '%(rule)s' contains data type '%(type)s' "
                           "with invalid value. It should be one of "
                           "%(valid_value)s")
                    raise exception.ValidationError(msg % {"rule": self,
                        "valid_value": ",".join(attribute_info[3]),
                        'type': attribute_info[1]})

            if error:
                msg = ("Rule '%(rule)s' contains invalid data type for value "
                       "'%(value)s'. The data type should be '%(type)s'")
                raise exception.ValidationError(msg % {"rule": self,
                    "value": value,
                    'type': attribute_info[1]})

            # Also, check whether the data type is supported by operator
            if attribute_info[1] not in \
                    self.OPERATOR_SUPPORTED_DATA_TYPES.get(self.operator):
                msg = ("Rule '%(rule)s' contains operator '%(operator)s' "
                       "which doesn't support data type '%(type)s' for "
                       "attribute '%(attribute)s'")
                raise exception.ValidationError(msg % {"rule": self,
                                "operator": self.operator,
                                'type': attribute_info[1],
                                'attribute': self.attribute})