Пример #1
0
def find_service_by_arguments(manager, account, service_id, path, show_simple_props = False):
    
    if service_id is not None:
        show_service_instance = manager.get_service_by_id(account, service_id)
        if show_service_instance.has_key("GetServiceByIdResult") and len(show_service_instance["GetServiceByIdResult"]) == 1:
            for k in show_service_instance["GetServiceByIdResult"]:
                service_to_return = AtomiaService(show_simple_props = show_simple_props)
                service_to_return.from_simplexml(k)
                return service_to_return if service_to_return.logical_id is not None else None
        
    elif path is not None:
        show_service_locator = json.loads(path)
        if len(show_service_locator) > 0:
            parent_service_for_criteria = None
            for count in show_service_locator:
                if isinstance(count.values()[0], dict) or count.values()[0] == '':
                    service_search_criteria_list = []
                    search_properties = []
                    if parent_service_for_criteria is not None:
                        tmp_ssc = AtomiaServiceSearchCriteria(str(count.keys()[0]), '', parent_service_for_criteria)
                    else:
                        tmp_ssc = AtomiaServiceSearchCriteria(str(count.keys()[0]), '')
                        
                    service_search_criteria_list.append(tmp_ssc.to_xml_friendly_object('atom:ServiceSearchCriteria', 'ServiceSearchCriteria'))
                    if isinstance(count.values()[0], dict):
                        for propk in count.values()[0]:
                            tmp_property = AtomiaServiceSearchCriteriaProperty(str(propk), str(count.values()[0][propk]))
                            search_properties.append(tmp_property.to_xml_friendly_object('arr:KeyValueOfstringstring', 'KeyValueOfstringstring'))
                    test = manager.find_services_by_path_with_paging(service_search_criteria_list, account, search_properties=search_properties)
                    if test.itervalues().next() is not None and test.itervalues().next().children() is not None and len(test.itervalues().next().children()) > 0:
                        for k in test.itervalues().next().children():
                            parent_service_for_criteria = AtomiaService(show_simple_props = show_simple_props)
                            parent_service_for_criteria.from_simplexml(k)
                            break
                    else:
                        parent_service_for_criteria = None
                        break
        
                elif count.values()[0] is not None:
                    parent_service_for_criteria = manager.get_service_by_id(account, str(count.values()[0])) 
                    parent_service_for_criteria_pretty = AtomiaService(show_simple_props = show_simple_props)
                    parent_service_for_criteria_pretty.from_simplexml(parent_service_for_criteria.itervalues().next())
                    
                    if parent_service_for_criteria_pretty.logical_id is None:
                        parent_service_for_criteria = None
                    else:
                        parent_service_for_criteria = parent_service_for_criteria_pretty
                else:
                    raise InputError("Wrong input format of service locator for: " + str(count.keys()[0]))
                
            return parent_service_for_criteria
    else:
        return None
Пример #2
0
def find_service_by_arguments(manager,
                              account,
                              service_id,
                              path,
                              show_simple_props=False):

    if service_id is not None:
        show_service_instance = manager.get_service_by_id(account, service_id)
        if show_service_instance.has_key("GetServiceByIdResult") and len(
                show_service_instance["GetServiceByIdResult"]) == 1:
            for k in show_service_instance["GetServiceByIdResult"]:
                service_to_return = AtomiaService(
                    show_simple_props=show_simple_props)
                service_to_return.from_simplexml(k)
                return service_to_return if service_to_return.logical_id is not None else None

    elif path is not None:
        show_service_locator = json.loads(path)
        if len(show_service_locator) > 0:
            parent_service_for_criteria = None
            for count in show_service_locator:
                if isinstance(count.values()[0],
                              dict) or count.values()[0] == '':
                    service_search_criteria_list = []
                    search_properties = []
                    if parent_service_for_criteria is not None:
                        tmp_ssc = AtomiaServiceSearchCriteria(
                            str(count.keys()[0]), '',
                            parent_service_for_criteria)
                    else:
                        tmp_ssc = AtomiaServiceSearchCriteria(
                            str(count.keys()[0]), '')

                    service_search_criteria_list.append(
                        tmp_ssc.to_xml_friendly_object(
                            'atom:ServiceSearchCriteria',
                            'ServiceSearchCriteria'))
                    if isinstance(count.values()[0], dict):
                        for propk in count.values()[0]:
                            tmp_property = AtomiaServiceSearchCriteriaProperty(
                                str(propk), str(count.values()[0][propk]))
                            search_properties.append(
                                tmp_property.to_xml_friendly_object(
                                    'arr:KeyValueOfstringstring',
                                    'KeyValueOfstringstring'))
                    test = manager.find_services_by_path_with_paging(
                        service_search_criteria_list,
                        account,
                        search_properties=search_properties)
                    if test.itervalues(
                    ).next() is not None and test.itervalues().next().children(
                    ) is not None and len(
                            test.itervalues().next().children()) > 0:
                        for k in test.itervalues().next().children():
                            parent_service_for_criteria = AtomiaService(
                                show_simple_props=show_simple_props)
                            parent_service_for_criteria.from_simplexml(k)
                            break
                    else:
                        parent_service_for_criteria = None
                        break

                elif count.values()[0] is not None:
                    parent_service_for_criteria = manager.get_service_by_id(
                        account, str(count.values()[0]))
                    parent_service_for_criteria_pretty = AtomiaService(
                        show_simple_props=show_simple_props)
                    parent_service_for_criteria_pretty.from_simplexml(
                        parent_service_for_criteria.itervalues().next())

                    if parent_service_for_criteria_pretty.logical_id is None:
                        parent_service_for_criteria = None
                    else:
                        parent_service_for_criteria = parent_service_for_criteria_pretty
                else:
                    raise InputError(
                        "Wrong input format of service locator for: " +
                        str(count.keys()[0]))

            return parent_service_for_criteria
    else:
        return None
Пример #3
0
def service_find(args, manager):
    
    if args.query is not None:
        find_options = json.loads(args.query)
        if isinstance(find_options, dict):
            if find_options.has_key('name'):
                service_name = find_options['name']
            else:
                raise InputError("find_options argument must contain key name")
            
            relative_path = find_options['path'] if find_options.has_key('path') else ''
            result_page = find_options['page'] if find_options.has_key('page') else '0'
            result_count = find_options['count'] if find_options.has_key('count') else '100'
            
            if find_options.has_key('properties'):
                if isinstance(find_options['properties'], dict):
                    service_properties = find_options['properties']
                else:
                    raise InputError("Invalid format of the properties key")
            else:
                service_properties = None
        else:
            raise InputError("Invalid format of query argument.")
    else:
        raise InputError("query is required argument for this action.")
    
    parent_service = find_service_by_arguments(manager, args.account, args.parent, args.path, args.simpleProps)
    
    service_search_criteria_list = []
    search_properties = []
    if parent_service is not None:
        tmp_ssc = AtomiaServiceSearchCriteria(service_name, relative_path, parent_service)
    else:
        tmp_ssc = AtomiaServiceSearchCriteria(service_name, relative_path)
    
    service_search_criteria_list.append(tmp_ssc.to_xml_friendly_object('atom:ServiceSearchCriteria', 'ServiceSearchCriteria'))
    
    if service_properties is not None:
        for propk in service_properties:
            tmp_property = AtomiaServiceSearchCriteriaProperty(str(propk), str(service_properties[propk]))
            search_properties.append(tmp_property.to_xml_friendly_object('arr:KeyValueOfstringstring', 'KeyValueOfstringstring'))
    
    find_action_res = manager.find_services_by_path_with_paging(service_search_criteria_list, args.account, search_properties=search_properties, page_number=result_page, page_size = result_count)
    
    find_result_list = []
    if find_action_res.itervalues().next() is not None and find_action_res.itervalues().next().children() is not None:
        
        for k in find_action_res.itervalues().next().children():
            find_action_result = AtomiaService(show_simple_props = args.simpleProps)
            find_action_result.from_simplexml(k)
            find_result_list.append(find_action_result.to_print_friendly(False, True))
        
        result = json_repr(find_result_list)
        
        ''' filter results '''
        if args.filter is not None:
            import jsonpath
            result = jsonpath.jsonpath(json.loads(result), args.filter)
            
            if args.first is True:
                if result:
                    result = result[0]
                    print json_repr(result).strip('"')
                else:
                    print ""
            else: 
                print json_repr(result)
        else:
            print result
    ''' else:
        raise Exception("Could not find service: " + service_name) '''
    return find_result_list
Пример #4
0
def service_find(args, manager):

    if args.query is not None:
        find_options = json.loads(args.query)
        if isinstance(find_options, dict):
            if find_options.has_key('name'):
                service_name = find_options['name']
            else:
                raise InputError("find_options argument must contain key name")

            relative_path = find_options['path'] if find_options.has_key(
                'path') else ''
            result_page = find_options['page'] if find_options.has_key(
                'page') else '0'
            result_count = find_options['count'] if find_options.has_key(
                'count') else '100'

            if find_options.has_key('properties'):
                if isinstance(find_options['properties'], dict):
                    service_properties = find_options['properties']
                else:
                    raise InputError("Invalid format of the properties key")
            else:
                service_properties = None
        else:
            raise InputError("Invalid format of query argument.")
    else:
        raise InputError("query is required argument for this action.")

    parent_service = find_service_by_arguments(manager, args.account,
                                               args.parent, args.path,
                                               args.simpleProps)

    service_search_criteria_list = []
    search_properties = []
    if parent_service is not None:
        tmp_ssc = AtomiaServiceSearchCriteria(service_name, relative_path,
                                              parent_service)
    else:
        tmp_ssc = AtomiaServiceSearchCriteria(service_name, relative_path)

    service_search_criteria_list.append(
        tmp_ssc.to_xml_friendly_object('atom:ServiceSearchCriteria',
                                       'ServiceSearchCriteria'))

    if service_properties is not None:
        for propk in service_properties:
            tmp_property = AtomiaServiceSearchCriteriaProperty(
                str(propk), str(service_properties[propk]))
            search_properties.append(
                tmp_property.to_xml_friendly_object(
                    'arr:KeyValueOfstringstring', 'KeyValueOfstringstring'))

    find_action_res = manager.find_services_by_path_with_paging(
        service_search_criteria_list,
        args.account,
        search_properties=search_properties,
        page_number=result_page,
        page_size=result_count)

    find_result_list = []
    if find_action_res.itervalues().next(
    ) is not None and find_action_res.itervalues().next().children(
    ) is not None:

        for k in find_action_res.itervalues().next().children():
            find_action_result = AtomiaService(
                show_simple_props=args.simpleProps)
            find_action_result.from_simplexml(k)
            find_result_list.append(
                find_action_result.to_print_friendly(False, True))

        result = json_repr(find_result_list)
        ''' filter results '''
        if args.filter is not None:
            import jsonpath
            result = jsonpath.jsonpath(json.loads(result), args.filter)

            if args.first is True:
                if result:
                    result = result[0]
                    print json_repr(result).strip('"')
                else:
                    print ""
            else:
                print json_repr(result)
        else:
            print result
    ''' else:
        raise Exception("Could not find service: " + service_name) '''
    return find_result_list