# jenkins-account-creation
This repo connects to Amazon Managed Streaming for Apache Kafka (MSK) to produce messages to topics, triggering account creation via stepfunction. 

It contains scripts used in the following Jenkins Job: [jenkins-account-creation](https://pipeline.theorchard.io/job/jenkins-account-creation/) 

## Setup
* Install [docker](https://docs.docker.com/get-docker/) and [Python3.8]
* Clone this repository
* Set Env variables for new account creation
* Run the Container

#### Env Variables
New account creation relies on parameters usually passed from the jenkins job. These can be replicated in local development using the .env.shadow file. Please reference [jenkins config](https://pipeline.theorchard.io/job/jenkins-account-creation/configure) for what these can be set to.

`cp .env.shadow .env`

Set the message parameter values you'd like to use in the created `.env` file. You don't need to use quotations for the strings.

##### Special Values
* `BOOTSTRAP_SERVERS` - determines your connection to akhq. `.env.shadow` has values for qa. You can find the required values for other servers in [akHQ's Node view](https://qa-akhq.theorchard.io/ui/qa-artist-diy/node)
* `CUSTOMER_TYPE` - determines the event triggered: `neighbouring_rights` > `event.knrAccountCreation` and `awal_distro` > `event.awalPlusAccountCreation`
* `BACKFILL_CSV` - determines if you are running a backfill, or creating a single message. Set this value in the [config](https://github.com/theorchard/jenkins-account-creation/blob/master/src/config.py) file, defaults to `False` otherwise.
## Running
```
docker-compose up --exit-code-from jenkins-account-creation --abort-on-container-exit --build jenkins-account-creation
```

#### Testing Backfill Locally
The csv usually gets injected via the jenkins parameter. To test this functionality locally, add your own `backfill.csv` to your working `jenkins-account-creation` directory. For an example, check out [tests/backfill.csv](https://github.com/theorchard/jenkins-account-creation/blob/master/tests/backfill.csv) or look at the file parameter of a recent build in the jenkins job. Once your file is added, run the container using the command above.

You will need AWS credentials in order to both _produce_ the messages to the kafka topic and _kick_ off the step function. Follow the instructions in the [aws-creds-generator](https://github.com/theorchard/collab/tree/master/jcarrion/aws-creds-generator) to create your temporary credentials, then `export AWS_PROFILE=default` before composing the container.

If you are still recieving a `NoCredentialsError()`, you can add [this value](https://github.com/theorchard/lambda-notifications/blob/338ddf12c319f26cedfeda0cd337d472cb4040cc/lambda/buffer_poller/docker-compose.yaml#L12) to your docker-compose file, but please take it out before you merge.
#### Linting and Testing
```
docker-compose up --build lint-and-test
```

### Set pytest Options
Sometimes you may want to customize how tests run using [pytest options](https://docs.pytest.org/en/stable/usage.html) when doing development locally, only running a subset of tests using `-k` for example. Simply edit `TEST_ARGS` value in `docker-compose.yaml`, **without** quotes, to set the options you need.

:warning: Commited changes will modify how the build pipeline runs tests

### Set pytest-cov Options
By default test coverage is calculated for files in `./src` and displayed in a table after tests run.
* If you want to see a detailed output, edit `COV_REPORT` value in `docker-compose.yaml` to be `html` and visit `http://localhost:8000/`
* If you want to turn off coverage report generation, edit `COV_REPORT` value in `docker-compose.yaml` to be `off`

:warning: Commited changes will modify how the build pipeline runs tests, **do not** commit `COV_REPORT=html`

### Skip Linting
When developing locally, you might not want to worry about linting when working to make tests pass. Simple edit the `SKIP_LINT` value in `docker-compose.yaml` to be `1`.

:warning: Careful not to commit changes unless you **really** want to not run linting on the build pipeline

### Capture Exit Code
Unless the container itself crashes, or is killed, the exit code will be `0`. When the exit code of `lint-and-test.sh` script matters, a pull-request builder for example, run tests with the following flags.
```
$ docker-compose up --exit-code-from lint-and-test --abort-on-container-exit --build lint-and-test
```