~jk0/nova/diagnostics-per-instance

« back to all changes in this revision

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

  • Committer: Josh Kearney
  • Date: 2010-12-29 17:35:49 UTC
  • mfrom: (466.2.36 nova)
  • Revision ID: josh.kearney@rackspace.com-20101229173549-ql7crx0ujxu6v9i7
Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
LOG.setLevel(logging.DEBUG)
36
36
 
37
37
 
38
 
def _entity_list(entities):
39
 
    """ Coerces a list of servers into proper dictionary format """
40
 
    return dict(servers=entities)
41
 
 
42
 
 
43
 
def _entity_detail(inst):
44
 
    """ Maps everything to Rackspace-like attributes for return"""
 
38
def _translate_detail_keys(inst):
 
39
    """ Coerces into dictionary format, mapping everything to Rackspace-like
 
40
    attributes for return"""
45
41
    power_mapping = {
 
42
        None: 'build',
46
43
        power_state.NOSTATE: 'build',
47
44
        power_state.RUNNING: 'active',
48
45
        power_state.BLOCKED: 'active',
67
64
    return dict(server=inst_dict)
68
65
 
69
66
 
70
 
def _entity_inst(inst):
71
 
    """ 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 """
72
70
    return dict(server=dict(id=inst['internal_id'], name=inst['display_name']))
73
71
 
74
72
 
87
85
 
88
86
    def index(self, req):
89
87
        """ Returns a list of server names and ids for a given user """
90
 
        return self._items(req, entity_maker=_entity_inst)
 
88
        return self._items(req, entity_maker=_translate_keys)
91
89
 
92
90
    def detail(self, req):
93
91
        """ Returns a list of server details for a given user """
94
 
        return self._items(req, entity_maker=_entity_detail)
 
92
        return self._items(req, entity_maker=_translate_detail_keys)
95
93
 
96
94
    def _items(self, req, entity_maker):
97
95
        """Returns a list of servers for a given user.
98
96
 
99
 
        entity_maker - either _entity_detail or _entity_inst
 
97
        entity_maker - either _translate_detail_keys or _translate_keys
100
98
        """
101
99
        instance_list = self.compute_api.get_instances(
102
100
            req.environ['nova.context'])
103
101
        limited_list = common.limited(instance_list, req)
104
102
        res = [entity_maker(inst)['server'] for inst in limited_list]
105
 
        return _entity_list(res)
 
103
        return dict(servers=res)
106
104
 
107
105
    def show(self, req, id):
108
106
        """ Returns server details by server id """
109
107
        try:
110
108
            instance = self.compute_api.get_instance(
111
109
                req.environ['nova.context'], int(id))
112
 
            return _entity_detail(instance)
 
110
            return _translate_detail_keys(instance)
113
111
        except exception.NotFound:
114
112
            return faults.Fault(exc.HTTPNotFound())
115
113
 
138
136
            description=env['server']['name'],
139
137
            key_name=key_pair['name'],
140
138
            key_data=key_pair['public_key'])
141
 
        return _entity_inst(instances[0])
 
139
        return _translate_keys(instances[0])
142
140
 
143
141
    def update(self, req, id):
144
142
        """ Updates the server name or password """
153
151
            update_dict['display_name'] = inst_dict['server']['name']
154
152
 
155
153
        try:
156
 
            self.compute_api.update_instance(req.environ['nova.context'],
157
 
                                             instance['id'],
 
154
            ctxt = req.environ['nova.context']
 
155
            self.compute_api.update_instance(ctxt,
 
156
                                             id,
158
157
                                             **update_dict)
159
158
        except exception.NotFound:
160
159
            return faults.Fault(exc.HTTPNotFound())