Skip to content

Commit

Permalink
Merge pull request #25 from oracle/develop
Browse files Browse the repository at this point in the history
Adding functionality for LB and Object Store
  • Loading branch information
markxnelson authored Dec 15, 2021
2 parents 471c67c + 36da01a commit 0aaeea9
Show file tree
Hide file tree
Showing 24 changed files with 2,127 additions and 39 deletions.
42 changes: 42 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2020, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

pipeline {
options {
disableConcurrentBuilds()
}

agent {
docker {
image "${RUNNER_IMAGE}"
}
}

stages {
stage('Build') {
steps {
sh """
# create virtual environment
sudo pip-3.6 install virtualenv
virtualenv ~/.venvs/chaostk
# enter the python virtual environment
source ~/.venvs/chaostk/bin/activate
# install oci sdk
pip-3.6 install oci
# install dependencies
pip-3.6 install -r requirements.txt -r requirements-dev.txt
chaos --version
# setup dev environment
python3.6 setup.py develop
# run the CI script
bash ./ci.bash
"""
}
}
}
}
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
Loading

0 comments on commit 0aaeea9

Please sign in to comment.