github.com/vmware/govmomi@v0.43.0/govc/library/policy/ls.go (about) 1 /* 2 Copyright (c) 2022-2022 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 */ 16 17 package policy 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "text/tabwriter" 25 26 "github.com/vmware/govmomi/govc/cli" 27 "github.com/vmware/govmomi/govc/flags" 28 "github.com/vmware/govmomi/vapi/library" 29 ) 30 31 type ls struct { 32 *flags.ClientFlag 33 *flags.OutputFlag 34 } 35 36 func init() { 37 cli.Register("library.policy.ls", &ls{}) 38 } 39 40 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 41 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 42 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 43 cmd.ClientFlag.Register(ctx, f) 44 cmd.OutputFlag.Register(ctx, f) 45 } 46 47 func (cmd *ls) Process(ctx context.Context) error { 48 if err := cmd.ClientFlag.Process(ctx); err != nil { 49 return err 50 } 51 return nil 52 } 53 54 func (cmd *ls) Description() string { 55 return `List security policies for content libraries. 56 57 Examples: 58 govc library.policy.ls 59 ` 60 } 61 62 type lsResultsWriter struct { 63 Policies []library.ContentSecurityPoliciesInfo `json:"policies,omitempty"` 64 } 65 66 func (r lsResultsWriter) Write(w io.Writer) error { 67 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) 68 69 if len(r.Policies) == 0 { 70 _, _ = fmt.Fprintln(tw, "No Policies found") 71 return tw.Flush() 72 } 73 74 for _, p := range r.Policies { 75 if _, err := fmt.Fprintf(tw, "Name:\t%s\n", p.Name); err != nil { 76 return err 77 } 78 if _, err := fmt.Fprintf(tw, "Policy ID:\t%s\n", p.Policy); err != nil { 79 return err 80 } 81 if err := writeItemRules(tw, p); err != nil { 82 return err 83 } 84 } 85 86 return tw.Flush() 87 } 88 89 func (r lsResultsWriter) Dump() interface{} { 90 return r.Policies 91 } 92 93 func writeItemRules(w io.Writer, policy library.ContentSecurityPoliciesInfo) error { 94 tw := tabwriter.NewWriter(w, 2, 0, 4, ' ', 0) 95 if _, err := fmt.Fprintln(w, "Rules:"); err != nil { 96 return err 97 } 98 for k, v := range policy.ItemTypeRules { 99 if _, err := fmt.Fprintf(tw, "\tItem: %s\n", k); err != nil { 100 return err 101 } 102 if _, err := fmt.Fprintf(tw, "\tRule: %s\n", v); err != nil { 103 return err 104 } 105 } 106 return tw.Flush() 107 } 108 109 func (cmd *ls) Run(ctx context.Context, _ *flag.FlagSet) error { 110 c, err := cmd.RestClient() 111 if err != nil { 112 return err 113 } 114 115 m := library.NewManager(c) 116 policies, err := m.ListSecurityPolicies(ctx) 117 if err != nil { 118 return err 119 } 120 return cmd.WriteResult(&lsResultsWriter{policies}) 121 }