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.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()

View File

@@ -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):

View File

@@ -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)

View File

@@ -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

View File

@@ -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):

View File

@@ -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

View File

@@ -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

View File

@@ -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