Skip to content

Commit

Permalink
Merge pull request vmware#3475 from dougm/zonal-policy
Browse files Browse the repository at this point in the history
govc: add storage.policy.create zonal option
  • Loading branch information
dougm authored Jun 21, 2024
2 parents 78199c0 + ae316d6 commit bd85d9c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 21 deletions.
2 changes: 2 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5478,11 +5478,13 @@ Create VM Storage Policy.
Examples:
govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement
govc storage.policy.create -z MyZonalPolicy # Zonal topology
Options:
-category= Category
-d= Description
-tag= Tag
-z=false Enable Zonal topology for multi-zone Supervisor
```

## storage.policy.info
Expand Down
75 changes: 54 additions & 21 deletions govc/storage/policy/create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2020 VMware, Inc. All Rights Reserved.
Copyright (c) 2020-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -33,6 +33,7 @@ type create struct {
spec types.PbmCapabilityProfileCreateSpec
tag string
cat string
zone bool
}

func init() {
Expand All @@ -46,6 +47,7 @@ func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
f.StringVar(&cmd.spec.Description, "d", "", "Description")
f.StringVar(&cmd.tag, "tag", "", "Tag")
f.StringVar(&cmd.cat, "category", "", "Category")
f.BoolVar(&cmd.zone, "z", false, "Enable Zonal topology for multi-zone Supervisor")
}

func (cmd *create) Usage() string {
Expand All @@ -56,7 +58,8 @@ func (cmd *create) Description() string {
return `Create VM Storage Policy.
Examples:
govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement`
govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy # Tag based placement
govc storage.policy.create -z MyZonalPolicy # Zonal topology`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
Expand All @@ -65,33 +68,63 @@ func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
}

cmd.spec.Name = f.Arg(0)
cmd.spec.Category = string(types.PbmProfileCategoryEnumREQUIREMENT)
cmd.spec.ResourceType.ResourceType = string(types.PbmProfileResourceTypeEnumSTORAGE)

if cmd.tag == "" {
if cmd.tag == "" && !cmd.zone {
return flag.ErrHelp
}

id := fmt.Sprintf("com.vmware.storage.tag.%s.property", cmd.cat)
instance := types.PbmCapabilityInstance{
Id: types.PbmCapabilityMetadataUniqueId{
Namespace: "http://www.vmware.com/storage/tag",
Id: cmd.cat,
},
Constraint: []types.PbmCapabilityConstraintInstance{{
PropertyInstance: []types.PbmCapabilityPropertyInstance{{
Id: id,
Value: types.PbmCapabilityDiscreteSet{
Values: []vim.AnyType{cmd.tag},
},
var profiles []types.PbmCapabilitySubProfile

if cmd.tag != "" {
id := fmt.Sprintf("com.vmware.storage.tag.%s.property", cmd.cat)
instance := types.PbmCapabilityInstance{
Id: types.PbmCapabilityMetadataUniqueId{
Namespace: "http://www.vmware.com/storage/tag",
Id: cmd.cat,
},
Constraint: []types.PbmCapabilityConstraintInstance{{
PropertyInstance: []types.PbmCapabilityPropertyInstance{{
Id: id,
Value: types.PbmCapabilityDiscreteSet{
Values: []vim.AnyType{cmd.tag},
},
}},
}},
}},
}
profiles = append(profiles, types.PbmCapabilitySubProfile{
Name: "Tag based placement",
Capability: []types.PbmCapabilityInstance{instance},
})
}

cmd.spec.Constraints = &types.PbmCapabilitySubProfileConstraints{
SubProfiles: []types.PbmCapabilitySubProfile{{
Name: "Tag based placement",
if cmd.zone {
instance := types.PbmCapabilityInstance{
Id: types.PbmCapabilityMetadataUniqueId{
Namespace: "com.vmware.storage.consumptiondomain",
Id: "StorageTopology",
},
Constraint: []types.PbmCapabilityConstraintInstance{
{
PropertyInstance: []types.PbmCapabilityPropertyInstance{
{
Id: "StorageTopologyType",
Operator: "",
Value: "Zonal",
},
},
},
},
}
profiles = append(profiles, types.PbmCapabilitySubProfile{
Name: "Consumption domain",
Capability: []types.PbmCapabilityInstance{instance},
}},
})
}

cmd.spec.Constraints = &types.PbmCapabilitySubProfileConstraints{
SubProfiles: profiles,
}

c, err := cmd.PbmClient()
Expand Down
25 changes: 25 additions & 0 deletions govc/test/storage.bats
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,28 @@ load test_helper
run govc storage.policy.info "vSAN Default Storage Policy"
assert_success
}

@test "storage.policy.create" {
vcsim_env

run govc storage.policy.create MyStoragePolicy
assert_failure # at least one of -z or -tag required

run govc storage.policy.create -category my_cat -tag my_tag MyStoragePolicy
assert_success

run govc storage.policy.info MyStoragePolicy
assert_success

govc storage.policy.create -z MyZonalPolicy
assert_success

run govc storage.policy.info MyZonalPolicy
assert_success

run govc storage.policy.create -category my_cat -tag my_tag -z MyCombinedPolicy
assert_success

run govc storage.policy.info MyCombinedPolicy
assert_success
}

0 comments on commit bd85d9c

Please sign in to comment.