Skip to content

Commit

Permalink
Use filebase64 terraform function for launch template userdata and TF…
Browse files Browse the repository at this point in the history
… 0.12
  • Loading branch information
rifelpet committed Jul 9, 2020
1 parent 60d5851 commit 1cf9838
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions upup/pkg/fi/cloudup/terraform/hcl2.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func writeLiteral(body *hclwrite.Body, key string, literal *Literal) {
tokens := hclwrite.Tokens{
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(fmt.Sprintf("file(%q)", literal.FilePath)),
Bytes: []byte(fmt.Sprintf("%v(%q)", literal.FileFn, literal.FilePath)),
},
}
body.SetAttributeRaw(key, tokens)
Expand Down Expand Up @@ -193,7 +193,7 @@ func writeMap(body *hclwrite.Body, key string, values map[string]cty.Value) {
// For maps of literals we currently only support file references
// If we ever need to support a map of strings to resource property references that can be added here
if literal.FilePath != "" {
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenIdent, Bytes: []byte(fmt.Sprintf("file(%q)", literal.FilePath))})
tokens = append(tokens, &hclwrite.Token{Type: hclsyntax.TokenIdent, Bytes: []byte(fmt.Sprintf("%v(%q)", literal.FileFn, literal.FilePath))})
} else if literal.Value != "" {
tokens = append(tokens, []*hclwrite.Token{
{Type: hclsyntax.TokenOQuote, Bytes: []byte{'"'}, SpacesBefore: 1},
Expand Down
20 changes: 16 additions & 4 deletions upup/pkg/fi/cloudup/terraform/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import (
"k8s.io/klog"
)

type fileFn string

const (
fileFnFile fileFn = "file"
fileFnFileBase64 fileFn = "filebase64"
)

// Literal represents a literal in terraform syntax
type Literal struct {
// Value is only used in terraform 0.11 and represents the literal string to use as a value.
Expand All @@ -37,8 +44,10 @@ type Literal struct {
ResourceName string `cty:"resource_name"`
// ResourceProp represents the property of a resource in a literal reference
ResourceProp string `cty:"resource_prop"`
// FilePath represents the path for a file() reference
// FilePath represents the path for a file reference
FilePath string `cty:"file_path"`
// FileFn represents the function used to reference the file
FileFn fileFn `cty:"file_fn"`
}

var _ json.Marshaler = &Literal{}
Expand All @@ -48,13 +57,16 @@ func (l *Literal) MarshalJSON() ([]byte, error) {
}

func LiteralFileExpression(modulePath string, base64 bool) *Literal {
fn := "file"
fn := fileFnFile
if base64 {
fn = "filebase64"
fn = fileFnFileBase64
}
return &Literal{
Value: fmt.Sprintf("${%v(%q)}", fn, modulePath),
// file() is hardcoded here because this field is
// used for Terraform 0.11 which does not have filebase64()
Value: fmt.Sprintf("${file(%q)}", modulePath),
FilePath: modulePath,
FileFn: fn,
}
}

Expand Down

0 comments on commit 1cf9838

Please sign in to comment.