Skip to content

Commit

Permalink
resourceop tf: support ${telophase.region} replacement in tf
Browse files Browse the repository at this point in the history
This supports using the telophase.region when Region is set on a
terraform stack
  • Loading branch information
dschofie committed Apr 19, 2024
1 parent ceb4e0c commit 8e50550
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/localstack/tf/workspace/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ locals {

provider "aws" {
# Two options can use ${telophase.region} or look at local config
region = local.region
region = "${telophase.region}"
}

terraform {
Expand Down
18 changes: 13 additions & 5 deletions lib/terraform/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TmpPath(acct resource.Account, filePath string) string {
return path.Join("telophasedirs", fmt.Sprintf("tf-tmp%s-%s", acct.ID(), hashString))
}

func CopyDir(src string, dst string, resource resource.Resource) error {
func CopyDir(stack resource.Stack, dst string, resource resource.Resource) error {
ignoreDir := "telophasedirs"

abs, err := filepath.Abs(src)
abs, err := filepath.Abs(stack.Path)
if err != nil {
return oops.Wrapf(err, "could not get absolute file path for path: %s", src)
return oops.Wrapf(err, "could not get absolute file path for path: %s", stack.Path)
}
return filepath.Walk(abs, func(path string, info fs.FileInfo, err error) error {
if err != nil {
Expand All @@ -46,12 +46,12 @@ func CopyDir(src string, dst string, resource resource.Resource) error {
if info.IsDir() {
return os.MkdirAll(targetPath, info.Mode())
} else {
return replaceVariablesInFile(path, targetPath, resource)
return replaceVariablesInFile(path, targetPath, resource, stack)
}
})
}

func replaceVariablesInFile(srcFile, dstFile string, resource resource.Resource) error {
func replaceVariablesInFile(srcFile, dstFile string, resource resource.Resource, stack resource.Stack) error {
content, err := ioutil.ReadFile(srcFile)
if err != nil {
return err
Expand All @@ -68,5 +68,13 @@ func replaceVariablesInFile(srcFile, dstFile string, resource resource.Resource)
updatedContent = strings.ReplaceAll(updatedContent, "${telophase.resource_name}", resource.Name())
updatedContent = strings.ReplaceAll(updatedContent, "telophase.resource_name", fmt.Sprintf("\"%s\"", resource.Name()))

// Update Region
preRegionContent := updatedContent
updatedContent = strings.ReplaceAll(updatedContent, "${telophase.region}", stack.Region)
updatedContent = strings.ReplaceAll(updatedContent, "telophase.region", stack.Region)
if updatedContent != preRegionContent && stack.Region == "" {
return oops.Errorf("Region needs to be set on stack if performing substitution")
}

return ioutil.WriteFile(dstFile, []byte(updatedContent), 0644)
}
2 changes: 1 addition & 1 deletion resourceoperation/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (so *scpOperation) initTf() (*exec.Cmd, error) {
return nil, fmt.Errorf("failed to create directory %s: %v", terraformDir, err)
}

if err := terraform.CopyDir(so.Stack.Path, workingPath, so.targetResource()); err != nil {
if err := terraform.CopyDir(so.Stack, workingPath, so.targetResource()); err != nil {
return nil, fmt.Errorf("failed to copy files from %s to %s: %v", so.Stack.Path, workingPath, err)
}

Expand Down
2 changes: 1 addition & 1 deletion resourceoperation/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (to *tfOperation) initTf(role *sts.AssumeRoleOutput) *exec.Cmd {
return nil
}

if err := terraform.CopyDir(to.Stack.Path, workingPath, *to.Account); err != nil {
if err := terraform.CopyDir(to.Stack, workingPath, *to.Account); err != nil {
to.OutputUI.Print(fmt.Sprintf("Error: failed to copy files from %s to %s: %v", to.Stack.Path, workingPath, err), *to.Account)
return nil
}
Expand Down

0 comments on commit 8e50550

Please sign in to comment.