예제 #1
0
    def __init__(self, id_prefix):
        """
        @param id_prefix: a string prefix that is used in created artifacts names
        """
        self.id_prefix = id_prefix
        self.id_counter = 0

        self.c = GenericClient()
        self.c_drive = Drive()
        self.c_libdrive = LibDrive()
        self.c_guest = Server()
from cloudsigma.generic import GenericClient
from cloudsigma.resource import Websocket
from cloudsigma.errors import ClientError

ws = Websocket(timeout=None)
client = GenericClient()

print "Display Websocket activity.\nExit with ^C."

while True:
    try:
        get_action = ws.ws.recv()
        action_uri = get_action['resource_uri']
        print 'Received Action: %s' % get_action
        print 'Result:\n%s' % client.get(action_uri)
    except ClientError as e:
        print 'Error retrieving: %s' % e
from cloudsigma.generic import GenericClient
from cloudsigma.resource import Websocket
from cloudsigma.errors import ClientError, PermissionError

ws = Websocket(timeout=None)
client = GenericClient()

print("Display Websocket activity.\nExit with ^C.")

while True:
    try:
        get_action = ws.ws.recv()
        action_uri = get_action['resource_uri']
        print('Received Action: %s' % get_action)
        print('Result:\n%s' % client.get(action_uri))
    except ClientError as e:
        if e.args[0] == 404:
            print("Resource %s was deleted" % action_uri)
        else:
            print('Error retrieving: %s' % e)
    except PermissionError as e:
        print("No permissions for resource %s" % action_uri)
예제 #4
0
 def __init__(self, *args, **kwargs):
     self.c = GenericClient(*args, **kwargs)
예제 #5
0
class ResourceBase(object):
    resource_name = None

    def __init__(self, *args, **kwargs):
        self.c = GenericClient(*args, **kwargs)

    def attach_response_hook(self, func):
        self.c.response_hook = func

    def detach_response_hook(self):
        self.c.response_hook = None

    def _get_url(self):
        assert self.resource_name, 'Descendant class must set the resource_name field'
        return '/%s/' % (self.resource_name,)

    def get(self, uuid=None):
        url = self._get_url()
        if uuid is not None:
            url += uuid
        return self.c.get(url, return_list=False)

    def get_schema(self):
        url = self._get_url() + 'schema'
        return self.c.get(url)

    def get_from_url(self, url):
        return self.c.get(url, return_list=False)

    def list(self, query_params=None):
        url = self._get_url()
        _query_params = {
            'limit': 0,  # get all results, do not use pagination
        }
        if query_params:
            _query_params.update(query_params)
        return self.c.get(url, query_params=_query_params, return_list=True)

    def list_detail(self, query_params=None):
        url = self._get_url() + 'detail/'
        _query_params = {
            'limit': 0,  # get all results, do not use pagination
        }
        if query_params:
            _query_params.update(query_params)
        return self.c.get(url, query_params=_query_params, return_list=True)

    def _pepare_data(self, data):
        res_data = data
        if isinstance(data, (list, tuple)):
            res_data = {'objects': data}
        elif isinstance(data, (dict,)):
            if not data.has_key('objects'):
                res_data = {'objects': [data]}
        else:
            raise TypeError('%r is not should be of type list, tuple or dict' % data)
        return res_data

    def create(self, data):
        url = self._get_url()
        return self.c.post(url, self._pepare_data(data), return_list=False)

    def update(self, uuid, data):
        url = self._get_url() + uuid + '/'
        return self.c.put(url, data, return_list=False)

    def delete(self, uuid, query_params=None):
        url = self._get_url() + uuid
        return self.c.delete(url, query_params)

    def _action(self, uuid, action, data=None):
        if uuid is None:
            url = self._get_url() + 'action/'
        else:
            url = self._get_url() + uuid + '/action/'
        return self.c.post(url, data, query_params={'do': action}, return_list=False)