From 0aaaf132789d977da0796b09b78a6d96a03d3a63 Mon Sep 17 00:00:00 2001 From: Alexander Chadin Date: Thu, 14 Apr 2016 16:13:07 +0300 Subject: [PATCH] Add audit-template name checking in CLI This patch set allows to send audit_template_uuid as uuid type only. If audit_template argument given as name, watcherclient will send request to get audit template uuid. Change-Id: Idf5f07ca08f2e5d871dc2163c32fbda9ed338a99 Related-Bug: #1532843 --- watcherclient/common/utils.py | 14 ++++++++++++++ watcherclient/v1/audit_shell.py | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/watcherclient/common/utils.py b/watcherclient/common/utils.py index bb5b9e3..990818e 100644 --- a/watcherclient/common/utils.py +++ b/watcherclient/common/utils.py @@ -19,6 +19,7 @@ from __future__ import print_function import argparse import json +import uuid from oslo_utils import importutils @@ -178,3 +179,16 @@ def common_filters(limit=None, sort_key=None, sort_dir=None): if sort_dir is not None: filters.append('sort_dir=%s' % sort_dir) return filters + + +def is_uuid_like(val): + """Returns validation of a value as a UUID. + + For our purposes, a UUID is a canonical form string: + aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa + + """ + try: + return str(uuid.UUID(val)) == val + except (TypeError, ValueError, AttributeError): + return False diff --git a/watcherclient/v1/audit_shell.py b/watcherclient/v1/audit_shell.py index a882a1b..331d405 100644 --- a/watcherclient/v1/audit_shell.py +++ b/watcherclient/v1/audit_shell.py @@ -115,6 +115,10 @@ def do_audit_create(cc, args): field_list = ['audit_template_uuid', 'type', 'deadline'] fields = dict((k, v) for (k, v) in vars(args).items() if k in field_list and not (v is None)) + if fields.get('audit_template_uuid'): + if not utils.is_uuid_like(fields['audit_template_uuid']): + fields['audit_template_uuid'] = cc.audit_template.get( + fields['audit_template']) audit = cc.audit.create(**fields) field_list.append('uuid') data = dict([(f, getattr(audit, f, '')) for f in field_list])