Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

govc: add default output formatting for namespace commands #3410

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4284,31 +4284,32 @@ Options:
## namespace.create

```
Usage: govc namespace.create [OPTIONS]
Usage: govc namespace.create [OPTIONS] NAME

Creates a new vSphere Namespace on a Supervisor.
Creates a new vSphere Namespace on a Supervisor.

The '-library' and '-vm-class' flags can each be specified multiple times.

Examples:
govc namespace.create -namespace=test-namespace -supervisor=domain-c1
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,dca9cc16-9460-4da0-802c-4aa148ac6cf7
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -vm-classes=best-effort-2xlarge
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -vm-classes=best-effort-2xlarge,best-effort-4xlarge
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,dca9cc16-9460-4da0-802c-4aa148ac6cf7 -vm-classes=best-effort-2xlarge,best-effort-4xlarge
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

Options:
-content-libraries= The identifiers of the content libraries to associate with the vSphere Namespace.
-namespace= The name of the vSphere Namespace.
-library=[] Content library IDs to associate with the vSphere Namespace.
-supervisor= The identifier of the Supervisor.
-vm-classes= The identifiers of the virtual machine classes to associate with the vSphere Namespace.
-vm-class=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

## namespace.info

```
Usage: govc namespace.info [OPTIONS] NAME

Displays the details of a vSphere Namespace.
Displays the details of a vSphere Namespace.

Examples:
govc namespace.info test-namespace
Expand Down Expand Up @@ -4341,7 +4342,7 @@ Options:
```
Usage: govc namespace.ls [OPTIONS]

Displays the list of vSphere Namespaces.
Displays the list of vSphere Namespaces.

Examples:
govc namespace.ls
Expand Down Expand Up @@ -4449,48 +4450,47 @@ Options:
```
Usage: govc namespace.update [OPTIONS] NAME

Modifies an existing vSphere Namespace on a Supervisor.
Modifies an existing vSphere Namespace on a Supervisor.

Examples:
govc namespace.update -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7 test-namespace
govc namespace.update -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,617a3ee3-a2ff-4311-9a7c-0016ccf958bd test-namespace
govc namespace.update -vm-classes=best-effort-2xlarge test-namespace
govc namespace.update -vm-classes=best-effort-2xlarge,best-effort-4xlarge test-namespace
govc namespace.update -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,617a3ee3-a2ff-4311-9a7c-0016ccf958bd -vm-classes=best-effort-2xlarge,best-effort-4xlarge test-namespace
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

Options:
-content-libraries= The list of content libraries to associate with the vSphere Namespace.
-vm-classes= The list of virtual machine classes to associate with the vSphere Namespace.
-library=[] Content library IDs to associate with the vSphere Namespace.
-vm-class=[] Virtual machine class IDs to associate with the vSphere Namespace.
```

## namespace.vmclass.create

```
Usage: govc namespace.vmclass.create [OPTIONS]
Usage: govc namespace.vmclass.create [OPTIONS] NAME

Creates a new virtual machine class.
Creates a new virtual machine class.

The name of the virtual machine class has DNS_LABEL restrictions
as specified in "https://tools.ietf.org/html/rfc1123". It
must be an alphanumeric (a-z and 0-9) string and with maximum length
of 63 characters and with the '-' character allowed anywhere except
the first or last character. This name is unique in this vCenter server.
The name of the virtual machine class has DNS_LABEL restrictions
as specified in "https://tools.ietf.org/html/rfc1123". It
must be an alphanumeric (a-z and 0-9) string and with maximum length
of 63 characters and with the '-' character allowed anywhere except
the first or last character. This name is unique in this vCenter server.

Examples:
govc namespace.vmclass.create -name=test-class-01 -cpus=8 -memory=8192
govc namespace.vmclass.create -cpus=8 -memory=8192 test-class-01

Options:
-cpus=0 The number of CPUs.
-memory=0 The amount of memory (in MB).
-name= The name of the virtual machine class.
```

## namespace.vmclass.info

```
Usage: govc namespace.vmclass.info [OPTIONS] NAME

Displays the details of a virtual machine class.
Displays the details of a virtual machine class.

Examples:
govc namespace.vmclass.info test-class
Expand All @@ -4503,7 +4503,7 @@ Options:
```
Usage: govc namespace.vmclass.ls [OPTIONS]

Displays the list of virtual machine classes.
Displays the list of virtual machine classes.

Examples:
govc namespace.vmclass.ls
Expand Down
45 changes: 25 additions & 20 deletions govc/namespace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package namespace
import (
"context"
"flag"
"strings"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
Expand All @@ -29,8 +28,8 @@ import (
type create struct {
*flags.ClientFlag

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

Expand All @@ -43,36 +42,42 @@ func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag.Register(ctx, f)

f.StringVar(&cmd.spec.Cluster, "supervisor", "", "The identifier of the Supervisor.")
f.StringVar(&cmd.spec.Namespace, "namespace", "", "The name of the vSphere Namespace.")
f.StringVar(&cmd.libraries, "content-libraries", "", "The identifiers of the content libraries to associate with the vSphere Namespace.")
f.StringVar(&cmd.vmClasses, "vm-classes", "", "The identifiers of the virtual machine classes to associate with the vSphere Namespace.")
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"
}

func (cmd *create) Process(ctx context.Context) error {
if len(cmd.libraries) > 0 {
cmd.spec.VmServiceSpec.ContentLibraries = strings.Split(cmd.libraries, ",")
}
if len(cmd.vmClasses) > 0 {
cmd.spec.VmServiceSpec.VmClasses = strings.Split(cmd.vmClasses, ",")
}
cmd.spec.VmServiceSpec.ContentLibraries = cmd.libraries
cmd.spec.VmServiceSpec.VmClasses = cmd.vmClasses

return cmd.ClientFlag.Process(ctx)
}

func (cmd *create) Description() string {
return `Creates a new vSphere Namespace on a Supervisor.
return `Creates a new vSphere Namespace on a Supervisor.

The '-library' and '-vm-class' flags can each be specified multiple times.

Examples:
govc namespace.create -namespace=test-namespace -supervisor=domain-c1
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,dca9cc16-9460-4da0-802c-4aa148ac6cf7
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -vm-classes=best-effort-2xlarge
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -vm-classes=best-effort-2xlarge,best-effort-4xlarge
govc namespace.create -namespace=test-namespace -supervisor=domain-c1 -content-libraries=dca9cc16-9460-4da0-802c-4aa148ac6cf7,dca9cc16-9460-4da0-802c-4aa148ac6cf7 -vm-classes=best-effort-2xlarge,best-effort-4xlarge`
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`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
rc, err := cmd.RestClient()
cmd.spec.Namespace = f.Arg(0)
if f.NArg() != 1 {
return flag.ErrHelp
}

rc, err := cmd.RestClient()
if err != nil {
return err
}
Expand Down
20 changes: 15 additions & 5 deletions govc/namespace/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package namespace
import (
"context"
"flag"
"fmt"
"io"
"strings"
"text/tabwriter"

"github.com/vmware/govmomi/vapi/namespace"

Expand All @@ -30,7 +33,12 @@ import (
type infoResult namespace.NamespacesInstanceInfo

func (r infoResult) Write(w io.Writer) error {
return nil
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
fmt.Fprintf(tw, "Cluster:\t%s\n", r.ClusterId)
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, ","))
return tw.Flush()
}

type info struct {
Expand All @@ -47,6 +55,7 @@ func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag.Register(ctx, f)

cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)
}

func (cmd *info) Process(ctx context.Context) error {
Expand All @@ -65,27 +74,28 @@ func (cmd *info) Usage() string {
}

func (cmd *info) Description() string {
return `Displays the details of a vSphere Namespace.
return `Displays the details of a vSphere Namespace.

Examples:
govc namespace.info test-namespace`
}

func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
rc, err := cmd.RestClient()
if f.NArg() != 1 {
return flag.ErrHelp
}

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

nm := namespace.NewManager(rc)

d, err := nm.GetNamespace(ctx, f.Arg(0))

if err != nil {
return err
}

cmd.JSON = !cmd.All()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI - I did the same thing on another PR over a month ago #3368
I'm saying this in case you're working on eliminating the "only-JSON-output-supported" pattern :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember, but I've yet to use vLCM (directly at least). I use vSphere Namespaces everyday and am happy to replace stuff like below with the new govc commands, thanks!

# now govc namespace.create
govc session.login -r -X POST /api/vcenter/namespaces/instances \
  <<<'{"cluster": "domain-c9","namespace": "velero"}'

# now govc namespace.ls
govc session.login -r -X GET /api/vcenter/namespaces/instances | jq -r .[].namespace

return cmd.WriteResult(infoResult(d))
}
10 changes: 6 additions & 4 deletions govc/namespace/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package namespace
import (
"context"
"flag"
"fmt"
"io"

"github.com/vmware/govmomi/vapi/namespace"
Expand All @@ -30,6 +31,9 @@ import (
type lsResult []namespace.NamespacesInstanceSummary

func (r lsResult) Write(w io.Writer) error {
for _, e := range r {
fmt.Fprintln(w, e.Namespace)
}
return nil
}

Expand All @@ -47,6 +51,7 @@ func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag.Register(ctx, f)

cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)
}

func (cmd *ls) Process(ctx context.Context) error {
Expand All @@ -61,27 +66,24 @@ func (cmd *ls) Process(ctx context.Context) error {
}

func (cmd *ls) Description() string {
return `Displays the list of vSphere Namespaces.
return `Displays the list of vSphere Namespaces.

Examples:
govc namespace.ls`
}

func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
rc, err := cmd.RestClient()

if err != nil {
return err
}

nm := namespace.NewManager(rc)

d, err := nm.ListNamespaces(ctx)

if err != nil {
return err
}

cmd.JSON = !cmd.All()
return cmd.WriteResult(lsResult(d))
}
Loading
Loading