forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
show.go
304 lines (256 loc) · 8.14 KB
/
show.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
Copyright 2017 WALLIX
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"bytes"
"errors"
"fmt"
"os"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/wallix/awless/cloud"
"github.com/wallix/awless/config"
"github.com/wallix/awless/console"
"github.com/wallix/awless/graph"
"github.com/wallix/awless/logger"
"github.com/wallix/awless/sync"
)
var (
listAllSiblingsFlag bool
showPropertiesValuesOnlyFlag []string
)
func init() {
RootCmd.AddCommand(showCmd)
showCmd.Flags().BoolVar(&listAllSiblingsFlag, "siblings", false, "List all the resource's siblings")
showCmd.Flags().StringSliceVar(&showPropertiesValuesOnlyFlag, "values-for", []string{}, "Output values only for given properties keys")
}
var showCmd = &cobra.Command{
Use: "show REFERENCE",
Short: "Show a resource and its interrelations given a REFERENCE: id or name",
Example: ` awless show i-8d43b21b # show an instance via its ref
awless show AIDAJ3Z24GOKHTZO4OIX6 # show a user via its ref
awless show jsmith # show a user via its ref,
awless show @jsmith # forcing search by name`,
PersistentPreRun: applyHooks(initLoggerHook, initAwlessEnvHook, initCloudServicesHook, initSyncerHook, firstInstallDoneHook),
PersistentPostRun: applyHooks(verifyNewVersionHook, onVersionUpgrade),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("REFERENCE required. See examples.")
}
ref := args[0]
notFound := fmt.Sprintf("resource with reference %s not found", deprefix(ref))
var resource *graph.Resource
var gph *graph.Graph
resource, gph = findResourceInLocalGraphs(ref)
if resource == nil && localGlobalFlag {
logger.Info(notFound)
return nil
} else if resource == nil {
runFullSync()
if resource, gph = findResourceInLocalGraphs(ref); resource == nil {
logger.Info(notFound)
return nil
}
}
if !localGlobalFlag && config.GetAutosync() {
srv, err := cloud.GetServiceForType(resource.Type())
exitOn(err)
logger.Verbosef("syncing service for %s type", resource.Type())
if _, err = sync.DefaultSyncer.Sync(srv); err != nil {
logger.Verbose(err)
}
resource, gph = findResourceInLocalGraphs(ref)
}
if resource != nil {
if len(showPropertiesValuesOnlyFlag) > 0 {
showResourceValuesOnlyFor(resource, showPropertiesValuesOnlyFlag)
return nil
}
showResource(resource, gph)
}
return nil
},
}
func showResourceValuesOnlyFor(resource *graph.Resource, propKeys []string) {
var normalized []string
for _, p := range propKeys {
normalized = append(normalized, strings.ToLower(strings.Replace(p, " ", "", -1)))
}
valuesForKeys := map[string]string{}
isIncluded := func(s string) (bool, string) {
for _, n := range normalized {
if n == strings.ToLower(s) {
return true, n
}
}
return false, ""
}
for k, v := range resource.Properties {
if ok, p := isIncluded(k); ok {
valuesForKeys[p] = fmt.Sprint(v)
}
}
var values []string
for _, n := range normalized {
if v, ok := valuesForKeys[n]; ok {
values = append(values, v)
}
}
fmt.Println(strings.Join(values, ","))
}
func showResource(resource *graph.Resource, gph *graph.Graph) {
displayer, err := console.BuildOptions(
console.WithHeaders(console.DefaultsColumnDefinitions[resource.Type()]),
console.WithFormat(listingFormat),
console.WithMaxWidth(console.GetTerminalWidth()),
).SetSource(resource).Build()
exitOn(err)
exitOn(displayer.Print(os.Stdout))
var parents []*graph.Resource
err = gph.Accept(&graph.ParentsVisitor{From: resource, Each: graph.VisitorCollectFunc(&parents)})
exitOn(err)
var parentsW bytes.Buffer
var count int
for i := len(parents) - 1; i >= 0; i-- {
if count == 0 {
fmt.Fprintf(&parentsW, "%s\n", parents[i])
} else {
fmt.Fprintf(&parentsW, "%s↳ %s\n", strings.Repeat("\t", count), parents[i])
}
count++
}
var childrenW bytes.Buffer
var hasChildren bool
printWithTabs := func(r *graph.Resource, distance int) error {
var tabs bytes.Buffer
tabs.WriteString(strings.Repeat("\t", count))
for i := 0; i < distance; i++ {
tabs.WriteByte('\t')
}
display := r.String()
if r.Same(resource) {
display = renderGreenFn(resource.String())
} else {
hasChildren = true
}
fmt.Fprintf(&childrenW, "%s↳ %s\n", tabs.String(), display)
return nil
}
err = gph.Accept(&graph.ChildrenVisitor{From: resource, Each: printWithTabs, IncludeFrom: true})
exitOn(err)
if len(parents) > 0 || hasChildren {
fmt.Println(renderCyanBoldFn("\n# Relations:"))
fmt.Printf(parentsW.String())
fmt.Printf(childrenW.String())
}
appliedOn, err := gph.ListResourcesAppliedOn(resource)
exitOn(err)
printResourceList(renderCyanBoldFn("Applied on"), appliedOn)
dependingOn, err := gph.ListResourcesDependingOn(resource)
exitOn(err)
printResourceList(renderCyanBoldFn("Depending on"), dependingOn)
var siblings []*graph.Resource
err = gph.Accept(&graph.SiblingsVisitor{From: resource, Each: graph.VisitorCollectFunc(&siblings)})
exitOn(err)
printResourceList(renderCyanBoldFn("Siblings"), siblings, "display all with flag --siblings")
}
func runFullSync() {
if !config.GetAutosync() {
logger.Info("autosync disabled")
return
}
logger.Info("cannot resolve resource - running full sync")
var services []cloud.Service
for _, srv := range cloud.ServiceRegistry {
services = append(services, srv)
}
if _, err := sync.DefaultSyncer.Sync(services...); err != nil {
logger.Verbose(err)
}
}
func findResourceInLocalGraphs(ref string) (*graph.Resource, *graph.Graph) {
g, resources := resolveResourceFromRef(ref)
switch len(resources) {
case 0:
return nil, nil
case 1:
return resources[0], g
default:
logger.Infof("%d resources found with name '%s'. Show a specific resource with:", len(resources), deprefix(ref))
for _, res := range resources {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("\t`awless show %s` to show the %s", res.Id(), res.Type()))
if state, ok := res.Properties["State"].(string); ok {
buf.WriteString(fmt.Sprintf(" (state: '%s')", state))
}
logger.Infof(buf.String())
}
os.Exit(0)
}
return nil, nil
}
func resolveResourceFromRef(ref string) (*graph.Graph, []*graph.Resource) {
g, err := sync.LoadAllLocalGraphs()
exitOn(err)
name := deprefix(ref)
byName := &graph.ByProperty{Key: "Name", Value: name}
if strings.HasPrefix(ref, "@") {
logger.Verbosef("prefixed with @: forcing research by name '%s'", name)
rs, err := g.ResolveResources(byName)
exitOn(err)
return g, rs
} else {
rs, err := g.ResolveResources(&graph.ById{Id: name})
exitOn(err)
if len(rs) > 0 {
return g, rs
} else {
rs, err := g.ResolveResources(
byName,
&graph.ByProperty{Key: "Arn", Value: name},
)
exitOn(err)
return g, rs
}
}
}
func deprefix(s string) string {
return strings.TrimPrefix(s, "@")
}
func printResourceList(title string, list []*graph.Resource, shortenListMsg ...string) {
sort.Sort(byTypeAndString{list})
all := graph.Resources(list).Map(func(r *graph.Resource) string { return r.String() })
count := len(all)
max := 3
if count > 0 {
if !listAllSiblingsFlag && len(shortenListMsg) > 0 && count > max {
fmt.Printf("\n%s: %s, ... (%s)\n", title, strings.Join(all[0:max], ", "), shortenListMsg[0])
} else {
fmt.Printf("\n%s: %s\n", title, strings.Join(all, ", "))
}
}
}
type byTypeAndString struct {
res []*graph.Resource
}
func (b byTypeAndString) Len() int { return len(b.res) }
func (b byTypeAndString) Swap(i, j int) {
b.res[i], b.res[j] = b.res[j], b.res[i]
}
func (b byTypeAndString) Less(i, j int) bool {
if b.res[i].Type() != b.res[j].Type() {
return b.res[i].Type() < b.res[j].Type()
}
return b.res[i].String() <= b.res[j].String()
}