Skip to content

Commit

Permalink
Merge pull request #21 from oracle/develop
Browse files Browse the repository at this point in the history
0.2.0 release
  • Loading branch information
markxnelson authored Jul 29, 2020
2 parents dc00606 + 03fbc51 commit 471c67c
Show file tree
Hide file tree
Showing 20 changed files with 614 additions and 145 deletions.
28 changes: 21 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

## [Unreleased][]

[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-oci/compare/0.1.0...HEAD
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-oci/compare/0.2.0...HEAD

- Stop random instance, allowing to do so in terms of the requested filters.
## [0.2.0][]

[0.2.0]: https://github.com/chaostoolkit-incubator/chaostoolkit-oci/tree/0.2.0

### Added

- Probe to count the number of instances in the compartment with the possibility of added filters.
- Associated documentation on how to use the probe.
Actions:

- Delete route table by ID
- Delete route tables using filter

Probes:

- Count route tables

Other:

- Updated/organized documentation
- Stop random instance added filters.
- Probe to count instances added filters.

## [0.1.0][]

Expand All @@ -19,9 +33,9 @@

Actions:

- stop instance
- stop random instance
- Stop instance
- Stop random instance

Probes:

- count instances
- Count instances
157 changes: 26 additions & 131 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,150 +7,45 @@ This project is a collection of [actions][] and [probes][], gathered as an
extension to the [Chaos Toolkit][chaostoolkit].

[actions]: http://chaostoolkit.org/reference/api/experiment/#action
[probes]: http://chaostoolkit.org/reference/api/experiment/#probe
[changelog]: ./CHANGELOG.md
[chaostoolkit]: http://chaostoolkit.org
[contributing]: ./docs/dev/contribute.md
[experiments]: ./docs/use/experiments.md
[install chaostoolkit-oci]: ./docs/setup/chaostoolkit.md
[keys and ocids]: https://docs.cloud.oracle.com/iaas/Content/API/Concepts/apisigningkey.htm
[oci]: ./docs/setup/oci.md
[oci config]: https://docs.cloud.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm
[probes]: http://chaostoolkit.org/reference/api/experiment/#probe
[python]: ./docs/setup/python.md
[virtualenv]: ./docs/setup/venv.md

[oci-configuration]: https://docs.cloud.oracle.com/iaas/Content/API/Concepts/apisigningkey.htm
[oci-python-sdk]: https://docs.cloud.oracle.com/en-us/iaas/Content/API/SDKDocs/pythonsdk.htm

## Pre-requisites

* Install Python 3.5+
* Install pip

## Create a virtual environment

Create a local virtual environment where you can install dependencies:

```
python3 -m venv ~/.venvs/chaostk
```

Activate the environment:

```
source ~/.venvs/chaostk/bin/activate
```

## Install dependencies

If you intend to only __use__ this toolkit, install the following dependencies in your virtual environment:

```
pip install chaostoolkit
pip install -U chaostoolkit-oci
```

If you intend to develop and contribute or use the latest from this repo, install the additional developer dependencies and point your environment to this directory:

```
pip install chaostoolkit
pip install -r requirements-dev.txt -r requirements.txt
python setup.py develop
```

Now, you can edit the files and they will be automatically be seen by your
environment, even when running from the `chaos` command locally.

Verify that the chaostoolkit has been installed properly:

```
chaos --version
```

## Discover capabilities and experiments

```
chaos discover --no-install chaostoolkit-oci
```

## Configuration

### Credentials
## Setup environment

This extension uses the [oci-python-sdk][oci-python-sdk] library under the hood. It expects that you have properly [configured][oci-configuration] your environment to connect and authenticate with the OCI services.
[Install Python and pip][python]

The easiest way of doing this is by having a ~/.oci directory that contains a config file
and the necessary api access pem files.
[Install and create a virtual environment][virtualenv]

```
[DEFAULT]
user=ocid1.user.oc1..aaaaaaaabyneck4zsklp55y4ebgxxxxxxx7ypsz7hskluh3hpshfb3jelsew
fingerprint=00:ab:23:45:bc:6d:e7:fg:h8:i9:00:jk:lm:12:34:56
key_file=/home/user/.oci/oci_api_key.pem
tenancy=ocid1.tenancy.oc1..aaaaaaaarx3oltzmcws24bsfxxxxxxxxbqwieembc74s2gohnfjjanmedxqj
compartment=ocid1.compartment.oc1..aaaaaaaaeaoardsao2cyxxxxxxxcrf6okik5uan4msovmdai44akwxje7tla
region=uk-london-1
namespace=tenancyname
```
[Install chaostoolkit, chaostoolkit-oci][install chaostoolkit-oci]

## Usage
Configure your [signing keys and obtain your OCIDs][keys and ocids].

To use the probes and actions from this package, add the following to your
experiment file:
Create your [OCI Configuration file][oci config].

```json
{
"type": "action",
"name": "stop-a-compute-instance",
"provider": {
"type": "python",
"module": "chaosoci.core.compute.actions",
"func": "stop_instance",
"arguments": {
"instance_id": "ocid1.instance.oc1.uk-london-1.abwgiljr4hngf7ktirgpp4ebl3w7fdarvhe6if4tu4r7y4fh3tsde7vbm5lq"
}
}
}
```
## Using chaostoolkit-oci
[Creating and running chaos experiments][experiments].

That's it!
## Contributing

Now, let us say we only want an action to be executed after probing that a given value is within
a desired threshold; simple enough, we need to add a probe to our experiment, as follows:
[Contributing to chaostoolkit-oci][contributing].

```
"type": "probe",
"name": "count-the-number-of-instances",
"tolerance": [2, 10],
"provider": {
"type": "python",
"module": "chaosoci.core.compute.probes",
"func": "count_instances",
"arguments": {
"filters": {
"region": "uk-london-1",
"lifecycle_state": "RUNNING"
}
}
}
```
## Changelog

For a list of available filters please refer to: [oci.core.models.Instance](https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/api/core/models/oci.core.models.Instance.html#oci.core.models.Instance).
View the [CHANGELOG][changelog].

Please explore the code to see existing probes and actions.
## Acknowledgement

## Contribute
Chaos installation: [chaostoolkit.org][chaostoolkit]

If you wish to contribute more functions to this package, you are more than
welcome to do so. Please, fork this project, make your changes following the
usual [PEP 8][pep8] code style, sprinkling with tests and submit a PR for
review.

[pep8]: https://pycodestyle.readthedocs.io/en/latest/

The Chaos Toolkit projects require all contributors must sign a
[Developer Certificate of Origin][dco] on each commit they would like to merge
into the master branch of the repository. Please, make sure you can abide by
the rules of the DCO before submitting a PR.

[dco]: https://github.com/probot/dco#how-it-works

### Test
Python, pip installation and virtualenv documentation: https://docs.python-guide.org/#

To run the tests for the project execute the following:

```
$ pytest
```

## License
6 changes: 6 additions & 0 deletions chaosoci/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2020, Oracle Corporation and/or its affiliates.

from typing import List

from oci.config import from_file, validate_config
Expand Down Expand Up @@ -55,6 +57,10 @@ def load_exported_activities() -> List[DiscoveredActivities]:
activities = []
activities.extend(discover_actions("chaosoci.core.compute.actions"))
activities.extend(discover_probes("chaosoci.core.compute.probes"))

activities.extend(discover_actions("chaosoci.core.networking.actions"))
activities.extend(discover_probes("chaosoci.core.networking.probes"))
activities.extend(discover_probes("chaosoci.core.networking.rollbacks"))
return activities


Expand Down
84 changes: 84 additions & 0 deletions chaosoci/core/networking/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# coding: utf-8
# Copyright 2020, Oracle Corporation and/or its affiliates.

__all__ = ["delete_route_table_by_id", "delete_route_table_by_filters"]

from random import choice
from typing import Any, Dict, List

from chaoslib.exceptions import ActivityFailed
from chaoslib.types import Configuration, Secrets

from chaosoci import oci_client
from chaosoci.types import OCIResponse
from chaosoci.util.constants import FILTER_ERR

from logzero import logger

from oci.config import from_file
from oci.core import VirtualNetworkClient

from .common import (get_route_tables)

from .filters import (filter_route_tables)


def delete_route_table_by_id(rt_id: str, force: bool = False,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""
Deletes a given route table using the route table id.
Parameters:
Required:
- rt_id: the id of the route table
"""

client = oci_client(VirtualNetworkClient, configuration, secrets,
skip_deserialization=True)
if not rt_id:
raise ActivityFailed('A route table id is required.')

ret = client.delete_route_table(rt_id=rt_id).data
logger.debug("Route table %s deleted", rt_id)
return ret


def delete_route_table_by_filters(compartment_id: str, vcn_id: str,
filters: Dict[str, Any], force: bool = False,
configuration: Configuration = None,
secrets: Secrets = None) -> OCIResponse:
"""
Search for a route table in VCN using the specified filters and
then deletes it.
Parameters:
Required:
- compartment_id: the compartment id of the VCN
- vcn_id: the id of the VCN
- filters: the set of filters for the route table.
Please refer to
https://oracle-cloud-infrastructure-python-sdk.readthedocs.io/en/latest/api/core/models/oci.core.models.RouteTable.html#
for the route table filters.
"""

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

if compartment_id is None or vcn_id is None:
raise ActivityFailed('A compartment id or vcn id is required.')
else:
unfiltered = get_route_tables(client, compartment_id, vcn_id)

if filters is None:
raise ActivityFailed(FILTER_ERR)
else:
filtered = filter_route_tables(unfiltered, filters)

if (len(filtered) == 0):
raise ActivityFailed(FILTER_ERR)
else:
ret = client.delete_route_table(filtered[0].id).data
logger.debug("Route table %s deleted",
filtered[0].display_name)
return ret
36 changes: 36 additions & 0 deletions chaosoci/core/networking/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# coding: utf-8
# Copyright 2020, Oracle Corporation and/or its affiliates.

__all__ = ["get_route_table", "get_route_tables"]

from typing import Any, Dict, List

from chaoslib.exceptions import ActivityFailed

from logzero import logger

from oci.core import VirtualNetworkClient
from oci.core.models import (RouteRule,
RouteTable)


def get_route_tables(client: VirtualNetworkClient = None,
compartment_id: str = None,
vcn_id: str = None) -> List[RouteTable]:
"""
Returns a complete, unfiltered list of route tables of a vcn in the
compartment.
"""

route_tables = []
route_tables_raw = client.list_route_tables(compartment_id=compartment_id,
vcn_id=vcn_id)
route_tables.extend(route_tables_raw.data)
while route_tables_raw.has_next_page:
route_tables_raw = client.list_route_tables(
compartment_id=compartment_id,
vcn_id=vcn_id,
page=route_tables_raw.next_page)
route_tables.extend(route_tables_raw.data)

return route_tables
Loading

0 comments on commit 471c67c

Please sign in to comment.