github.com/vmware/govmomi@v0.37.2/govc/disk/tags.go (about)

     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 disk
    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/vim25/types"
    26  	"github.com/vmware/govmomi/vslm"
    27  )
    28  
    29  type tags struct {
    30  	*flags.ClientFlag
    31  	types.VslmTagEntry
    32  	attach bool
    33  }
    34  
    35  func init() {
    36  	cli.Register("disk.tags.attach", &tags{attach: true})
    37  	cli.Register("disk.tags.detach", &tags{attach: false})
    38  }
    39  
    40  func (cmd *tags) Register(ctx context.Context, f *flag.FlagSet) {
    41  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    42  	cmd.ClientFlag.Register(ctx, f)
    43  
    44  	f.StringVar(&cmd.ParentCategoryName, "c", "", "Tag category")
    45  }
    46  
    47  func (cmd *tags) Usage() string {
    48  	return "NAME ID"
    49  }
    50  
    51  func (cmd *tags) name() string {
    52  	if cmd.attach {
    53  		return "attach"
    54  	}
    55  	return "detach"
    56  }
    57  
    58  func (cmd *tags) Description() string {
    59  	if cmd.attach {
    60  		return `Attach tag NAME to disk ID.
    61  
    62  Examples:
    63    govc disk.tags.attach -c k8s-region k8s-region-us $id`
    64  	}
    65  
    66  	return `Detach tag NAME from disk ID.
    67  
    68  Examples:
    69    govc disk.tags.detach -c k8s-region k8s-region-us $id`
    70  }
    71  
    72  func (cmd *tags) Run(ctx context.Context, f *flag.FlagSet) error {
    73  	c, err := cmd.Client()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	m := vslm.NewObjectManager(c)
    79  	cmd.TagName = f.Arg(0)
    80  	method := m.DetachTag
    81  	if cmd.attach {
    82  		method = m.AttachTag
    83  	}
    84  	return method(ctx, f.Arg(1), cmd.VslmTagEntry)
    85  }