Exemplo n.º 1
0
 def test_fetch_filter_context(self):
     """ Test filter context directly, in v6.1 400 is returned if the filter_context is
     not a valid type. Can be of valid entry point types or other documented filter_context
     types
     """
     result = fetch_href_by_name(name='123test123', filter_context='bogus')
     self.assertIsNotNone(result.msg)
Exemplo n.º 2
0
 def __get__(self, instance, cls=None):
     # Does the instance already have meta data
     if instance is not None and instance._meta:
         return instance._meta.href
     else:
         if hasattr(cls, 'typeof'):
             if instance is not None:
                 element = fetch_href_by_name(
                     instance.name, filter_context=instance.typeof)
                 if element.json:
                     instance._meta = Meta(**element.json[0])
                     return instance._meta.href
                 raise ElementNotFound(
                     'Cannot find specified element: {}, type: {}'.format(
                         unicode_to_bytes(instance.name), instance.typeof))
             else:
                 try:
                     element = fetch_entry_point(cls.typeof)
                 except UnsupportedEntryPoint as e:
                     raise ElementNotFound(e)
                 return element
         else:
             raise ElementNotFound(
                 'This class does not have the required attribute '
                 'and cannot be referenced directly, type: {}'.format(
                     instance))
Exemplo n.º 3
0
def element_href(name):
    """ Get specified element href by element name 
    
    :param name: name of element
    :return: string href location of object, else None 
    """
    if name:
        element = fetch_href_by_name(name)
        if element.href:
            return element.href
Exemplo n.º 4
0
def element_href_use_wildcard(name):
    """ Get element href using a wildcard rather than matching only on the name field
    This will likely return multiple results
    
    :param name: name of element
    :return: list of matched elements
    """
    if name:
        element = fetch_href_by_name(name, exact_match=False)
        return element.json
Exemplo n.º 5
0
def element_href(name):
    """ Get specified element href by element name 
    
    :param name: name of element
    :return: string href location of object, else None 
    """
    if name:
        element = fetch_href_by_name(name)
        if element.href:
            return element.href
Exemplo n.º 6
0
def element_href_use_wildcard(name):
    """ Get element href using a wildcard rather than matching only on the name field
    This will likely return multiple results
    
    :param name: name of element
    :return: list of matched elements, else None
    """
    if name:
        element = fetch_href_by_name(name, exact_match=False)
        if element.json:
            return element.json  #list
Exemplo n.º 7
0
def element_info_as_json_with_filter(name, _filter):
    """
    Top level json meta data (href, name, type) for element
    
    :param str name: name of element
    :param str _filter: filter of entry point
    :return: list dict with metadata, otherwise None
    """
    if name and _filter:
        element = fetch_href_by_name(name, filter_context=_filter)
        if element.json:
            return element.json
Exemplo n.º 8
0
def element_info_as_json(name):
    """ Get specified element full json based on search query
    This is the base level search that returns basic object info
    including the href to find the full data
    
    :param name: name of element
    :return: json representation of top level element (multiple attributes), else None
    """   
    if name:
        element = fetch_href_by_name(name)
        if element.json:
            return element.json.pop()
Exemplo n.º 9
0
def element_href_use_filter(name, _filter):
    """ Get element href using filter 
    
    Filter should be a valid entry point value, ie host, router, network, single_fw, etc
    
    :param name: name of element
    :param _filter: filter type, unknown filter will result in no matches
    :return: element href (if found), else None
    """
    if name and _filter:
        element = fetch_href_by_name(name, filter_context=_filter)
        if element.json:
            return element.json.pop().get('href')
Exemplo n.º 10
0
def element_as_smcresult_use_filter(name, _filter):
    """ Return SMCResult object and use search filter to
    find object
    
    :param name: name of element to find
    :param _filter: filter to use, i.e. tcp_service, host, etc
    :return: :py:class:`smc.api.web.SMCResult`
    """
    if name:
        element = fetch_href_by_name(name, filter_context=_filter)
        if element.msg:
            return element
        if element.json:
            return element_by_href_as_smcresult(element.json.pop().get('href'))
Exemplo n.º 11
0
def element_href_use_filter(name, _filter):
    """ Get element href using filter 
    
    Filter should be a valid entry point value, ie host, router, network, single_fw, etc
    
    :param name: name of element
    :param _filter: filter type, unknown filter will result in no matches
    :return: element href (if found), else None
    """
    if name and _filter:
        #element = fetch_by_name_and_filter(name, _filter)
        element = fetch_href_by_name(name, filter_context=_filter)
        if element.json:
            return element.json.pop().get('href')
Exemplo n.º 12
0
def element_info_as_json(name):
    """ 
    Get specified element META data based on search query
    This is the base level search that returns basic object info
    with the following attributes:
    
    * href: link to element
    * name: name of element
    * type: type of element
    
    :param str name: name of element
    :return: list dict with meta (href, name, type) if found, otherwise None
    """
    if name:
        element = fetch_href_by_name(name)
        if element.json:
            return element.json
Exemplo n.º 13
0
def generic_list_builder(typeof, name=None, exact_match=True, klazz=None):
    """
    Build the query to SMC based on parameters
    
    :param list name: Name of host object (optional)
    :param exact_match: Do exact match against name field (default True)
    :return: list :py:class:`smc.elements.collections.Element`
    """
    if not klazz:
        klazz = SMCElement
    result=[]
    if not name:
        lst = fetch_json_by_href(
                    session.cache.get_entry_href(typeof)).json
        if lst:
            for item in lst:
                result.append(klazz(**item))
    else: #Filter provided
        for element in name:
            for item in fetch_href_by_name(element, 
                                           filter_context=typeof, 
                                           exact_match=exact_match).json:
                result.append(klazz(**item))
    return result