| 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 | |
| Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 18 | |
| 19 | def 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 Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def image_meta_to_headers(**metadata): |
| 44 | headers = {} |
| 45 | fields_copy = copy.deepcopy(metadata) |
| 46 | |
| 47 | copy_from = fields_copy.pop('copy_from', None) |
| zhufl | 43488a5 | 2016-06-30 12:18:35 +0800 | [diff] [blame] | 48 | 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 Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 53 | if copy_from is not None: |
| 54 | headers['x-glance-api-copy-from'] = copy_from |
| 55 | |
| guo yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 56 | for key, value in fields_copy.pop('properties', {}).items(): |
| Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 57 | headers['x-image-meta-property-%s' % key] = str(value) |
| 58 | |
| guo yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 59 | for key, value in fields_copy.pop('api', {}).items(): |
| Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 60 | headers['x-glance-api-property-%s' % key] = str(value) |
| 61 | |
| guo yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 62 | for key, value in fields_copy.items(): |
| Ken'ichi Ohmichi | 02bcdf3 | 2016-06-17 16:41:26 -0700 | [diff] [blame] | 63 | headers['x-image-meta-%s' % key] = str(value) |
| 64 | |
| 65 | return headers |