Watcher API supports strategy name when creating audit template
when directly using the `curl` command to create audit template,
strategy name can be accepted.
Closes-Bug: #1884174
Change-Id: I7c0ca760a7fa414faca03c5293df34a84aad6fac
(cherry picked from commit 3f7a508a2e
)
This commit is contained in:
parent
ec62bbe5bd
commit
0543ccfe74
|
@ -161,18 +161,23 @@ class AuditTemplatePostType(wtypes.Base):
|
||||||
"included and excluded together"))
|
"included and excluded together"))
|
||||||
|
|
||||||
if audit_template.strategy:
|
if audit_template.strategy:
|
||||||
available_strategies = objects.Strategy.list(
|
try:
|
||||||
AuditTemplatePostType._ctx)
|
if (common_utils.is_uuid_like(audit_template.strategy) or
|
||||||
available_strategies_map = {
|
common_utils.is_int_like(audit_template.strategy)):
|
||||||
s.uuid: s for s in available_strategies}
|
strategy = objects.Strategy.get(
|
||||||
if audit_template.strategy not in available_strategies_map:
|
AuditTemplatePostType._ctx, audit_template.strategy)
|
||||||
|
else:
|
||||||
|
strategy = objects.Strategy.get_by_name(
|
||||||
|
AuditTemplatePostType._ctx, audit_template.strategy)
|
||||||
|
except Exception:
|
||||||
raise exception.InvalidStrategy(
|
raise exception.InvalidStrategy(
|
||||||
strategy=audit_template.strategy)
|
strategy=audit_template.strategy)
|
||||||
|
|
||||||
strategy = available_strategies_map[audit_template.strategy]
|
|
||||||
# Check that the strategy we indicate is actually related to the
|
# Check that the strategy we indicate is actually related to the
|
||||||
# specified goal
|
# specified goal
|
||||||
if strategy.goal_id != goal.id:
|
if strategy.goal_id != goal.id:
|
||||||
|
available_strategies = objects.Strategy.list(
|
||||||
|
AuditTemplatePostType._ctx)
|
||||||
choices = ["'%s' (%s)" % (s.uuid, s.name)
|
choices = ["'%s' (%s)" % (s.uuid, s.name)
|
||||||
for s in available_strategies]
|
for s in available_strategies]
|
||||||
raise exception.InvalidStrategy(
|
raise exception.InvalidStrategy(
|
||||||
|
|
|
@ -555,6 +555,35 @@ class TestPost(FunctionalTestWithSetup):
|
||||||
response.json['created_at']).replace(tzinfo=None)
|
response.json['created_at']).replace(tzinfo=None)
|
||||||
self.assertEqual(test_time, return_created_at)
|
self.assertEqual(test_time, return_created_at)
|
||||||
|
|
||||||
|
@mock.patch.object(timeutils, 'utcnow')
|
||||||
|
def test_create_audit_template_with_strategy_name(self, mock_utcnow):
|
||||||
|
audit_template_dict = post_get_test_audit_template(
|
||||||
|
goal=self.fake_goal1.uuid,
|
||||||
|
strategy=self.fake_strategy1.name)
|
||||||
|
test_time = datetime.datetime(2000, 1, 1, 0, 0)
|
||||||
|
mock_utcnow.return_value = test_time
|
||||||
|
|
||||||
|
response = self.post_json('/audit_templates', audit_template_dict)
|
||||||
|
self.assertEqual('application/json', response.content_type)
|
||||||
|
self.assertEqual(201, response.status_int)
|
||||||
|
# Check location header
|
||||||
|
self.assertIsNotNone(response.location)
|
||||||
|
expected_location = \
|
||||||
|
'/v1/audit_templates/%s' % response.json['uuid']
|
||||||
|
self.assertEqual(urlparse.urlparse(response.location).path,
|
||||||
|
expected_location)
|
||||||
|
self.assertTrue(utils.is_uuid_like(response.json['uuid']))
|
||||||
|
self.assertNotIn('updated_at', response.json.keys)
|
||||||
|
self.assertNotIn('deleted_at', response.json.keys)
|
||||||
|
self.assertEqual(self.fake_goal1.uuid, response.json['goal_uuid'])
|
||||||
|
self.assertEqual(self.fake_strategy1.uuid,
|
||||||
|
response.json['strategy_uuid'])
|
||||||
|
self.assertEqual(self.fake_strategy1.name,
|
||||||
|
response.json['strategy_name'])
|
||||||
|
return_created_at = timeutils.parse_isotime(
|
||||||
|
response.json['created_at']).replace(tzinfo=None)
|
||||||
|
self.assertEqual(test_time, return_created_at)
|
||||||
|
|
||||||
def test_create_audit_template_validation_with_aggregates(self):
|
def test_create_audit_template_validation_with_aggregates(self):
|
||||||
scope = [{'compute': [{'host_aggregates': [{'id': '*'}]},
|
scope = [{'compute': [{'host_aggregates': [{'id': '*'}]},
|
||||||
{'availability_zones': [{'name': 'AZ1'},
|
{'availability_zones': [{'name': 'AZ1'},
|
||||||
|
|
Loading…
Reference in New Issue