| 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/tags" |
| 26 |
) |
| 27 |
|
| 28 |
type detach struct { |
| 29 |
*flags.DatacenterFlag |
| 30 |
cat string |
| 31 |
} |
| 32 |
|
| 33 |
func init() { |
| 34 |
cli.Register("tags.detach", &detach{}) |
| 35 |
} |
| 36 |
|
| 37 |
func (cmd *detach) Register(ctx context.Context, f *flag.FlagSet) { |
| 38 |
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx) |
| 39 |
cmd.DatacenterFlag.Register(ctx, f) |
| 40 |
|
| 41 |
f.StringVar(&cmd.cat, "c", "", "Tag category") |
| 42 |
} |
| 43 |
|
| 44 |
func (cmd *detach) Usage() string { |
| 45 |
return "NAME PATH" |
| 46 |
} |
| 47 |
|
| 48 |
func (cmd *detach) Description() string { |
| 49 |
return `Detach tag NAME from object PATH. |
| 50 |
|
| 51 |
Examples: |
| 52 |
govc tags.detach k8s-region-us /dc1 |
| 53 |
govc tags.detach -c k8s-region us-ca1 /dc1/host/cluster1` |
| 54 |
} |
| 55 |
|
| 56 |
func (cmd *detach) Run(ctx context.Context, f *flag.FlagSet) error { |
| 57 |
if f.NArg() != 2 { |
| 58 |
return flag.ErrHelp |
| 59 |
} |
| 60 |
|
| 61 |
tagID := f.Arg(0) |
| 62 |
managedObj := f.Arg(1) |
| 63 |
|
| 64 |
c, err := cmd.RestClient() |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
|
| 69 |
ref, err := convertPath(ctx, c, cmd.DatacenterFlag, managedObj) |
| 70 |
if err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
m := tags.NewManager(c) |
| 74 |
tag, err := m.GetTagForCategory(ctx, tagID, cmd.cat) |
| 75 |
if err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
return m.DetachTag(ctx, tag.ID, ref) |
| 79 |
} |