diff --git a/watcher/applier/mapper/default.py b/watcher/applier/mapper/default.py index 1dac0dbf1..0d45eb336 100644 --- a/watcher/applier/mapper/default.py +++ b/watcher/applier/mapper/default.py @@ -19,10 +19,12 @@ from watcher.applier.mapper.base import BaseActionMapper -from watcher.applier.primitive.hypervisor_state import HypervisorStateCommand -from watcher.applier.primitive.migration import MigrateCommand -from watcher.applier.primitive.nop import NopCommand -from watcher.applier.primitive.power_state import PowerStateCommand +from watcher.applier.primitives.change_nova_service_state import \ + ChangeNovaServiceState +from watcher.applier.primitives.migration import Migrate +from watcher.applier.primitives.nop import Nop +from watcher.applier.primitives.power_state import ChangePowerState + from watcher.common.exception import ActionNotFound from watcher.decision_engine.planner.default import Primitives @@ -30,18 +32,18 @@ from watcher.decision_engine.planner.default import Primitives class DefaultActionMapper(BaseActionMapper): def build_primitive_from_action(self, action): if action.action_type == Primitives.COLD_MIGRATE.value: - return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE, - action.src, - action.dst) + return Migrate(action.applies_to, Primitives.COLD_MIGRATE, + action.src, + action.dst) elif action.action_type == Primitives.LIVE_MIGRATE.value: - return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE, - action.src, - action.dst) + return Migrate(action.applies_to, Primitives.COLD_MIGRATE, + action.src, + action.dst) elif action.action_type == Primitives.HYPERVISOR_STATE.value: - return HypervisorStateCommand(action.applies_to, action.parameter) + return ChangeNovaServiceState(action.applies_to, action.parameter) elif action.action_type == Primitives.POWER_STATE.value: - return PowerStateCommand() + return ChangePowerState() elif action.action_type == Primitives.NOP.value: - return NopCommand() + return Nop() else: raise ActionNotFound() diff --git a/watcher/applier/primitive/__init__.py b/watcher/applier/primitives/__init__.py similarity index 100% rename from watcher/applier/primitive/__init__.py rename to watcher/applier/primitives/__init__.py diff --git a/watcher/applier/primitive/base.py b/watcher/applier/primitives/base.py similarity index 97% rename from watcher/applier/primitive/base.py rename to watcher/applier/primitives/base.py index 2e9b86efd..29248b60e 100644 --- a/watcher/applier/primitive/base.py +++ b/watcher/applier/primitives/base.py @@ -22,7 +22,7 @@ from watcher.applier.promise import Promise @six.add_metaclass(abc.ABCMeta) -class PrimitiveCommand(object): +class BasePrimitive(object): @Promise @abc.abstractmethod def execute(self): diff --git a/watcher/applier/primitive/hypervisor_state.py b/watcher/applier/primitives/change_nova_service_state.py similarity index 50% rename from watcher/applier/primitive/hypervisor_state.py rename to watcher/applier/primitives/change_nova_service_state.py index a8a4be417..eff149032 100644 --- a/watcher/applier/primitive/hypervisor_state.py +++ b/watcher/applier/primitives/change_nova_service_state.py @@ -20,42 +20,61 @@ from oslo_config import cfg -from watcher.applier.primitive.base import PrimitiveCommand -from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper +from watcher.applier.primitives.base import BasePrimitive +from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper from watcher.applier.promise import Promise - +from watcher.common.exception import IllegalArgumentException from watcher.common.keystone import KeystoneClient from watcher.decision_engine.model.hypervisor_state import HypervisorState CONF = cfg.CONF -class HypervisorStateCommand(PrimitiveCommand): - def __init__(self, host, status): - self.host = host - self.status = status +class ChangeNovaServiceState(BasePrimitive): + def __init__(self, host, state): + """This class allows us to change the state of nova-compute service. - def nova_manage_service(self, status): - keystone = KeystoneClient() - wrapper = NovaWrapper(keystone.get_credentials(), - session=keystone.get_session()) - if status is True: - return wrapper.enable_service_nova_compute(self.host) - else: - return wrapper.disable_service_nova_compute(self.host) + :param host: the uuid of the host + :param state: (enabled/disabled) + """ + super(BasePrimitive, self).__init__() + self._host = host + self._state = state + + @property + def host(self): + return self._host + + @property + def state(self): + return self._state @Promise def execute(self): - if self.status == HypervisorState.OFFLINE.value: - state = False + target_state = None + if self.state == HypervisorState.OFFLINE.value: + target_state = False elif self.status == HypervisorState.ONLINE.value: - state = True - return self.nova_manage_service(state) + target_state = True + return self.nova_manage_service(target_state) @Promise def undo(self): - if self.status == HypervisorState.OFFLINE.value: - state = True - elif self.status == HypervisorState.ONLINE.value: - state = False - return self.nova_manage_service(state) + target_state = None + if self.state == HypervisorState.OFFLINE.value: + target_state = True + elif self.state == HypervisorState.ONLINE.value: + target_state = False + return self.nova_manage_service(target_state) + + def nova_manage_service(self, state): + if state is None: + raise IllegalArgumentException("The target state is not defined") + + keystone = KeystoneClient() + wrapper = NovaWrapper(keystone.get_credentials(), + session=keystone.get_session()) + if state is True: + return wrapper.enable_service_nova_compute(self.host) + else: + return wrapper.disable_service_nova_compute(self.host) diff --git a/watcher/applier/primitive/migration.py b/watcher/applier/primitives/migration.py similarity index 94% rename from watcher/applier/primitive/migration.py rename to watcher/applier/primitives/migration.py index 2e370b68b..461460185 100644 --- a/watcher/applier/primitive/migration.py +++ b/watcher/applier/primitives/migration.py @@ -21,8 +21,8 @@ from keystoneclient.auth.identity import v3 from keystoneclient import session from oslo_config import cfg -from watcher.applier.primitive.base import PrimitiveCommand -from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper +from watcher.applier.primitives.base import BasePrimitive +from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper from watcher.applier.promise import Promise from watcher.common.keystone import KeystoneClient from watcher.decision_engine.planner.default import Primitives @@ -30,11 +30,12 @@ from watcher.decision_engine.planner.default import Primitives CONF = cfg.CONF -class MigrateCommand(PrimitiveCommand): +class Migrate(BasePrimitive): def __init__(self, vm_uuid=None, migration_type=None, source_hypervisor=None, destination_hypervisor=None): + super(BasePrimitive, self).__init__() self.instance_uuid = vm_uuid self.migration_type = migration_type self.source_hypervisor = source_hypervisor diff --git a/watcher/applier/primitive/nop.py b/watcher/applier/primitives/nop.py similarity index 87% rename from watcher/applier/primitive/nop.py rename to watcher/applier/primitives/nop.py index fc78f2de2..78ef60200 100644 --- a/watcher/applier/primitive/nop.py +++ b/watcher/applier/primitives/nop.py @@ -19,16 +19,14 @@ from oslo_log import log -from watcher.applier.primitive.base import PrimitiveCommand +from watcher.applier.primitives.base import BasePrimitive from watcher.applier.promise import Promise LOG = log.getLogger(__name__) -class NopCommand(PrimitiveCommand): - def __init__(self): - pass +class Nop(BasePrimitive): @Promise def execute(self): diff --git a/watcher/applier/primitive/power_state.py b/watcher/applier/primitives/power_state.py similarity index 74% rename from watcher/applier/primitive/power_state.py rename to watcher/applier/primitives/power_state.py index 10f0e421a..37cfd3aa1 100644 --- a/watcher/applier/primitive/power_state.py +++ b/watcher/applier/primitives/power_state.py @@ -16,20 +16,16 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from watcher.applier.primitive.base import PrimitiveCommand +from watcher.applier.primitives.base import BasePrimitive from watcher.applier.promise import Promise -class PowerStateCommand(PrimitiveCommand): - def __init__(self): - pass +class ChangePowerState(BasePrimitive): @Promise def execute(self): - pass + raise NotImplementedError # pragma:no cover @Promise def undo(self): - # TODO(jde): migrate VM from target_hypervisor - # to current_hypervisor in model - return True + raise NotImplementedError # pragma:no cover diff --git a/watcher/applier/primitive/wrapper/__init__.py b/watcher/applier/primitives/wrapper/__init__.py similarity index 100% rename from watcher/applier/primitive/wrapper/__init__.py rename to watcher/applier/primitives/wrapper/__init__.py diff --git a/watcher/applier/primitive/wrapper/nova_wrapper.py b/watcher/applier/primitives/wrapper/nova_wrapper.py similarity index 100% rename from watcher/applier/primitive/wrapper/nova_wrapper.py rename to watcher/applier/primitives/wrapper/nova_wrapper.py diff --git a/watcher/metrics_engine/cluster_model_collector/manager.py b/watcher/metrics_engine/cluster_model_collector/manager.py index f3777477b..a2ad6a41b 100644 --- a/watcher/metrics_engine/cluster_model_collector/manager.py +++ b/watcher/metrics_engine/cluster_model_collector/manager.py @@ -20,7 +20,7 @@ from oslo_config import cfg from oslo_log import log -from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper +from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper from watcher.common.keystone import KeystoneClient from watcher.metrics_engine.cluster_model_collector.nova import \ NovaClusterModelCollector diff --git a/watcher/tests/applier/primitive/__init__.py b/watcher/tests/applier/actions/__init__.py similarity index 100% rename from watcher/tests/applier/primitive/__init__.py rename to watcher/tests/applier/actions/__init__.py diff --git a/watcher/tests/applier/primitive/wrapper/__init__.py b/watcher/tests/applier/actions/wrapper/__init__.py similarity index 100% rename from watcher/tests/applier/primitive/wrapper/__init__.py rename to watcher/tests/applier/actions/wrapper/__init__.py diff --git a/watcher/tests/applier/primitive/wrapper/test_nova_wrapper.py b/watcher/tests/applier/actions/wrapper/test_nova_wrapper.py similarity index 97% rename from watcher/tests/applier/primitive/wrapper/test_nova_wrapper.py rename to watcher/tests/applier/actions/wrapper/test_nova_wrapper.py index 79d36ea43..aeab4806b 100644 --- a/watcher/tests/applier/primitive/wrapper/test_nova_wrapper.py +++ b/watcher/tests/applier/actions/wrapper/test_nova_wrapper.py @@ -18,7 +18,7 @@ # import mock import time -from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper +from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper from watcher.common import utils from watcher.tests import base