github.com/vmware/govmomi@v0.43.0/govc/storage/policy/create.go (about) 1 /* 2 Copyright (c) 2020-2024 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 zone bool 37 } 38 39 func init() { 40 cli.Register("storage.policy.create", &create{}) 41 } 42 43 func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) { 44 cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) 45 cmd.ClientFlag.Register(ctx, f) 46 47 f.StringVar(&cmd.spec.Description, "d", "", "Description") 48 f.StringVar(&cmd.tag, "tag", "", "Tag") 49 f.StringVar(&cmd.cat, "category", "", "Category") 50 f.BoolVar(&cmd.zone, "z", false, "Enable Zonal topology for multi-zone Supervisor") 51 } 52 53 func (cmd *create) Usage() string { 54 return "NAME" 55 } 56 57 func (cmd *create) Description() string { 58 return `Create VM Storage Policy. 59 60 Examples: 61 govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement 62 govc storage.policy.create -z MyZonalPolicy # Zonal topology` 63 } 64 65 func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error { 66 if f.NArg() != 1 { 67 return flag.ErrHelp 68 } 69 70 cmd.spec.Name = f.Arg(0) 71 cmd.spec.Category = string(types.PbmProfileCategoryEnumREQUIREMENT) 72 cmd.spec.ResourceType.ResourceType = string(types.PbmProfileResourceTypeEnumSTORAGE) 73 74 if cmd.tag == "" && !cmd.zone { 75 return flag.ErrHelp 76 } 77 78 var profiles []types.PbmCapabilitySubProfile 79 80 if cmd.tag != "" { 81 id := fmt.Sprintf("com.vmware.storage.tag.%s.property", cmd.cat) 82 instance := types.PbmCapabilityInstance{ 83 Id: types.PbmCapabilityMetadataUniqueId{ 84 Namespace: "http://www.vmware.com/storage/tag", 85 Id: cmd.cat, 86 }, 87 Constraint: []types.PbmCapabilityConstraintInstance{{ 88 PropertyInstance: []types.PbmCapabilityPropertyInstance{{ 89 Id: id, 90 Value: types.PbmCapabilityDiscreteSet{ 91 Values: []vim.AnyType{cmd.tag}, 92 }, 93 }}, 94 }}, 95 } 96 profiles = append(profiles, types.PbmCapabilitySubProfile{ 97 Name: "Tag based placement", 98 Capability: []types.PbmCapabilityInstance{instance}, 99 }) 100 } 101 102 if cmd.zone { 103 instance := types.PbmCapabilityInstance{ 104 Id: types.PbmCapabilityMetadataUniqueId{ 105 Namespace: "com.vmware.storage.consumptiondomain", 106 Id: "StorageTopology", 107 }, 108 Constraint: []types.PbmCapabilityConstraintInstance{ 109 { 110 PropertyInstance: []types.PbmCapabilityPropertyInstance{ 111 { 112 Id: "StorageTopologyType", 113 Operator: "", 114 Value: "Zonal", 115 }, 116 }, 117 }, 118 }, 119 } 120 profiles = append(profiles, types.PbmCapabilitySubProfile{ 121 Name: "Consumption domain", 122 Capability: []types.PbmCapabilityInstance{instance}, 123 }) 124 } 125 126 cmd.spec.Constraints = &types.PbmCapabilitySubProfileConstraints{ 127 SubProfiles: profiles, 128 } 129 130 c, err := cmd.PbmClient() 131 if err != nil { 132 return err 133 } 134 135 pid, err := c.CreateProfile(ctx, cmd.spec) 136 if err != nil { 137 return err 138 } 139 140 fmt.Println(pid.UniqueId) 141 return nil 142 }