Commit 17b255c7 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Use _format_image_headers: image_register,get_meta

- Ensure uniform image meta output
- Easy to apply the correct meta format

Refs: #3797
parent 4e424eaa
......@@ -23,7 +23,7 @@ Changes:
move and copy
- Rename file/server-meta commands to file/server-metadata
- Rename image-[add|del]member commands to members-[add|delete]
- Remove update option from imagre-register
- Remove update option from image-register
- In image-compute split properties to properties-list and properties-get
- Add optional output to methods[#3756, #3732]:
- file:
......@@ -52,6 +52,9 @@ Changes:
- image: members, member
- image compute: properties
- server: firewall, metadata
- Add a _format_image_headers method and use it in image_register and get_meta
for uniform image meta output [#3797]
Features:
- A logger module container a set of basic loging method for kamaki [#3668]
......
......@@ -35,6 +35,22 @@ from kamaki.clients import Client, ClientError
from kamaki.clients.utils import path4url, filter_in
def _format_image_headers(headers):
reply = dict(properties=dict())
meta_prefix = 'x-image-meta-'
property_prefix = 'x-image-meta-property-'
for key, val in headers.items():
key = key.lower()
if key.startswith(property_prefix):
key = key[len(property_prefix):].upper().replace('-', '_')
reply['properties'][key] = val
elif key.startswith(meta_prefix):
key = key[len(meta_prefix):]
reply[key] = val
return reply
class ImageClient(Client):
"""Synnefo Plankton API client"""
......@@ -80,23 +96,7 @@ class ImageClient(Client):
path = path4url('images', image_id)
r = self.head(path, success=200)
reply = {}
properties = {}
meta_prefix = 'x-image-meta-'
property_prefix = 'x-image-meta-property-'
for key, val in r.headers.items():
key = key.lower()
if key.startswith(property_prefix):
key = key[len(property_prefix):]
properties[key] = val
elif key.startswith(meta_prefix):
key = key[len(meta_prefix):]
reply[key] = val
if properties:
reply['properties'] = properties
return reply
return _format_image_headers(r.headers)
def register(self, name, location, params={}, properties={}):
"""Register an image that is uploaded at location
......@@ -110,7 +110,7 @@ class ImageClient(Client):
:param properties: (dict) image properties (X-Image-Meta-Property)
:returns: (dict) details of the created image
:returns: (dict) metadata of the created image
"""
path = path4url('images') + '/'
self.set_header('X-Image-Meta-Name', name)
......@@ -127,7 +127,8 @@ class ImageClient(Client):
async_headers['x-image-meta-property-%s' % key] = val
r = self.post(path, success=200, async_headers=async_headers)
return filter_in(r.headers, 'X-Image-')
return _format_image_headers(r.headers)
def unregister(self, image_id):
"""Unregister an image
......
......@@ -228,7 +228,8 @@ class ImageClient(TestCase):
params=params, properties=props)
expectedict = dict(example_image_headers)
expectedict.pop('extraheaders')
self.assert_dicts_are_equal(expectedict, r)
from kamaki.clients.image import _format_image_headers
self.assert_dicts_are_equal(_format_image_headers(expectedict), r)
self.assertEqual(
post.mock_calls[-1],
call('/images/', async_headers=async_headers, success=200))
......
......@@ -85,7 +85,7 @@ class Image(livetest.Generic):
self.location,
params=dict(is_public=True))
self._imglist[self.imgname] = dict(
name=r['x-image-meta-name'], id=r['x-image-meta-id'])
name=r['name'], id=r['id'])
self._imgdetails[self.imgname] = r
def tearDown(self):
......@@ -154,7 +154,7 @@ class Image(livetest.Generic):
'properties',
'size'):
self.assertTrue(term in img)
if img['properties']:
if len(img['properties']):
for interm in ('osfamily', 'users', 'root_partition'):
self.assertTrue(interm in img['properties'])
size_max = 1000000000
......@@ -185,13 +185,14 @@ class Image(livetest.Generic):
'container-format'):
self.assertTrue(term in r)
for interm in (
'kernel',
'osfamily',
'users',
'gui', 'sortorder',
'root-partition',
'os',
'description'):
'KERNEL',
'OSFAMILY',
'USERS',
'GUI',
'SORTORDER',
'ROOT_PARTITION',
'OS',
'DESCRIPTION'):
self.assertTrue(interm in r['properties'])
def test_register(self):
......@@ -204,8 +205,7 @@ class Image(livetest.Generic):
for img in self._imglist.values():
self.assertTrue(img is not None)
r = set(self._imgdetails[img['name']].keys())
self.assertTrue(
r.issubset(['x-image-meta-%s' % k for k in IMGMETA]))
self.assertTrue(r.issubset(IMGMETA.union(['properties'])))
def test_unregister(self):
"""Test unregister"""
......
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