-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (125 loc) · 4.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/olekukonko/tablewriter"
)
var CURRENT_WORKSPACE string
func get_workspaces(binary_path string) []string {
// Execute terraform command for getting all workspaces
cmd := exec.Command(string(binary_path), "workspace", "list")
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return nil
}
lines := strings.Split(string(stdout), "\n")
// Remove the * character, strip whitespace from the lines, and skip blank lines.
var workspaces []string
for _, line := range lines {
if strings.Contains(line, "*") {
CURRENT_WORKSPACE = line
line = strings.ReplaceAll(line, "*", "")
}
line = strings.TrimSpace(line)
if line != "" {
workspaces = append(workspaces, line)
}
}
// Return list of workspaces
return workspaces
}
func deployment_data(binary_path string, workspaces []string) [][]string {
// Store workspace, hostname and IP
var machines [][]string
for _, workspace := range workspaces {
// Change workspace
workspace_cmd := exec.Command(string(binary_path), "workspace", "select", workspace)
_, err := workspace_cmd.Output()
if err != nil {
fmt.Println(err.Error())
return nil
}
output_cmd := exec.Command(string(binary_path), "show", "-json")
stdout, err := output_cmd.Output()
if err != nil {
fmt.Println(err.Error())
return nil
}
// Parse the JSON
var data map[string]interface{}
err = json.Unmarshal(stdout, &data)
if err != nil {
fmt.Println(err)
return nil
}
if len(data) == 1 {
machines = append(machines, []string{workspace, "", ""})
} else {
for _, child := range data["values"].(map[string]interface{})["root_module"].(map[string]interface{})["child_modules"].([]interface{})[1].(map[string]interface{})["child_modules"].([]interface{})[0].(map[string]interface{})["child_modules"].([]interface{}) {
for _, resource := range child.(map[string]interface{})["resources"].([]interface{}) {
if values, ok := resource.(map[string]interface{})["values"].(map[string]interface{}); ok {
if clone, ok := values["clone"].([]interface{}); ok && len(clone) > 0 {
if customize, ok := clone[0].(map[string]interface{})["customize"].([]interface{}); ok && len(customize) > 0 {
if linuxOptions, ok := customize[0].(map[string]interface{})["linux_options"].([]interface{}); ok && len(linuxOptions) > 0 {
if hostName, ok := linuxOptions[0].(map[string]interface{})["host_name"].(string); ok {
if networkInterface, ok := customize[0].(map[string]interface{})["network_interface"].([]interface{}); ok && len(networkInterface) > 0 {
if ipv4Address, ok := networkInterface[0].(map[string]interface{})["ipv4_address"].(string); ok {
if strings.Contains(CURRENT_WORKSPACE, workspace) {
machines = append(machines, []string{CURRENT_WORKSPACE, hostName, ipv4Address})
} else {
machines = append(machines, []string{workspace, hostName, ipv4Address})
}
}
}
}
}
}
}
}
}
}
}
}
return machines
}
func main() {
// Must set ENV for path to terraform binary
terraform_binary_path := os.Getenv("TFB_PATH")
if terraform_binary_path == "" {
fmt.Println("Must set TFB_PATH Environment variable for path to terraform binary.")
}
// Get all workspaces
workspaces := get_workspaces(terraform_binary_path)
if workspaces == nil {
fmt.Println("Could not get workspaces from terraform.")
return
}
// Populate machines data
machines := deployment_data(terraform_binary_path, workspaces)
if machines == nil {
fmt.Println("Something went wrong getting workspace data.")
return
}
// Build table for data
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Workspace", "HostName", "IP Address"})
table.SetAutoMergeCells(true)
table.SetRowLine(true)
for _, v := range machines {
table.Append(v)
}
table.Render()
// Return to current workspace
current_workspace := strings.ReplaceAll(CURRENT_WORKSPACE, "*", "")
current_workspace = strings.ReplaceAll(current_workspace, " ", "")
return_cmd := exec.Command(string(terraform_binary_path), "workspace", "select", current_workspace)
_, err := return_cmd.Output()
if err != nil {
fmt.Println(err)
return
}
}