This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
/
show.go
257 lines (225 loc) · 6.31 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
// Copyright © 2017 Intel Corporation
//
// 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 cmd
import (
"fmt"
"github.com/ciao-project/ciao/ciao-controller/api"
"github.com/ciao-project/ciao/ciao-controller/types"
"github.com/intel/tfortools"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var showCmd = &cobra.Command{
Use: "show",
Short: "Show detailed information about an object",
}
var cnciShowCmd = &cobra.Command{
Use: "cnci ID",
Short: "Show information about a CNCI",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !c.IsPrivileged() {
return errors.New("CNCI information is restricted to privileged users")
}
cncis, err := c.ListCNCIs()
if err != nil {
return errors.Wrap(err, "Error listing CNCIs")
}
var cnci *types.CiaoCNCI
for i := range cncis.CNCIs {
if cncis.CNCIs[i].ID == args[0] {
cnci = &cncis.CNCIs[0]
break
}
}
if cnci == nil {
return fmt.Errorf("CNCI %s not found", args[0])
}
return render(cmd, cnci)
},
Annotations: map[string]string{
"template_usage": tfortools.GenerateUsageUndecorated(types.CiaoCNCI{}),
},
}
var imageShowCmd = &cobra.Command{
Use: "image ID",
Short: "Show information about an image",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
image, err := c.GetImage(args[0])
if err != nil {
return errors.Wrap(err, "Error getting image")
}
return render(cmd, image)
},
Annotations: map[string]string{
"template_usage": tfortools.GenerateUsageUndecorated(types.Image{}),
},
}
var instanceShowCmd = &cobra.Command{
Use: "instance ID",
Short: "Show information about an instance",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
server, err := c.GetInstance(args[0])
if err != nil {
return errors.Wrap(err, "Error getting instance")
}
return render(cmd, server.Server)
},
Annotations: map[string]string{
"template_usage": tfortools.GenerateUsageUndecorated(api.ServerDetails{}),
},
}
var nodeShowCmd = &cobra.Command{
Use: "node ID",
Short: "Show information about a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !c.IsPrivileged() {
return errors.New("Node information is restricted to privileged users")
}
nodes, err := c.ListNodes()
if err != nil {
return errors.Wrap(err, "Error listing node")
}
var node *types.CiaoNode
for i := range nodes.Nodes {
if nodes.Nodes[i].ID == args[0] {
node = &nodes.Nodes[i]
break
}
}
if node == nil {
return fmt.Errorf("Node %s not found", args[0])
}
return render(cmd, node)
},
Annotations: map[string]string{
"template_usage": tfortools.GenerateUsageUndecorated(types.CiaoNode{}),
},
}
var tenantShowCmd = &cobra.Command{
Use: "tenant ID",
Short: "Show tenant configuration",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !c.IsPrivileged() {
return errors.New("Tenant configuration is restricted to privileged users")
}
tenant, err := c.GetTenantConfig(args[0])
if err != nil {
return errors.Wrap(err, "Error getting tenant config")
}
return render(cmd, tenant)
},
Annotations: map[string]string{
"default_template": `{{ htable (cols (sliceof .) "ID" "Name") }}`,
"template_usage": tfortools.GenerateUsageUndecorated(types.TenantConfig{}),
},
}
var traceShowCmd = &cobra.Command{
Use: "trace LABEL",
Short: "Show trace data for a label",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
data, err := c.GetTraceData(args[0])
if err != nil {
return errors.Wrap(err, "Error gettting trace data")
}
return render(cmd, data.Summary)
},
Annotations: map[string]string{
"template_usage": tfortools.GenerateUsageUndecorated(types.CiaoBatchFrameStat{}),
},
}
var volumeShowTemplate = `ID: {{ .ID }}
Name: {{ .Name }}
Description: {{ .Description }}
State: {{ .State }}
Size: {{ .Size }}
CreateTime: {{ .CreateTime }}
`
var volumeShowCmd = &cobra.Command{
Use: "volume ID",
Short: "Show volume information",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
volume, err := c.GetVolume(args[0])
if err != nil {
return errors.Wrap(err, "Error getting volume")
}
return render(cmd, volume)
},
Annotations: map[string]string{
"default_template": volumeShowTemplate,
"template_usage": tfortools.GenerateUsageUndecorated(types.Volume{}),
},
}
var workloadShowTemplate = `ID: {{ .ID }}
Description: {{ .Description }}
{{ if eq .VMType "qemu" -}}
FWType: {{ .FWType }}
{{ else -}}
ImageName: {{ .ImageName }}
{{ end -}}
Visibility: {{ .Visibility }}
Requirements:
MemMB: {{ .Requirements.MemMB }}
VCPUs: {{ .Requirements.VCPUs }}
NodeID: {{ .Requirements.NodeID }}
Hostname {{ .Requirements.Hostname }}
NetworkNode {{ .Requirements.NetworkNode }}
Privileged {{ .Requirements.Privileged }}
Storage:
{{- range .Storage }}
ID: {{ .ID }}
Size: {{ .Size }}
Ephemeral: {{ .Ephemeral }}
Bootable: {{ .Bootable }}
SourceType: {{ .SourceType }}
Source: {{ .Source }}
{{ end }}`
var workloadShowCmd = &cobra.Command{
Use: "workload ID",
Short: "Show workload information",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
workload, err := c.GetWorkload(args[0])
if err != nil {
return errors.Wrap(err, "Error getting workload")
}
return render(cmd, workload)
},
Annotations: map[string]string{
"default_template": workloadShowTemplate,
"template_usage": tfortools.GenerateUsageUndecorated(types.Workload{}),
},
}
var showCmds = []*cobra.Command{
cnciShowCmd,
imageShowCmd,
instanceShowCmd,
nodeShowCmd,
tenantShowCmd,
traceShowCmd,
volumeShowCmd,
workloadShowCmd,
}
func init() {
for _, cmd := range showCmds {
showCmd.AddCommand(cmd)
}
rootCmd.AddCommand(showCmd)
}