| 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 association |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"strings" |
| 25 |
"text/tabwriter" |
| 26 |
|
| 27 |
"github.com/vmware/govmomi/govc/cli" |
| 28 |
"github.com/vmware/govmomi/govc/flags" |
| 29 |
"github.com/vmware/govmomi/vapi/tags" |
| 30 |
"github.com/vmware/govmomi/vim25/mo" |
| 31 |
) |
| 32 |
|
| 33 |
type ls struct { |
| 34 |
*flags.DatacenterFlag |
| 35 |
r bool |
| 36 |
l bool |
| 37 |
} |
| 38 |
|
| 39 |
func init() { |
| 40 |
cli.Register("tags.attached.ls", &ls{}) |
| 41 |
} |
| 42 |
|
| 43 |
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { |
| 44 |
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx) |
| 45 |
cmd.DatacenterFlag.Register(ctx, f) |
| 46 |
f.BoolVar(&cmd.r, "r", false, "List tags attached to resource") |
| 47 |
f.BoolVar(&cmd.l, "l", false, "Long listing format") |
| 48 |
} |
| 49 |
|
| 50 |
func (cmd *ls) Usage() string { |
| 51 |
return "NAME" |
| 52 |
} |
| 53 |
|
| 54 |
func (cmd *ls) Description() string { |
| 55 |
return `List attached tags or objects. |
| 56 |
|
| 57 |
Examples: |
| 58 |
govc tags.attached.ls k8s-region-us |
| 59 |
govc tags.attached.ls -json k8s-zone-us-ca1 | jq . |
| 60 |
govc tags.attached.ls -r /dc1/host/cluster1 |
| 61 |
govc tags.attached.ls -json -r /dc1 | jq .` |
| 62 |
} |
| 63 |
|
| 64 |
type lsResult []string |
| 65 |
|
| 66 |
func (r lsResult) Write(w io.Writer) error { |
| 67 |
for i := range r { |
| 68 |
fmt.Fprintln(w, r[i]) |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
type lsTagResult []tags.AttachedTags |
| 74 |
|
| 75 |
func (r lsTagResult) Write(w io.Writer) error { |
| 76 |
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 77 |
for _, item := range r { |
| 78 |
var ids []string |
| 79 |
for _, id := range item.Tags { |
| 80 |
ids = append(ids, id.Name) |
| 81 |
} |
| 82 |
fmt.Fprintf(tw, "%s:\t%s\n", item.ObjectID.Reference(), strings.Join(ids, ",")) |
| 83 |
} |
| 84 |
return tw.Flush() |
| 85 |
} |
| 86 |
|
| 87 |
type lsObjectResult []tags.AttachedObjects |
| 88 |
|
| 89 |
func (r lsObjectResult) Write(w io.Writer) error { |
| 90 |
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 91 |
for _, item := range r { |
| 92 |
for _, id := range item.ObjectIDs { |
| 93 |
var ids []string |
| 94 |
ids = append(ids, id.Reference().String()) |
| 95 |
fmt.Fprintf(tw, "%s:\t%s\n", item.Tag.Name, strings.Join(ids, ",")) |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
return tw.Flush() |
| 100 |
} |
| 101 |
|
| 102 |
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { |
| 103 |
c, err := cmd.RestClient() |
| 104 |
if err != nil { |
| 105 |
return err |
| 106 |
} |
| 107 |
|
| 108 |
var res flags.OutputWriter |
| 109 |
m := tags.NewManager(c) |
| 110 |
|
| 111 |
if cmd.r { |
| 112 |
var refs []mo.Reference |
| 113 |
for _, arg := range f.Args() { |
| 114 |
ref, err := convertPath(ctx, c, cmd.DatacenterFlag, arg) |
| 115 |
if err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
refs = append(refs, ref) |
| 119 |
} |
| 120 |
attached, err := m.GetAttachedTagsOnObjects(ctx, refs) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
if cmd.l { |
| 125 |
res = lsTagResult(attached) |
| 126 |
} else { |
| 127 |
var r lsResult |
| 128 |
for i := range attached { |
| 129 |
for _, tag := range attached[i].Tags { |
| 130 |
r = append(r, tag.Name) |
| 131 |
} |
| 132 |
} |
| 133 |
res = r |
| 134 |
} |
| 135 |
} else { |
| 136 |
attached, err := m.GetAttachedObjectsOnTags(ctx, f.Args()) |
| 137 |
if err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
if cmd.l { |
| 141 |
res = lsObjectResult(attached) |
| 142 |
} else { |
| 143 |
var r lsResult |
| 144 |
for _, obj := range attached { |
| 145 |
for _, ref := range obj.ObjectIDs { |
| 146 |
r = append(r, ref.Reference().String()) |
| 147 |
} |
| 148 |
} |
| 149 |
res = r |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return cmd.WriteResult(res) |
| 154 |
} |