Skip to content

Commit 088003e

Browse files
committed
govc: Add vm.policy.ls command
See discussion #3727 Signed-off-by: Doug MacEachern <[email protected]>
1 parent 053de4a commit 088003e

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

cli/vm/policy/ls.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package policy
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"io"
12+
"text/tabwriter"
13+
"time"
14+
15+
"github.com/vmware/govmomi/cli"
16+
"github.com/vmware/govmomi/cli/flags"
17+
"github.com/vmware/govmomi/pbm/types"
18+
vim "github.com/vmware/govmomi/vim25/types"
19+
)
20+
21+
type ls struct {
22+
*flags.VirtualMachineFlag
23+
}
24+
25+
func init() {
26+
cli.Register("vm.policy.ls", &ls{})
27+
}
28+
29+
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
30+
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
31+
cmd.VirtualMachineFlag.Register(ctx, f)
32+
}
33+
34+
func (cmd *ls) Description() string {
35+
return `List VM storage policies.
36+
37+
Examples:
38+
govc vm.policy.ls -vm $name
39+
govc vm.policy.ls -vm $name -json`
40+
}
41+
42+
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
43+
vm, err := cmd.VirtualMachine()
44+
if err != nil {
45+
return err
46+
}
47+
48+
if vm == nil {
49+
return flag.ErrHelp
50+
}
51+
52+
c, err := cmd.PbmClient()
53+
if err != nil {
54+
return err
55+
}
56+
57+
devices, err := vm.Device(ctx)
58+
if err != nil {
59+
return err
60+
}
61+
62+
m, err := c.ProfileMap(ctx)
63+
if err != nil {
64+
return err
65+
}
66+
67+
ref := vm.Reference().Value
68+
69+
res := []Entity{{
70+
Name: "VM home",
71+
ID: types.PbmServerObjectRef{
72+
ObjectType: string(types.PbmObjectTypeVirtualMachine),
73+
Key: ref,
74+
},
75+
}}
76+
77+
for _, disk := range devices.SelectByType((*vim.VirtualDisk)(nil)) {
78+
key := disk.GetVirtualDevice().Key
79+
80+
entity := Entity{
81+
Name: devices.Name(disk),
82+
ID: types.PbmServerObjectRef{
83+
ObjectType: string(types.PbmObjectTypeVirtualDiskId),
84+
Key: fmt.Sprintf("%s:%d", ref, key),
85+
},
86+
}
87+
88+
res = append(res, entity)
89+
}
90+
91+
for i := range res {
92+
entity := &res[i]
93+
entity.PolicyName = "None"
94+
95+
ids, err := c.QueryAssociatedProfile(ctx, entity.ID)
96+
if err != nil {
97+
return err
98+
}
99+
100+
if len(ids) != 0 {
101+
entity.PolicyID = ids[0].UniqueId
102+
entity.PolicyName = m.Name[entity.PolicyID].GetPbmProfile().Name
103+
104+
res, err := c.FetchComplianceResult(ctx, []types.PbmServerObjectRef{entity.ID})
105+
if err != nil {
106+
return err
107+
}
108+
109+
entity.Compliance = &res[0]
110+
}
111+
}
112+
113+
return cmd.WriteResult(List(res))
114+
}
115+
116+
type Entity struct {
117+
Name string `json:"name"`
118+
ID types.PbmServerObjectRef `json:"id"`
119+
PolicyID string `json:"policyID"`
120+
PolicyName string `json:"policyName"`
121+
Compliance *types.PbmComplianceResult `json:"compliance"`
122+
}
123+
124+
type List []Entity
125+
126+
func (r List) Write(w io.Writer) error {
127+
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
128+
129+
for _, disk := range r {
130+
status := ""
131+
checked := ""
132+
133+
if c := disk.Compliance; c != nil {
134+
status = disk.Compliance.ComplianceStatus
135+
checked = c.CheckTime.Format(time.ANSIC)
136+
}
137+
138+
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", disk.Name, disk.PolicyName, status, checked)
139+
}
140+
141+
return tw.Flush()
142+
}

govc/USAGE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ but appear via `govc $cmd -h`:
421421
- [vm.network.change](#vmnetworkchange)
422422
- [vm.option.info](#vmoptioninfo)
423423
- [vm.option.ls](#vmoptionls)
424+
- [vm.policy.ls](#vmpolicyls)
424425
- [vm.power](#vmpower)
425426
- [vm.question](#vmquestion)
426427
- [vm.rdm.attach](#vmrdmattach)
@@ -7472,6 +7473,21 @@ Options:
74727473
-vm= Virtual machine [GOVC_VM]
74737474
```
74747475

7476+
## vm.policy.ls
7477+
7478+
```
7479+
Usage: govc vm.policy.ls [OPTIONS]
7480+
7481+
List VM storage policies.
7482+
7483+
Examples:
7484+
govc vm.policy.ls -vm $name
7485+
govc vm.policy.ls -vm $name -json
7486+
7487+
Options:
7488+
-vm= Virtual machine [GOVC_VM]
7489+
```
7490+
74757491
## vm.power
74767492

74777493
```

govc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import (
114114
_ "github.com/vmware/govmomi/cli/vm/guest"
115115
_ "github.com/vmware/govmomi/cli/vm/network"
116116
_ "github.com/vmware/govmomi/cli/vm/option"
117+
_ "github.com/vmware/govmomi/cli/vm/policy"
117118
_ "github.com/vmware/govmomi/cli/vm/rdm"
118119
_ "github.com/vmware/govmomi/cli/vm/snapshot"
119120
_ "github.com/vmware/govmomi/cli/vm/target"

govc/test/storage.bats

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ load test_helper
6363
run govc storage.policy.info MyCombinedPolicy
6464
assert_success
6565
}
66+
67+
@test "vm.policy.ls" {
68+
vcsim_env
69+
70+
run govc vm.policy.ls -vm DC0_H0_VM0
71+
assert_success
72+
73+
run govc vm.policy.ls -vm DC0_H0_VM0 -json
74+
assert_success
75+
}

0 commit comments

Comments
 (0)