Commit 7063c64f authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add 'links' to compute endpoints

Add 'links' attribute to API responses for servers, networks, flavors
and images endpoints.
parent d24bcdfe
......@@ -56,6 +56,7 @@ urlpatterns = patterns(
def flavor_to_dict(flavor, detail=True):
d = {'id': flavor.id, 'name': flavor.name}
d['links'] = util.flavor_to_links(flavor.id)
if detail:
d['ram'] = flavor.ram
d['disk'] = flavor.disk
......
......@@ -85,6 +85,7 @@ def network_demux(request, network_id):
def network_to_dict(network, user_id, detail=True):
d = {'id': str(network.id), 'name': network.name}
d['links'] = util.network_to_links(network.id)
if detail:
d['cidr'] = network.subnet
d['cidr6'] = network.subnet6
......
......@@ -126,6 +126,7 @@ def nic_to_dict(nic):
def vm_to_dict(vm, detail=False):
d = dict(id=vm.id, name=vm.name)
d['links'] = util.vm_to_links(vm.id)
if detail:
d['status'] = get_rsapi_state(vm)
d['progress'] = 100 if get_rsapi_state(vm) == 'ACTIVE' \
......@@ -133,13 +134,14 @@ def vm_to_dict(vm, detail=False):
d['hostId'] = vm.hostid
d['updated'] = utils.isoformat(vm.updated)
d['created'] = utils.isoformat(vm.created)
d['flavor'] = vm.flavor.id
d['image'] = vm.imageid
d['flavor'] = {"id": vm.flavor.id,
"links": util.flavor_to_links(vm.flavor.id)}
d['image'] = {"id": vm.imageid,
"links": util.image_to_links(vm.imageid)}
d['suspended'] = vm.suspended
metadata = dict((m.meta_key, m.meta_value) for m in vm.metadata.all())
if metadata:
d['metadata'] = metadata
d['metadata'] = metadata
vm_nics = vm.nics.filter(state="ACTIVE").order_by("index")
attachments = map(nic_to_dict, vm_nics)
......@@ -149,6 +151,8 @@ def vm_to_dict(vm, detail=False):
diagnostic = vm.get_last_diagnostic()
if diagnostic:
d['diagnostics'] = diagnostics_to_dict([diagnostic])
else:
d['diagnostics'] = []
return d
......
......@@ -90,8 +90,9 @@ class ServerAPITest(ComputeAPITest):
self.assertSuccess(response)
servers = json.loads(response.content)['servers']
db_server = self.vm1
self.assertEqual(servers, [{'name': db_server.name,
'id': db_server.id}])
server = servers[0]
self.assertEqual(server["name"], db_server.name)
self.assertEqual(server["id"], db_server.id)
def test_server_list_detail(self):
"""Test if the servers list details are returned."""
......@@ -104,10 +105,10 @@ class ServerAPITest(ComputeAPITest):
self.assertEqual(len(servers), len(user_vms))
for api_vm in servers:
db_vm = user_vms[api_vm['id']]
self.assertEqual(api_vm['flavor'], db_vm.flavor.id)
self.assertEqual(api_vm['flavor']["id"], db_vm.flavor.id)
self.assertEqual(api_vm['hostId'], db_vm.hostid)
self.assertEqual(api_vm['id'], db_vm.id)
self.assertEqual(api_vm['image'], db_vm.imageid)
self.assertEqual(api_vm['image']["id"], db_vm.imageid)
self.assertEqual(api_vm['name'], db_vm.name)
self.assertEqual(api_vm['status'], get_rsapi_state(db_vm))
self.assertSuccess(response)
......@@ -124,10 +125,10 @@ class ServerAPITest(ComputeAPITest):
response = self.myget('servers/%d' % db_vm.id, user)
server = json.loads(response.content)['server']
self.assertEqual(server['flavor'], db_vm.flavor.id)
self.assertEqual(server['flavor']["id"], db_vm.flavor.id)
self.assertEqual(server['hostId'], db_vm.hostid)
self.assertEqual(server['id'], db_vm.id)
self.assertEqual(server['image'], db_vm.imageid)
self.assertEqual(server['image']["id"], db_vm.imageid)
self.assertEqual(server['name'], db_vm.name)
self.assertEqual(server['status'], get_rsapi_state(db_vm))
api_nic = server['attachments'][0]
......
......@@ -66,6 +66,22 @@ from snf_django.lib.astakos import get_user
from synnefo.plankton.utils import image_backend
from synnefo.settings import MAX_CIDR_BLOCK
from synnefo.cyclades_settings import cyclades_services, BASE_HOST
from synnefo.lib.services import get_service_path
from synnefo.lib import join_urls
COMPUTE_URL = \
join_urls(BASE_HOST,
get_service_path(cyclades_services, "compute", version="v2.0"))
SERVERS_URL = join_urls(COMPUTE_URL, "servers/")
NETWORKS_URL = join_urls(COMPUTE_URL, "networks/")
FLAVORS_URL = join_urls(COMPUTE_URL, "flavors/")
IMAGES_URL = join_urls(COMPUTE_URL, "images/")
PLANKTON_URL = \
join_urls(BASE_HOST,
get_service_path(cyclades_services, "image", version="v1.0"))
IMAGES_PLANKTON_URL = join_urls(PLANKTON_URL, "images/")
log = getLogger('synnefo.api')
......@@ -454,3 +470,26 @@ def get_existing_users():
return set(list(keypairusernames) + list(serverusernames) +
list(networkusernames))
def vm_to_links(vm_id):
link = join_urls(SERVERS_URL, str(vm_id))
return [{"ref": rel, "link": link} for rel in ("self", "bookmark")]
def network_to_links(network_id):
link = join_urls(NETWORKS_URL, str(network_id))
return [{"ref": rel, "link": link} for rel in ("self", "bookmark")]
def flavor_to_links(flavor_id):
link = join_urls(FLAVORS_URL, str(flavor_id))
return [{"ref": rel, "link": link} for rel in ("self", "bookmark")]
def image_to_links(image_id):
link = join_urls(IMAGES_URL, str(image_id))
links = [{"ref": rel, "link": link} for rel in ("self", "bookmark")]
links.append({"rel": "alternate",
"link": join_urls(IMAGES_PLANKTON_URL, str(image_id))})
return links
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment