Пример #1
0
    def __getitem__(self, attr):
        """
        This method is overridden so that contained elements can be accessed
        using their 'xml names' - e.g. user['first-name']. The implementation
        just calls __getattr__ internally.

        See doc for __getattr__ for more details.
        """

        return self.__getattr__(xmlutil.unmake_xml_name(attr))
Пример #2
0
    def __getitem__(self, attr):
        """
        This method is overridden so that contained elements can be accessed
        using their 'xml names' - e.g. user['first-name']. The implementation
        just calls __getattr__ internally.

        See doc for __getattr__ for more details.
        """

        return self.__getattr__(xmlutil.unmake_xml_name(attr))
Пример #3
0
def fetch_resource(rest_end_point, href):
    """This is the method you should use to create a Resource instance if you
    have the href for it. It gets the current state of the resource with the
    given *href*. Once the current state is fetched from the Space server, this
    method creates a new instance of ``jnpr.space.resource.Resource`` with this
    state and returns it.

    :param rest_end_point: A *Space* object encapsulating the Junos
        Space cluster which contains this resource.
    :type rest_end_point: jnpr.space.rest.Space

    :param str href: The href of the resource that needs to be fetched.

    :returns: A new instance of jnpr.space.resource.Resource

    :raises: ``jnpr.space.rest.RestException`` if the GET request results in an
        error response. The exception's ``response`` attribute will have the
        full response from Space.
    """
    response = rest_end_point.get(href)
    if response.status_code != 200:
        raise rest.RestException("GET failed on %s" % href, response)

    media_type = response.headers['content-type']
    end = media_type.index('+')
    parts = media_type[:end].split('.')
    app_ = parts[len(parts)-3]
    service_ = parts[len(parts)-2]
    type_ = parts[len(parts)-1]

    if app_ == 'space':
        type_name = xmlutil.unmake_xml_name('.'.join([service_, type_]))
    else:
        type_name = xmlutil.unmake_xml_name('.'.join([app_, service_, type_]))

    xml_data = xmlutil.get_xml_obj_from_response(response)
    return make_resource(type_name, rest_end_point, xml_data)