github.com/vmware/govmomi@v0.37.2/govc/tags/create.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 tags
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"fmt"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/vapi/tags"
    27  )
    28  
    29  type create struct {
    30  	*flags.ClientFlag
    31  	tag tags.Tag
    32  }
    33  
    34  func init() {
    35  	cli.Register("tags.create", &create{})
    36  }
    37  
    38  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    39  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    40  	cmd.ClientFlag.Register(ctx, f)
    41  	f.StringVar(&cmd.tag.CategoryID, "c", "", "Category name")
    42  	f.StringVar(&cmd.tag.Description, "d", "", "Description of tag")
    43  }
    44  
    45  func (cmd *create) Process(ctx context.Context) error {
    46  	if err := cmd.ClientFlag.Process(ctx); err != nil {
    47  		return err
    48  	}
    49  	return nil
    50  }
    51  
    52  func (cmd *create) Usage() string {
    53  	return "NAME"
    54  }
    55  
    56  func (cmd *create) Description() string {
    57  	return `Create tag.
    58  
    59  The '-c' option to specify a tag category is required.
    60  This command will output the ID of the new tag.
    61  
    62  Examples:
    63    govc tags.create -d "Kubernetes Zone US CA1" -c k8s-zone k8s-zone-us-ca1`
    64  }
    65  
    66  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    67  	if f.NArg() != 1 || cmd.tag.CategoryID == "" {
    68  		return flag.ErrHelp
    69  	}
    70  
    71  	cmd.tag.Name = f.Arg(0)
    72  
    73  	c, err := cmd.RestClient()
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	id, err := tags.NewManager(c).CreateTag(ctx, &cmd.tag)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	fmt.Println(id)
    84  	return nil
    85  }