Skip to content

Commit

Permalink
Merge pull request #3466 from dougm/storage-helpers
Browse files Browse the repository at this point in the history
Add storage profile related api and govc helpers
  • Loading branch information
dougm authored Jun 12, 2024
2 parents 1db964c + 3e648db commit 420dc85
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 127 deletions.
33 changes: 18 additions & 15 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4311,20 +4311,21 @@ Usage: govc namespace.create [OPTIONS] NAME
Creates a new vSphere Namespace on a Supervisor.
The '-library' and '-vm-class' flags can each be specified multiple times.
The '-library', '-vmclass' and '-storage' flags can each be specified multiple times.
Examples:
govc namespace.create -supervisor=domain-c1 test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge test-namespace
govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge -vm-class best-effort-4xlarge test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace
govc namespace.create -cluster C1 test-namespace
govc namespace.create -cluster C1 -library vmsvc test-namespace
govc namespace.create -cluster C1 -library vmsvc -library tkgs test-namespace -storage wcp-policy
govc namespace.create -cluster C1 -vmclass best-effort-2xlarge test-namespace
govc namespace.create -cluster C1 -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
govc namespace.create -cluster C1 -library vmsvc -library tkgs -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
Options:
-cluster= Cluster [GOVC_CLUSTER]
-library=[] Content library IDs to associate with the vSphere Namespace.
-supervisor= The identifier of the Supervisor.
-vm-class=[] Virtual machine class IDs to associate with the vSphere Namespace.
-storage=[] Storage profile IDs to associate with the vSphere Namespace.
-vmclass=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

## namespace.info
Expand Down Expand Up @@ -4476,15 +4477,16 @@ Usage: govc namespace.update [OPTIONS] NAME
Modifies an existing vSphere Namespace on a Supervisor.
Examples:
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=617a3ee3-a2ff-4311-9a7c-0016ccf958bd test-namespace
govc namespace.update -vm-class=best-effort-2xlarge test-namespace
govc namespace.update -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=617a3ee3-a2ff-4311-9a7c-0016ccf958bd -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace
govc namespace.update -library vmsvc test-namespace
govc namespace.update -library vmsvc -library tkgs -storage wcp-policy test-namespace
govc namespace.update -vmclass best-effort-2xlarge test-namespace
govc namespace.update -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
govc namespace.update -library vmsvc -library tkgs -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
Options:
-library=[] Content library IDs to associate with the vSphere Namespace.
-vm-class=[] Virtual machine class IDs to associate with the vSphere Namespace.
-storage=[] Storage profile IDs to associate with the vSphere Namespace.
-vmclass=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

## namespace.vmclass.create
Expand Down Expand Up @@ -6318,6 +6320,7 @@ Options:
-net.protocol= Network device protocol. Applicable to vmxnet3vrdma. Default to 'rocev2'
-on=true Power on VM
-pool= Resource pool [GOVC_RESOURCE_POOL]
-profile= Storage profile name or ID
-version= ESXi hardware version [2|3|4|5.0|5.1|5.5|6.0|6.5|6.7|6.7.2|7.0|7.0.1|7.0.2|8.0|8.0.1|8.0.2]
```

Expand Down
57 changes: 34 additions & 23 deletions govc/namespace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,60 @@ import (
)

type create struct {
*flags.ClientFlag
*flags.ClusterFlag
*namespaceFlag

libraries flags.StringList
vmClasses flags.StringList
spec namespace.NamespacesInstanceCreateSpec
spec namespace.NamespacesInstanceCreateSpec
}

func init() {
cli.Register("namespace.create", &create{})
}

func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
cmd.ClusterFlag.Register(ctx, f)

f.StringVar(&cmd.spec.Cluster, "supervisor", "", "The identifier of the Supervisor.")
f.Var(&cmd.libraries, "library", "Content library IDs to associate with the vSphere Namespace.")
f.Var(&cmd.vmClasses, "vm-class", "Virtual machine class IDs to associate with the vSphere Namespace.")
}

func (*create) Usage() string {
return "NAME"
cmd.namespaceFlag = &namespaceFlag{}
cmd.namespaceFlag.Register(ctx, f)
}

func (cmd *create) Process(ctx context.Context) error {
cmd.spec.VmServiceSpec.ContentLibraries = cmd.libraries
cmd.spec.VmServiceSpec.VmClasses = cmd.vmClasses
if err := cmd.ClusterFlag.Process(ctx); err != nil {
return err
}
if err := cmd.namespaceFlag.Process(ctx); err != nil {
return err
}

return cmd.ClientFlag.Process(ctx)
cluster, err := cmd.Cluster()
if err != nil {
return err
}

cmd.spec.Cluster = cluster.Reference().Value
cmd.spec.StorageSpecs = cmd.storageSpec()
cmd.spec.VmServiceSpec = cmd.vmServiceSpec()

return nil
}

func (*create) Usage() string {
return "NAME"
}

func (cmd *create) Description() string {
return `Creates a new vSphere Namespace on a Supervisor.
The '-library' and '-vm-class' flags can each be specified multiple times.
The '-library', '-vmclass' and '-storage' flags can each be specified multiple times.
Examples:
govc namespace.create -supervisor=domain-c1 test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge test-namespace
govc namespace.create -supervisor=domain-c1 -vm-class=best-effort-2xlarge -vm-class best-effort-4xlarge test-namespace
govc namespace.create -supervisor=domain-c1 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace`
govc namespace.create -cluster C1 test-namespace
govc namespace.create -cluster C1 -library vmsvc test-namespace
govc namespace.create -cluster C1 -library vmsvc -library tkgs test-namespace -storage wcp-policy
govc namespace.create -cluster C1 -vmclass best-effort-2xlarge test-namespace
govc namespace.create -cluster C1 -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
govc namespace.create -cluster C1 -library vmsvc -library tkgs -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
Expand Down
93 changes: 93 additions & 0 deletions govc/namespace/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright (c) 2024-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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package namespace

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vapi/namespace"
)

type namespaceFlag struct {
*flags.ClientFlag

library flags.StringList
vmclass flags.StringList
storage flags.StringList
}

func (ns *namespaceFlag) Register(ctx context.Context, f *flag.FlagSet) {
ns.ClientFlag, ctx = flags.NewClientFlag(ctx)
ns.ClientFlag.Register(ctx, f)

f.Var(&ns.library, "library", "Content library IDs to associate with the vSphere Namespace.")
f.Var(&ns.vmclass, "vmclass", "Virtual machine class IDs to associate with the vSphere Namespace.")
f.Var(&ns.storage, "storage", "Storage profile IDs to associate with the vSphere Namespace.")
}

func (ns *namespaceFlag) Process(ctx context.Context) error {
if err := ns.ClientFlag.Process(ctx); err != nil {
return err
}

rc, err := ns.RestClient()
if err != nil {
return err
}

for i, name := range ns.library {
l, err := flags.ContentLibrary(ctx, rc, name)
if err == nil {
ns.library[i] = l.ID
}
}

pc, err := ns.PbmClient()
if err != nil {
return err
}

m, err := pc.ProfileMap(ctx)
if err != nil {
return err
}

for i, name := range ns.storage {
if n, ok := m.Name[name]; ok {
ns.storage[i] = n.GetPbmProfile().ProfileId.UniqueId
}
}

return nil
}

func (ns *namespaceFlag) storageSpec() []namespace.StorageSpec {
s := make([]namespace.StorageSpec, len(ns.storage))
for i := range ns.storage {
s[i].Policy = ns.storage[i]
}
return s
}

func (ns *namespaceFlag) vmServiceSpec() namespace.VmServiceSpec {
return namespace.VmServiceSpec{
ContentLibraries: ns.library,
VmClasses: ns.vmclass,
}
}
60 changes: 56 additions & 4 deletions govc/namespace/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,72 @@ import (
"strings"
"text/tabwriter"

"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/vapi/library"
"github.com/vmware/govmomi/vapi/namespace"
"github.com/vmware/govmomi/vim25/types"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
)

type infoResult namespace.NamespacesInstanceInfo
type infoResult struct {
namespace.NamespacesInstanceInfo

ctx context.Context
cmd *info
}

func (r infoResult) Write(w io.Writer) error {
c, err := r.cmd.Client()
if err != nil {
return err
}

rc, err := r.cmd.RestClient()
if err != nil {
return err
}
l := library.NewManager(rc)

pc, err := r.cmd.PbmClient()
if err != nil {
return err
}

var ids []string
for _, s := range r.StorageSpecs {
ids = append(ids, s.Policy)
}
m, err := pc.ProfileMap(r.ctx, ids...)
if err != nil {
return err
}

tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
fmt.Fprintf(tw, "Cluster:\t%s\n", r.ClusterId)

cluster := types.ManagedObjectReference{Type: "ClusterComputeResource", Value: r.ClusterId}
path, err := find.InventoryPath(r.ctx, c, cluster)
if err != nil {
return err
}

fmt.Fprintf(tw, "Cluster:\t%s\n", path)
fmt.Fprintf(tw, "Status:\t%s\n", r.ConfigStatus)
fmt.Fprintf(tw, "VM Classes:\t%s\n", strings.Join(r.VmServiceSpec.VmClasses, ","))
fmt.Fprintf(tw, "VM Libraries:\t%s\n", strings.Join(r.VmServiceSpec.ContentLibraries, ","))

for _, s := range r.VmServiceSpec.ContentLibraries {
info, err := l.GetLibraryByID(r.ctx, s)
if err != nil {
return err
}
fmt.Fprintf(tw, "Content Library:\t%s\n", info.Name)
}

for _, s := range r.StorageSpecs {
fmt.Fprintf(tw, "Storage Policy:\t%s\n", m.Name[s.Policy].GetPbmProfile().Name)
}

return tw.Flush()
}

Expand Down Expand Up @@ -97,5 +149,5 @@ func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
return err
}

return cmd.WriteResult(infoResult(d))
return cmd.WriteResult(&infoResult{d, ctx, cmd})
}
34 changes: 16 additions & 18 deletions govc/namespace/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,33 @@ import (
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/vapi/namespace"
)

type update struct {
*flags.ClientFlag
*namespaceFlag

libraries flags.StringList
vmClasses flags.StringList
spec namespace.NamespacesInstanceUpdateSpec
spec namespace.NamespacesInstanceUpdateSpec
}

func init() {
cli.Register("namespace.update", &update{})
}

func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

f.Var(&cmd.libraries, "library", "Content library IDs to associate with the vSphere Namespace.")
f.Var(&cmd.vmClasses, "vm-class", "Virtual machine class IDs to associate with the vSphere Namespace.")
cmd.namespaceFlag = &namespaceFlag{}
cmd.namespaceFlag.Register(ctx, f)
}

func (cmd *update) Process(ctx context.Context) error {
cmd.spec.VmServiceSpec.ContentLibraries = cmd.libraries
cmd.spec.VmServiceSpec.VmClasses = cmd.vmClasses
if err := cmd.namespaceFlag.Process(ctx); err != nil {
return err
}

cmd.spec.StorageSpecs = cmd.storageSpec()
cmd.spec.VmServiceSpec = cmd.vmServiceSpec()

return cmd.ClientFlag.Process(ctx)
return nil
}

func (cmd *update) Usage() string {
Expand All @@ -60,11 +58,11 @@ func (cmd *update) Description() string {
return `Modifies an existing vSphere Namespace on a Supervisor.
Examples:
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=617a3ee3-a2ff-4311-9a7c-0016ccf958bd test-namespace
govc namespace.update -vm-class=best-effort-2xlarge test-namespace
govc namespace.update -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace
govc namespace.update -library=dca9cc16-9460-4da0-802c-4aa148ac6cf7 -library=617a3ee3-a2ff-4311-9a7c-0016ccf958bd -vm-class=best-effort-2xlarge -vm-class=best-effort-4xlarge test-namespace`
govc namespace.update -library vmsvc test-namespace
govc namespace.update -library vmsvc -library tkgs -storage wcp-policy test-namespace
govc namespace.update -vmclass best-effort-2xlarge test-namespace
govc namespace.update -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace
govc namespace.update -library vmsvc -library tkgs -vmclass best-effort-2xlarge -vmclass best-effort-4xlarge test-namespace`
}

func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
Expand Down
Loading

0 comments on commit 420dc85

Please sign in to comment.