Skip to content

Commit

Permalink
Convert to TF 0.12. Add tests. Add Codefresh test pipeline (#5)
Browse files Browse the repository at this point in the history
* Convert to TF 0.12

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline

* Convert to TF 0.12. Add tests. Add Codefresh test pipeline
  • Loading branch information
aknysh committed Dec 10, 2019
1 parent 1cac9e4 commit 5268756
Show file tree
Hide file tree
Showing 29 changed files with 597 additions and 137 deletions.
20 changes: 14 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
.build-harness
build-harness
.terraform
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*
.idea

**/node_modules/*
**/package-lock.json

.build-harness
build-harness

*.zip

*.iml
.viminfo
.terraform.d
.idea
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2018-2019 Cloud Posse LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
167 changes: 111 additions & 56 deletions README.md

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ github_repo: cloudposse/terraform-external-module-artifact

# Badges to display
badges:
- name: "Build Status"
image: "https://travis-ci.org/cloudposse/terraform-external-module-artifact.svg?branch=master"
url: "https://travis-ci.org/cloudposse/terraform-external-module-artifact"
- name: "Codefresh Build Status"
image: "https://g.codefresh.io/api/badges/pipeline/cloudposse/terraform-modules%2Fterraform-external-module-artifact?type=cf-1"
url: "https://g.codefresh.io/public/accounts/cloudposse/pipelines/5defbab3f04032f54518b539"
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-external-module-artifact.svg"
url: "https://github.com/cloudposse/terraform-external-module-artifact/releases/latest"
Expand All @@ -38,12 +38,12 @@ references:

# Short description of this project
description: |-
Terraform Module to fetch remote build artifacts via http(s) using `curl`.
Terraform Module to fetch remote build artifacts via http(s) using `curl`.
introduction: |-
This terraform module uses the [`external`](https://www.terraform.io/docs/providers/external/data_source.html) data provider to execute `curl`, which downloads the file locally to the modules cache folder.
It's ideally suited for downloading public build artifacts (E.g. for use with Lambdas) associated with terraform modules.
This terraform module uses the [`external`](https://www.terraform.io/docs/providers/external/data_source.html) data provider to execute `curl`, which downloads the file locally to the modules cache folder.
It's ideally suited for downloading public build artifacts (e.g. for use with Lambdas) associated with terraform modules.
We recommend using this pattern to avoid adding binary artifacts like `.zip` files to Git repositories.
**NOTE:** The term `external` refers to the primary Terraform [provider](https://www.terraform.io/docs/providers/external/data_source.html) used in this module.
Expand All @@ -55,17 +55,21 @@ introduction: |-
# How to use this project
usage: |-
For a complete example, see [examples/complete](examples/complete).
For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) (which tests and deploys the example on AWS), see [test](test).
```hcl
module "example" {
module "external_module_artifact" {
source = "git::https://github.com/cloudposse/terraform-external-module-artifact.git?ref=master"
filename = "test.zip"
git_ref = "example"
module_name = "terraform-external-module-artifact"
module_path = "${path.module}"
module_path = path.module
}
```
__NOTE:__ Leave `git_ref` blank and it will be automatically computed from the `module_path`
__NOTE:__ Leave `git_ref` blank and it will be automatically computed from the `module_path`
__NOTE:__ We've published [an artifact](https://artifacts.cloudposse.com/terraform-external-module-artifact/example/test.zip) for purpose of testing this module
include:
Expand All @@ -76,3 +80,7 @@ include:
contributors:
- name: "Erik Osterman"
github: "osterman"
- name: "Andriy Knysh"
github: "aknysh"
- name: "Igor Rodionov"
github: "goruha"
77 changes: 77 additions & 0 deletions codefresh/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
version: '1.0'

stages:
- Prepare
- Test

steps:
wait:
title: Wait
stage: Prepare
image: codefresh/cli:latest
commands:
- codefresh get builds --pipeline=${{CF_REPO_NAME}} --status running --limit 1000 -o json | jq --arg id ${{CF_BUILD_ID}} -ser 'flatten|.[-1].id==$id'
retry:
maxAttempts: 10
delay: 20
exponentialFactor: 1.1

main_clone:
title: "Clone repository"
type: git-clone
stage: Prepare
description: "Initialize"
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
git: CF-default
revision: ${{CF_REVISION}}

clean_init:
title: Prepare build-harness and test-harness
image: ${{TEST_IMAGE}}
stage: Prepare
commands:
- cf_export PATH="/usr/local/terraform/0.12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
- make init
- git -C build-harness checkout master
- make -C test/ clean init TEST_HARNESS_BRANCH=master
- make -C test/src clean init
- find . -type d -name '.terraform' | xargs rm -rf
- find . -type f -name 'terraform.tfstate*' -exec rm -f {} \;

test:
type: "parallel"
title: "Run tests"
description: "Run all tests in parallel"
stage: Test
steps:
test_readme_lint:
title: "Test README.md updated"
stage: "Test"
image: ${{TEST_IMAGE}}
description: Test "readme/lint"
commands:
- make readme/lint

test_module:
title: Test module with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ module

test_examples_complete:
title: Test "examples/complete" with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ examples/complete

test_examples_complete_terratest:
title: Test "examples/complete" with terratest
image: ${{TEST_IMAGE}}
stage: Test
commands:
- unset AWS_PROFILE
- unset AWS_DEFAULT_PROFILE
- unset AWS_MFA_PROFILE
- make -C test/src
6 changes: 3 additions & 3 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| curl_arguments | Arguments that should get passed to `curl` | string | `-sSL` | no |
| curl_arguments | Arguments that should get passed to `curl` | string | `-fsSL` | no |
| filename | Artifact filename | string | `lambda.zip` | no |
| git_ref | Git hash corresponding to the remote artifact. Leave blank and it will be computed from the `module_path` checkout | string | `` | no |
| module_name | Name of the terraform module | string | - | yes |
| module_path | Local path to the terraform module; e.g. `${path.module}` | string | - | yes |
| url | URL template for the remote artifact | string | `https://artifacts.cloudposse.com/$${module_name}/$${git_ref}/$${filename}` | no |
| module_path | Local path to the terraform module; e.g. `$${path.module}` | string | - | yes |
| url | URL template for the remote artifact | string | `https://artifacts.cloudposse.com/$$${module_name}/$$${git_ref}/$$${filename}` | no |

## Outputs

Expand Down
1 change: 0 additions & 1 deletion examples/.gitignore

This file was deleted.

17 changes: 17 additions & 0 deletions examples/complete/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

**/node_modules/*
**/package-lock.json

.build-harness
build-harness

*.zip

*.iml
.idea
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
region = "us-east-2"

filename = "lambda.zip"

module_name = "terraform-external-module-artifact"

git_ref = ""

url = "https://artifacts.cloudposse.com/terraform-external-module-artifact/example/test.zip"

curl_arguments = "-fsSL"
13 changes: 13 additions & 0 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
provider "aws" {
region = var.region
}

module "external_module_artifact" {
source = "../../"
filename = var.filename
module_name = var.module_name
module_path = path.module
url = var.url
git_ref = var.git_ref
curl_arguments = var.curl_arguments
}
19 changes: 19 additions & 0 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "git_ref" {
description = "Git commit hash corresponding to the artifact"
value = module.external_module_artifact.git_ref
}

output "file" {
description = "Full path to the locally downloaded file"
value = module.external_module_artifact.file
}

output "url" {
description = "URL corresponding to the artifact"
value = module.external_module_artifact.url
}

output "base64sha256" {
description = "Base64 encoded SHA256 hash of the local file"
value = module.external_module_artifact.base64sha256
}
32 changes: 32 additions & 0 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
variable "region" {
type = string
description = "AWS Region"
}

variable "filename" {
type = string
description = "Artifact filename"
}

variable "module_name" {
type = string
description = "Name of the terraform module"
}

variable "git_ref" {
type = string
description = "Git hash corresponding to the remote artifact. Leave blank and it will be computed from the `module_path` checkout"
default = ""
}

variable "url" {
type = string
description = "URL template for the remote artifact"
default = "https://artifacts.cloudposse.com/$$${module_name}/$$${git_ref}/$$${filename}"
}

variable "curl_arguments" {
type = string
description = "Arguments that should get passed to `curl`"
default = "-fsSL"
}
10 changes: 10 additions & 0 deletions examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 0.12.0"

required_providers {
aws = "~> 2.0"
template = "~> 2.0"
local = "~> 1.3"
external = "~> 1.2"
}
}
19 changes: 0 additions & 19 deletions examples/main.tf

This file was deleted.

32 changes: 13 additions & 19 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
data "external" "curl" {
program = ["curl", "${var.curl_arguments}", "--write-out", "{\"success\": \"true\", \"filename_effective\": \"%{filename_effective}\"}", "-o", "${local.output_file}", "${local.url}"]
program = ["curl", var.curl_arguments, "--write-out", "{\"success\": \"true\", \"filename_effective\": \"%%{filename_effective}\"}", "-o", local.output_file, local.url]
}

data "external" "git" {
count = "${var.git_ref == "" ? 1 : 0}"
program = ["git", "-C", "${var.module_path}", "log", "-n", "1", "--pretty=format:{\"ref\": \"%H\"}"]
count = var.git_ref == "" ? 1 : 0
program = ["git", "-C", var.module_path, "log", "-n", "1", "--pretty=format:{\"ref\": \"%H\"}"]
}

locals {
external_curl_filename_effective = "${data.external.curl.result.filename_effective}"
external_git_ref = "${join("", data.external.git.*.result.ref)}"
git_ref = "${var.git_ref == "" ? local.external_git_ref : var.git_ref}"
}

locals {
output_file = "${join("/", list(var.module_path, var.filename))}"
external_curl_filename_effective = data.external.curl.result.filename_effective
external_git_ref = join("", data.external.git.*.result.ref)
git_ref = var.git_ref == "" ? local.external_git_ref : var.git_ref
output_file = join("/", [var.module_path, var.filename])
url = data.template_file.url.rendered
}

data "template_file" "url" {
template = "${replace(var.url, "$$$$", "$")}"
template = replace(var.url, "$$", "$")

vars {
filename = "${var.filename}"
git_ref = "${local.git_ref}"
module_name = "${var.module_name}"
vars = {
filename = var.filename
git_ref = local.git_ref
module_name = var.module_name
}
}

locals {
url = "${data.template_file.url.rendered}"
}
Loading

0 comments on commit 5268756

Please sign in to comment.