Tidy up - Primitive

Some Python class and packages need to be renamed
for a better compliance with the shared terminology
which provides a better understanding of Watcher
objects and components by every contributor.

This patchset is there to change
Primitive to Primitives.
Add BasePrimitive.

Partially implements: blueprint glossary-related-refactoring

Change-Id: I839bddd12b5320b338b2f207d74963afa23de522
This commit is contained in:
Jean-Emile DARTOIS
2015-12-07 11:36:22 +01:00
parent 64747cad1f
commit f98e96da42
13 changed files with 71 additions and 55 deletions

View File

@@ -19,10 +19,12 @@
from watcher.applier.mapper.base import BaseActionMapper from watcher.applier.mapper.base import BaseActionMapper
from watcher.applier.primitive.hypervisor_state import HypervisorStateCommand from watcher.applier.primitives.change_nova_service_state import \
from watcher.applier.primitive.migration import MigrateCommand ChangeNovaServiceState
from watcher.applier.primitive.nop import NopCommand from watcher.applier.primitives.migration import Migrate
from watcher.applier.primitive.power_state import PowerStateCommand from watcher.applier.primitives.nop import Nop
from watcher.applier.primitives.power_state import ChangePowerState
from watcher.common.exception import ActionNotFound from watcher.common.exception import ActionNotFound
from watcher.decision_engine.planner.default import Primitives from watcher.decision_engine.planner.default import Primitives
@@ -30,18 +32,18 @@ from watcher.decision_engine.planner.default import Primitives
class DefaultActionMapper(BaseActionMapper): class DefaultActionMapper(BaseActionMapper):
def build_primitive_from_action(self, action): def build_primitive_from_action(self, action):
if action.action_type == Primitives.COLD_MIGRATE.value: if action.action_type == Primitives.COLD_MIGRATE.value:
return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE, return Migrate(action.applies_to, Primitives.COLD_MIGRATE,
action.src, action.src,
action.dst) action.dst)
elif action.action_type == Primitives.LIVE_MIGRATE.value: elif action.action_type == Primitives.LIVE_MIGRATE.value:
return MigrateCommand(action.applies_to, Primitives.COLD_MIGRATE, return Migrate(action.applies_to, Primitives.COLD_MIGRATE,
action.src, action.src,
action.dst) action.dst)
elif action.action_type == Primitives.HYPERVISOR_STATE.value: 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: elif action.action_type == Primitives.POWER_STATE.value:
return PowerStateCommand() return ChangePowerState()
elif action.action_type == Primitives.NOP.value: elif action.action_type == Primitives.NOP.value:
return NopCommand() return Nop()
else: else:
raise ActionNotFound() raise ActionNotFound()

View File

@@ -22,7 +22,7 @@ from watcher.applier.promise import Promise
@six.add_metaclass(abc.ABCMeta) @six.add_metaclass(abc.ABCMeta)
class PrimitiveCommand(object): class BasePrimitive(object):
@Promise @Promise
@abc.abstractmethod @abc.abstractmethod
def execute(self): def execute(self):

View File

@@ -20,42 +20,61 @@
from oslo_config import cfg from oslo_config import cfg
from watcher.applier.primitive.base import PrimitiveCommand from watcher.applier.primitives.base import BasePrimitive
from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper
from watcher.applier.promise import Promise from watcher.applier.promise import Promise
from watcher.common.exception import IllegalArgumentException
from watcher.common.keystone import KeystoneClient from watcher.common.keystone import KeystoneClient
from watcher.decision_engine.model.hypervisor_state import HypervisorState from watcher.decision_engine.model.hypervisor_state import HypervisorState
CONF = cfg.CONF CONF = cfg.CONF
class HypervisorStateCommand(PrimitiveCommand): class ChangeNovaServiceState(BasePrimitive):
def __init__(self, host, status): def __init__(self, host, state):
self.host = host """This class allows us to change the state of nova-compute service.
self.status = status
def nova_manage_service(self, status): :param host: the uuid of the host
keystone = KeystoneClient() :param state: (enabled/disabled)
wrapper = NovaWrapper(keystone.get_credentials(), """
session=keystone.get_session()) super(BasePrimitive, self).__init__()
if status is True: self._host = host
return wrapper.enable_service_nova_compute(self.host) self._state = state
else:
return wrapper.disable_service_nova_compute(self.host) @property
def host(self):
return self._host
@property
def state(self):
return self._state
@Promise @Promise
def execute(self): def execute(self):
if self.status == HypervisorState.OFFLINE.value: target_state = None
state = False if self.state == HypervisorState.OFFLINE.value:
target_state = False
elif self.status == HypervisorState.ONLINE.value: elif self.status == HypervisorState.ONLINE.value:
state = True target_state = True
return self.nova_manage_service(state) return self.nova_manage_service(target_state)
@Promise @Promise
def undo(self): def undo(self):
if self.status == HypervisorState.OFFLINE.value: target_state = None
state = True if self.state == HypervisorState.OFFLINE.value:
elif self.status == HypervisorState.ONLINE.value: target_state = True
state = False elif self.state == HypervisorState.ONLINE.value:
return self.nova_manage_service(state) 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)

View File

@@ -21,8 +21,8 @@ from keystoneclient.auth.identity import v3
from keystoneclient import session from keystoneclient import session
from oslo_config import cfg from oslo_config import cfg
from watcher.applier.primitive.base import PrimitiveCommand from watcher.applier.primitives.base import BasePrimitive
from watcher.applier.primitive.wrapper.nova_wrapper import NovaWrapper from watcher.applier.primitives.wrapper.nova_wrapper import NovaWrapper
from watcher.applier.promise import Promise from watcher.applier.promise import Promise
from watcher.common.keystone import KeystoneClient from watcher.common.keystone import KeystoneClient
from watcher.decision_engine.planner.default import Primitives from watcher.decision_engine.planner.default import Primitives
@@ -30,11 +30,12 @@ from watcher.decision_engine.planner.default import Primitives
CONF = cfg.CONF CONF = cfg.CONF
class MigrateCommand(PrimitiveCommand): class Migrate(BasePrimitive):
def __init__(self, vm_uuid=None, def __init__(self, vm_uuid=None,
migration_type=None, migration_type=None,
source_hypervisor=None, source_hypervisor=None,
destination_hypervisor=None): destination_hypervisor=None):
super(BasePrimitive, self).__init__()
self.instance_uuid = vm_uuid self.instance_uuid = vm_uuid
self.migration_type = migration_type self.migration_type = migration_type
self.source_hypervisor = source_hypervisor self.source_hypervisor = source_hypervisor

View File

@@ -19,16 +19,14 @@
from oslo_log import log 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 from watcher.applier.promise import Promise
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
class NopCommand(PrimitiveCommand): class Nop(BasePrimitive):
def __init__(self):
pass
@Promise @Promise
def execute(self): def execute(self):

View File

@@ -16,20 +16,16 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# #
from watcher.applier.primitive.base import PrimitiveCommand from watcher.applier.primitives.base import BasePrimitive
from watcher.applier.promise import Promise from watcher.applier.promise import Promise
class PowerStateCommand(PrimitiveCommand): class ChangePowerState(BasePrimitive):
def __init__(self):
pass
@Promise @Promise
def execute(self): def execute(self):
pass raise NotImplementedError # pragma:no cover
@Promise @Promise
def undo(self): def undo(self):
# TODO(jde): migrate VM from target_hypervisor raise NotImplementedError # pragma:no cover
# to current_hypervisor in model
return True

View File

@@ -20,7 +20,7 @@
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log 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.common.keystone import KeystoneClient
from watcher.metrics_engine.cluster_model_collector.nova import \ from watcher.metrics_engine.cluster_model_collector.nova import \
NovaClusterModelCollector NovaClusterModelCollector

View File

@@ -18,7 +18,7 @@
# #
import mock import mock
import time 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.common import utils
from watcher.tests import base from watcher.tests import base