Add client API for accelerator attribute create

Change-Id: I4212b4eab6850502855d38e7ce6e9ecc0914bdd6
This commit is contained in:
songwenping 2023-05-16 18:32:52 +08:00
parent 91cf9d7715
commit b841f546b0
2 changed files with 66 additions and 0 deletions

View File

@ -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='<deployable_id>',
help=_("Deployable_id for the attribute."))
parser.add_argument(
'key',
metavar='<key>',
help=_("""Key for the attribute.
e.g. '[{"resources:<type>":1,
"trait:CUSTOM_<type>_<product_id>": "required",
"trait:CUSTOM_<type>_<vendor>": "required"}]'"""))
parser.add_argument(
'value',
metavar='<value>',
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)

View File

@ -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