From 4422878ec73c8b5612e5875ba35f198e951d2c26 Mon Sep 17 00:00:00 2001 From: licanwei Date: Thu, 4 Jun 2020 16:45:01 +0800 Subject: [PATCH] Compatible with old scope format Scope format changed from old to new after bp cdm-scoping. old format: - availability_zones: - name: nova - host_aggregates: - id: 1 - name: agg - exclude: - compute_nodes: - name: w012 new format: - compute: - availability_zones: - name: nova - host_aggregates: - id: 1 - name: agg - exclude: - compute_nodes: - name: w012 Change-Id: I2b5cd4d1cee19f5588e4d2185eb074343fff1187 Closed-Bug: #1882049 (cherry picked from commit 4a1915bec4c53f6aa893e649d14cd7106a983817) --- watcher/api/controllers/v1/audit_template.py | 3 +++ watcher/tests/api/v1/test_audit_templates.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/watcher/api/controllers/v1/audit_template.py b/watcher/api/controllers/v1/audit_template.py index b493270a8..933b4e8a5 100644 --- a/watcher/api/controllers/v1/audit_template.py +++ b/watcher/api/controllers/v1/audit_template.py @@ -138,6 +138,9 @@ class AuditTemplatePostType(wtypes.Base): raise exception.InvalidGoal(goal=audit_template.goal) if audit_template.scope: + keys = [list(s)[0] for s in audit_template.scope] + if keys[0] not in ('compute', 'storage'): + audit_template.scope = [dict(compute=audit_template.scope)] common_utils.Draft4Validator( AuditTemplatePostType._build_schema() ).validate(audit_template.scope) diff --git a/watcher/tests/api/v1/test_audit_templates.py b/watcher/tests/api/v1/test_audit_templates.py index dc990e4d2..44fa33148 100644 --- a/watcher/tests/api/v1/test_audit_templates.py +++ b/watcher/tests/api/v1/test_audit_templates.py @@ -663,6 +663,26 @@ class TestPost(FunctionalTestWithSetup): self.assertEqual(400, response.status_int) assert not cn_mock.called + def test_create_audit_template_with_old_scope(self): + scope = [{'host_aggregates': [{'id': '*'}]}, + {'availability_zones': [{'name': 'AZ1'}, + {'name': 'AZ2'}]}, + {'exclude': [ + {'instances': [ + {'uuid': 'INSTANCE_1'}, + {'uuid': 'INSTANCE_2'}]}, + {'compute_nodes': [ + {'name': 'Node_1'}, + {'name': 'Node_2'}]}, + ]} + ] + audit_template_dict = post_get_test_audit_template( + goal=self.fake_goal1.uuid, + strategy=self.fake_strategy1.uuid, scope=scope) + response = self.post_json('/audit_templates', + audit_template_dict) + self.assertEqual(201, response.status_int) + class TestDelete(api_base.FunctionalTest):