diff --git a/senlinclient/common/sdk.py b/senlinclient/common/sdk.py index 84eeedc3..0882cf9e 100644 --- a/senlinclient/common/sdk.py +++ b/senlinclient/common/sdk.py @@ -104,6 +104,22 @@ class Resource(base.Resource): value = cls.existing(**data) yield value + def create(self, session, extra_attrs=False): + """Create a remote resource from this instance. + + :param extra_attrs: If true, all attributions that + included in response will be collected and returned + to user after resource creation + + """ + resp = self.create_by_id(session, self._attrs, self.id, path_args=self) + self._attrs[self.id_attribute] = resp[self.id_attribute] + if extra_attrs: + for attr in resp: + self._attrs[attr] = resp[attr] + self._reset_dirty() + return self + def create_connection(preferences, user_agent, **kwargs): try: diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index 5d26fb6c..5dffd7de 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -64,10 +64,10 @@ class Client(object): except Exception as ex: client_exc.parse_exception(ex) - def create(self, cls, params): + def create(self, cls, params, extra_attrs=False): obj = cls.new(**params) try: - return obj.create(self.session) + return obj.create(self.session, extra_attrs=extra_attrs) except Exception as ex: client_exc.parse_exception(ex) diff --git a/senlinclient/v1/models.py b/senlinclient/v1/models.py index 75e5bcfe..131a4f3d 100644 --- a/senlinclient/v1/models.py +++ b/senlinclient/v1/models.py @@ -194,6 +194,52 @@ class Policy(resource.Resource): return pb_dict +class Webhook(resource.Resource): + resource_key = 'webhook' + resources_key = 'webhooks' + base_path = '/webhooks' + service = clustering_service.ClusteringService() + + # Capabilities + allow_list = True + allow_retrieve = True + allow_create = True + allow_delete = True + + # Properties + id = resource.prop('id') + user = resource.prop('user') + domain = resource.prop('domain') + project = resource.prop('project') + name = resource.prop('name') + obj_type = resource.prop('obj_type') + obj_id = resource.prop('obj_id') + action = resource.prop('action') + created_time = resource.prop('created_time') + deleted_time = resource.prop('deleted_time') + credential = resource.prop('credential', type=dict) + params = resource.prop('params', type=dict) + url = resource.prop('url') + + def to_dict(self): + webhook_dict = { + 'id': self.id, + 'user': self.user, + 'domain': self.domain, + 'project': self.project, + 'name': self.name, + 'obj_type': self.obj_type, + 'obj_id': self.obj_id, + 'action': self.action, + 'credential': self.credential, + 'params': self.params, + 'created_time': self.created_time, + 'deleted_time': self.deleted_time, + 'url': self.url, + } + return webhook_dict + + class Cluster(resource.Resource): resource_key = 'cluster' resources_key = 'clusters' diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index ec1b3ad3..c06534e1 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -273,6 +273,109 @@ def do_policy_type_schema(sc, args): print(utils.format_output(schema)) +# WEBHOOKS + + +@utils.arg('-d', '--show-deleted', default=False, action="store_true", + help=_('Include deleted webhooks if any.')) +@utils.arg('-l', '--limit', metavar='', + help=_('Limit the number of webhooks returned.')) +@utils.arg('-m', '--marker', metavar='', + help=_('Only return webhooks that appear after the given ID.')) +@utils.arg('-F', '--full-id', default=False, action="store_true", + help=_('Print full IDs in list.')) +def do_webhook_list(sc, args=None): + '''List webhooks that meet the criteria.''' + def _short_id(obj): + return obj.id[:8] + ' ...' + + fields = ['id', 'name', 'obj_id', 'obj_type', 'action', 'created_time'] + queries = { + 'show_deleted': args.show_deleted, + 'limit': args.limit, + 'marker': args.marker, + } + + webhooks = sc.list(models.Webhook, **queries) + formatters = {} + if not args.full_id: + formatters = { + 'id': _short_id, + } + utils.print_list(webhooks, fields, formatters=formatters, sortby_index=1) + + +def _show_webhook(sc, webhook_id=None, webhook=None): + if webhook is None: + try: + params = {'id': webhook_id} + webhook = sc.get(models.Webhook, params) + except exc.HTTPNotFound: + raise exc.CommandError(_('Webhook not found: %s') % webhook_id) + + formatters = {} + utils.print_dict(webhook.to_dict(), formatters=formatters) + + +@utils.arg('id', metavar='', + help=_('Name of the webhook to be updated.')) +def do_webhook_show(sc, args): + '''Show the webhook details.''' + _show_webhook(sc, webhook_id=args.id) + + +@utils.arg('-t', '--obj-type', metavar='', required=True, + help=_('Object type name used for this webhook.')) +@utils.arg('-i', '--obj-id', metavar='', required=True, + help=_('Object id used for this webhook.')) +@utils.arg('-a', '--action', metavar='', required=True, + help=_('Name of action used for this webhook.')) +@utils.arg('-c', '--credential', metavar='', + required=True, + help=_('The credential used when triggering the webhook.'), + action='append') +@utils.arg('-p', '--params', metavar='', + help=_('A dictionary of parameters that will be passed to object ' + 'action when webhook is triggered.'), + action='append') +@utils.arg('name', metavar='', + help=_('Name of the webhook to create.')) +def do_webhook_create(sc, args): + '''Create a webhook.''' + params = { + 'name': args.name, + 'obj_id': args.obj_id, + 'obj_type': args.obj_type, + 'action': args.action, + 'credential': utils.format_parameters(args.credential), + 'params': utils.format_parameters(args.params) + } + + webhook = sc.create(models.Webhook, params, True) + _show_webhook(sc, webhook=webhook) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of webhook(s) to delete.')) +def do_webhook_delete(sc, args): + '''Delete webhook(s).''' + failure_count = 0 + + for cid in args.id: + try: + query = { + 'id': cid, + } + sc.delete(models.Webhook, query) + except exc.HTTPNotFound as ex: + failure_count += 1 + print(ex) + if failure_count == len(args.id): + msg = _('Failed to delete any of the specified webhook(s).') + raise exc.CommandError(msg) + print('Webhook deleted: %s' % args.id) + + # POLICIES