Coverage for watcher/decision_engine/solution/default.py: 91%

25 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# -*- encoding: utf-8 -*- 

2# Copyright (c) 2015 b<>com 

3# 

4# Authors: Jean-Emile DARTOIS <jean-emile.dartois@b-com.com> 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, software 

13# distributed under the License is distributed on an "AS IS" BASIS, 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 

15# implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18# 

19from oslo_log import log 

20 

21from watcher.applier.actions import base as baction 

22from watcher.common import exception 

23from watcher.decision_engine.solution import base 

24 

25LOG = log.getLogger(__name__) 

26 

27 

28class DefaultSolution(base.BaseSolution): 

29 def __init__(self, goal, strategy): 

30 """Stores a set of actions generated by a strategy 

31 

32 The DefaultSolution class store a set of actions generated by a 

33 strategy in order to achieve the goal. 

34 

35 :param goal: Goal associated to this solution 

36 :type goal: :py:class:`~.base.Goal` instance 

37 :param strategy: Strategy associated to this solution 

38 :type strategy: :py:class:`~.BaseStrategy` instance 

39 """ 

40 super(DefaultSolution, self).__init__(goal, strategy) 

41 self._actions = [] 

42 

43 def add_action(self, action_type, input_parameters=None, resource_id=None): 

44 if input_parameters is not None: 

45 if baction.BaseAction.RESOURCE_ID in input_parameters.keys(): 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true

46 raise exception.ReservedWord(name=baction.BaseAction. 

47 RESOURCE_ID) 

48 else: 

49 input_parameters = {} 

50 

51 if resource_id is not None: 

52 input_parameters[baction.BaseAction.RESOURCE_ID] = resource_id 

53 action = { 

54 'action_type': action_type, 

55 'input_parameters': input_parameters 

56 } 

57 if action not in self._actions: 

58 self._actions.append(action) 

59 else: 

60 LOG.warning('Action %s has been added into the solution, ' 

61 'duplicate action will be dropped.', str(action)) 

62 

63 def __str__(self): 

64 return "\n".join(self._actions) 

65 

66 @property 

67 def actions(self): 

68 """Get the current actions of the solution""" 

69 return self._actions