Skip to content

Commit

Permalink
Merge pull request stfc#69 from stfc/action_for_migrate_flavor
Browse files Browse the repository at this point in the history
Actions for migrating Flavors
  • Loading branch information
DavidFair authored Nov 29, 2022
2 parents fa7e55e + b375fcb commit 4865c48
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
23 changes: 23 additions & 0 deletions actions/flavor.list.missing.flavors.yaml
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
36 changes: 36 additions & 0 deletions actions/src/flavor_actions.py
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,
)
1 change: 0 additions & 1 deletion lib/openstack_api/openstack_flavor.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def get_flavor_specs(
extra_specs = flavor.extra_specs
else:
extra_specs = flavor_id.extra_specs

return extra_specs

def set_flavor_specs(self, cloud_account: str, flavor_id: str, extra_specs: dict):
Expand Down
47 changes: 47 additions & 0 deletions tests/actions/test_flavor_actions.py
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
)
1 change: 1 addition & 0 deletions tests/lib/test_openstack_flavor.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def test_set_specs(self):

self.instance.set_flavor_specs(mocked_cloud_name, flavor_id, extra_specs)
self.mocked_connection.assert_called_once_with(mocked_cloud_name)

self.api.set_flavor_specs.assert_called_once_with(flavor_id, extra_specs)

def test_empty_list_migrate_flavors(self):
Expand Down

0 comments on commit 4865c48

Please sign in to comment.