Skip to content

Commit

Permalink
govc: add namespace.registervm command
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Aug 23, 2024
1 parent 6461021 commit 23390c2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
15 changes: 15 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ but appear via `govc $cmd -h`:
- [namespace.info](#namespaceinfo)
- [namespace.logs.download](#namespacelogsdownload)
- [namespace.ls](#namespacels)
- [namespace.registervm](#namespaceregistervm)
- [namespace.rm](#namespacerm)
- [namespace.service.activate](#namespaceserviceactivate)
- [namespace.service.create](#namespaceservicecreate)
Expand Down Expand Up @@ -4483,6 +4484,20 @@ Examples:
Options:
```

## namespace.registervm

```
Usage: govc namespace.registervm [OPTIONS] NAME
Register an existing virtual machine as VM Service managed VM.
Examples:
govc namespace.registervm -vm my-vm my-namespace
Options:
-vm= Virtual machine [GOVC_VM]
```

## namespace.rm

```
Expand Down
84 changes: 84 additions & 0 deletions govc/namespace/registervm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
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"
"fmt"

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

type registervm struct {
*flags.VirtualMachineFlag
}

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

func (cmd *registervm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
cmd.VirtualMachineFlag.Register(ctx, f)
}

func (cmd *registervm) Usage() string {
return "NAME"
}

func (cmd *registervm) Description() string {
return `Register an existing virtual machine as VM Service managed VM.
Examples:
govc namespace.registervm -vm my-vm my-namespace`
}

func (cmd *registervm) Run(ctx context.Context, f *flag.FlagSet) error {
vm, err := cmd.VirtualMachine()
if err != nil {
return err
}

if vm == nil || f.NArg() != 1 {
return flag.ErrHelp
}

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

spec := namespace.RegisterVMSpec{VM: vm.Reference().Value}

id, err := namespace.NewManager(rc).RegisterVM(ctx, f.Arg(0), spec)
if err != nil {
return err
}

task := object.NewTask(vm.Client(), vapi.Task(id))

logger := cmd.ProgressLogger(fmt.Sprintf("registervm %s... ", vm.InventoryPath))
_, err = task.WaitForResult(ctx, logger)
logger.Wait()

return err
}
17 changes: 15 additions & 2 deletions vapi/resource.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2022-2022 VMware, Inc. All Rights Reserved.
Copyright (c) 2022-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 All @@ -16,7 +16,20 @@ limitations under the License.

package vapi

import (
"strings"

"github.com/vmware/govmomi/vim25/types"
)

const (
// Path is the new-style endpoint for API resources. It supersedes /rest.
Path = "/api"
)

func Task(id string) types.ManagedObjectReference {
return types.ManagedObjectReference{
Type: "Task",
Value: strings.SplitN(id, ":", 2)[0],
}
}

0 comments on commit 23390c2

Please sign in to comment.