def _image_meta_from_headers(self, headers): meta = {"properties": {}} safe_decode = strutils.safe_decode for key, value in six.iteritems(headers): value = safe_decode(value, incoming="utf-8") if key.startswith("x-image-meta-property-"): _key = safe_decode(key[22:], incoming="utf-8") meta["properties"][_key] = value elif key.startswith("x-image-meta-"): _key = safe_decode(key[13:], incoming="utf-8") meta[_key] = value for key in ["is_public", "protected", "deleted"]: if key in meta: meta[key] = strutils.bool_from_string(meta[key]) return self._format_image_meta_for_user(meta)
def _image_meta_from_headers(self, headers): meta = {'properties': {}} safe_decode = strutils.safe_decode for key, value in six.iteritems(headers): value = safe_decode(value, incoming='utf-8') if key.startswith('x-image-meta-property-'): _key = safe_decode(key[22:], incoming='utf-8') meta['properties'][_key] = value elif key.startswith('x-image-meta-'): _key = safe_decode(key[13:], incoming='utf-8') meta[_key] = value for key in ['is_public', 'protected', 'deleted']: if key in meta: meta[key] = strutils.bool_from_string(meta[key]) return self._format_image_meta_for_user(meta)
def do_add(gc, args): """DEPRECATED! Use image-create instead.""" try: fields = get_image_fields_from_args(args.fields) except RuntimeError as e: print(e) return FAILURE image_meta = { 'is_public': strutils.bool_from_string( fields.pop('is_public', 'False')), 'protected': strutils.bool_from_string( fields.pop('protected', 'False')), 'min_disk': fields.pop('min_disk', 0), 'min_ram': fields.pop('min_ram', 0), } #NOTE(bcwaldon): Use certain properties only if they are explicitly set optional = ['id', 'name', 'disk_format', 'container_format'] for field in optional: if field in fields: image_meta[field] = fields.pop(field) # Strip any args that are not supported unsupported_fields = ['status', 'size'] for field in unsupported_fields: if field in fields.keys(): print('Found non-settable field %s. Removing.' % field) fields.pop(field) # We need either a location or image data/stream to add... image_data = None if 'location' in fields.keys(): image_meta['location'] = fields.pop('location') if 'checksum' in fields.keys(): image_meta['checksum'] = fields.pop('checksum') elif 'copy_from' in fields.keys(): image_meta['copy_from'] = fields.pop('copy_from') else: # Grab the image data stream from stdin or redirect, # otherwise error out image_data = sys.stdin image_meta['data'] = image_data # allow owner to be set when image is created if 'owner' in fields.keys(): image_meta['owner'] = fields.pop('owner') # Add custom attributes, which are all the arguments remaining image_meta['properties'] = fields if not args.dry_run: image = gc.images.create(**image_meta) print("Added new image with ID: %s" % image.id) if args.verbose: print("Returned the following metadata for the new image:") for k, v in sorted(image.to_dict().items()): print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) else: print("Dry run. We would have done the following:") def _dump(dict): for k, v in sorted(dict.items()): print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) print("Add new image with metadata:") _dump(image_meta) return SUCCESS
def do_add(gc, args): """DEPRECATED! Use image-create instead.""" try: fields = get_image_fields_from_args(args.fields) except RuntimeError as e: print(e) return FAILURE image_meta = { 'is_public': strutils.bool_from_string(fields.pop('is_public', 'False')), 'protected': strutils.bool_from_string(fields.pop('protected', 'False')), 'min_disk': fields.pop('min_disk', 0), 'min_ram': fields.pop('min_ram', 0), } #NOTE(bcwaldon): Use certain properties only if they are explicitly set optional = ['id', 'name', 'disk_format', 'container_format'] for field in optional: if field in fields: image_meta[field] = fields.pop(field) # Strip any args that are not supported unsupported_fields = ['status', 'size'] for field in unsupported_fields: if field in fields.keys(): print('Found non-settable field %s. Removing.' % field) fields.pop(field) # We need either a location or image data/stream to add... image_data = None if 'location' in fields.keys(): image_meta['location'] = fields.pop('location') if 'checksum' in fields.keys(): image_meta['checksum'] = fields.pop('checksum') elif 'copy_from' in fields.keys(): image_meta['copy_from'] = fields.pop('copy_from') else: # Grab the image data stream from stdin or redirect, # otherwise error out image_data = sys.stdin image_meta['data'] = image_data # allow owner to be set when image is created if 'owner' in fields.keys(): image_meta['owner'] = fields.pop('owner') # Add custom attributes, which are all the arguments remaining image_meta['properties'] = fields if not args.dry_run: image = gc.images.create(**image_meta) print("Added new image with ID: %s" % image.id) if args.verbose: print("Returned the following metadata for the new image:") for k, v in sorted(image.to_dict().items()): print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) else: print("Dry run. We would have done the following:") def _dump(dict): for k, v in sorted(dict.items()): print(" %(k)30s => %(v)s" % {'k': k, 'v': v}) print("Add new image with metadata:") _dump(image_meta) return SUCCESS