Skip to content

Commit beb5a11

Browse files
authored
Merge pull request #3357 from cspngo/issue_3220
add -json to snapshot.tree
2 parents 5d7849f + b948e3c commit beb5a11

1 file changed

Lines changed: 77 additions & 4 deletions

File tree

govc/vm/snapshot/tree.go

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package snapshot
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"flag"
2223
"fmt"
2324
"path"
2425
"strings"
26+
"time"
2527

2628
"github.com/vmware/govmomi/govc/cli"
2729
"github.com/vmware/govmomi/govc/flags"
@@ -83,12 +85,80 @@ func (cmd *tree) Process(ctx context.Context) error {
8385
return nil
8486
}
8587

88+
type SnapshotRecord struct {
89+
CreateTime *time.Time `json:"createTime,omitempty"`
90+
Id *string `json:"id,omitempty"`
91+
Size *int `json:"size,omitempty"`
92+
Name string `json:"name"`
93+
Description *string `json:"description,omitempty"`
94+
IsCurrent bool `json:"current"`
95+
ChildSnapshotList []SnapshotRecord `json:"childSnapshotList"`
96+
}
97+
98+
func (cmd *tree) IsCurrent(vm mo.VirtualMachine, moref types.ManagedObjectReference) bool {
99+
return vm.Snapshot.CurrentSnapshot.Value == moref.Value
100+
}
101+
102+
func (cmd *tree) CreateTime(snapshot types.VirtualMachineSnapshotTree) *time.Time {
103+
if cmd.date {
104+
return &snapshot.CreateTime
105+
}
106+
return nil
107+
108+
}
109+
110+
func (cmd *tree) SnapshotId(snapshot types.VirtualMachineSnapshotTree) *string {
111+
if cmd.id {
112+
return &snapshot.Snapshot.Value
113+
}
114+
return nil
115+
}
116+
117+
func (cmd *tree) SnapshotDescription(snapshot types.VirtualMachineSnapshotTree) *string {
118+
if cmd.description {
119+
return &snapshot.Description
120+
}
121+
return nil
122+
}
123+
124+
func (cmd *tree) SnapshotSize(vm mo.VirtualMachine, snapshot types.ManagedObjectReference, parent *types.ManagedObjectReference) *int {
125+
if cmd.size {
126+
size := object.SnapshotSize(snapshot, parent, vm.LayoutEx, cmd.IsCurrent(vm, snapshot))
127+
return &size
128+
}
129+
return nil
130+
}
131+
132+
func (cmd *tree) makeSnapshotRecord(vm mo.VirtualMachine, node types.VirtualMachineSnapshotTree, parent *types.ManagedObjectReference) SnapshotRecord {
133+
134+
var SnapshotRecords []SnapshotRecord
135+
for _, snapshot := range node.ChildSnapshotList {
136+
SnapshotRecords = append(SnapshotRecords, cmd.makeSnapshotRecord(vm, snapshot, &node.Snapshot))
137+
}
138+
return SnapshotRecord{Name: node.Name,
139+
Id: cmd.SnapshotId(node),
140+
CreateTime: cmd.CreateTime(node),
141+
Description: cmd.SnapshotDescription(node),
142+
ChildSnapshotList: SnapshotRecords,
143+
Size: cmd.SnapshotSize(vm, node.Snapshot, parent),
144+
IsCurrent: cmd.IsCurrent(vm, node.Snapshot)}
145+
146+
}
147+
148+
func (cmd *tree) writeJson(vm mo.VirtualMachine) {
149+
var SnapshotRecords []SnapshotRecord
150+
for _, rootSnapshot := range vm.Snapshot.RootSnapshotList {
151+
SnapshotRecords = append(SnapshotRecords, cmd.makeSnapshotRecord(vm, rootSnapshot, nil))
152+
}
153+
b, _ := json.MarshalIndent(SnapshotRecords, "", " ")
154+
println(string(b))
155+
156+
}
86157
func (cmd *tree) write(level int, parent string, pref *types.ManagedObjectReference, st []types.VirtualMachineSnapshotTree) {
87158
for _, s := range st {
88159
s := s // avoid implicit memory aliasing
89160

90161
sname := s.Name
91-
92162
if cmd.fullPath && parent != "" {
93163
sname = path.Join(parent, sname)
94164
}
@@ -126,7 +196,7 @@ func (cmd *tree) write(level int, parent string, pref *types.ManagedObjectRefere
126196
}
127197

128198
if cmd.date {
129-
attr = append(attr, s.CreateTime.Format("Jan 2 15:04"))
199+
attr = append(attr, s.CreateTime.Format("2006-01-02T15:04:05Z07:00"))
130200
}
131201

132202
if len(attr) > 0 {
@@ -178,8 +248,11 @@ func (cmd *tree) Run(ctx context.Context, f *flag.FlagSet) error {
178248

179249
cmd.info = o.Snapshot
180250
cmd.layout = o.LayoutEx
181-
182-
cmd.write(0, "", nil, o.Snapshot.RootSnapshotList)
251+
if cmd.JSON {
252+
cmd.writeJson(o)
253+
} else {
254+
cmd.write(0, "", nil, o.Snapshot.RootSnapshotList)
255+
}
183256

184257
return nil
185258
}

0 commit comments

Comments
 (0)