Skip to content

Commit

Permalink
Resolve local variables (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
obierlaire authored Jun 27, 2023
1 parent ecfacb4 commit d7f6fa4
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 56 deletions.
3 changes: 2 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/carboniferio/carbonifer/internal/estimate"
"github.com/carboniferio/carbonifer/internal/output"
"github.com/carboniferio/carbonifer/internal/plan"
"github.com/carboniferio/carbonifer/internal/terraform"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -60,7 +61,7 @@ Example usages:
}

// Read resources from terraform plan
resources, err := terraform.GetResources(tfPlan)
resources, err := plan.GetResources(tfPlan)
if err != nil {
log.Fatal(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func GetDefaults(awsConfig *tfjson.ProviderConfig, tfPlan *tfjson.Plan, terrafor

region := getDefaultRegion(awsConfig, tfPlan)
if region != nil {
terraformRefs.ProviderConfigs["region"] = region.(string)
terraformRefs.ProviderConfigs["region"] = *region
}
}

func getDefaultRegion(awsConfig *tfjson.ProviderConfig, tfPlan *tfjson.Plan) interface{} {
func getDefaultRegion(awsConfig *tfjson.ProviderConfig, tfPlan *tfjson.Plan) *string {
var region interface{}
regionExpr := awsConfig.Expressions["region"]
if regionExpr != nil {
Expand Down Expand Up @@ -61,5 +61,10 @@ func getDefaultRegion(awsConfig *tfjson.ProviderConfig, tfPlan *tfjson.Plan) int
region, _ = svc.Region()
}
}
return region
regionPtr, ok := region.(*string)
if ok {
return regionPtr
}
regionString := region.(string)
return &regionString
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package aws
import (
"io"
"os"
"path"
"path/filepath"
"testing"

"github.com/carboniferio/carbonifer/internal/terraform"
"github.com/carboniferio/carbonifer/internal/testutils"
_ "github.com/carboniferio/carbonifer/internal/testutils"

"github.com/carboniferio/carbonifer/internal/utils"
Expand All @@ -28,7 +31,7 @@ func Test_getDefaultRegion_providerConstant(t *testing.T) {
tfPlan := &tfjson.Plan{}

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "test1", region)
assert.Equal(t, "test1", *region)

}

Expand All @@ -53,7 +56,7 @@ func Test_getDefaultRegion_providerVariable(t *testing.T) {
}

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "test2", region)
assert.Equal(t, "test2", *region)

}

Expand All @@ -68,7 +71,7 @@ func Test_getDefaultRegion_EnvVar(t *testing.T) {
t.Setenv("AWS_REGION", "test3")

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "test3", region)
assert.Equal(t, "test3", *region)

}

Expand All @@ -83,7 +86,7 @@ func Test_getDefaultRegion_EnvDefaultVar(t *testing.T) {
t.Setenv("AWS_DEFAULT_REGION", "test4")

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "test4", region)
assert.Equal(t, "test4", *region)

}

Expand Down Expand Up @@ -116,7 +119,7 @@ func Test_getDefaultRegion_AWSConfigFile(t *testing.T) {
tfPlan := &tfjson.Plan{}

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "region_from_config_file", region)
assert.Equal(t, "region_from_config_file", *region)

}

Expand Down Expand Up @@ -164,7 +167,7 @@ func Test_getDefaultRegion_ModuleOutput(t *testing.T) {
}

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "region_from_module_output", region)
assert.Equal(t, "region_from_module_output", *region)
}

func Test_getDefaultRegion_ModuleVariable(t *testing.T) {
Expand Down Expand Up @@ -208,7 +211,7 @@ func Test_getDefaultRegion_ModuleVariable(t *testing.T) {
}

region := getDefaultRegion(awsConfigs, tfPlan)
assert.Equal(t, "region_module_variable", region)
assert.Equal(t, "region_module_variable", *region)
}

func TestGetValueOfExpression_ModuleCalls(t *testing.T) {
Expand All @@ -223,3 +226,20 @@ func TestGetValueOfExpression_ModuleCalls(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "region_from_module_calls", value)
}

func TestGetValueOfExpression_ModuleLocalVar(t *testing.T) {
terraform.ResetTerraformExec()
wd := path.Join(testutils.RootDir, "test/terraform/gcp_calling_module")

plan, err := terraform.CarboniferPlan(wd) // Replace with the path to your plan JSON
assert.NoError(t, err)
expr := &tfjson.Expression{
ExpressionData: &tfjson.ExpressionData{
References: []string{"module.globals.common_region"},
},
}

value, err := utils.GetValueOfExpression(expr, plan)
assert.NoError(t, err)
assert.Equal(t, "local_module_region", value)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package terraform
package plan

import (
"encoding/json"
"strings"

"github.com/carboniferio/carbonifer/internal/plan/aws"
"github.com/carboniferio/carbonifer/internal/plan/gcp"
"github.com/carboniferio/carbonifer/internal/resources"
"github.com/carboniferio/carbonifer/internal/terraform/aws"
"github.com/carboniferio/carbonifer/internal/terraform/gcp"
"github.com/carboniferio/carbonifer/internal/terraform/tfrefs"
tfjson "github.com/hashicorp/terraform-json"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package terraform
package plan_test

import (
"log"
"path"
"testing"

"github.com/carboniferio/carbonifer/internal/plan"
"github.com/carboniferio/carbonifer/internal/providers"
"github.com/carboniferio/carbonifer/internal/resources"
"github.com/carboniferio/carbonifer/internal/terraform"
"github.com/carboniferio/carbonifer/internal/testutils"
"github.com/shopspring/decimal"
"github.com/spf13/viper"
Expand All @@ -18,7 +20,7 @@ func TestGetResource_DiskFromAMI(t *testing.T) {
testutils.SkipWithCreds(t)

// reset
ResetTerraformExec()
terraform.ResetTerraformExec()

wd := path.Join(testutils.RootDir, "test/terraform/aws_ec2")
viper.Set("workdir", wd)
Expand Down Expand Up @@ -56,9 +58,9 @@ func TestGetResource_DiskFromAMI(t *testing.T) {
}
log.Default().Println(wantResources)

tfPlan, err := TerraformPlan()
tfPlan, err := terraform.TerraformPlan()
assert.NoError(t, err)
gotResources, err := GetResources(tfPlan)
gotResources, err := plan.GetResources(tfPlan)
assert.NoError(t, err)
for _, res := range gotResources {
if res.GetIdentification().ResourceType == "aws_instance" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package terraform_test
package plan_test

import (
"log"
"path"
"testing"

"github.com/carboniferio/carbonifer/internal/plan"
"github.com/carboniferio/carbonifer/internal/providers"
"github.com/carboniferio/carbonifer/internal/resources"
"github.com/carboniferio/carbonifer/internal/terraform"
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestGetResources(t *testing.T) {
},
}
tfPlan, _ := terraform.TerraformPlan()
resources, _ := terraform.GetResources(tfPlan)
resources, _ := plan.GetResources(tfPlan)
assert.Equal(t, len(resources), len(wantResources))
for i, resource := range resources {
wantResource := wantResources[i]
Expand Down Expand Up @@ -176,7 +177,7 @@ func TestGetResources_DiskImage(t *testing.T) {
}

tfPlan, _ := terraform.TerraformPlan()
resourceList, err := terraform.GetResources(tfPlan)
resourceList, err := plan.GetResources(tfPlan)
if assert.NoError(t, err) {
assert.Equal(t, len(wantResources), len(resourceList))
for i, resource := range resourceList {
Expand Down Expand Up @@ -235,7 +236,7 @@ func TestGetResources_GroupInstance(t *testing.T) {
}

tfPlan, _ := terraform.TerraformPlan()
resources, err := terraform.GetResources(tfPlan)
resources, err := plan.GetResources(tfPlan)
if assert.NoError(t, err) {
for i, resource := range resources {
wantResource := wantResources[i]
Expand Down Expand Up @@ -292,7 +293,7 @@ func TestGetResources_InstanceFromTemplate(t *testing.T) {
}

tfPlan, _ := terraform.TerraformPlan()
resources, err := terraform.GetResources(tfPlan)
resources, err := plan.GetResources(tfPlan)
if assert.NoError(t, err) {
for i, resource := range resources {
wantResource := wantResources[i]
Expand Down
41 changes: 41 additions & 0 deletions internal/terraform/terraform.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package terraform

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -137,6 +139,45 @@ func CarboniferPlan(input string) (*tfjson.Plan, error) {
}
}

func RunTerraformConsole(command string) (*string, error) {
tfExec, err := GetTerraformExec()
if err != nil {
return nil, err
}
cmd := exec.Command(tfExec.ExecPath(), "console")

cmd.Dir = terraformExec.WorkingDir() // set the working directory

var stdin bytes.Buffer
stdin.Write([]byte(command + "\n"))
cmd.Stdin = &stdin

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("error running terraform console: %w\nstderr: %s", err, stderr.String())
}

output := strings.TrimSpace(stdout.String())

// Parse the output as JSON
var result map[string]string
if err := json.Unmarshal([]byte(output), &result); err == nil {
// If the output is valid JSON, extract the value of the command key
var ok bool
output, ok = result[command]
if !ok {
return nil, fmt.Errorf("output does not contain key %q", command)
}
}
// remove quotes surrounding the value
output = strings.Trim(output, "\"")
return &output, nil
}

func TerraformPlan() (*tfjson.Plan, error) {
tf, ctx, err := TerraformInit()
if err != nil {
Expand Down
Loading

0 comments on commit d7f6fa4

Please sign in to comment.