Debian Codegraph @

govc/sso/service/ls.go

raw
1 /*
2 Copyright (c) 2018 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 service
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/lookup"
29 "github.com/vmware/govmomi/lookup/types"
30 )
31
32 type ls struct {
33 *flags.ClientFlag
34 *flags.OutputFlag
35
36 long bool
37 url bool
38
39 types.LookupServiceRegistrationFilter
40 }
41
42 func init() {
43 cli.Register("sso.service.ls", &ls{})
44 }
45
46 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
47 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
48 cmd.ClientFlag.Register(ctx, f)
49
50 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
51 cmd.OutputFlag.Register(ctx, f)
52
53 f.BoolVar(&cmd.long, "l", false, "Long listing format")
54 f.BoolVar(&cmd.url, "U", false, "List endpoint URL(s) only")
55
56 cmd.LookupServiceRegistrationFilter.EndpointType = new(types.LookupServiceRegistrationEndpointType)
57 cmd.LookupServiceRegistrationFilter.ServiceType = new(types.LookupServiceRegistrationServiceType)
58 f.StringVar(&cmd.SiteId, "s", "", "Site ID")
59 f.StringVar(&cmd.NodeId, "n", "", "Node ID")
60 f.StringVar(&cmd.ServiceType.Type, "t", "", "Service type")
61 f.StringVar(&cmd.ServiceType.Product, "p", "", "Service product")
62 f.StringVar(&cmd.EndpointType.Type, "T", "", "Endpoint type")
63 f.StringVar(&cmd.EndpointType.Protocol, "P", "", "Endpoint protocol")
64 }
65
66 func (cmd *ls) Description() string {
67 return `List platform services.
68
69 Examples:
70 govc sso.service.ls
71 govc sso.service.ls -t vcenterserver -P vmomi
72 govc sso.service.ls -t cs.identity
73 govc sso.service.ls -t cs.identity -P wsTrust -U
74 govc sso.service.ls -t cs.identity -json | jq -r .[].ServiceEndpoints[].Url`
75 }
76
77 func (cmd *ls) Process(ctx context.Context) error {
78 if err := cmd.ClientFlag.Process(ctx); err != nil {
79 return err
80 }
81 return cmd.OutputFlag.Process(ctx)
82 }
83
84 type infoResult []types.LookupServiceRegistrationInfo
85
86 func (r infoResult) Dump() interface{} {
87 return []types.LookupServiceRegistrationInfo(r)
88 }
89
90 func (r infoResult) Write(w io.Writer) error {
91 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
92
93 for _, info := range r {
94 fmt.Fprintf(tw, "%s\t%s\t%s\n", info.ServiceType.Product, info.ServiceType.Type, info.ServiceId)
95 }
96
97 return tw.Flush()
98 }
99
100 type infoResultLong []types.LookupServiceRegistrationInfo
101
102 func (r infoResultLong) Dump() interface{} {
103 return []types.LookupServiceRegistrationInfo(r)
104 }
105
106 func (r infoResultLong) Write(w io.Writer) error {
107 tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
108
109 for _, info := range r {
110 for _, s := range info.ServiceEndpoints {
111 fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n",
112 info.ServiceType.Product, info.ServiceType.Type, info.ServiceId,
113 s.EndpointType.Protocol, s.EndpointType.Type, s.Url)
114 }
115 }
116
117 return tw.Flush()
118 }
119
120 type infoResultURL []types.LookupServiceRegistrationInfo
121
122 func (r infoResultURL) Dump() interface{} {
123 return []types.LookupServiceRegistrationInfo(r)
124 }
125
126 func (r infoResultURL) Write(w io.Writer) error {
127 for _, info := range r {
128 for _, s := range info.ServiceEndpoints {
129 fmt.Fprintln(w, s.Url)
130 }
131 }
132
133 return nil
134 }
135
136 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
137 vc, err := cmd.Client()
138 if err != nil {
139 return err
140 }
141
142 c, err := lookup.NewClient(ctx, vc)
143 if err != nil {
144 return err
145 }
146
147 info, err := c.List(ctx, &cmd.LookupServiceRegistrationFilter)
148 if err != nil {
149 return err
150 }
151
152 switch {
153 case cmd.long:
154 return cmd.WriteResult(infoResultLong(info))
155 case cmd.url:
156 return cmd.WriteResult(infoResultURL(info))
157 default:
158 return cmd.WriteResult(infoResult(info))
159 }
160 }