github.com/vmware/govmomi@v0.51.0/cli/storage/policy/create.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package policy
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/pbm"
    15  	"github.com/vmware/govmomi/pbm/types"
    16  )
    17  
    18  type create struct {
    19  	*flags.ClientFlag
    20  
    21  	spec pbm.CapabilityProfileCreateSpec
    22  	tag  string
    23  	cat  string
    24  	zone bool
    25  	enc  bool
    26  }
    27  
    28  func init() {
    29  	cli.Register("storage.policy.create", &create{})
    30  }
    31  
    32  func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
    33  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    34  	cmd.ClientFlag.Register(ctx, f)
    35  
    36  	f.StringVar(&cmd.spec.Description, "d", "", "Description")
    37  	f.StringVar(&cmd.tag, "tag", "", "Tag")
    38  	f.StringVar(&cmd.cat, "category", "", "Category")
    39  	f.BoolVar(&cmd.enc, "e", false, "Enable encryption")
    40  	f.BoolVar(&cmd.zone, "z", false, "Enable Zonal topology for multi-zone Supervisor")
    41  }
    42  
    43  func (cmd *create) Usage() string {
    44  	return "NAME"
    45  }
    46  
    47  func (cmd *create) Description() string {
    48  	return `Create VM Storage Policy.
    49  
    50  Examples:
    51    govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement
    52    govc storage.policy.create -z MyZonalPolicy # Zonal topology`
    53  }
    54  
    55  func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
    56  	if f.NArg() != 1 {
    57  		return flag.ErrHelp
    58  	}
    59  
    60  	cmd.spec.Name = f.Arg(0)
    61  	cmd.spec.Category = string(types.PbmProfileCategoryEnumREQUIREMENT)
    62  
    63  	if cmd.tag == "" && !cmd.zone && !cmd.enc {
    64  		return flag.ErrHelp
    65  	}
    66  
    67  	if cmd.tag != "" {
    68  		cmd.spec.CapabilityList = append(cmd.spec.CapabilityList, pbm.Capability{
    69  			ID:        cmd.cat,
    70  			Namespace: "http://www.vmware.com/storage/tag",
    71  			PropertyList: []pbm.Property{{
    72  				ID:       fmt.Sprintf("com.vmware.storage.tag.%s.property", cmd.cat),
    73  				Value:    cmd.tag,
    74  				DataType: "set",
    75  			}},
    76  		})
    77  	}
    78  
    79  	if cmd.zone {
    80  		cmd.spec.CapabilityList = append(cmd.spec.CapabilityList, pbm.Capability{
    81  			ID:        "StorageTopology",
    82  			Namespace: "com.vmware.storage.consumptiondomain",
    83  			PropertyList: []pbm.Property{{
    84  				ID:       "StorageTopologyType",
    85  				Value:    "Zonal",
    86  				DataType: "string",
    87  			}},
    88  		})
    89  	}
    90  
    91  	if cmd.enc {
    92  		const encryptionCapabilityID = "ad5a249d-cbc2-43af-9366-694d7664fa52"
    93  
    94  		cmd.spec.CapabilityList = append(cmd.spec.CapabilityList, pbm.Capability{
    95  			ID:        encryptionCapabilityID,
    96  			Namespace: "com.vmware.storageprofile.dataservice",
    97  			PropertyList: []pbm.Property{{
    98  				ID:       encryptionCapabilityID,
    99  				Value:    encryptionCapabilityID,
   100  				DataType: "string",
   101  			}},
   102  		})
   103  	}
   104  
   105  	c, err := cmd.PbmClient()
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	spec, err := pbm.CreateCapabilityProfileSpec(cmd.spec)
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	pid, err := c.CreateProfile(ctx, *spec)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	fmt.Println(pid.UniqueId)
   121  	return nil
   122  }