-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from ministryofjustice/bulk-plan
Add Terraform plan to CLI commands
- Loading branch information
Showing
6 changed files
with
225 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package terraform | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// targetDirs return the directories where terraform plan is going to be executed | ||
func targetDirs(file string) ([]string, error) { | ||
var dirs []string // Directories where tf plan is going to be executed | ||
|
||
dirsWhitelist := []string{ | ||
"terraform/cloud-platform-components", | ||
"terraform/cloud-platform", | ||
"terraform/cloud-platform-eks/components", | ||
"terraform/cloud-platform-eks", | ||
} | ||
|
||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
// The first condition evaluates if the element already exists in the slice (why to execute | ||
// plan twice against the same dir?). The second condition evaluates if the element is in | ||
// the desired list to execute Plan (we don't want to execute Plan against everything) | ||
if contains(dirs, filepath.Dir(scanner.Text())) != true && | ||
contains(dirsWhitelist, filepath.Dir(scanner.Text())) == true { | ||
dirs = append(dirs, filepath.Dir(scanner.Text())) | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return dirs, nil | ||
} | ||
|
||
func contains(s []string, e string) bool { | ||
for _, a := range s { | ||
if a == e { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package terraform | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// TestTargetDirs confirms given a file directory (string) in a text file, | ||
// the targetDirs function will extract the directory path successfully. | ||
func TestTargetDirs(t *testing.T) { | ||
fileName := "changedFiles" | ||
fileString := "/this/is/a/test/dir" | ||
|
||
// A temp file is created with a string inside | ||
file, err := os.Create(fileName) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
_, err = file.WriteString(fileString) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
err = file.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// The temp file is passed to the targetDirs function so it can extract the string containing | ||
// a directory path. | ||
target, err := targetDirs(fileName) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// The value extracted from the text file is compared to its expected value. If false, the | ||
// test will fail. | ||
for _, v := range target { | ||
if strings.Contains(fileString, v) { | ||
fmt.Println("Test passes") | ||
} else { | ||
t.Error("Files do not match, test fails") | ||
} | ||
} | ||
os.Remove(fileName) | ||
} |