Skip to content

Commit d5c4d8a

Browse files
Undermyspellhuf1
authored andcommitted
feat(terraform-recipe): extent terraform recipe to retrieve all tf output values as json
1 parent cc72ff2 commit d5c4d8a

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

pkg/recipes/terraform/terraform.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"reflect"
11+
"strings"
12+
713
"github.com/conplementag/cops-hq/v2/internal"
814
"github.com/conplementag/cops-hq/v2/internal/cmdutil"
915
"github.com/conplementag/cops-hq/v2/internal/file_handling"
@@ -12,11 +18,6 @@ import (
1218
"github.com/conplementag/cops-hq/v2/pkg/error_handling"
1319
"github.com/conplementag/cops-hq/v2/pkg/recipes/terraform/file_paths"
1420
"github.com/sirupsen/logrus"
15-
"os"
16-
"os/exec"
17-
"path/filepath"
18-
"reflect"
19-
"strings"
2021
)
2122

2223
// Terraform is a wrapper around common terraform functionality used in IaC projects with Azure. In includes remote state
@@ -84,6 +85,11 @@ type Terraform interface {
8485
// Parameters are:
8586
// parameterName will restrict output to single output parameter and output the parameter in raw mode. If not set all available output is given
8687
Output(parameterName *string) (string, error)
88+
89+
// OutputAsJson returns the terraform provided output as json string
90+
//
91+
// CAUTION: Returns sensitive values in plain text
92+
OutputAsJson() (string, error)
8793
}
8894

8995
type terraformWrapper struct {
@@ -310,6 +316,16 @@ func (tf *terraformWrapper) Output(parameterName *string) (string, error) {
310316
return tf.executor.Execute(tfCommand)
311317
}
312318

319+
func (tf *terraformWrapper) OutputAsJson() (string, error) {
320+
321+
tfCommand := "terraform" +
322+
" -chdir=" + tf.terraformDirectory +
323+
" output" +
324+
" -json"
325+
326+
return tf.executor.ExecuteSilent(tfCommand)
327+
}
328+
313329
func (tf *terraformWrapper) addStorageAccountNetworkRules() error {
314330
existingIpAddresses, err := tf.determineCurrentAllowedIpAddresses()
315331
if err != nil {

0 commit comments

Comments
 (0)