blob: 3618f7e13169d170038237bda72c2188a38e88f7 [file] [log] [blame]
Ken'ichi Ohmichi01151e82016-06-10 11:19:52 -07001# Copyright 2016 NEC Corporation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070016import copy
17
Ken'ichi Ohmichi01151e82016-06-10 11:19:52 -070018
19def get_image_meta_from_headers(resp):
20 meta = {'properties': {}}
21 for key in resp.response:
22 value = resp.response[key]
23 if key.startswith('x-image-meta-property-'):
24 _key = key[22:]
25 meta['properties'][_key] = value
26 elif key.startswith('x-image-meta-'):
27 _key = key[13:]
28 meta[_key] = value
29
30 for key in ['is_public', 'protected', 'deleted']:
31 if key in meta:
32 meta[key] = meta[key].strip().lower() in ('t', 'true', 'yes', '1')
33
34 for key in ['size', 'min_ram', 'min_disk']:
35 if key in meta:
36 try:
37 meta[key] = int(meta[key])
38 except ValueError:
39 pass
40 return meta
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070041
42
43def image_meta_to_headers(**metadata):
44 headers = {}
45 fields_copy = copy.deepcopy(metadata)
46
47 copy_from = fields_copy.pop('copy_from', None)
zhufl43488a52016-06-30 12:18:35 +080048 purge = fields_copy.pop('purge_props', None)
49
50 if purge is not None:
51 headers['x-glance-registry-purge-props'] = purge
52
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070053 if copy_from is not None:
54 headers['x-glance-api-copy-from'] = copy_from
55
guo yunxian7bbbec12016-08-21 20:03:10 +080056 for key, value in fields_copy.pop('properties', {}).items():
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070057 headers['x-image-meta-property-%s' % key] = str(value)
58
guo yunxian7bbbec12016-08-21 20:03:10 +080059 for key, value in fields_copy.pop('api', {}).items():
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070060 headers['x-glance-api-property-%s' % key] = str(value)
61
guo yunxian7bbbec12016-08-21 20:03:10 +080062 for key, value in fields_copy.items():
Ken'ichi Ohmichi02bcdf32016-06-17 16:41:26 -070063 headers['x-image-meta-%s' % key] = str(value)
64
65 return headers