External OpenStack action mapping file support

Release note and command line parameter added.
Additionally from now on user don't need list all the
openstack modules in mapping file.

Change-Id: Ibdd2e2e471ecf23016b44f742166f6b22f1649b0
This commit is contained in:
Istvan Imre 2017-01-26 09:38:33 +01:00
parent ab2919e389
commit a5fd5d452a
4 changed files with 72 additions and 4 deletions

View File

@ -23,12 +23,19 @@ from mistral.actions import action_generator
from mistral.utils import inspect_utils as i_u
from mistral import version
os_actions_mapping_path = cfg.StrOpt('openstack_actions_mapping_path',
default='actions/openstack/mapping.json')
os_actions_mapping_path = cfg.StrOpt(
'openstack_actions_mapping_path',
short='m',
metavar='MAPPING_PATH',
default='actions/openstack/mapping.json',
help='Path to openstack action mapping json file.'
'It could be relative to mistral package '
'directory or absolute.'
)
CONF = cfg.CONF
CONF.register_opt(os_actions_mapping_path)
CONF.register_cli_opt(os_actions_mapping_path)
LOG = logging.getLogger(__name__)
@ -84,7 +91,7 @@ class OpenStackActionGenerator(action_generator.ActionGenerator):
@classmethod
def create_actions(cls):
mapping = get_mapping()
method_dict = mapping[cls.action_namespace]
method_dict = mapping.get(cls.action_namespace, {})
action_classes = []

View File

@ -0,0 +1,6 @@
{
"_comment": "Mapping OpenStack action namespaces to all its actions. Each action name is mapped to python-client method name in this namespace.",
"nova": {
"servers_get": "servers.get"
}
}

View File

@ -10,11 +10,21 @@
# 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 contextlib
import os
from oslo_config import cfg
from mistral.actions import generator_factory
from mistral.actions.openstack import actions
from mistral.tests.unit import base
ABSOLUTE_TEST_MAPPING_PATH = os.path.realpath(
os.path.join(os.path.dirname(__file__),
"../../../resources/openstack/test_mapping.json")
)
RELATIVE_TEST_MAPPING_PATH = "tests/resources/openstack/test_mapping.json"
MODULE_MAPPING = {
'nova': ['nova.servers_get', actions.NovaAction],
@ -44,6 +54,9 @@ MODULE_MAPPING = {
EXTRA_MODULES = ['neutron', 'swift', 'zaqar', 'tacker']
CONF = cfg.CONF
class GeneratorTest(base.BaseTest):
def test_generator(self):
for generator_cls in generator_factory.all_generators():
@ -65,3 +78,38 @@ class GeneratorTest(base.BaseTest):
self.assertTrue(issubclass(action['class'], action_cls))
self.assertEqual(method_name, action['class'].client_method_name)
def test_missing_module_from_mapping(self):
with _patch_openstack_action_mapping_path(RELATIVE_TEST_MAPPING_PATH):
for generator_cls in generator_factory.all_generators():
action_classes = generator_cls.create_actions()
action_names = [action['name'] for action in action_classes]
cls = MODULE_MAPPING.get(generator_cls.action_namespace)[1]
if cls == actions.NovaAction:
self.assertEqual(['nova.servers_get'], action_names)
else:
self.assertEqual([], action_names)
def test_absolute_mapping_path(self):
with _patch_openstack_action_mapping_path(ABSOLUTE_TEST_MAPPING_PATH):
self.assertTrue(os.path.isabs(ABSOLUTE_TEST_MAPPING_PATH),
"Mapping path is relative: %s" %
ABSOLUTE_TEST_MAPPING_PATH)
for generator_cls in generator_factory.all_generators():
action_classes = generator_cls.create_actions()
action_names = [action['name'] for action in action_classes]
cls = MODULE_MAPPING.get(generator_cls.action_namespace)[1]
if cls == actions.NovaAction:
self.assertEqual(['nova.servers_get'], action_names)
else:
self.assertEqual([], action_names)
@contextlib.contextmanager
def _patch_openstack_action_mapping_path(path):
original_path = CONF.openstack_actions_mapping_path
CONF.set_default("openstack_actions_mapping_path", path)
yield
CONF.set_default("openstack_actions_mapping_path", original_path)

View File

@ -0,0 +1,7 @@
---
features:
- Openstack action mapping file could be specified at sync_db.sh script.
For more details see 'sync_db.sh --help'.
Additionally it is not required from now to list all of the modules
in mapping file.