35
35
LOG.setLevel(logging.DEBUG)
38
def _entity_list(entities):
39
""" Coerces a list of servers into proper dictionary format """
40
return dict(servers=entities)
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"""
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)
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']))
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)
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)
96
94
def _items(self, req, entity_maker):
97
95
"""Returns a list of servers for a given user.
99
entity_maker - either _entity_detail or _entity_inst
97
entity_maker - either _translate_detail_keys or _translate_keys
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)
107
105
def show(self, req, id):
108
106
""" Returns server details by server id """
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())
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])
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']
156
self.compute_api.update_instance(req.environ['nova.context'],
154
ctxt = req.environ['nova.context']
155
self.compute_api.update_instance(ctxt,
159
158
except exception.NotFound:
160
159
return faults.Fault(exc.HTTPNotFound())