Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / cyclades.py @ 6add54fd

History | View | Annotate | Download (5.2 kB)

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

    
34
from .compute import ComputeClient
35
from .utils import path4url
36

    
37

    
38
class CycladesClient(ComputeClient):
39
    """GRNet Cyclades API client"""
40
    
41
    def start_server(self, server_id):
42
        """Submit a startup request for a server specified by id"""
43
        
44
        path = path4url('servers', server_id, action)#'/servers/%s/action' % (server_id,)
45
        req = {'start': {}}
46
        self.post(path, json=req, success=202)
47
    
48
    def shutdown_server(self, server_id):
49
        """Submit a shutdown request for a server specified by id"""
50
        
51
        path = path4url('servers', server_id, 'action')#'/servers/%s/action' % (server_id,)
52
        req = {'shutdown': {}}
53
        self.post(path, json=req, success=202)
54
    
55
    def get_server_console(self, server_id):
56
        """Get a VNC connection to the console of a server specified by id"""
57
        
58
        path = path4url('servers', server_id, 'action')#'/servers/%s/action' % (server_id,)
59
        req = {'console': {'type': 'vnc'}}
60
        r = self.post(path, json=req, success=200)
61
        return r.json['console']
62
    
63
    def set_firewall_profile(self, server_id, profile):
64
        """Set the firewall profile for the public interface of a server
65

66
        The server is specified by id, the profile argument
67
        is one of (ENABLED, DISABLED, PROTECTED).
68
        """
69
        path = path4url('servers', server_id, 'action')#'/servers/%s/action' % (server_id,)
70
        req = {'firewallProfile': {'profile': profile}}
71
        self.post(path, json=req, success=202)
72
    
73
    def list_server_addresses(self, server_id, network=None):
74
        path = path4url('servers', server_id, 'ips')#'/servers/%s/ips' % (server_id,)
75
        if network:
76
            path += path4url(network)#'/%s' % network
77
        r = self.get(path, success=200)
78
        if network:
79
            return [r.json['network']]
80
        else:
81
            return r.json['addresses']['values']
82
    
83
    def get_server_stats(self, server_id):
84
        path = path4url('servers', server_id, 'stats')#'/servers/%s/stats' % (server_id,)
85
        r = self.get(path, success=200)
86
        return r.json['stats']
87
    
88
    def list_networks(self, detail=False):
89
        path = path4url('networks', 'detail') if detail else path4url('networks')
90
        print('ready to get network list with this req: '+path)
91
        r = self.get(path, success=200)
92
        return r.json['networks']['values']
93

    
94
    def create_network(self, name):
95
        req = {'network': {'name': name}}
96
        r = self.post(path4url('networks'), json=req, success=202)
97
        return r.json['network']
98

    
99
    def get_network_details(self, network_id):
100
        path = path4url('networks', network_id)#'/networks/%s' % (network_id,)
101
        r = self.get(path, success=200)
102
        print(unicode(r.json))
103
        return r.json['network']
104

    
105
    def update_network_name(self, network_id, new_name):
106
        path = path4url('networks', network_id)#'/networks/%s' % (network_id,)
107
        req = {'network': {'name': new_name}}
108
        self.put(path, json=req, success=204)
109

    
110
    def delete_network(self, network_id):
111
        path = path4url('networks', network_id)#'/networks/%s' % (network_id,)
112
        self.delete(path, success=204)
113

    
114
    def connect_server(self, server_id, network_id):
115
        path = path4url('networks', network_id, 'action')
116
        req = {'add': {'serverRef': server_id}}
117
        self.post(path, json=req, success=202)
118

    
119
    def disconnect_server(self, server_id, network_id):
120
        matched_nets = [net for net in self.list_server_addresses(server_id) if net['network_id'] == network_id]
121
        path = path4url('networks', network_id, 'action')
122
        for nic in matched_nets:
123
            req = {'remove': {'attachment': unicode(nic['id'])}}
124
            self.post(path, json=req, success=202)