forked from stfc/st2-cloud-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request stfc#69 from stfc/action_for_migrate_flavor
Actions for migrating Flavors
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
description: List missing flavors | ||
enabled: true | ||
entry_point: src/flavor_actions.py | ||
name: flavor.list.missing.flavors | ||
parameters: | ||
timeout: | ||
default: 5400 | ||
submodule: | ||
default: list_missing_flavors | ||
type: string | ||
immutable: true | ||
source_cloud: | ||
description: The clouds.yaml account for source Cloud to use whilst performing this action | ||
required: true | ||
type: string | ||
default: "prod" | ||
dest_cloud: | ||
description: The clouds.yaml account for the destination Cloud to use while performing this action | ||
required: true | ||
type: string | ||
default: "dev" | ||
runner_type: python-script |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Dict, Callable, List | ||
from openstack_api.openstack_flavor import OpenstackFlavor | ||
from st2common.runners.base_action import Action | ||
|
||
|
||
class FlavorActions(Action): | ||
def __init__(self, *args, config: Dict = None, **kwargs): | ||
"""constructor class""" | ||
super().__init__(*args, config=config, **kwargs) | ||
self._flavor_api: OpenstackFlavor = config.get( | ||
"openstack_flavor_api", OpenstackFlavor() | ||
) | ||
|
||
def run(self, submodule: str, **kwargs): | ||
""" | ||
Dynamically dispatches to the method wanted | ||
""" | ||
func: Callable = getattr(self, submodule) | ||
return func(**kwargs) | ||
|
||
def list_missing_flavors( | ||
self, | ||
source_cloud: str, | ||
dest_cloud: str, | ||
) -> List[str]: | ||
""" | ||
Calls missing_flavors from _flavor_api to get a list of flavors that are | ||
in the source cloud but are missing from the destination cloud. | ||
:param source_cloud: Cloud account for source cloud | ||
:param dest_cloud: Cloud account for destination cloud | ||
:returns: List of the names of missing flavors or empty List if no flavors are missing | ||
""" | ||
return self._flavor_api.migrate_flavors( | ||
source_cloud=source_cloud, | ||
dest_cloud=dest_cloud, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from unittest.mock import create_autospec, NonCallableMock | ||
|
||
from openstack_api.openstack_flavor import OpenstackFlavor | ||
from src.flavor_actions import FlavorActions | ||
from tests.actions.openstack_action_test_base import OpenstackActionTestBase | ||
|
||
|
||
class TestFlavorActions(OpenstackActionTestBase): | ||
""" | ||
Unit tests for Flavor.* actions | ||
""" | ||
|
||
action_cls = FlavorActions | ||
|
||
# pylint: disable=invalid-name | ||
|
||
def setUp(self): | ||
""" | ||
Prepares the mock API and injects it into a new instance | ||
""" | ||
super().setUp() | ||
self.flavor_mock = create_autospec(OpenstackFlavor) | ||
self.action: FlavorActions = self.get_action_instance( | ||
api_mocks={"openstack_flavor_api": self.flavor_mock} | ||
) | ||
|
||
def test_run_method(self): | ||
""" | ||
Tests that run can dispatch to the Stackstorm facing methods | ||
""" | ||
expected_methods = [ | ||
"list_missing_flavors", | ||
] | ||
self._test_run_dynamic_dispatch(expected_methods) | ||
|
||
def test_migrate_flavors(self): | ||
""" | ||
Tests the action that lists missing flavors (if any) | ||
""" | ||
source_cloud, dest_cloud = NonCallableMock(), NonCallableMock() | ||
returned = self.action.list_missing_flavors( | ||
source_cloud=source_cloud, dest_cloud=dest_cloud | ||
) | ||
assert returned == self.flavor_mock.migrate_flavors.return_value | ||
self.flavor_mock.migrate_flavors.assert_called_once_with( | ||
source_cloud=source_cloud, dest_cloud=dest_cloud | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters