| 1 |
/* |
| 2 |
Copyright (c) 2020 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 cluster |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"os" |
| 26 |
"text/tabwriter" |
| 27 |
|
| 28 |
"github.com/vmware/govmomi/find" |
| 29 |
"github.com/vmware/govmomi/govc/cli" |
| 30 |
"github.com/vmware/govmomi/govc/flags" |
| 31 |
"github.com/vmware/govmomi/object" |
| 32 |
"github.com/vmware/govmomi/vapi/namespace" |
| 33 |
) |
| 34 |
|
| 35 |
type ls struct { |
| 36 |
*flags.ClientFlag |
| 37 |
*flags.OutputFlag |
| 38 |
|
| 39 |
long bool |
| 40 |
} |
| 41 |
|
| 42 |
func init() { |
| 43 |
cli.Register("namespace.cluster.ls", &ls{}) |
| 44 |
} |
| 45 |
|
| 46 |
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { |
| 47 |
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 48 |
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 49 |
cmd.ClientFlag.Register(ctx, f) |
| 50 |
cmd.OutputFlag.Register(ctx, f) |
| 51 |
|
| 52 |
f.BoolVar(&cmd.long, "l", false, "Long listing format") |
| 53 |
} |
| 54 |
|
| 55 |
func (cmd *ls) Process(ctx context.Context) error { |
| 56 |
if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
return cmd.OutputFlag.Process(ctx) |
| 60 |
} |
| 61 |
|
| 62 |
func (cmd *ls) Description() string { |
| 63 |
return `List namepace enabled clusters. |
| 64 |
|
| 65 |
Examples: |
| 66 |
govc namespace.cluster.ls |
| 67 |
govc namespace.cluster.ls -l |
| 68 |
govc namespace.cluster.ls -json | jq .` |
| 69 |
} |
| 70 |
|
| 71 |
type lsWriter struct { |
| 72 |
cmd *ls |
| 73 |
Cluster []namespace.ClusterSummary |
| 74 |
} |
| 75 |
|
| 76 |
func (r *lsWriter) Write(w io.Writer) error { |
| 77 |
tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) |
| 78 |
|
| 79 |
for _, cluster := range r.Cluster { |
| 80 |
path, err := r.path(cluster) |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
fmt.Fprintf(tw, "%s", path) |
| 85 |
if r.cmd.long { |
| 86 |
fmt.Fprintf(tw, "\t%s", cluster.ConfigStatus) |
| 87 |
fmt.Fprintf(tw, "\t%s", cluster.KubernetesStatus) |
| 88 |
} |
| 89 |
fmt.Fprintf(tw, "\n") |
| 90 |
} |
| 91 |
return tw.Flush() |
| 92 |
} |
| 93 |
|
| 94 |
func (r *lsWriter) path(cluster namespace.ClusterSummary) (string, error) { |
| 95 |
ref := cluster.Reference() |
| 96 |
|
| 97 |
c, err := r.cmd.Client() |
| 98 |
if err != nil { |
| 99 |
return "", err |
| 100 |
} |
| 101 |
|
| 102 |
finder := find.NewFinder(c, false) |
| 103 |
obj, err := finder.ObjectReference(context.Background(), ref) |
| 104 |
if err != nil { |
| 105 |
return "", err |
| 106 |
} |
| 107 |
|
| 108 |
return obj.(*object.ClusterComputeResource).InventoryPath, nil |
| 109 |
} |
| 110 |
|
| 111 |
func (r *lsWriter) MarshalJSON() ([]byte, error) { |
| 112 |
return json.Marshal(r.Cluster) |
| 113 |
} |
| 114 |
|
| 115 |
func (r *lsWriter) Dump() interface{} { |
| 116 |
return r.Cluster |
| 117 |
} |
| 118 |
|
| 119 |
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { |
| 120 |
c, err := cmd.RestClient() |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
m := namespace.NewManager(c) |
| 126 |
clusters, err := m.ListClusters(ctx) |
| 127 |
if err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
|
| 131 |
return cmd.WriteResult(&lsWriter{cmd, clusters}) |
| 132 |
} |