Debian Codegraph @

govc/tags/association/attach.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 association
18
19 import (
20 "context"
21 "flag"
22
23 "github.com/vmware/govmomi/govc/cli"
24 "github.com/vmware/govmomi/govc/flags"
25 "github.com/vmware/govmomi/vapi/library"
26 "github.com/vmware/govmomi/vapi/library/finder"
27 "github.com/vmware/govmomi/vapi/rest"
28 "github.com/vmware/govmomi/vapi/tags"
29 "github.com/vmware/govmomi/vim25/types"
30 )
31
32 type attach struct {
33 *flags.DatacenterFlag
34 cat string
35 }
36
37 func init() {
38 cli.Register("tags.attach", &attach{})
39 }
40
41 func (cmd *attach) Register(ctx context.Context, f *flag.FlagSet) {
42 cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
43 cmd.DatacenterFlag.Register(ctx, f)
44
45 f.StringVar(&cmd.cat, "c", "", "Tag category")
46 }
47
48 func (cmd *attach) Usage() string {
49 return "NAME PATH"
50 }
51
52 func (cmd *attach) Description() string {
53 return `Attach tag NAME to object PATH.
54
55 Examples:
56 govc tags.attach k8s-region-us /dc1
57 govc tags.attach -c k8s-region us-ca1 /dc1/host/cluster1`
58 }
59
60 func convertPath(ctx context.Context, c *rest.Client, cmd *flags.DatacenterFlag, managedObj string) (*types.ManagedObjectReference, error) {
61 client, err := cmd.ClientFlag.Client()
62 if err != nil {
63 return nil, err
64 }
65
66 ref := client.ServiceContent.RootFolder
67
68 switch managedObj {
69 case "", "-":
70 default:
71 ref, err = cmd.ManagedObject(ctx, managedObj)
72 if err != nil {
73 m := library.NewManager(c)
74 res, _ := finder.NewFinder(m).Find(ctx, managedObj)
75 if len(res) != 1 {
76 return nil, err
77 }
78 switch t := res[0].GetResult().(type) {
79 case library.Library:
80 ref = types.ManagedObjectReference{Type: "com.vmware.content.Library", Value: t.ID}
81 case library.Item:
82 ref = types.ManagedObjectReference{Type: "com.vmware.content.library.Item", Value: t.ID}
83 default:
84 return nil, err
85 }
86 }
87 }
88 return &ref, nil
89 }
90
91 func (cmd *attach) Run(ctx context.Context, f *flag.FlagSet) error {
92 if f.NArg() != 2 {
93 return flag.ErrHelp
94 }
95
96 tagID := f.Arg(0)
97 managedObj := f.Arg(1)
98
99 c, err := cmd.RestClient()
100 if err != nil {
101 return err
102 }
103
104 ref, err := convertPath(ctx, c, cmd.DatacenterFlag, managedObj)
105 if err != nil {
106 return err
107 }
108 m := tags.NewManager(c)
109 tag, err := m.GetTagForCategory(ctx, tagID, cmd.cat)
110 if err != nil {
111 return err
112 }
113 return m.AttachTag(ctx, tag.ID, ref)
114 }