Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-71] Allow define one optional volume #3

Merged
merged 6 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This module creates a basic ECS Task Definition.

module "taskdef" {
source = "github.com/mergermarket/tf_ecs_task_definition"

family = "live-service-name"
container_definitions = [
<<END
Expand All @@ -15,6 +15,11 @@ This module creates a basic ECS Task Definition.
}
END
]

volume = {
name = 'data'
host_path = '/mnt/data'
}
}

## API
Expand All @@ -23,6 +28,8 @@ This module creates a basic ECS Task Definition.

* `family` - the name of the task definition. For ECS services it is recommended to use the same name as for the service, and for that name to consist of the environment name (e.g. "live"), the comonent name (e.g. "foobar-service"), and an optional suffix (if an environment has multiple services for the component running - e.g. in a multi-tenant setup), separated by hyphens.
* `container_definitions` - list of strings. Each string should be a JSON document describing a single container definition - see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html.
* `task_role_arn` - The Amazon Resource Name for an IAM role for the task.
* `volume` - Volume block map with 'name' and 'host_path'. See https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html#volume for more info.

### Outputs

Expand Down
11 changes: 8 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
resource "aws_ecs_task_definition" "taskdef" {
family = "${var.family}"
container_definitions = "[${join(",", var.container_definitions)}]"
task_role_arn = "${var.task_role_arn}"
family = "${var.family}"
container_definitions = "[${join(",", var.container_definitions)}]"
task_role_arn = "${var.task_role_arn}"

volume = {
name = "${lookup(var.volume, "name", "dummy")}"
host_path = "${lookup(var.volume, "host_path", "/tmp/dummy_volume")}"
}
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "arn" {
value = "${aws_ecs_task_definition.taskdef.arn}"
value = "${aws_ecs_task_definition.taskdef.arn}"
}
2 changes: 2 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ENV TERRAFORM_VERSION=0.9.5
ENV TERRAFORM_ZIP=terraform_${TERRAFORM_VERSION}_linux_amd64.zip
ENV TERRAFORM_SUM=0cbb5474c76d878fbc99e7705ce6117f4ea0838175c13b2663286a207e38d783

ENV PYTHONDONTWRITEBYTECODE donot

RUN apk add -U ca-certificates curl && \
cd /tmp && \
curl -fsSLO https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/${TERRAFORM_ZIP} && \
Expand Down
19 changes: 14 additions & 5 deletions test/infra/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ provider "aws" {
}

variable "task_role_arn_param" {
description = "Allow the test to pass this in"
type = "string"
default = ""
description = "The test can set this var to be passed to the module"
type = "string"
default = ""
}

variable "task_volume_param" {
description = "The test can set this var to be passed to the module"
type = "map"
default = {}
}

module "taskdef" {
source = "../.."

family = "tf_ecs_taskdef_test_family"
family = "tf_ecs_taskdef_test_family"
task_role_arn = "${var.task_role_arn_param}"
volume = "${var.task_volume_param}"

container_definitions = [
<<END
{
Expand All @@ -31,9 +39,10 @@ module "taskdef" {
"essential": true
}
END
,
]
}

output "taskdef_arn" {
value = "${module.taskdef.arn}"
value = "${module.taskdef.arn}"
}
81 changes: 60 additions & 21 deletions test/test_taskdef.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import re
import unittest
import os
import time
from textwrap import dedent
import shutil
import tempfile
import unittest
from subprocess import check_call, check_output
from textwrap import dedent

cwd = os.getcwd()

class TestCreateTaskdef(unittest.TestCase):

def setUp(self):
check_call([ 'terraform', 'get', 'test/infra' ])
self.workdir = tempfile.mkdtemp()
self.module_path = os.path.join(os.getcwd(), 'test', 'infra')

check_call(
['terraform', 'get', self.module_path],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this not fit on a single line?

cwd=self.workdir)

def tearDown(self):
if os.path.isdir(self.workdir):
shutil.rmtree(self.workdir)

def test_create_taskdef(self):
output = check_output([
'terraform',
'plan',
'-no-color',
'test/infra'
]).decode('utf-8')
self.module_path],
cwd=self.workdir
).decode('utf-8')

assert dedent("""
+ module.taskdef.aws_ecs_task_definition.taskdef
arn: "<computed>"
container_definitions: "a173db30ec08bc3c9ca77b5797aeae40987c1ef7"
family: "tf_ecs_taskdef_test_family"
network_mode: "<computed>"
revision: "<computed>"
arn: "<computed>"
container_definitions: "a173db30ec08bc3c9ca77b5797aeae40987c1ef7"
family: "tf_ecs_taskdef_test_family"
network_mode: "<computed>"
revision: "<computed>"
volume.#: "1"
volume.3039886685.host_path: "/tmp/dummy_volume"
volume.3039886685.name: "dummy"
Plan: 1 to add, 0 to change, 0 to destroy.
""").strip() in output

Expand All @@ -36,16 +48,43 @@ def test_task_role_arn_is_included(self):
'plan',
'-var', 'task_role_arn_param=arn::iam:123',
'-no-color',
'test/infra'
]).decode('utf-8')
self.module_path],
cwd=self.workdir
).decode('utf-8')

assert dedent("""
+ module.taskdef.aws_ecs_task_definition.taskdef
arn: "<computed>"
container_definitions: "a173db30ec08bc3c9ca77b5797aeae40987c1ef7"
family: "tf_ecs_taskdef_test_family"
network_mode: "<computed>"
revision: "<computed>"
task_role_arn: "arn::iam:123"
volume.#: "1"
volume.3039886685.host_path: "/tmp/dummy_volume"
volume.3039886685.name: "dummy"
Plan: 1 to add, 0 to change, 0 to destroy.
""").strip() in output

def test_task_volume_is_included(self):
output = check_output([
'terraform',
'plan',
'-var', 'task_volume_param={name="data_volume",host_path="/mnt/data"}',
'-no-color',
self.module_path],
cwd=self.workdir
).decode('utf-8')

assert dedent("""
+ module.taskdef.aws_ecs_task_definition.taskdef
arn: "<computed>"
container_definitions: "a173db30ec08bc3c9ca77b5797aeae40987c1ef7"
family: "tf_ecs_taskdef_test_family"
network_mode: "<computed>"
revision: "<computed>"
task_role_arn: "arn::iam:123"
arn: "<computed>"
container_definitions: "a173db30ec08bc3c9ca77b5797aeae40987c1ef7"
family: "tf_ecs_taskdef_test_family"
network_mode: "<computed>"
revision: "<computed>"
volume.#: "1"
volume.27251535.host_path: "/mnt/data"
volume.27251535.name: "data_volume"
Plan: 1 to add, 0 to change, 0 to destroy.
""").strip() in output
20 changes: 13 additions & 7 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
variable "family" {
description = "A unique name for your task defintion."
type = "string"
description = "A unique name for your task defintion."
type = "string"
}

variable "container_definitions" {
description = "A list of valid container definitions provided as a single valid JSON document."
type = "list"
description = "A list of valid container definitions provided as a single valid JSON document."
type = "list"
}

variable "task_role_arn" {
description = "The Amazon Resource Name for an IAM role for the task"
type = "string"
default = ""
description = "The Amazon Resource Name for an IAM role for the task"
type = "string"
default = ""
}

variable "volume" {
description = "Volume block map with 'name' and 'host_path'."
type = "map"
default = {}
}