-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subsequent tests for filter and oci client. Reorganise the code to about DRY code. Add a new common file with common functions Isort the module imports for better organisation Add object type to List return annotation for annotation clarity Restore .gitignore Signed-off-by: Nazareno Feito <[email protected]>
- Loading branch information
Nazareno Feito
committed
Dec 7, 2018
1 parent
b855242
commit ccd053a
Showing
8 changed files
with
177 additions
and
73 deletions.
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
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 |
---|---|---|
@@ -1,26 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
from oci.core import ComputeClient | ||
from random import choice | ||
from typing import Any, Dict, List | ||
|
||
from chaoslib.exceptions import ActivityFailed | ||
from chaoslib.types import Configuration, Secrets | ||
from oci.config import from_file | ||
from oci.core import ComputeClient | ||
|
||
from chaosoci import oci_client | ||
from chaosoci.types import OCIResponse | ||
|
||
from .common import filter_instances, get_instances | ||
|
||
__all__ = ["stop_instance"] | ||
__all__ = ["stop_instance", "stop_random_instance"] | ||
|
||
|
||
def stop_instance(instance_id: str, force: bool = False, | ||
configuration: Configuration = None, | ||
secrets: Secrets = None) -> OCIResponse: | ||
"""Stop a given Compute instance.""" | ||
action = "SOFTSTOP" | ||
client = oci_client(ComputeClient, configuration, secrets, | ||
skip_deserialization=True) | ||
|
||
if force is True: | ||
action = "STOP" | ||
action = "STOP" if force else "SOFTSTOP" | ||
ret = client.instance_action(instance_id=instance_id, action=action).data | ||
|
||
return ret | ||
|
||
|
||
def stop_random_instance(filters: List[Dict[str, Any]], | ||
compartment_id: str = None, | ||
force: bool = False, | ||
configuration: Configuration = None, | ||
secrets: Secrets = None) -> OCIResponse: | ||
""" | ||
Stop a a random compute instance within a given compartment. | ||
If filters are provided, the scope will be reduced to those instances | ||
matching the filters. | ||
Please refer to: https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/api/core/models/oci.core.models.Instance.html#oci.core.models.Instance | ||
for details on the available filters under the 'parameters' section. | ||
""" # noqa: E501 | ||
client = oci_client(ComputeClient, configuration, secrets, | ||
skip_deserialization=False) | ||
|
||
action = "STOP" if force else "SOFTSTOP" | ||
|
||
compartment_id = compartment_id or from_file().get('compartment') | ||
if compartment_id is None: | ||
raise ActivityFailed('We have not been able to find a compartment,' | ||
' without one, we cannot continue.') | ||
|
||
instances = get_instances(client, compartment_id) | ||
|
||
filters = filters or None | ||
if filters is not None: | ||
instances = filter_instances(instances, filters=filters) | ||
|
||
instance_id = choice(instances).id | ||
|
||
ret = client.instance_action(instance_id=instance_id, action=action) | ||
s_client = oci_client(ComputeClient, configuration, secrets, | ||
skip_deserialization=True) | ||
ret = s_client.instance_action(instance_id=instance_id, action=action) | ||
|
||
return ret.data |
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,54 @@ | ||
from typing import Any, Dict, List | ||
|
||
from chaoslib.exceptions import ActivityFailed | ||
from oci.core import ComputeClient | ||
|
||
from chaosoci.types import OCIInstance | ||
|
||
__all__ = ["get_instances", "filter_instances"] | ||
|
||
|
||
def get_instances(client: ComputeClient = None, | ||
compartment_id: str = None) -> List[OCIInstance]: | ||
"""Return a complete, unfiltered list of instances in the compartment.""" | ||
instances = [] | ||
|
||
instances_raw = client.list_instances(compartment_id=compartment_id) | ||
instances.extend(instances_raw.data) | ||
while instances_raw.has_next_page: | ||
instances_raw = client.list_instances(compartment_id=compartment_id, | ||
page=instances_raw.next_page) | ||
instances.extend(instances_raw.data) | ||
|
||
return instances | ||
|
||
|
||
def filter_instances(instances: List[OCIInstance] = None, | ||
filters: Dict[str, Any] = None) -> List[OCIInstance]: | ||
"""Return only those instances that match the filters provided.""" | ||
instances = instances or None | ||
|
||
if instances is None: | ||
raise ActivityFailed('No instances were found.') | ||
|
||
filters_set = {x for x in filters} | ||
available_filters_set = {x for x in instances[0].attribute_map} | ||
|
||
# Partial filtering may return instances we do not want. We avoid it. | ||
if not filters_set.issubset(available_filters_set): | ||
raise ActivityFailed('Some of the chosen filters were not found,' | ||
' we cannot continue.') | ||
|
||
# Walk the instances and find those that match the given filters. | ||
filtered = [] | ||
for instance in instances: | ||
sentinel = True | ||
for attr, val in filters.items(): | ||
if val != getattr(instance, attr, None): | ||
sentinel = False | ||
break | ||
|
||
if sentinel: | ||
filtered.append(instance) | ||
|
||
return filtered |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# -*- coding: utf-8 -*- | ||
from typing import Any, Dict | ||
from oci.core.models.instance import Instance | ||
|
||
__all__ = ["OCIResponse"] | ||
__all__ = ["OCIResponse", "OCIInstance"] | ||
|
||
# really dependent on the type of resource called | ||
OCIResponse = Dict[str, Any] | ||
OCIInstance = Instance |
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