1
- /*
2
- Copyright (c) 2020-2023 VMware, Inc. All Rights Reserved.
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
1
+ // © Broadcom. All Rights Reserved.
2
+ // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3
+ // SPDX-License-Identifier: Apache-2.0
16
4
17
5
package volume
18
6
@@ -35,6 +23,7 @@ import (
35
23
type ls struct {
36
24
* flags.ClientFlag
37
25
* flags.DatastoreFlag
26
+ * flags.StorageProfileFlag
38
27
* flags.OutputFlag
39
28
40
29
types.CnsQueryFilter
@@ -45,6 +34,21 @@ type ls struct {
45
34
back bool
46
35
}
47
36
37
+ type keyValue []vim.KeyValue
38
+
39
+ func (e * keyValue ) String () string {
40
+ return fmt .Sprintf ("%v" , * e )
41
+ }
42
+
43
+ func (e * keyValue ) Set (v string ) error {
44
+ r := strings .SplitN (v , "=" , 2 )
45
+ if len (r ) < 2 {
46
+ return fmt .Errorf ("failed to parse: %s" , v )
47
+ }
48
+ * e = append (* e , vim.KeyValue {Key : r [0 ], Value : r [1 ]})
49
+ return nil
50
+ }
51
+
48
52
func init () {
49
53
cli .Register ("volume.ls" , & ls {})
50
54
}
@@ -56,13 +60,21 @@ func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
56
60
cmd .DatastoreFlag , ctx = flags .NewDatastoreFlag (ctx )
57
61
cmd .DatastoreFlag .Register (ctx , f )
58
62
63
+ cmd .StorageProfileFlag , ctx = flags .NewStorageProfileFlag (ctx )
64
+ cmd .StorageProfileFlag .Register (ctx , f )
65
+
59
66
cmd .OutputFlag , ctx = flags .NewOutputFlag (ctx )
60
67
cmd .OutputFlag .Register (ctx , f )
61
68
62
69
f .BoolVar (& cmd .long , "l" , false , "Long listing format" )
63
70
f .BoolVar (& cmd .id , "i" , false , "List volume ID only" )
64
71
f .BoolVar (& cmd .disk , "L" , false , "List volume disk or file backing ID only" )
65
72
f .BoolVar (& cmd .back , "b" , false , "List file backing path" )
73
+
74
+ f .Var ((* flags .StringList )(& cmd .Names ), "n" , "List volumes with names" )
75
+ f .Var ((* keyValue )(& cmd .Labels ), "label" , "List volumes with labels" )
76
+ f .StringVar (& cmd .HealthStatus , "H" , "" , "List volumes with health status" )
77
+ f .Var ((* flags .StringList )(& cmd .ContainerClusterIds ), "c" , "List volumes in clusters" )
66
78
}
67
79
68
80
func (cmd * ls ) Process (ctx context.Context ) error {
@@ -72,6 +84,9 @@ func (cmd *ls) Process(ctx context.Context) error {
72
84
if err := cmd .DatastoreFlag .Process (ctx ); err != nil {
73
85
return err
74
86
}
87
+ if err := cmd .StorageProfileFlag .Process (ctx ); err != nil {
88
+ return err
89
+ }
75
90
return cmd .OutputFlag .Process (ctx )
76
91
}
77
92
@@ -123,6 +138,8 @@ func (r *lsWriter) Write(w io.Writer) error {
123
138
id = backing .BackingFileId
124
139
case * types.CnsVsanFileShareBackingDetails :
125
140
id = backing .Name
141
+ case * types.CnsBackingObjectDetails :
142
+ id = volume .VolumeId .Id
126
143
default :
127
144
log .Printf ("%s unknown backing type: %T" , volume .VolumeId .Id , backing )
128
145
}
@@ -135,16 +152,16 @@ func (r *lsWriter) Write(w io.Writer) error {
135
152
tw := tabwriter .NewWriter (r .cmd .Out , 2 , 0 , 2 , ' ' , 0 )
136
153
137
154
for _ , volume := range r .Volume {
138
- fmt .Printf ( "%s\t %s" , volume .VolumeId .Id , volume .Name )
155
+ fmt .Fprintf ( tw , "%s\t %s" , volume .VolumeId .Id , volume .Name )
139
156
if r .cmd .back {
140
- fmt .Printf ( "\t %s" , r .backing (volume .VolumeId ))
157
+ fmt .Fprintf ( tw , "\t %s" , r .backing (volume .VolumeId ))
141
158
}
142
159
if r .cmd .long {
143
160
capacity := volume .BackingObjectDetails .GetCnsBackingObjectDetails ().CapacityInMb
144
161
c := volume .Metadata .ContainerCluster
145
- fmt .Printf ( "\t %s\t %s\t %s" , units .ByteSize (capacity * 1024 * 1024 ), c .ClusterType , c .ClusterId )
162
+ fmt .Fprintf ( tw , "\t %s\t %s\t %s" , units .ByteSize (capacity * 1024 * 1024 ), c .ClusterType , c .ClusterId )
146
163
}
147
- fmt .Println ( )
164
+ fmt .Fprintln ( tw )
148
165
}
149
166
150
167
return tw .Flush ()
@@ -188,6 +205,11 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
188
205
cmd .Datastores = []vim.ManagedObjectReference {ds .Reference ()}
189
206
}
190
207
208
+ cmd .StoragePolicyId , err = cmd .StorageProfile (ctx )
209
+ if err != nil {
210
+ return err
211
+ }
212
+
191
213
c , err := cmd .CnsClient ()
192
214
if err != nil {
193
215
return err
0 commit comments