def get_by_uid(cls, uid, fail_if_not_found=False, lock_for_update=False): """Get instance by it's uid (PK in case of SQLAlchemy) :param uid: uid of object :param fail_if_not_found: raise an exception if object is not found :param lock_for_update: lock returned object for update (DB mutex) :returns: instance of an object (model) """ q = db().query(cls.model) if lock_for_update: q = q.with_lockmode('update') res = q.get(uid) if not res and fail_if_not_found: raise errors.ObjectNotFound( "Object '{0}' with UID={1} is not found in DB".format( cls.__name__, uid)) return res
def get_one(cls, fail_if_not_found=False, lock_for_update=False): """Get one instance from table. :param fail_if_not_found: raise an exception if object is not found :param lock_for_update: lock returned object for update (DB mutex) :return: instance of an object (model) """ q = db().query(cls.model) if lock_for_update: q = q.with_lockmode('update') res = q.first() if not res and fail_if_not_found: raise errors.ObjectNotFound( "Object '{0}' is not found in DB".format(cls.__name__)) return res
def validate_node(cls, node_id): node = adapters.NailgunNodeAdapter.get_by_uid(node_id) if not node: raise errors.ObjectNotFound( "Node with id {0} was not found.".format(node_id), log_message=True) # node can go to error state while upgrade process allowed_statuses = (consts.NODE_STATUSES.ready, consts.NODE_STATUSES.provisioned, consts.NODE_STATUSES.error) if node.status not in allowed_statuses: raise errors.InvalidData("Node should be in one of statuses: {0}." " Currently node has {1} status.".format( allowed_statuses, node.status), log_message=True) if node.status == consts.NODE_STATUSES.error and\ node.error_type != consts.NODE_ERRORS.deploy: raise errors.InvalidData("Node should be in error state only with" "deploy error type. Currently error type" " of node is {0}".format(node.error_type), log_message=True) return node