Refactor GSPath use of Literal

This commit is contained in:
John Gardiner Myers 2022-11-26 17:48:22 -08:00
parent c2d9b871a5
commit 8b6f2973f8
3 changed files with 19 additions and 13 deletions

View File

@ -27,10 +27,6 @@ import (
type Literal struct {
// String is the Terraform representation.
String string `cty:"string"`
// FnArgs contains string representations of arguments to the function call.
// Any string arguments must be quoted.
FnArgs []string `cty:"fn_arg"`
}
var _ json.Marshaler = &Literal{}
@ -42,7 +38,6 @@ func (l *Literal) MarshalJSON() ([]byte, error) {
func LiteralFunctionExpression(functionName string, args ...string) *Literal {
return &Literal{
String: fmt.Sprintf("%v(%v)", functionName, strings.Join(args, ", ")),
FnArgs: args,
}
}

View File

@ -75,6 +75,20 @@ func (t *TerraformWriter) InitTerraformWriter() {
}
func (t *TerraformWriter) AddFileBytes(resourceType string, resourceName string, key string, data []byte, base64 bool) (*Literal, error) {
path, err := t.AddFilePath(resourceType, resourceName, key, data, base64)
if err != nil {
return nil, err
}
fn := "file"
if base64 {
fn = "filebase64"
}
return LiteralFunctionExpression(fn, path.String), nil
}
func (t *TerraformWriter) AddFilePath(resourceType string, resourceName string, key string, data []byte, base64 bool) (*Literal, error) {
id := resourceType + "_" + resourceName + "_" + key
t.mutex.Lock()
@ -84,11 +98,8 @@ func (t *TerraformWriter) AddFileBytes(resourceType string, resourceName string,
t.Files[p] = data
modulePath := fmt.Sprintf("%q", path.Join("${path.module}", p))
fn := "file"
if base64 {
fn = "filebase64"
}
return LiteralFunctionExpression(fn, modulePath), nil
return LiteralTokens(modulePath), nil
}
func (t *TerraformWriter) RenderDataSource(dataType string, dataName string, e interface{}) error {

View File

@ -389,7 +389,7 @@ func (p *GSPath) Hash(a hashing.HashAlgorithm) (*hashing.Hash, error) {
type terraformGSObject struct {
Bucket string `json:"bucket" cty:"bucket"`
Name string `json:"name" cty:"name"`
Source string `json:"source" cty:"source"`
Source *terraformWriter.Literal `json:"source" cty:"source"`
Provider *terraformWriter.Literal `json:"provider,omitempty" cty:"provider"`
}
@ -406,7 +406,7 @@ func (p *GSPath) RenderTerraform(w *terraformWriter.TerraformWriter, name string
return fmt.Errorf("reading data: %v", err)
}
content, err := w.AddFileBytes("google_storage_bucket_object", name, "content", bytes, false)
content, err := w.AddFilePath("google_storage_bucket_object", name, "content", bytes, false)
if err != nil {
return fmt.Errorf("rendering GCS file: %v", err)
}
@ -414,7 +414,7 @@ func (p *GSPath) RenderTerraform(w *terraformWriter.TerraformWriter, name string
tf := &terraformGSObject{
Bucket: p.Bucket(),
Name: p.Object(),
Source: content.FnArgs[0],
Source: content,
Provider: terraformWriter.LiteralTokens("google", "files"),
}
err = w.RenderResource("google_storage_bucket_object", name, tf)