Skip to content

Commit

Permalink
Merge pull request #24 from telia-oss/expose-userdatabase64-variable
Browse files Browse the repository at this point in the history
Expose userdatabase64 variable
  • Loading branch information
Kristian authored Aug 18, 2019
2 parents 140bc61 + 55e4184 commit 5b00449
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ notifications:
email: false

before_script:
- curl -fSL "https://releases.hashicorp.com/terraform/0.12.3/terraform_0.12.3_linux_amd64.zip" -o terraform.zip
- curl -fSL "https://releases.hashicorp.com/terraform/0.12.6/terraform_0.12.6_linux_amd64.zip" -o terraform.zip
- sudo unzip terraform.zip -d /opt/terraform
- sudo ln -s /opt/terraform/terraform /usr/bin/terraform
- rm -f terraform.zip
Expand Down
21 changes: 20 additions & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module "asg" {
instance_volume_size = 10
min_size = 2
max_size = 4
user_data = "#!bin/bash\necho hello world"
user_data_base64 = data.template_cloudinit_config.user_data.rendered

ebs_block_devices = [
{
Expand All @@ -67,6 +67,25 @@ module "asg" {
}
}

data "template_cloudinit_config" "user_data" {
gzip = true
base64_encode = true

part {
content_type = "text/cloud-config"
content = <<EOF
#cloud-config
runcmd:
- echo "Cloud init part 1"
EOF
}

part {
content_type = "text/x-shellscript"
content = "echo \"Cloud init part 2\""
}
}

data "aws_iam_policy_document" "permissions" {
statement {
effect = "Allow"
Expand Down
1 change: 1 addition & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ resource "aws_launch_configuration" "main" {
image_id = var.instance_ami
key_name = var.instance_key
user_data = var.user_data
user_data_base64 = var.user_data_base64

dynamic "ebs_block_device" {
iterator = device
Expand Down
38 changes: 28 additions & 10 deletions test/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package module

import (
"bytes"
"compress/gzip"
"encoding/base64"
"io/ioutil"
"testing"
"time"

Expand All @@ -13,13 +16,14 @@ import (
)

type Expectations struct {
MinSize int64
MaxSize int64
DesiredCapacity int64
UserData []string
InstanceType string
Volumes []string
InstanceTags map[string]string
MinSize int64
MaxSize int64
DesiredCapacity int64
UserData []string
IsGzippedUserData bool
InstanceType string
Volumes []string
InstanceTags map[string]string
}

func RunTestSuite(t *testing.T, name, region string, expected Expectations) {
Expand All @@ -37,7 +41,7 @@ func RunTestSuite(t *testing.T, name, region string, expected Expectations) {

config = DescribeLaunchConfiguration(t, sess, aws.StringValue(group.LaunchConfigurationName))

userData := DecodeUserData(t, config.UserData)
userData := DecodeUserData(t, config.UserData, expected.IsGzippedUserData)
for _, data := range expected.UserData {
assert.Contains(t, userData, data)
}
Expand Down Expand Up @@ -77,12 +81,26 @@ func NewSession(t *testing.T, region string) *session.Session {
return sess
}

func DecodeUserData(t *testing.T, data *string) string {
func DecodeUserData(t *testing.T, data *string, isGzipped bool) string {
b, err := base64.StdEncoding.DecodeString(aws.StringValue(data))
if err != nil {
t.Fatalf("failed to decode user data: %s", err)
}
return string(b)

var s []byte
if isGzipped {
r, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
t.Fatalf("failed to initialize gzip reader: %s", err)
}
s, err = ioutil.ReadAll(r)
if err != nil {
t.Fatalf("failed to read gzip data: %s", err)
}
} else {
s = b
}
return string(s)
}

func GetInstanceBlockDeviceMappings(instance *ec2.Instance) map[string]*ec2.InstanceBlockDeviceMapping {
Expand Down
8 changes: 6 additions & 2 deletions test/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ func TestModule(t *testing.T) {
MinSize: 2,
MaxSize: 4,
DesiredCapacity: 2,
UserData: []string{"#!bin/bash\necho hello world"},
InstanceType: "t3.micro",
UserData: []string{
`echo "Cloud init part 1"`,
`echo "Cloud init part 2"`,
},
IsGzippedUserData: true,
InstanceType: "t3.micro",
Volumes: []string{
"/dev/xvdcz",
},
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ variable "user_data" {
default = null
}

variable "user_data_base64" {
description = "Use this instead of user_data whenever the value is not a valid UTF-8 string."
type = string
default = null
}

variable "instance_type" {
description = "Type of instance to provision."
type = string
Expand Down

0 comments on commit 5b00449

Please sign in to comment.