rest_api.py 4.58 KB
Newer Older
1
# Copyright 2012-2015 GRNET S.A. All rights reserved.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from kamaki.clients.compute import ComputeClient
35
from kamaki.clients.blockstorage import BlockStorageClient
36
37
38
from kamaki.clients.utils import path4url


39
class CycladesComputeRestClient(ComputeClient):
40
    """Synnefo Cyclades REST API Client"""
41

42
    def servers_stats_get(self, server_id, **kwargs):
43
        """GET endpoint_url/servers/<server_id>/stats"""
44
        path = path4url('servers', server_id, 'stats')
45
        return self.get(path, success=200, **kwargs)
46
47

    def servers_diagnostics_get(self, server_id, **kwargs):
48
        """GET endpoint_url/servers/<server_id>/diagnostics"""
49
50
        path = path4url('servers', server_id, 'diagnostics')
        return self.get(path, success=200, **kwargs)
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    def volume_attachment_get(self, server_id, attachment_id=None, **kwargs):
        path_args = ['servers', server_id, 'os-volume_attachments']
        path_args += [attachment_id, ] if attachment_id else []
        path = path4url(*path_args)
        success = kwargs.pop('success', 200)
        return self.get(path, success=success, **kwargs)

    def volume_attachment_post(self, server_id, volume_id, **kwargs):
        path = path4url('servers', server_id, 'os-volume_attachments')
        data = dict(volumeAttachment=dict(volumeId=volume_id))
        success = kwargs.pop('success', 202)
        return self.post(path, json=data, success=success, **kwargs)

    def volume_attachment_delete(self, server_id, attachment_id, **kwargs):
        path = path4url(
            'servers', server_id, 'os-volume_attachments', attachment_id)
        success = kwargs.pop('success', 202)
        return self.delete(path, success=success, **kwargs)

71
72
73

#  Backwards compatibility
CycladesRestClient = CycladesComputeRestClient
74
75
76
77
78
79


class CycladesBlockStorageRestClient(BlockStorageClient):
    """Synnefo Cyclades Block Storage REST API Client"""

    def volumes_post(
80
81
            self, size, display_name,
            server_id=None,
82
83
84
85
86
87
88
89
90
            display_description=None,
            snapshot_id=None,
            imageRef=None,
            volume_type=None,
            metadata=None,
            project=None,
            success=202,
            **kwargs):
        path = path4url('volumes')
91
92
93
        volume = dict(size=int(size), display_name=display_name)
        if server_id is not None:
            volume['server_id'] = server_id
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        if display_description is not None:
            volume['display_description'] = display_description
        if snapshot_id is not None:
            volume['snapshot_id'] = snapshot_id
        if imageRef is not None:
            volume['imageRef'] = imageRef
        if volume_type is not None:
            volume['volume_type'] = volume_type
        if metadata is not None:
            volume['metadata'] = metadata
        if project is not None:
            volume['project'] = project
        return self.post(
            path, json=dict(volume=volume), success=success, **kwargs)

    def volumes_action_post(self, volume_id, json_data, success=200, **kwargs):
        path = path4url('volumes', volume_id, 'action')
        return self.post(path, json=json_data, success=success, **kwargs)