def get_client_api_pointer(): """Return the client api pointer. :return: MIQ client api pointer :rtype: object """ try: return click.get_current_context().find_root().client_api except AttributeError: log.error('Unable to get client api pointer.')
def __call__(self, query, attr=None): """Performs a advanced query on a collection. :param query: multiple queries containing name, operand and value :type query: list :return: collection resources matching the supplied query :rtype: list Usage: .. code-block: python query = AdvancedQuery(vm_collection) query([('name', '=', 'vm_foo'), '&', ('id', '>', '9999934')]) # -- or -- query.__call__([('name', '=', 'vm_foo'), '&', ('id', '>', '9999934')]) """ adv_query = '' if len(query) % 2 == 0: log.warning('Query attempted is invalid: {0}'.format(query)) return self.resources # build query in string form while query: _query = query.pop(0) if isinstance(_query, tuple) and len(_query) != 3: log.warning('Query must contain three indexes (name, operator,' ' value)') return self.resources elif isinstance(_query, tuple): adv_query += str("Q('" + str(_query[0]) + "', '" + str(_query[1])) + "', '" + str( _query[2]) + "')" elif isinstance(_query, str): adv_query += ' {0} '.format(_query) try: resources = getattr(self.collection, 'filter')(eval(adv_query)) self.resources = resources.resources if attr: for ent in resources.resources: ent.reload(True, True, attr) except (APIException, ValueError, TypeError) as e: # most likely user passed an invalid attribute name log.error('Query attempted failed: {0}, error: {1}'.format( query, e)) return self.resources
def __call__(self, query, attr=None): """Performs a basic query on a collection. :param query: query containing name, operand and value :type query: tuple :return: collection resources matching the supplied query :rtype: list Usage: .. code-block: python # query vms collection to find the following vm by name query = BasicQuery(vm_collection) query(('name', '=', 'vm_foo')) # -- or -- query.__call__(('name', '=', 'vm_foo')) """ if len(query) != 3: log.warning('Query must contain three indexes. i.e. ' '(name, operand, value)') return self.resources try: resources = getattr(self.collection, 'filter')(Q(query[0], query[1], query[2])) self.resources = resources.resources if attr: for ent in resources.resources: ent.reload(True, True, attr) except (APIException, ValueError) as e: log.error('Query attempted failed: {0}, error: {1}'.format( query, e)) return self.resources
def get_attribute(self, ent_id, attribute): """Get the attribute for the collection entity. Return should be checked for None by caller. :param ent_id: Entity ID of Collection :type ent_id: id :param attribute: Attribute to retrieve :type attribute: string :return: Attribute :rtype: """ atts = None try: atts = self.get_entity(ent_id, attribute).__getattr__(attribute) except AttributeError as e: log.error('Get Attribute failed: Attribute: ' "'{0}' ID({1}): {2} error: {3}".format( attribute, self.__collection_name__, ent_id, e)) except APIException as e: log.abort('Unexpected Error in Get Attribute: Attribute: ' "'{0}' ID({1}): {2} error: {3}".format( attribute, self.__collection_name__, ent_id, e)) return atts
def cli(): """Print an error message""" miqcli_log.error(MESSAGE)