/
terraform_get_output.go
182 lines (171 loc) · 5 KB
/
terraform_get_output.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// originally generated by 'threeport-sdk codegen api-model' but will not be regenerated - intended for modification
package cmd
import (
"fmt"
"net/http"
"os"
"text/tabwriter"
v0 "github.com/threeport/threeport/pkg/api/v0"
client "github.com/threeport/threeport/pkg/client/v0"
util "github.com/threeport/threeport/pkg/util/v0"
)
// outputGetTerraformsCmd produces the tabular output for the
// 'tptctl get terraforms' command.
func outputGetTerraformsCmd(
terraformInstances *[]v0.TerraformInstance,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t TERRAFORM DEFINITION\t TERRAFORM INSTANCE\t AWS ACCOUNT\t STATUS\t AGE")
metadataErr := false
var terraformDefErr error
var awsAccountErr error
for _, terraformInst := range *terraformInstances {
// get terraform definition name for instance
var terraformDef string
terraformDefinition, err := client.GetTerraformDefinitionByID(apiClient, apiEndpoint, *terraformInst.TerraformDefinitionID)
if err != nil {
metadataErr = true
terraformDefErr = err
terraformDef = "<error>"
} else {
terraformDef = *terraformDefinition.Name
}
// get AWS acocunt name
var awsAccountName string
awsAccount, err := client.GetAwsAccountByID(apiClient, apiEndpoint, *terraformInst.AwsAccountID)
if err != nil {
metadataErr = true
awsAccountErr = err
awsAccountName = "<error>"
} else {
awsAccountName = *awsAccount.Name
}
// get terraform instance status
terraformInstStatus := "Reconciling"
if *terraformInst.Reconciled {
terraformInstStatus = "Healthy"
}
if *terraformInst.CreationFailed {
terraformInstStatus = "Failed"
}
fmt.Fprintln(
writer,
terraformDef, "\t",
terraformDef, "\t",
*terraformInst.Name, "\t",
awsAccountName, "\t",
terraformInstStatus, "\t",
util.GetAge(terraformInst.CreatedAt),
)
}
writer.Flush()
if metadataErr {
multiError := util.MultiError{}
if terraformDefErr != nil {
multiError.AppendError(
fmt.Errorf("encountered an error retrieving terraform definition info: %w", terraformDefErr),
)
}
if awsAccountErr != nil {
multiError.AppendError(
fmt.Errorf("encountered an error retrieving AWS account info: %w", terraformDefErr),
)
}
return multiError.Error()
}
return nil
}
// outputGetTerraformDefinitionsCmd produces the tabular output for the
// 'tptctl get terraform-definitions' command.
func outputGetTerraformDefinitionsCmd(
terraformDefinitions *[]v0.TerraformDefinition,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t AGE")
for _, terraformDefinition := range *terraformDefinitions {
fmt.Fprintln(
writer,
*terraformDefinition.Name, "\t",
util.GetAge(terraformDefinition.CreatedAt),
)
}
writer.Flush()
return nil
}
// outputGetTerraformInstancesCmd produces the tabular output for the
// 'tptctl get terraform-instances' command.
func outputGetTerraformInstancesCmd(
terraformInstances *[]v0.TerraformInstance,
apiClient *http.Client,
apiEndpoint string,
) error {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', 0)
fmt.Fprintln(writer, "NAME\t TERRAFORM DEFINITION\t AWS ACCOUNT NAME\t STATUS\t AGE")
metadataErr := false
var terraformDefErr error
var awsAccountErr error
var statusErr error
for _, terraformInst := range *terraformInstances {
// get terraform definition name for instance
var terraformDef string
terraformDefinition, err := client.GetTerraformDefinitionByID(apiClient, apiEndpoint, *terraformInst.TerraformDefinitionID)
if err != nil {
metadataErr = true
terraformDefErr = err
terraformDef = "<error>"
} else {
terraformDef = *terraformDefinition.Name
}
// get AWS acocunt name
var awsAccountName string
awsAccount, err := client.GetAwsAccountByID(apiClient, apiEndpoint, *terraformInst.AwsAccountID)
if err != nil {
metadataErr = true
awsAccountErr = err
awsAccountName = "<error>"
} else {
awsAccountName = *awsAccount.Name
}
// get terraform instance status
terraformInstStatus := "Reconciling"
if *terraformInst.Reconciled {
terraformInstStatus = "Healthy"
}
if *terraformInst.CreationFailed {
terraformInstStatus = "Failed"
}
fmt.Fprintln(
writer,
*terraformInst.Name, "\t",
terraformDef, "\t",
awsAccountName, "\t",
terraformInstStatus, "\t",
util.GetAge(terraformInst.CreatedAt),
)
}
writer.Flush()
if metadataErr {
multiError := util.MultiError{}
if terraformDefErr != nil {
multiError.AppendError(
fmt.Errorf("encountered an error retrieving terraform definition info: %w", terraformDefErr),
)
}
if awsAccountErr != nil {
multiError.AppendError(
fmt.Errorf("encountered an error retrieving AWS account info: %w", terraformDefErr),
)
}
if statusErr != nil {
multiError.AppendError(
fmt.Errorf("encountered an error retrieving terraform instance status: %w", statusErr),
)
}
return multiError.Error()
}
return nil
}