def get(cls, context, bay_id): """Find a bay based on its id or uuid and return a Bay object. :param bay_id: the id *or* uuid of a bay. :returns: a :class:`Bay` object. """ if utils.is_int_like(bay_id): return cls.get_by_id(context, bay_id) elif utils.is_uuid_like(bay_id): return cls.get_by_uuid(context, bay_id) else: raise exception.InvalidIdentity(identity=bay_id)
def get(cls, context, baymodel_id): """Find a baymodel based on its id or uuid and return a BayModel object. :param baymodel_id: the id *or* uuid of a baymodel. :returns: a :class:`BayModel` object. """ if utils.is_int_like(baymodel_id): return cls.get_by_id(context, baymodel_id) elif utils.is_uuid_like(baymodel_id): return cls.get_by_uuid(context, baymodel_id) else: raise exception.InvalidIdentity(identity=baymodel_id)
def get(cls, context, pod_id): """Find a pod based on its id or uuid and return a Pod object. :param pod_id: the id *or* uuid of a pod. :returns: a :class:`Pod` object. """ if utils.is_int_like(pod_id): return cls.get_by_id(context, pod_id) elif utils.is_uuid_like(pod_id): return cls.get_by_uuid(context, pod_id) else: raise exception.InvalidIdentity(identity=pod_id)
def get(cls, context, node_id): """Find a node based on its id or uuid and return a Node object. :param node_id: the id *or* uuid of a node. :returns: a :class:`Node` object. """ if utils.is_int_like(node_id): return cls.get_by_id(context, node_id) elif utils.is_uuid_like(node_id): return cls.get_by_uuid(context, node_id) else: raise exception.InvalidIdentity(identity=node_id)
def get(cls, context, service_id): """Find a service based on its id or uuid and return a Service object. :param service_id: the id *or* uuid of a service. :returns: a :class:`Service` object. """ if utils.is_int_like(service_id): return cls.get_by_id(context, service_id) elif utils.is_uuid_like(service_id): return cls.get_by_uuid(context, service_id) else: raise exception.InvalidIdentity(identity=service_id)
def get(cls, context, x509keypair_id): """Find a x509keypair_id based on its id or uuid and return a X509KeyPair object. :param x509keypair_id: the id *or* uuid of a x509keypair. :returns: a :class:`X509KeyPair` object. """ if utils.is_int_like(x509keypair_id): return cls.get_by_id(context, x509keypair_id) elif utils.is_uuid_like(x509keypair_id): return cls.get_by_uuid(context, x509keypair_id) else: raise exception.InvalidIdentity(identity=x509keypair_id)
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 utils.is_int_like(value): return query.filter_by(id=value) elif utils.is_uuid_like(value): return query.filter_by(uuid=value) else: raise exception.InvalidIdentity(identity=value)
def test_is_int_like(self): self.assertTrue(utils.is_int_like(1)) self.assertTrue(utils.is_int_like("1")) self.assertTrue(utils.is_int_like("514")) self.assertTrue(utils.is_int_like("0")) self.assertFalse(utils.is_int_like(1.1)) self.assertFalse(utils.is_int_like("1.1")) self.assertFalse(utils.is_int_like("1.1.1")) self.assertFalse(utils.is_int_like(None)) self.assertFalse(utils.is_int_like("0.")) self.assertFalse(utils.is_int_like("aaaaaa")) self.assertFalse(utils.is_int_like("....")) self.assertFalse(utils.is_int_like("1g")) self.assertFalse( utils.is_int_like("0cc3346e-9fef-4445-abe6-5d2b2690ec64")) self.assertFalse(utils.is_int_like("a1"))