Skip to content

Commit

Permalink
build: Add ability to set version
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Crone <christopher.crone@docker.com>
  • Loading branch information
chris-crone committed Jul 24, 2020
1 parent d773ef4 commit f5a58f1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
32 changes: 30 additions & 2 deletions pkg/terraform/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package terraform

import (
"fmt"

"get.porter.sh/porter/pkg/exec/builder"
"github.com/ghodss/yaml"
)

const terraformClientVersion = "0.12.17"
const dockerfileLines = `ENV TERRAFORM_VERSION=%s
RUN apt-get update && apt-get install -y wget unzip && \
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
Expand All @@ -13,7 +15,33 @@ COPY . $BUNDLE_DIR
RUN cd %s && terraform init -backend=false
`

// BuildInput represents stdin passed to the mixin for the build command.
type BuildInput struct {
Config MixinConfig
}

// MixinConfig represents configuration that can be set on the terraform mixin in porter.yaml
// mixins:
// - terraform:
// version: 0.12.17
type MixinConfig struct {
ClientVersion string `yaml:"clientVersion,omitempty"`
}

func (m *Mixin) Build() error {
fmt.Fprintf(m.Out, dockerfileLines, terraformClientVersion, m.WorkingDir)
var input BuildInput
err := builder.LoadAction(m.Context, "", func(contents []byte) (interface{}, error) {
err := yaml.Unmarshal(contents, &input)
return &input, err
})
if err != nil {
return err
}

if input.Config.ClientVersion != "" {
m.TerraformVersion = input.Config.ClientVersion
}

fmt.Fprintf(m.Out, dockerfileLines, m.TerraformVersion, m.WorkingDir)
return nil
}
40 changes: 31 additions & 9 deletions pkg/terraform/build_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
package terraform

import (
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMixin_Build(t *testing.T) {
m := NewTestMixin(t)

err := m.Build()
require.NoError(t, err)

wantOutput := `ENV TERRAFORM_VERSION=0.12.17
const buildOutputTemplate = `ENV TERRAFORM_VERSION=%s
RUN apt-get update && apt-get install -y wget unzip && \
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin
COPY . $BUNDLE_DIR
RUN cd /cnab/app/terraform && terraform init -backend=false
`

gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
func TestMixin_Build(t *testing.T) {
t.Run("build with the default Terraform version", func(t *testing.T) {
m := NewTestMixin(t)

err := m.Build()
require.NoError(t, err)

wantOutput := fmt.Sprintf(buildOutputTemplate, "0.12.17")

gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
})

t.Run("build with custom Terrafrom version", func(t *testing.T) {
b, err := ioutil.ReadFile("testdata/build-input-with-version.yaml")
require.NoError(t, err)

m := NewTestMixin(t)
m.In = bytes.NewReader(b)
err = m.Build()
require.NoError(t, err)

wantOutput := fmt.Sprintf(buildOutputTemplate, "0.13.0-rc1")

gotOutput := m.TestContext.GetOutput()
assert.Equal(t, wantOutput, gotOutput)
})
}
14 changes: 9 additions & 5 deletions pkg/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ import (
// DefaultWorkingDir is the default working directory for Terraform
const DefaultWorkingDir = "/cnab/app/terraform"

const defaultTerraformVersion = "0.12.17"

// terraform is the logic behind the terraform mixin
type Mixin struct {
*context.Context
schema *packr.Box
WorkingDir string
schema *packr.Box
WorkingDir string
TerraformVersion string
}

// New terraform mixin client, initialized with useful defaults.
func New() *Mixin {
return &Mixin{
Context: context.New(),
schema: packr.New("schema", "./schema"),
WorkingDir: DefaultWorkingDir,
Context: context.New(),
schema: packr.New("schema", "./schema"),
WorkingDir: DefaultWorkingDir,
TerraformVersion: defaultTerraformVersion,
}
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/terraform/testdata/build-input-with-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config:
clientVersion: 0.13.0-rc1
install:
- terraform:
description: "noop"

0 comments on commit f5a58f1

Please sign in to comment.