bp/vitrage_template_loader - load templates from templates dir

Change-Id: I63f7ffce134486dbc5cf91a06e31b3871aacf266
This commit is contained in:
Liat Har-Tal 2016-01-28 13:52:47 +00:00
parent 9d7abdbcb6
commit befb9c3dd1
7 changed files with 188 additions and 3 deletions

View File

@ -0,0 +1,24 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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 os
def load_files(dir_path, suffix=None):
loaded_files = os.listdir(dir_path)
if suffix:
loaded_files = [f for f in loaded_files if f.endswith(suffix)]
return loaded_files

View File

@ -0,0 +1,21 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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.
from oslo_log import log
LOG = log.getLogger(__name__)
class Template(object):
pass

View File

@ -0,0 +1,37 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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 yaml
from oslo_log import log
from vitrage.common import file_utils
LOG = log.getLogger(__name__)
def load_templates_files(conf):
templates_dir_path = conf.evaluator.templates_dir
templates_files = file_utils.load_files(templates_dir_path, '.yaml')
templates_configs = []
for template_file in templates_files:
full_path = templates_dir_path + '/' + template_file
with open(full_path, 'r') as stream:
config = yaml.load(stream)
templates_configs.append(config)
return templates_configs

View File

@ -1,4 +1,4 @@
# Copyright 2015 - Alcatel-Lucent
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
@ -57,12 +57,12 @@ def _get_full_path(target_filename, target_folder):
"""
if target_folder is None:
target_folder = _get_resources_dir()
target_folder = get_resources_dir()
target = '{0}/{1}'.format(target_folder, target_filename)
return target
def _get_resources_dir(filename=None, target_folder='resources'):
def get_resources_dir(filename=None, target_folder='resources'):
"""Locates the resources directory dynamically.
:param filename: file to locate in directory

View File

@ -0,0 +1,36 @@
metadata:
id=host_high_cpu_load_to_instance_cpu_suboptimal
definitions:
entities:
- entity:
category: ALARM
type: HOST_HIGH_CPU_LOAD
internal_id: 1
- entity:
category: ALARM
type: VM_CPU_SUBOPTIMAL_PERFORMANCE
internal_id: 2
- entity:
category: RESOURCE
type: HOST
internal_id: 3
- entity:
category: RESOURCE
type: INSTANCE
internal_id: 4
relationships:
- relationship:
source: 1
target: 3
type: on
internal_id : alarm_on_host
- relationship:
source: 2
target: 4
type: on
internal_id : alarm_on_instance
- relationship:
source: 3
target: 4
type: contains
internal_id : host_contains_instance

View File

@ -0,0 +1,15 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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.
__author__ = 'stack'

View File

@ -0,0 +1,52 @@
# Copyright 2016 - Nokia
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, 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 os
from oslo_config import cfg
from oslo_log import log as logging
from vitrage.evaluator import template_loader
from vitrage.tests import base
from vitrage.tests.mocks import utils
LOG = logging.getLogger(__name__)
class TemplateLoaderTest(base.BaseTest):
OPTS = [
cfg.StrOpt('templates_dir',
default=utils.get_resources_dir() + '/templates',
),
]
def setUp(self):
super(TemplateLoaderTest, self).setUp()
self.template_dir_path = utils.get_resources_dir() + '/templates'
self.conf = cfg.ConfigOpts()
self.conf.register_opts(self.OPTS, group='evaluator')
def test_template_loader(self):
# Setup
total_templates = os.listdir(self.template_dir_path)
# Action
template_configs = template_loader.load_templates_files(self.conf)
# Test assertions
self.assertEqual(len(total_templates), len(template_configs))