Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 1 | # 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 Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 16 | import copy |
| 17 | |
| 18 | import six |
| 19 | |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 20 | |
| 21 | def get_image_meta_from_headers(resp): |
| 22 | meta = {'properties': {}} |
| 23 | for key in resp.response: |
| 24 | value = resp.response[key] |
| 25 | if key.startswith('x-image-meta-property-'): |
| 26 | _key = key[22:] |
| 27 | meta['properties'][_key] = value |
| 28 | elif key.startswith('x-image-meta-'): |
| 29 | _key = key[13:] |
| 30 | meta[_key] = value |
| 31 | |
| 32 | for key in ['is_public', 'protected', 'deleted']: |
| 33 | if key in meta: |
| 34 | meta[key] = meta[key].strip().lower() in ('t', 'true', 'yes', '1') |
| 35 | |
| 36 | for key in ['size', 'min_ram', 'min_disk']: |
| 37 | if key in meta: |
| 38 | try: |
| 39 | meta[key] = int(meta[key]) |
| 40 | except ValueError: |
| 41 | pass |
| 42 | return meta |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def image_meta_to_headers(**metadata): |
| 46 | headers = {} |
| 47 | fields_copy = copy.deepcopy(metadata) |
| 48 | |
| 49 | copy_from = fields_copy.pop('copy_from', None) |
zhufl | 43488a5 | 2016-06-30 12:18:35 +0800 | [diff] [blame] | 50 | purge = fields_copy.pop('purge_props', None) |
| 51 | |
| 52 | if purge is not None: |
| 53 | headers['x-glance-registry-purge-props'] = purge |
| 54 | |
Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 55 | if copy_from is not None: |
| 56 | headers['x-glance-api-copy-from'] = copy_from |
| 57 | |
| 58 | for key, value in six.iteritems(fields_copy.pop('properties', {})): |
| 59 | headers['x-image-meta-property-%s' % key] = str(value) |
| 60 | |
| 61 | for key, value in six.iteritems(fields_copy.pop('api', {})): |
| 62 | headers['x-glance-api-property-%s' % key] = str(value) |
| 63 | |
| 64 | for key, value in six.iteritems(fields_copy): |
| 65 | headers['x-image-meta-%s' % key] = str(value) |
| 66 | |
| 67 | return headers |