|
| 1 | +# Copyright 2012-2013 OpenStack Foundation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Image V2 Action Implementations""" |
| 17 | + |
| 18 | +import argparse |
| 19 | +from base64 import b64encode |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import sys |
| 23 | + |
| 24 | +from cinderclient import api_versions |
| 25 | +from openstack.image import image_signer |
| 26 | +from osc_lib.api import utils as api_utils |
| 27 | +from osc_lib.cli import format_columns |
| 28 | +from osc_lib.cli import parseractions |
| 29 | +from osc_lib.command import command |
| 30 | +from osc_lib import exceptions |
| 31 | +from osc_lib import utils |
| 32 | +from oslo_utils import uuidutils |
| 33 | + |
| 34 | +from openstackclient.common import progressbar |
| 35 | +from openstackclient.i18n import _ |
| 36 | +from openstackclient.identity import common |
| 37 | + |
| 38 | +if os.name == "nt": |
| 39 | + import msvcrt |
| 40 | +else: |
| 41 | + msvcrt = None |
| 42 | + |
| 43 | +def _format_metadata_object(metadata_object, human_readable=False): |
| 44 | + """Format an image to make it more consistent with OSC operations.""" |
| 45 | + |
| 46 | + info = {} |
| 47 | + properties = {} |
| 48 | + |
| 49 | + # the only fields we're not including is "links", "tags" and the properties |
| 50 | + fields_to_show = [ |
| 51 | + 'name','namespace','properties' |
| 52 | + ] |
| 53 | + |
| 54 | + # TODO(gtema/anybody): actually it should be possible to drop this method, |
| 55 | + # since SDK already delivers a proper object |
| 56 | + metadata_object = metadata_object.to_dict(ignore_none=True, original_names=True) |
| 57 | + |
| 58 | + # split out the usual key and the properties which are top-level |
| 59 | + for key in metadata_object: |
| 60 | + if key in fields_to_show: |
| 61 | + info[key] = metadata_object.get(key) |
| 62 | + |
| 63 | + # add properties back into the dictionary as a top-level key |
| 64 | + if properties: |
| 65 | + info['properties'] = format_columns.DictColumn(properties) |
| 66 | + |
| 67 | + return info |
| 68 | + |
| 69 | + |
| 70 | +class CreateMetadataObject(command.ShowOne): |
| 71 | + _description = _("Create metadata object") |
| 72 | + |
| 73 | + def get_parser(self, prog_name): |
| 74 | + parser = super().get_parser(prog_name) |
| 75 | + # TODO(bunting): There are additional arguments that v1 supported |
| 76 | + |
| 77 | + parser.add_argument( |
| 78 | + "--name", |
| 79 | + metavar="<object-name>", |
| 80 | + help=_("New object name"), |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + "--namespace", |
| 84 | + metavar="<namespace>", |
| 85 | + help=_("namespace"), |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + "--properties", |
| 89 | + metavar="<properties>", |
| 90 | + help=_("Object schema JSON format"), |
| 91 | + ) |
| 92 | + common.add_project_domain_option_to_parser(parser) |
| 93 | + |
| 94 | + return parser |
| 95 | + |
| 96 | + def take_action(self, parsed_args): |
| 97 | + image_client = self.app.client_manager.image |
| 98 | + |
| 99 | + kwargs = {'allow_duplicates': True} |
| 100 | + copy_attrs = ('name', 'namespace', 'properties') |
| 101 | + for attr in copy_attrs: |
| 102 | + if attr in parsed_args: |
| 103 | + val = getattr(parsed_args, attr, None) |
| 104 | + if val: |
| 105 | + # Only include a value in kwargs for attributes that |
| 106 | + # are actually present on the command line |
| 107 | + kwargs[attr] = val |
| 108 | + |
| 109 | + |
| 110 | + metadata_object = image_client.create_metadata_object(**kwargs) |
| 111 | + info = _format_metadata_object(metadata_object) |
| 112 | + return zip(*sorted(info.items())) |
0 commit comments