~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to nova/api/openstack/servers.py

  • Committer: Soren Hansen
  • Date: 2011-01-03 09:56:21 UTC
  • mfrom: (430.2.79 nova)
  • Revision ID: soren@linux2go.dk-20110103095621-qy398qk1uk8o7cy3
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    License for the specific language governing permissions and limitations
16
16
#    under the License.
17
17
 
 
18
import logging
 
19
import traceback
 
20
 
18
21
from webob import exc
19
22
 
20
23
from nova import exception
21
24
from nova import wsgi
 
25
from nova.api.openstack import common
22
26
from nova.api.openstack import faults
23
27
from nova.auth import manager as auth_manager
24
28
from nova.compute import api as compute_api
27
31
import nova.api.openstack
28
32
 
29
33
 
30
 
def _entity_list(entities):
31
 
    """ Coerces a list of servers into proper dictionary format """
32
 
    return dict(servers=entities)
33
 
 
34
 
 
35
 
def _entity_detail(inst):
36
 
    """ Maps everything to Rackspace-like attributes for return"""
 
34
LOG = logging.getLogger('server')
 
35
LOG.setLevel(logging.DEBUG)
 
36
 
 
37
 
 
38
def _translate_detail_keys(inst):
 
39
    """ Coerces into dictionary format, mapping everything to Rackspace-like
 
40
    attributes for return"""
37
41
    power_mapping = {
 
42
        None: 'build',
38
43
        power_state.NOSTATE: 'build',
39
44
        power_state.RUNNING: 'active',
40
45
        power_state.BLOCKED: 'active',
41
 
        power_state.PAUSED: 'suspended',
 
46
        power_state.SUSPENDED: 'suspended',
 
47
        power_state.PAUSED: 'error',
42
48
        power_state.SHUTDOWN: 'active',
43
49
        power_state.SHUTOFF: 'active',
44
50
        power_state.CRASHED: 'error'}
58
64
    return dict(server=inst_dict)
59
65
 
60
66
 
61
 
def _entity_inst(inst):
62
 
    """ Filters all model attributes save for id and name """
 
67
def _translate_keys(inst):
 
68
    """ Coerces into dictionary format, excluding all model attributes
 
69
    save for id and name """
63
70
    return dict(server=dict(id=inst['internal_id'], name=inst['display_name']))
64
71
 
65
72
 
78
85
 
79
86
    def index(self, req):
80
87
        """ Returns a list of server names and ids for a given user """
81
 
        return self._items(req, entity_maker=_entity_inst)
 
88
        return self._items(req, entity_maker=_translate_keys)
82
89
 
83
90
    def detail(self, req):
84
91
        """ Returns a list of server details for a given user """
85
 
        return self._items(req, entity_maker=_entity_detail)
 
92
        return self._items(req, entity_maker=_translate_detail_keys)
86
93
 
87
94
    def _items(self, req, entity_maker):
88
95
        """Returns a list of servers for a given user.
89
96
 
90
 
        entity_maker - either _entity_detail or _entity_inst
 
97
        entity_maker - either _translate_detail_keys or _translate_keys
91
98
        """
92
99
        instance_list = self.compute_api.get_instances(
93
100
            req.environ['nova.context'])
94
 
        limited_list = nova.api.openstack.limited(instance_list, req)
 
101
        limited_list = common.limited(instance_list, req)
95
102
        res = [entity_maker(inst)['server'] for inst in limited_list]
96
 
        return _entity_list(res)
 
103
        return dict(servers=res)
97
104
 
98
105
    def show(self, req, id):
99
106
        """ Returns server details by server id """
100
107
        try:
101
108
            instance = self.compute_api.get_instance(
102
109
                req.environ['nova.context'], int(id))
103
 
            return _entity_detail(instance)
 
110
            return _translate_detail_keys(instance)
104
111
        except exception.NotFound:
105
112
            return faults.Fault(exc.HTTPNotFound())
106
113
 
129
136
            description=env['server']['name'],
130
137
            key_name=key_pair['name'],
131
138
            key_data=key_pair['public_key'])
132
 
        return _entity_inst(instances[0])
 
139
        return _translate_keys(instances[0])
133
140
 
134
141
    def update(self, req, id):
135
142
        """ Updates the server name or password """
144
151
            update_dict['display_name'] = inst_dict['server']['name']
145
152
 
146
153
        try:
147
 
            self.compute_api.update_instance(req.environ['nova.context'],
148
 
                                             instance['id'],
 
154
            ctxt = req.environ['nova.context']
 
155
            self.compute_api.update_instance(ctxt,
 
156
                                             id,
149
157
                                             **update_dict)
150
158
        except exception.NotFound:
151
159
            return faults.Fault(exc.HTTPNotFound())
166
174
        except:
167
175
            return faults.Fault(exc.HTTPUnprocessableEntity())
168
176
        return exc.HTTPAccepted()
 
177
 
 
178
    def pause(self, req, id):
 
179
        """ Permit Admins to Pause the server. """
 
180
        ctxt = req.environ['nova.context']
 
181
        try:
 
182
            self.compute_api.pause(ctxt, id)
 
183
        except:
 
184
            readable = traceback.format_exc()
 
185
            logging.error(_("Compute.api::pause %s"), readable)
 
186
            return faults.Fault(exc.HTTPUnprocessableEntity())
 
187
        return exc.HTTPAccepted()
 
188
 
 
189
    def unpause(self, req, id):
 
190
        """ Permit Admins to Unpause the server. """
 
191
        ctxt = req.environ['nova.context']
 
192
        try:
 
193
            self.compute_api.unpause(ctxt, id)
 
194
        except:
 
195
            readable = traceback.format_exc()
 
196
            logging.error(_("Compute.api::unpause %s"), readable)
 
197
            return faults.Fault(exc.HTTPUnprocessableEntity())
 
198
        return exc.HTTPAccepted()
 
199
 
 
200
    def suspend(self, req, id):
 
201
        """permit admins to suspend the server"""
 
202
        context = req.environ['nova.context']
 
203
        try:
 
204
            self.compute_api.suspend(context, id)
 
205
        except:
 
206
            readable = traceback.format_exc()
 
207
            logging.error(_("compute.api::suspend %s"), readable)
 
208
            return faults.Fault(exc.HTTPUnprocessableEntity())
 
209
        return exc.HTTPAccepted()
 
210
 
 
211
    def resume(self, req, id):
 
212
        """permit admins to resume the server from suspend"""
 
213
        context = req.environ['nova.context']
 
214
        try:
 
215
            self.compute_api.resume(context, id)
 
216
        except:
 
217
            readable = traceback.format_exc()
 
218
            logging.error(_("compute.api::resume %s"), readable)
 
219
            return faults.Fault(exc.HTTPUnprocessableEntity())
 
220
        return exc.HTTPAccepted()
 
221
 
 
222
    def diagnostics(self, req, id):
 
223
        """Permit Admins to retrieve server diagnostics."""
 
224
        ctxt = req.environ["nova.context"]
 
225
        return self.compute_api.get_diagnostics(ctxt, id)
 
226
 
 
227
    def actions(self, req, id):
 
228
        """Permit Admins to retrieve server actions."""
 
229
        ctxt = req.environ["nova.context"]
 
230
        return self.compute_api.get_actions(ctxt, id)