Exemplo n.º 1
0
    def modify_attribute(self, **kwargs):
        """
        Modify the attribute by key / value pair.
        Add append_lists=True kwarg if dict leaf is a list
        and you want to append, default: replace

        :param dict kwargs: key=value pair to change
        :param bool append_lists: if change is a list, append or overwrite
        :raises ElementNotFound: cannot find element specified
        :raises ModificationFailed, UpdateElementFailed: failure applying
            change with reason
        :return: href of the element modified
        :rtype: str
        """
        if self.data.get('system', False):
            raise ModificationFailed('Cannot modify system element: %s' %
                                     self.name)

        params = {'href': self.href, 'etag': self.etag}
        append_lists = kwargs.pop('append_lists', False)
        merge_dicts(self.data, kwargs, append_lists)
        params.update(json=self.data)
        del self.data

        request = SMCRequest(**params)
        request.exception = UpdateElementFailed
        return request.update().href
Exemplo n.º 2
0
    def delete(self):
        """
        Delete the element

        :raises DeleteElementFailed: possible dependencies, record locked, etc
        :return: None
        """
        request = SMCRequest(href=self.href, headers={'if-match': self.etag})
        request.exception = DeleteElementFailed
        request.delete()
Exemplo n.º 3
0
def LoadElement(href, only_etag=False):
    """
    Return an instance of a element as a ElementCache dict
    used as a cache.
    
    :rtype ElementCache
    """
    request = SMCRequest(href=href)
    request.exception = FetchElementFailed
    result = request.read()
    if only_etag:
        return result.etag
    return ElementCache(result.json, etag=result.etag)
Exemplo n.º 4
0
    def make_request(self, *exception, **kwargs):
        raw_result = kwargs.pop("raw_result", False)
        method = kwargs.pop("method", "read")
        ex = exception[0] if exception else ActionCommandFailed
        if "resource" in kwargs:
            try:
                kwargs.update(href=self.data.get_link(kwargs.pop("resource")))
            except ResourceNotFound as e:
                raise ex(e)

        request = SMCRequest(**kwargs)
        request.exception = ex
        result = getattr(request, method)()
        if raw_result:
            return result
        return result.json
Exemplo n.º 5
0
 def make_request(self, *exception, **kwargs):
     raw_result = kwargs.pop('raw_result', False)
     method = kwargs.pop('method', 'read')
     ex = exception[0] if exception else ActionCommandFailed
     if 'resource' in kwargs:
         try:
             kwargs.update(href=self.data.get_link(
                 kwargs.pop('resource')))
         except ResourceNotFound as e:
             raise ex(e)
     
     request = SMCRequest(**kwargs)
     request.exception = ex
     result = getattr(request, method)()
     if raw_result:
         return result
     return result.json
Exemplo n.º 6
0
    def update(self, *exception, **kwargs):
        """
        Update the existing element and clear the instance cache.
        Removing the cache will ensure subsequent calls requiring element
        attributes will force a new fetch to obtain the latest copy.
        
        Calling update() with no args will assume the element has already
        been modified directly and the data cache will be used to update.
        You can also override the following attributes: href, etag and
        json. If json is sent, it is expected to the be a complete payload
        to satisfy the update.
        
        For kwargs, if attribute values are a list, you can pass
        'append_lists=True' to add to an existing list, otherwise overwrite
        (default: overwrite)

        .. seealso:: To see different ways to utilize this method for updating,
            see: :ref:`update-elements-label`.

        :param exception: pass a custom exception to throw if failure
        :param kwargs: optional kwargs to update request data to server.
        :raises ModificationFailed: raised if element is tagged as System element
        :raises UpdateElementFailed: failed to update element with reason
        :return: href of the element modified
        :rtype: str
        """
        if self.data.get('system', False):
            raise ModificationFailed('Cannot modify system element: %s' %
                                     self.name)

        if not exception:
            exception = UpdateElementFailed
        else:
            exception = exception[0]

        params = {'href': self.href, 'etag': self.etag}

        if 'href' in kwargs:
            params.update(href=kwargs.pop('href'))

        if 'etag' in kwargs:
            params.update(etag=kwargs.pop('etag'))

        name = kwargs.get('name', None)

        json = kwargs.pop('json') if 'json' in kwargs else self.data
        del self.data  # Delete the cache before processing attributes

        # If kwarg settings are provided AND instance variables, kwargs
        # will overwrite collected instance attributes with the same name.
        if kwargs:
            append_lists = kwargs.pop('append_lists', False)
            merge_dicts(json, kwargs, append_lists)

        params.update(json=json)

        request = SMCRequest(**params)
        request.exception = exception
        result = request.update()

        if name:  # Reset instance name
            self._meta = Meta(name=name, href=self.href, type=self._meta.type)
            self._name = name

        return result.href
Exemplo n.º 7
0
    def update(self, *exception, **kwargs):
        """
        Update the existing element and clear the instance cache.
        Removing the cache will ensure subsequent calls requiring element
        attributes will force a new fetch to obtain the latest copy.

        If attributes are set via kwargs and instance attributes are also
        set, instance attributes are updated first, then kwargs. Typically
        you will want to use either instance attributes OR kwargs, not both.
        
        Calling update() with no args will assume the element has already
        been modified directly and the data cache will be used to update.
        You can also override the following attributes: href, etag and
        json. If json is sent, it is expected to the be a complete payload
        to satisfy the update.
        
        For kwargs, if attribute values are a list, you can pass
        'append_lists=True' to add to an existing list, otherwise overwrite
        (default: overwrite)

        If using instance attributes, the attribute value can be a callable
        and it will be evaluated and merged.

        .. seealso:: To see different ways to utilize this method for updating,
            see: :ref:`update-elements-label`.

        :param exception: pass a custom exception to throw if failure
        :param kwargs: optional kwargs to update request data to server.
        :return: href of the element modified
        :rtype: str
        """
        if not exception:
            exception = UpdateElementFailed
        else:
            exception = exception[0]

        params = {
            'href': self.href,
            'etag': self.etag
        }

        if 'href' in kwargs:
            params.update(href=kwargs.pop('href'))

        if 'etag' in kwargs:
            params.update(etag=kwargs.pop('etag'))

        name = kwargs.get('name', None)
        
        json = kwargs.pop('json') if 'json' in kwargs else self.data
        del self.data       # Delete the cache before processing attributes
        
        instance_attr = {k: v() if callable(v) else v
                         for k, v in vars(self).items()
                         if not k.startswith('_')}
        
        if instance_attr:
            json.update(**instance_attr)

        # If kwarg settings are provided AND instance variables, kwargs
        # will overwrite collected instance attributes with the same name.
        if kwargs:
            append_lists = kwargs.pop('append_lists', False)
            merge_dicts(json, kwargs, append_lists)

        params.update(json=json)
        
        # Remove attributes from instance if previously set
        if instance_attr:
            for attr in instance_attr:
                delattr(self, attr)
        
        request = SMCRequest(**params) 
        request.exception = exception
        result = request.update()
        
        if name: # Reset instance name
            self._meta = Meta(name=name, href=self.href, type=self._meta.type)
            self._name = name
        
        return result.href