// Runs the sequence of terraform commands most environemnts need, returns the
// output bytes of `terraform output -json` and a cleanup function to teardown
// the created resources.
//
// 1. Run terraform init
// 2. Create a new terraform workspace for the test run ID
// 3. Run terraform apply
// 4. Get output results from terraform output
//
// Cleanup method runs terraform destroy and then deletes the workspace.
Runs the sequence of terraform commands most environemnts need, returns the
output bytes of `terraform output -json` and a cleanup function to teardown
the created resources.

1. Run terraform init
2. Create a new terraform workspace for the test run ID
3. Run terraform apply
4. Get output results from terraform output

Cleanup method runs terraform destroy and then deletes the workspace.
// Runs the sequence of terraform commands most environemnts need returns the // output bytes of ` terraform output - json ` and a cleanup function to teardown // the created resources . // // 1 . Run terraform init // 2 . Create a new terraform workspace for the test run ID // 3 . Run terraform apply // 4 . Get output results from terraform output // // Cleanup method runs terraform destroy and then deletes the workspace .
func SetupTf(
	ctx context.Context,
	projectID string,
	testRunID string,
	tfDir string, 
	tfVars map[string]string, 
	logger *log.Logger,
) (*PubsubInfo, func(), error) {
	tfVarArgs := tfVarMapToArgs(projectID, tfVars)
	cmd := initCommand(ctx, projectID)
	cmd.Args = append(cmd.Args, tfVarArgs...)
	cmd.Dir = tfDir
	if err := runWithOutput(cmd, logger); err != nil {
		return nil, func() {}, err
	}
	cleanup := func() {
		defer deleteWorkspace(ctx, testRunID, tfDir, logger)
		cmd = exec.CommandContext(
			ctx,
			"terraform",
			"destroy",
			"-input=false",
			"-auto-approve",
		)
		cmd.Args = append(cmd.Args, tfVarArgs...)
		cmd.Dir = tfDir
		if err := runWithOutput(cmd, logger); err != nil {
			logger.Panic(err)
		}
	}
	cmd = exec.CommandContext(ctx, "terraform", "workspace", "new", testRunID)
	cmd.Dir = tfDir
	if err := runWithOutput(cmd, logger); err != nil {
		cmd = exec.CommandContext(ctx, "terraform", "workspace", "select", testRunID)
		cmd.Dir = tfDir
		if err := runWithOutput(cmd, logger); err != nil {
			return nil, cleanup, err
		}
	}
	cmd = exec.CommandContext(
		ctx,
		"terraform",
		"apply",
		"-input=false",
		"-auto-approve",
	)
	cmd.Args = append(cmd.Args, tfVarArgs...)
	cmd.Dir = tfDir
	if err := runWithOutput(cmd, logger); err != nil {
		return nil, cleanup, err
	}
	cmd = exec.CommandContext(ctx, "terraform", "output", "-json")
	cmd.Dir = tfDir
	out, err := cmd.Output()
	if err != nil {
		logger.Println(err)
		return nil, cleanup, err
	}
	tfOutput := &tfOutput{}
	if err := json.Unmarshal(out, tfOutput); err != nil {
		return nil, cleanup, err
	}
	return &tfOutput.PubsubInfoWrapper.Value, cleanup, nil
}
func SetupTf ( ctx context . Context , projectID string , testRunID string , tfDir string , tfVars map [ string ] string , logger * log . Logger , ) ( * PubsubInfo , func ( ) , error ) { tfVarArgs := tfVarMapToArgs ( projectID , tfVars ) cmd := initCommand ( ctx , projectID ) cmd . Args = append ( cmd . Args , tfVarArgs ... ) cmd . Dir = tfDir if err := runWithOutput ( cmd , logger ) ; err != nil { return nil , func ( ) { } , err } cleanup := func ( ) { defer deleteWorkspace ( ctx , testRunID , tfDir , logger ) cmd = exec . CommandContext ( ctx , " " , " " , " " , " " , ) cmd . Args = append ( cmd . Args , tfVarArgs ... ) cmd . Dir = tfDir if err := runWithOutput ( cmd , logger ) ; err != nil { logger . Panic ( err ) } } cmd = exec . CommandContext ( ctx , " " , " " , " " , testRunID ) cmd . Dir = tfDir if err := runWithOutput ( cmd , logger ) ; err != nil { cmd = exec . CommandContext ( ctx , " " , " " , " " , testRunID ) cmd . Dir = tfDir if err := runWithOutput ( cmd , logger ) ; err != nil { return nil , cleanup , err } } cmd = exec . CommandContext ( ctx , " " , " " , " " , " " , ) cmd . Args = append ( cmd . Args , tfVarArgs ... ) cmd . Dir = tfDir if err := runWithOutput ( cmd , logger ) ; err != nil { return nil , cleanup , err } cmd = exec . CommandContext ( ctx , " " , " " , " " ) cmd . Dir = tfDir out , err := cmd . Output ( ) if err != nil { logger . Println ( err ) return nil , cleanup , err } tfOutput := & tfOutput { } if err := json . Unmarshal ( out , tfOutput ) ; err != nil { return nil , cleanup , err } return & tfOutput . PubsubInfoWrapper . Value , cleanup , nil }
Runs the sequence of terraform commands most environemnts need, returns the
output bytes of `terraform output -json` and a cleanup function to teardown
the created resources.
Runs the sequence of terraform commands most environemnts need returns the output bytes of ` terraform output - json ` and a cleanup function to teardown the created resources .
// the Dir to set when running terraform commands in e.g. tf/gke // key-values for terraform input vars to send to terraform // Run terraform destroy // Create new terraform workspace // try to switch to workspace if it already exists // Run terraform apply // Run terraform output