Skip to content

Commit

Permalink
Adding more functionality (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: Akshat Akshat <[email protected]>
  • Loading branch information
akshat1145 authored Dec 15, 2021
1 parent 15f7064 commit 36da01a
Show file tree
Hide file tree
Showing 23 changed files with 2,085 additions and 39 deletions.
356 changes: 352 additions & 4 deletions chaosoci/core/compute/actions.py

Large diffs are not rendered by default.

52 changes: 49 additions & 3 deletions chaosoci/core/compute/common.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- coding: utf-8 -*-
__all__ = ["get_instances", "filter_instances"]
__all__ = ["get_instances", "filter_instances", "get_instance_pools", "filter_instance_pools"]

from typing import Any, Dict, List

from chaoslib.exceptions import ActivityFailed

from logzero import logger

from oci.core import ComputeClient
from oci.core.models import Instance
from oci.core import ComputeClient, ComputeManagementClient
from oci.core.models import Instance, InstancePool


def get_instances(client: ComputeClient = None,
Expand Down Expand Up @@ -55,3 +55,49 @@ def filter_instances(instances: List[Instance] = None,
filtered.append(instance)

return filtered


def get_instance_pools(client: ComputeManagementClient = None,
compartment_id: str = None) -> List[InstancePool]:
"""Return a complete, unfiltered list of Instance Pools in the compartment."""
instance_pools = []

instances_pool_raw = client.list_instance_pools(compartment_id=compartment_id)
instance_pools.extend(instances_pool_raw.data)
while instances_pool_raw.has_next_page:
instances_pool_raw = client.list_instances(compartment_id=compartment_id,
page=instances_pool_raw.next_page)
instance_pools.extend(instances_pool_raw.data)

return instance_pools


def filter_instance_pools(instance_pools: List[InstancePool] = None,
filters: Dict[str, Any] = None) -> List[InstancePool]:
"""Return only those Instance Pools that match the filters provided."""
instance_pools = instance_pools or None

if instance_pools is None:
raise ActivityFailed('No Instance Pools were found.')

filters_set = {x for x in filters}
available_filters_set = {x for x in instance_pools[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_pool in instance_pools:
sentinel = True
for attr, val in filters.items():
if val != getattr(instance_pool, attr, None):
sentinel = False
break

if sentinel:
filtered.append(instance_pool)

return filtered
34 changes: 31 additions & 3 deletions chaosoci/core/compute/probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from chaoslib.types import Configuration, Secrets

from oci.config import from_file
from oci.core import ComputeClient
from oci.core import ComputeClient, ComputeManagementClient

from chaosoci import oci_client

from .common import filter_instances, get_instances
from .common import filter_instances, get_instances, get_instance_pools

__all__ = ['count_instances']
__all__ = ['count_instances', 'count_instance_pools']


def count_instances(filters: List[Dict[str, Any]], compartment_id: str = None,
Expand Down Expand Up @@ -40,3 +40,31 @@ def count_instances(filters: List[Dict[str, Any]], compartment_id: str = None,
return len(filter_instances(instances, filters=filters))

return len(instances)


def count_instance_pools(filters: List[Dict[str, Any]], compartment_id: str = None,
configuration: Configuration = None,
secrets: Secrets = None) -> int:
"""
Return the number of instance pools in accordance with the given filters.
Please refer to: https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/api/core/models/oci.core.models.InstancePool.html#oci.core.models.Instance
for details on the available filters under the 'parameters' section.
""" # noqa: E501
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.')

client = oci_client(ComputeManagementClient, configuration, secrets,
skip_deserialization=False)

filters = filters or None
instance_pools = get_instance_pools(client, compartment_id)

if filters is not None:
return len(filter_instances(instance_pools, filters=filters))

return len(instance_pools)
Empty file.
112 changes: 112 additions & 0 deletions chaosoci/core/loadBalancer/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# -*- coding: utf-8 -*-

from chaoslib.types import Configuration, Secrets
from oci.load_balancer import LoadBalancerClient

from chaosoci import oci_client
from chaosoci.types import OCIResponse

__all__ = ["delete_backend_server", "delete_backend_set",
"delete_hostname", "delete_listener",
"delete_load_balancer", "delete_routing_policy",
"delete_path_route_set"]


# Compute Client Actions

def delete_backend_server(load_balancer_id: str,
backend_set_name: str,
backend_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given backend server"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)
delete_backend_response = client.delete_backend(load_balancer_id, backend_name, backend_set_name).data

return delete_backend_response


def delete_backend_set(load_balancer_id: str,
backend_set_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given backend set"""
client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=False)

delete_backend_set_response = client.delete_backend_set(load_balancer_id, backend_set_name).data

return delete_backend_set_response


# Compute Client Management Actions

def delete_hostname(load_balancer_id: str,
load_balancer_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given hostname"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)

delete_hostname_response = client.delete_hostname(load_balancer_id, load_balancer_name).data

return delete_hostname_response


def delete_listener(listener_id: str,
listener_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given LB Listener"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)

delete_listener_response = client.delete_listener(listener_id, listener_name).data

return delete_listener_response


def delete_load_balancer(load_balancer_id: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given Load Balancer"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)

delete_load_balancer_response = client.delete_load_balancer(load_balancer_id).data

return delete_load_balancer_response


def delete_path_route_set(load_balancer_id: str,
path_route_set_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given set path route"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)

delete_path_route_set_response = client.delete_path_route_set(load_balancer_id, path_route_set_name).data

return delete_path_route_set_response


def delete_routing_policy(load_balancer_id: str,
routing_policy_name: str,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""Delete a given routing policy"""

client = oci_client(LoadBalancerClient, configuration, secrets,
skip_deserialization=True)

delete_routing_policy_response = client.delete_routing_policy(load_balancer_id, routing_policy_name).data

return delete_routing_policy_response
74 changes: 74 additions & 0 deletions chaosoci/core/loadBalancer/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
__all__ = ["get_load_balancers", "filter_load_balancers", "get_backend_sets"]

from typing import Any, Dict, List

from chaoslib.exceptions import ActivityFailed

from logzero import logger

from oci.core import ComputeClient, ComputeManagementClient
from oci.core.models import Instance, InstancePool
from oci.load_balancer import LoadBalancerClient
from oci.load_balancer.models import LoadBalancer


def get_load_balancers(client: LoadBalancerClient = None,
compartment_id: str = None) -> List[Instance]:
"""Return a complete, unfiltered list of instances in the compartment."""
load_bals = []

load_bals_raw = client.list_load_balancers(compartment_id=compartment_id)
load_bals.extend(load_bals_raw.data)
while load_bals_raw.has_next_page:
load_bals_raw = client.list_load_balancers(compartment_id=compartment_id,
page=load_bals_raw.next_page)
load_bals.extend(load_bals_raw.data)

return load_bals


def filter_load_balancers(load_bals: List[LoadBalancer] = None,
filters: Dict[str, Any] = None) -> List[LoadBalancer]:
"""Return only those load_bals that match the filters provided."""
load_bals = load_bals or None

if load_bals is None:
raise ActivityFailed('No load_bals were found.')

filters_set = {x for x in filters}
available_filters_set = {x for x in load_bals[0].attribute_map}

# Partial filtering may return load_bals 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 load_bals and find those that match the given filters.
filtered = []
for load_bal in load_bals:
sentinel = True
for attr, val in filters.items():
if val != getattr(load_bal, attr, None):
sentinel = False
break

if sentinel:
filtered.append(load_bal)

return filtered


def get_backend_sets(client: LoadBalancerClient = None,
loadbalancer_id: str = None) -> List[Instance]:
"""Return a complete, unfiltered list of instances in the compartment."""
backend_set = []

backend_set_raw = client.list_backend_sets(load_balancer_id=loadbalancer_id)
backend_set.extend(backend_set_raw.data)
while backend_set_raw.has_next_page:
backend_set_raw = client.list_backend_sets(load_balancer_id=loadbalancer_id,
page=backend_set_raw.next_page)
backend_set.extend(backend_set_raw.data)

return backend_set
64 changes: 64 additions & 0 deletions chaosoci/core/loadBalancer/probes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
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, ComputeManagementClient

from chaosoci import oci_client

from .common import get_load_balancers, get_backend_sets, filter_load_balancers

__all__ = ['count_load_bal', 'count_backend_sets']


def count_load_bal(filters: List[Dict[str, Any]], compartment_id: str = None,
configuration: Configuration = None,
secrets: Secrets = None) -> int:
"""
Return the number of instances in accordance with the given 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
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.')

client = oci_client(ComputeClient, configuration, secrets,
skip_deserialization=False)

filters = filters or None
instances = get_load_balancers(client, compartment_id)

if filters is not None:
return len(filter_load_balancers(instances, filters=filters))

return len(instances)


def count_backend_sets(filters: List[Dict[str, Any]], loadbalancer_id: str = None,
configuration: Configuration = None,
secrets: Secrets = None) -> int:

loadbalancer_id = loadbalancer_id or from_file().get('load_balancer')

if loadbalancer_id is None:
raise ActivityFailed('We have not been able to find a compartment,'
' without one, we cannot continue.')

client = oci_client(ComputeClient, configuration, secrets,
skip_deserialization=False)

filters = filters or None
backend_sets = get_backend_sets(client, loadbalancer_id)

if filters is not None:
return len(filter_load_balancers(backend_sets, filters=filters))

return len(backend_sets)
Loading

0 comments on commit 36da01a

Please sign in to comment.