github.com/vmware/govmomi@v0.37.1/govc/storage/policy/create.go (about)

     1  /*
     2  Copyright (c) 2020 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 policy
    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/pbm/types"
    27  	vim "github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  type create struct {
    31  	*flags.ClientFlag
    32  
    33  	spec types.PbmCapabilityProfileCreateSpec
    34  	tag  string
    35  	cat  string
    36  }
    37  
    38  func init() {
    39  	cli.Register("storage.policy.create", &create{})
    40  }
    41  
    42  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    43  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    44  	cmd.ClientFlag.Register(ctx, f)
    45  
    46  	f.StringVar(&cmd.spec.Description, "d", "", "Description")
    47  	f.StringVar(&cmd.tag, "tag", "", "Tag")
    48  	f.StringVar(&cmd.cat, "category", "", "Category")
    49  }
    50  
    51  func (cmd *create) Usage() string {
    52  	return "NAME"
    53  }
    54  
    55  func (cmd *create) Description() string {
    56  	return `Create VM Storage Policy.
    57  
    58  Examples:
    59    govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement`
    60  }
    61  
    62  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    63  	if f.NArg() != 1 {
    64  		return flag.ErrHelp
    65  	}
    66  
    67  	cmd.spec.Name = f.Arg(0)
    68  	cmd.spec.ResourceType.ResourceType = string(types.PbmProfileResourceTypeEnumSTORAGE)
    69  
    70  	if cmd.tag == "" {
    71  		return flag.ErrHelp
    72  	}
    73  
    74  	id := fmt.Sprintf("com.vmware.storage.tag.%s.property", cmd.cat)
    75  	instance := types.PbmCapabilityInstance{
    76  		Id: types.PbmCapabilityMetadataUniqueId{
    77  			Namespace: "http://www.vmware.com/storage/tag",
    78  			Id:        cmd.cat,
    79  		},
    80  		Constraint: []types.PbmCapabilityConstraintInstance{{
    81  			PropertyInstance: []types.PbmCapabilityPropertyInstance{{
    82  				Id: id,
    83  				Value: types.PbmCapabilityDiscreteSet{
    84  					Values: []vim.AnyType{cmd.tag},
    85  				},
    86  			}},
    87  		}},
    88  	}
    89  
    90  	cmd.spec.Constraints = &types.PbmCapabilitySubProfileConstraints{
    91  		SubProfiles: []types.PbmCapabilitySubProfile{{
    92  			Name:       "Tag based placement",
    93  			Capability: []types.PbmCapabilityInstance{instance},
    94  		}},
    95  	}
    96  
    97  	c, err := cmd.PbmClient()
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	pid, err := c.CreateProfile(ctx, cmd.spec)
   103  	if err != nil {
   104  		return err
   105  	}
   106  
   107  	fmt.Println(pid.UniqueId)
   108  	return nil
   109  }