From 5d673603a254e2fbcb992b9ddecee632dfc60911 Mon Sep 17 00:00:00 2001 From: cid Date: Thu, 12 Dec 2024 15:44:42 +0100 Subject: [PATCH] Add CLI support for migrated inspection rules Change-Id: Iabb9d3bbc40a6706aa9627f8445c6027acbac40f --- ironicclient/common/http.py | 2 +- .../osc/v1/baremetal_inspection_rule.py | 412 ++++++++++++++++++ ironicclient/tests/unit/osc/v1/fakes.py | 26 ++ ironicclient/v1/client.py | 3 + ironicclient/v1/inspection_rule.py | 105 +++++ ironicclient/v1/resource_fields.py | 25 +- setup.cfg | 6 + 7 files changed, 577 insertions(+), 2 deletions(-) create mode 100644 ironicclient/osc/v1/baremetal_inspection_rule.py create mode 100644 ironicclient/v1/inspection_rule.py diff --git a/ironicclient/common/http.py b/ironicclient/common/http.py index a71a22a6f..055af2377 100644 --- a/ironicclient/common/http.py +++ b/ironicclient/common/http.py @@ -36,7 +36,7 @@ from ironicclient import exc # http://specs.openstack.org/openstack/ironic-specs/specs/kilo/api-microversions.html # noqa # for full details. DEFAULT_VER = '1.9' -LAST_KNOWN_API_VERSION = 95 +LAST_KNOWN_API_VERSION = 96 LATEST_VERSION = '1.{}'.format(LAST_KNOWN_API_VERSION) LOG = logging.getLogger(__name__) diff --git a/ironicclient/osc/v1/baremetal_inspection_rule.py b/ironicclient/osc/v1/baremetal_inspection_rule.py new file mode 100644 index 000000000..d9ce97280 --- /dev/null +++ b/ironicclient/osc/v1/baremetal_inspection_rule.py @@ -0,0 +1,412 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +import itertools +import json +import logging + +from osc_lib.command import command +from osc_lib import utils as oscutils + +from ironicclient.common.i18n import _ +from ironicclient.common import utils +from ironicclient import exc +from ironicclient.v1 import resource_fields as res_fields + + +class CreateBaremetalInspectionRule(command.ShowOne): + """Create a new rule""" + + log = logging.getLogger(__name__ + ".CreateBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(CreateBaremetalInspectionRule, self).get_parser( + prog_name) + + parser.add_argument( + '--uuid', + dest='uuid', + metavar='', + help=_('UUID of the rule.')) + parser.add_argument( + '--description', + metavar='', + help=_('A brief explanation about the rule.') + ) + parser.add_argument( + '--priority', + metavar='', + help=_("Specifies the rule's precedence level during execution.") + ) + parser.add_argument( + '--sensitive', + metavar='', + help=_('Indicates whether the rule contains sensitive ' + 'information.') + ) + parser.add_argument( + '--phase', + metavar='', + help=_('Specifies the processing phase when the rule should run.') + ) + parser.add_argument( + '--conditions', + metavar="", + help=_('Conditions under which the rule should be triggered.') + ) + parser.add_argument( + '--actions', + metavar="", + required=True, + help=_('Actions to be executed when the rule conditions are met.') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + baremetal_client = self.app.client_manager.baremetal + + actions = utils.handle_json_arg(parsed_args.actions, 'rule actions') + conditions = utils.handle_json_arg(parsed_args.conditions, + 'rule conditions') + + field_list = ['uuid', 'description', 'priority', 'sensitive', 'phase'] + fields = dict((k, v) for (k, v) in vars(parsed_args).items() + if k in field_list and v is not None) + rule = baremetal_client.inspection_rule.create(actions=actions, + conditions=conditions, + **fields) + + data = dict([(f, getattr(rule, f, '')) for f in + res_fields.INSPECTION_RULE_DETAILED_RESOURCE.fields]) + + return self.dict2columns(data) + + +class ShowBaremetalInspectionRule(command.ShowOne): + """Show baremetal rule details.""" + + log = logging.getLogger(__name__ + ".ShowBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(ShowBaremetalInspectionRule, self).get_parser( + prog_name) + parser.add_argument( + "rule", + metavar="", + help=_("UUID of the inspection rule.") + ) + parser.add_argument( + '--fields', + nargs='+', + dest='fields', + metavar='', + action='append', + choices=res_fields.INSPECTION_RULE_DETAILED_RESOURCE.fields, + default=[], + help=_("One or more rule fields. Only these fields " + "will be fetched from the server.") + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + baremetal_client = self.app.client_manager.baremetal + fields = list(itertools.chain.from_iterable(parsed_args.fields)) + fields = fields if fields else None + + rule = baremetal_client.inspection_rule.get( + parsed_args.rule, fields=fields)._info + + rule.pop("links", None) + return zip(*sorted(rule.items())) + + +class SetBaremetalInspectionRule(command.Command): + """Set baremetal rule properties.""" + + log = logging.getLogger(__name__ + ".SetBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(SetBaremetalInspectionRule, self).get_parser(prog_name) + + parser.add_argument( + 'rule', + metavar='', + help=_("UUID of the inspection rule") + ) + parser.add_argument( + '--description', + metavar='', + help=_('Set a brief explanation about the rule.') + ) + parser.add_argument( + '--priority', + metavar='', + help=_("Specifies the rule's precedence level during execution.") + ) + parser.add_argument( + '--phase', + metavar='', + help=_('Specifies the processing phase when the rule should run.') + ) + parser.add_argument( + '--conditions', + metavar="", + help=_('Conditions under which the rule should be triggered.') + ) + parser.add_argument( + '--actions', + metavar="", + help=_('Actions to be executed when the rule conditions are met.') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + baremetal_client = self.app.client_manager.baremetal + + properties = [] + if parsed_args.description: + description = ["description=%s" % parsed_args.description] + properties.extend(utils.args_array_to_patch('add', description)) + if parsed_args.priority: + priority = ["priority=%s" % parsed_args.priority] + properties.extend(utils.args_array_to_patch('add', priority)) + if parsed_args.phase: + phase = ["phase=%s" % parsed_args.phase] + properties.extend(utils.args_array_to_patch('add', phase)) + if parsed_args.actions: + actions = utils.handle_json_arg(parsed_args.actions, + 'rule actions') + actions = ["actions=%s" % json.dumps(actions)] + properties.extend(utils.args_array_to_patch('add', actions)) + if parsed_args.conditions: + conditions = utils.handle_json_arg(parsed_args.conditions, + 'rule conditions') + conditions = ["conditions=%s" % json.dumps(conditions)] + properties.extend(utils.args_array_to_patch('add', conditions)) + + if properties: + baremetal_client.inspection_rule.update(parsed_args.rule, + properties) + else: + self.log.warning("Please specify what to set.") + + +class UnsetBaremetalInspectionRule(command.Command): + """Unset baremetal rule properties.""" + log = logging.getLogger(__name__ + ".UnsetBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(UnsetBaremetalInspectionRule, self).get_parser( + prog_name) + + parser.add_argument( + 'rule', + metavar='', + help=_("UUID of the inspection rule") + ) + parser.add_argument( + '--description', + dest='description', + action='store_true', + help=_('Unset a brief explanation about the rule.') + ) + parser.add_argument( + '--priority', + dest='priority', + action='store_true', + help=_("Specifies the rule's precedence level during execution.") + ) + parser.add_argument( + '--phase', + dest='phase', + action='store_true', + help=_('Specifies the processing phase when the rule should run.') + ) + parser.add_argument( + '--condition', + metavar="", + action='append', + help=_('Condition to unset on this baremetal inspection rule ' + '(repeat option to unset multiple conditions).') + ) + parser.add_argument( + '--action', + metavar="", + action='append', + help=_('Action to unset on this baremetal inspection rule ' + '(repeat option to unset multiple actions).') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + baremetal_client = self.app.client_manager.baremetal + + properties = [] + field_list = ['description', 'priority', 'phase'] + for field in field_list: + if getattr(parsed_args, field): + properties.extend(utils.args_array_to_patch('remove', [field])) + + if parsed_args.action: + properties.extend(utils.args_array_to_patch('remove', + ['action/' + x for x in parsed_args.action])) + if parsed_args.condition: + properties.extend( + utils.args_array_to_patch( + 'remove', + ['condition/' + x for x in parsed_args.condition])) + + if properties: + baremetal_client.inspection_rule.update(parsed_args.rule, + properties) + else: + self.log.warning("Please specify what to unset.") + + +class DeleteBaremetalInspectionRule(command.Command): + """Delete rule(s).""" + + log = logging.getLogger(__name__ + ".DeleteBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(DeleteBaremetalInspectionRule, self).get_parser( + prog_name) + parser.add_argument( + "rules", + metavar="", + nargs="+", + help=_("UUID(s) of the rule(s) to delete."), + ) + + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + + baremetal_client = self.app.client_manager.baremetal + + failures = [] + if parsed_args.rules == 'all': + try: + baremetal_client.inspection_rule.delete() + print(_('Deleted all rules.')) + except exc.ClientException as e: + raise exc.ClientException( + _("Failed to delete all rules: %s") % e) + else: + for rule in parsed_args.rules: + try: + baremetal_client.inspection_rule.delete(rule) + print(_('Deleted rule %s') % rule) + except exc.ClientException as e: + failures.append(_("Failed to delete rule " + "%(rule)s: %(error)s") + % {'rule': rule, 'error': e}) + + if failures: + raise exc.ClientException("\n".join(failures)) + + +class ListBaremetalInspectionRule(command.Lister): + """List baremetal rules.""" + + log = logging.getLogger(__name__ + ".ListBaremetalInspectionRule") + + def get_parser(self, prog_name): + parser = super(ListBaremetalInspectionRule, self).get_parser(prog_name) + parser.add_argument( + '--limit', + metavar='', + type=int, + help=_('Maximum number of rules to return per request, ' + '0 for no limit. Default is the maximum number used ' + 'by the Baremetal API Service.') + ) + parser.add_argument( + '--marker', + metavar='', + help=_('InspectionRule UUID (for example, of the last rule ' + 'in the list from a previous request). Returns ' + 'the list of rules after this UUID.') + ) + parser.add_argument( + '--sort', + metavar="[:]", + help=_('Sort output by specified rule fields and ' + 'directions (asc or desc) (default: asc). Multiple fields ' + 'and directions can be specified, separated by comma.') + ) + display_group = parser.add_mutually_exclusive_group() + display_group.add_argument( + '--long', + dest='detail', + action='store_true', + default=False, + help=_("Show detailed information about rules.") + ) + display_group.add_argument( + '--fields', + nargs='+', + dest='fields', + metavar='', + action='append', + default=[], + choices=res_fields.INSPECTION_RULE_DETAILED_RESOURCE.fields, + help=_("One or more rule fields. Only these fields " + "will be fetched from the server. Can not be used when " + "'--long' is specified.") + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)", parsed_args) + client = self.app.client_manager.baremetal + + columns = res_fields.INSPECTION_RULE_RESOURCE.fields + labels = res_fields.INSPECTION_RULE_RESOURCE.labels + + params = {} + if parsed_args.limit is not None and parsed_args.limit < 0: + raise exc.CommandError( + _('Expected non-negative --limit, got %s') % + parsed_args.limit) + params['limit'] = parsed_args.limit + params['marker'] = parsed_args.marker + + if parsed_args.detail: + params['detail'] = parsed_args.detail + columns = res_fields.INSPECTION_RULE_DETAILED_RESOURCE.fields + labels = res_fields.INSPECTION_RULE_DETAILED_RESOURCE.labels + + elif parsed_args.fields: + params['detail'] = False + fields = itertools.chain.from_iterable(parsed_args.fields) + resource = res_fields.Resource(list(fields)) + columns = resource.fields + labels = resource.labels + params['fields'] = columns + + self.log.debug("params(%s)", params) + data = client.inspection_rule.list(**params) + + data = oscutils.sort_items(data, parsed_args.sort) + + return (labels, + (oscutils.get_item_properties(s, columns) for s in data)) diff --git a/ironicclient/tests/unit/osc/v1/fakes.py b/ironicclient/tests/unit/osc/v1/fakes.py index e2483855d..3277d00dc 100644 --- a/ironicclient/tests/unit/osc/v1/fakes.py +++ b/ironicclient/tests/unit/osc/v1/fakes.py @@ -313,6 +313,32 @@ FIRMWARE_COMPONENTS = [ ] +baremetal_inspection_rule_uuid = 'ddd-tttttt-dddd' +baremetal_inspection_rule_description = 'Blah' +baremetal_inspection_rule_priority = 0 +baremetal_inspection_rule_sensitive = False +baremetal_inspection_rule_phase = 'main' +baremetal_inspection_rule_actions = json.dumps([{ + 'op': 'set-attribute', + 'args': ["/driver", "idrac"], +}]) +baremetal_inspection_rule_conditions = json.dumps([{ + 'op': 'is-true', + 'args': ["{node.auto_discovered}"], + 'multiple': 'any', +}]) + +INSPECTION_RULE = { + 'uuid': baremetal_inspection_rule_uuid, + 'description': baremetal_inspection_rule_description, + 'priority': baremetal_inspection_rule_priority, + 'sensitive': baremetal_inspection_rule_sensitive, + 'phase': baremetal_inspection_rule_phase, + 'actions': baremetal_inspection_rule_actions, + 'conditions': baremetal_inspection_rule_conditions, +} + + class TestBaremetal(utils.TestCommand): def setUp(self): diff --git a/ironicclient/v1/client.py b/ironicclient/v1/client.py index d56643249..08cdc6aae 100644 --- a/ironicclient/v1/client.py +++ b/ironicclient/v1/client.py @@ -24,6 +24,7 @@ from ironicclient.v1 import conductor from ironicclient.v1 import deploy_template from ironicclient.v1 import driver from ironicclient.v1 import events +from ironicclient.v1 import inspection_rule from ironicclient.v1 import node from ironicclient.v1 import port from ironicclient.v1 import portgroup @@ -108,6 +109,8 @@ class Client(object): self.deploy_template = deploy_template.DeployTemplateManager( self.http_client) self.shard = shard.ShardManager(self.http_client) + self.inspection_rule = inspection_rule.InspectionRuleManager( + self.http_client) @property def current_api_version(self): diff --git a/ironicclient/v1/inspection_rule.py b/ironicclient/v1/inspection_rule.py new file mode 100644 index 000000000..a59e5dca3 --- /dev/null +++ b/ironicclient/v1/inspection_rule.py @@ -0,0 +1,105 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from ironicclient.common import base +from ironicclient.common.i18n import _ +from ironicclient.common import utils +from ironicclient import exc + + +class InspectionRule(base.Resource): + def __repr__(self): + return "" % self._info + + +class InspectionRuleManager(base.CreateManager): + resource_class = InspectionRule + _creation_attributes = ['uuid', 'description', 'priority', 'sensitive', + 'phase', 'conditions', 'actions'] + _resource_name = 'inspection_rules' + + def list(self, limit=None, marker=None, sort_key=None, sort_dir=None, + detail=False, fields=None, os_ironic_api_version=None, + global_request_id=None): + """Retrieve a list of rules. + + :param marker: Optional, the UUID of a deploy template, eg the last + template from a previous result set. Return the next + result set. + :param limit: The maximum number of results to return per + request, if: + + 1) limit > 0, the maximum number of rules to return. + 2) limit == 0, return the entire list of rules. + 3) limit param is NOT specified (None), the number of items + returned respect the maximum imposed by the Ironic API + (see Ironic's api.max_limit option). + + :param sort_key: Optional, field used for sorting. + + :param sort_dir: Optional, direction of sorting, either 'asc' (the + default) or 'desc'. + + :param detail: Optional, boolean whether to return detailed information + about rules. + + :param fields: Optional, a list with a specified set of fields + of the resource to be returned. Can not be used + when 'detail' is set. + + :param os_ironic_api_version: String version (e.g. "1.35") to use for + the request. If not specified, the client's default is used. + + :param global_request_id: String containing global request ID header + value (in form "req-") to use for the request. + + :returns: A list of rules. + + """ + if limit is not None: + limit = int(limit) + + if detail and fields: + raise exc.InvalidAttribute(_("Can't fetch a subset of fields " + "with 'detail' set")) + + filters = utils.common_filters(marker, limit, sort_key, sort_dir, + fields, detail=detail) + path = '' + if filters: + path += '?' + '&'.join(filters) + header_values = {"os_ironic_api_version": os_ironic_api_version, + "global_request_id": global_request_id} + if limit is None: + return self._list(self._path(path), "inspection_rules", + **header_values) + else: + return self._list_pagination(self._path(path), "inspection_rules", + limit=limit, **header_values) + + def get(self, rule_id, fields=None, os_ironic_api_version=None, + global_request_id=None): + return self._get(resource_id=rule_id, fields=fields, + os_ironic_api_version=os_ironic_api_version, + global_request_id=global_request_id) + + def delete(self, rule_id, os_ironic_api_version=None, + global_request_id=None): + return self._delete(resource_id=rule_id, + os_ironic_api_version=os_ironic_api_version, + global_request_id=global_request_id) + + def update(self, rule_id, patch, os_ironic_api_version=None, + global_request_id=None): + return self._update(resource_id=rule_id, patch=patch, + os_ironic_api_version=os_ironic_api_version, + global_request_id=global_request_id) diff --git a/ironicclient/v1/resource_fields.py b/ironicclient/v1/resource_fields.py index a538bf033..4854db3ac 100644 --- a/ironicclient/v1/resource_fields.py +++ b/ironicclient/v1/resource_fields.py @@ -159,7 +159,12 @@ class Resource(object): 'children': 'Child Nodes', 'firmware_interface': 'Firmware Interface', 'public': 'Public', - 'disable_power_off': 'Disable Power Off' + 'disable_power_off': 'Disable Power Off', + 'priority': 'Priority', + 'sensitive': 'Sensitive', + 'phase': 'Phase', + 'actions': 'Actions', + 'conditions': 'Conditions' } def __init__(self, field_ids, sort_excluded=None, override_labels=None): @@ -650,3 +655,21 @@ SHARD_RESOURCE = Resource( ['name', 'count'] ) + +INSPECTION_RULE_DETAILED_RESOURCE = Resource( + ['uuid', + 'description', + 'priority', + 'sensitive', + 'phase', + 'actions', + 'conditions', + 'created_at', + 'updated_at'], + sort_excluded=['actions', 'conditions'] +) + +INSPECTION_RULE_RESOURCE = Resource( + ['uuid', + 'description'], +) diff --git a/setup.cfg b/setup.cfg index 908a4416a..77d4a95e5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -139,6 +139,12 @@ openstack.baremetal.v1 = baremetal_runbook_set = ironicclient.osc.v1.baremetal_runbook:SetBaremetalRunbook baremetal_runbook_unset = ironicclient.osc.v1.baremetal_runbook:UnsetBaremetalRunbook baremetal_runbook_show = ironicclient.osc.v1.baremetal_runbook:ShowBaremetalRunbook + baremetal_inspection_rule_create = ironicclient.osc.v1.baremetal_inspection_rule:CreateBaremetalInspectionRule + baremetal_inspection_rule_delete = ironicclient.osc.v1.baremetal_inspection_rule:DeleteBaremetalInspectionRule + baremetal_inspection_rule_list = ironicclient.osc.v1.baremetal_inspection_rule:ListBaremetalInspectionRule + baremetal_inspection_rule_set = ironicclient.osc.v1.baremetal_inspection_rule:SetBaremetalInspectionRule + baremetal_inspection_rule_unset = ironicclient.osc.v1.baremetal_inspection_rule:UnsetBaremetalInspectionRule + baremetal_inspection_rule_show = ironicclient.osc.v1.baremetal_inspection_rule:ShowBaremetalInspectionRule [extras] cli =