From b841f546b0600a0ce72c73eecfe29c8063e41a91 Mon Sep 17 00:00:00 2001 From: songwenping Date: Tue, 16 May 2023 18:32:52 +0800 Subject: [PATCH] Add client API for accelerator attribute create Change-Id: I4212b4eab6850502855d38e7ce6e9ecc0914bdd6 --- cyborgclient/osc/v2/attribute.py | 65 ++++++++++++++++++++++++++++++++ setup.cfg | 1 + 2 files changed, 66 insertions(+) diff --git a/cyborgclient/osc/v2/attribute.py b/cyborgclient/osc/v2/attribute.py index 4e83d41..1b67f5d 100644 --- a/cyborgclient/osc/v2/attribute.py +++ b/cyborgclient/osc/v2/attribute.py @@ -14,9 +14,14 @@ """Cyborg v2 Acceleration accelerator action implementations""" +import logging + +from openstack import exceptions as sdk_exc from osc_lib.command import command from osc_lib import utils as oscutils +from cyborgclient.common import utils +from cyborgclient import exceptions as exc from cyborgclient.i18n import _ @@ -61,3 +66,63 @@ class ListAttribute(command.Lister): return (self.column_headers, (oscutils.get_item_properties( s, self.columns, formatters=formatters) for s in data)) + + +class CreateAttribute(command.ShowOne): + """Register a new attribute with the accelerator service""" + log = logging.getLogger(__name__ + ".CreateAttribute") + + def get_parser(self, prog_name): + parser = super(CreateAttribute, self).get_parser(prog_name) + parser.add_argument( + 'deployable_id', + metavar='', + help=_("Deployable_id for the attribute.")) + parser.add_argument( + 'key', + metavar='', + help=_("""Key for the attribute. + e.g. '[{"resources:":1, + "trait:CUSTOM__": "required", + "trait:CUSTOM__": "required"}]'""")) + parser.add_argument( + 'value', + metavar='', + help=_("Value for the attribute.")) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + acc_client = self.app.client_manager.accelerator + + attrs = { + 'deployable_id': parsed_args.deployable_id, + 'key': parsed_args.key, + 'value': parsed_args.value + } + attribute = acc_client.create_attribute(**attrs) + return _show_attribute(acc_client, attribute.uuid) + + +def _show_attribute(acc_client, uuid): + """Show detailed info about device_profile.""" + + columns = ( + "created_at", + "updated_at", + "uuid", + "deployable_id", + "key", + "value", + ) + try: + attribute = acc_client.get_attribute(uuid) + except sdk_exc.ResourceNotFound: + raise exc.CommandError(_('Attribute %s not found') % uuid) + except sdk_exc.HttpException as e: + raise exc.NotAcceptable(message=e.details) + + formatters = {'data': utils.json_formatter} + data = attribute.to_dict() + return columns, oscutils.get_dict_properties(data, columns, + formatters=formatters) diff --git a/setup.cfg b/setup.cfg index 7ad6fa7..d783be2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -41,6 +41,7 @@ openstack.accelerator.v2 = accelerator_device_list = cyborgclient.osc.v2.device:ListDevice accelerator_device_show = cyborgclient.osc.v2.device:ShowDevice accelerator_device_attribute_list = cyborgclient.osc.v2.attribute:ListAttribute + accelerator_device_attribute_create = cyborgclient.osc.v2.attribute:CreateAttribute accelerator_device_profile_list = cyborgclient.osc.v2.device_profile:ListDeviceProfile accelerator_device_profile_create = cyborgclient.osc.v2.device_profile:CreateDeviceProfile accelerator_device_profile_delete = cyborgclient.osc.v2.device_profile:DeleteDeviceProfile