Skip to content

Commit

Permalink
Merge pull request #7 from kristofferahl/feature/build-test-release-s…
Browse files Browse the repository at this point in the history
…cript

Feature: Build/Test/Release script
  • Loading branch information
kristofferahl committed Jun 18, 2019
2 parents 2881fa7 + 349c680 commit 8d91ba9
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 38 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ root = true
; Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
indent_size = 4
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ changelog:
sort: asc
filters:
exclude:
- '^examples:'
- '^docs:'
- '^test:'
65 changes: 62 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Terraform Provider for healthchecks.io

## Provider configuration
## Usage

### Provider configuration

| Property | Description | Environment variable | Type | Required |
|----------|-----------------------------|------------------------|--------|----------|
| api_key | The healthchecks.io API Key | HEALTHCHECKSIO_API_KEY | string | true |


## Example
### Example

```
provider "healthchecksio" {
Expand Down Expand Up @@ -45,10 +47,67 @@ data "healthchecksio_channel" "pagerduty" {

More examples can be found in the [examples directory](./examples).

## Import
### Import

Checks can be imported using the uuid, e.g.

```
$ terraform import healthchecksio_check.my_first_check 760ca858-576a-432b-8b1f-378049d7ce96
```

## Development

### Pre-requisites
- [Go](https://golang.org/) 1.12 or later
- [Terraform](https://www.terraform.io/) v0.12 or later
- [Healthchecks.io](https://healthchecks.io/) account and an API key
- [goreleaser](https://goreleaser.com/) 0.85.1 or later

### Help

```bash
./run --help
```

### Running unit tests

```bash
./run test
```

### Running integration tests

```bash
./run test-integration
```

### Running integration tests in docker

This command runs the integration tests against multiple versions of Terraform defined in the `run` script.

```bash
./run test-docker
```

### Building the provider

```bash
./run build
```

### Running examples

```bash
./run examples [<example>]
```

### Releasing a new version

```bash
./run release
```

## Contributors
- [masutaka](https://github.com/masutaka)
- [kristofferahl](https://github.com/kristofferahl)
- [rossmckelvie](https://github.com/rossmckelvie)
27 changes: 0 additions & 27 deletions examples/Makefile

This file was deleted.

6 changes: 0 additions & 6 deletions examples/full.tf → examples/full/main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
provider "healthchecksio" {
api_key = var.healthchecks_io_api_key
version = "~> 1.3"
}

variable "healthchecks_io_api_key" {
type = string
description = "API Key. tfvars can't be used here, to keep secrets out of code first set environment TF_VAR_healthchecks_io_api_key"
}

resource "healthchecksio_check" "test" {
name = "test-check"

Expand Down
2 changes: 1 addition & 1 deletion examples/minimal.tf → examples/minimal/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
provider "healthchecksio" {
api_key = "abc123"
version = "~> 1.3"
}

resource "healthchecksio_check" "bare_minimum" {
Expand Down
131 changes: 131 additions & 0 deletions run
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/usr/bin/env bash

set -euo pipefail

declare -r plugins_dir=".terraform/plugins"
declare -r dist_dir="${PWD:?}/dist"
declare -r examples_dir="${PWD:?}/examples"
declare -r test_dir="${PWD:?}/test"

function log() {
local -r level="${1}"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r script_name="$(basename "$0")"
echo >&2 -e "${timestamp} [\033[1;94m${level}\033[0m] [$script_name] ${message}"
}

install_plugin() {
local destination_dir="${1:?'A destination directory is required'}"
log INF "Installing plugin (src=${dist_dir:?} dest=${destination_dir:?})"
[[ ! -d "${dist_dir:?}" ]] && log ERR "Source directory not found (${dist_dir:?})" && exit 1
rm -rf "${destination_dir:?}/${plugins_dir:?}" &>/dev/null || true
mkdir -p "${destination_dir:?}/${plugins_dir:?}" &>/dev/null || true
cp -R "${dist_dir:?}/." "${destination_dir:?}/${plugins_dir:?}"
}

run_help() {
echo "usage: ./run <command> [<arg1> <arg2> ...]
commands:
build Build the provider
examples Run examples
test Run tests
test-docker Run integration tests in docker
test-integration Run integration tests
release Create a new release of the provider"
}

run_build() {
log INF 'Cross-compiling using goreleaser...'
goreleaser --snapshot --skip-publish --skip-validate --rm-dist
}

run_release() {
log INF 'Creating a release using goreleaser...'
goreleaser --rm-dist
}

run_examples() {
[ "${HEALTHCHECKSIO_API_KEY:?'Environment variable "HEALTHCHECKSIO_API_KEY" is required'}" ]

local example="${1:-}"

log INF "Running example(s)..."

for d in ${examples_dir:?}/**; do
if [[ -d "${d}" ]]; then
[[ "$(basename "${d:?}")" != "${example}" && "${example}" != "" ]] && continue

log INF "Example: $(basename "${d:?}") (${d:?})"

install_plugin "${d:?}"
cd "${d:?}" || exit 1

terraform init
terraform apply
fi
done
}

run_test() {
log INF 'Running unit tests...'
go test healthchecksio/**
}

run_test_integration() {
[ "${HEALTHCHECKSIO_API_KEY:?'Environment variable "HEALTHCHECKSIO_API_KEY" is required'}" ]

log INF "Running integration tests ($(terraform version | head -n 1))..."

for d in ${test_dir:?}/**; do
if [[ -d "${d}" ]]; then
log INF "Test: $(basename "${d:?}") (${d:?})"

rm -rf "${d:?}/terraform.tfstate" || true
rm -rf "${d:?}/terraform.tfstate.backup" || true
install_plugin "${d:?}"
cd "${d:?}" || exit 1

terraform init
terraform apply -auto-approve
terraform destroy -auto-approve
fi
done
}

run_test_docker() {
run_build

local -a versions=('0.11.14' '0.12.2')

for v in "${versions[@]}"; do
if ! docker image ls | grep hashicorp/terraform | grep -q "${v:?}-test"; then
log INF "Building test image for terraform v${v:?}"
docker build -t "hashicorp/terraform:${v:?}-test" --build-arg "VERSION=${v:?}" -f "${PWD:?}/test/Dockerfile.test" .
fi

docker run --rm -it -e HEALTHCHECKSIO_API_KEY -v "${PWD:?}:/work/" "hashicorp/terraform:${v:?}-test" -c "./run test-integration"

if [[ "$?" != "0" && ${DEBUG} == true ]]; then
docker run --rm -it -e HEALTHCHECKSIO_API_KEY -v "${PWD:?}:/work/" "hashicorp/terraform:${v:?}-test"
fi
done
}

main() {
local command="${1:-}"
shift || true

case "${command}" in
build) run_build "$@" ;;
examples) run_examples "$@" ;;
test) run_test "$@" ;;
test-docker) run_test_docker "$@" ;;
test-integration) run_test_integration "$@" ;;
release) run_release "$@" ;;
*) run_help ;;
esac
}

main "$@"
6 changes: 6 additions & 0 deletions test/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG VERSION
FROM hashicorp/terraform:$VERSION
LABEL maintainer="Dotnet Mentor <info@dotnetmentor.se>"
RUN apk add --update git bash openssh
WORKDIR /work/
ENTRYPOINT ["/bin/bash"]
8 changes: 8 additions & 0 deletions test/minimal/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
provider "healthchecksio" {
version = "~> 1.3"
}

resource "healthchecksio_check" "bare_minimum" {
name = "test-bare-minimum-check"
timeout = 86400
}

0 comments on commit 8d91ba9

Please sign in to comment.