Skip to content

Commit

Permalink
Merge pull request #3531 from dougm/registervm
Browse files Browse the repository at this point in the history
govc: add namespace.registervm command
  • Loading branch information
dougm committed Aug 27, 2024
2 parents 7be4a88 + 0517b87 commit 9090fda
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 6 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
}
18 changes: 18 additions & 0 deletions govc/test/namespace.bats
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,21 @@ load test_helper
run govc namespace.vmclass.rm test-class-1
assert_success
}

@test "namespace.registervm" {
vcsim_env

vm=DC0_C0_RP0_VM0

run govc namespace.create -cluster DC0_C0 test-namespace-1
assert_success

run govc namespace.registervm -vm $vm test-namespace-1
assert_failure # missing resource.yaml

run govc vm.change -vm $vm -e vmservice.virtualmachine.resource.yaml=b64
assert_success

run govc namespace.registervm -vm $vm test-namespace-1
assert_success
}
13 changes: 13 additions & 0 deletions vapi/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,14 @@ func (c *Manager) DeleteNamespace(ctx context.Context, namespace string) error {
return c.Do(ctx, request, nil)
}

// RegisterVM https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/namespaces/instances/namespace/registervm/post/
func (c *Manager) RegisterVM(ctx context.Context, namespace string, spec RegisterVMSpec) (string, error) {
resource := c.Resource(internal.NamespacesPath).WithSubpath(namespace).WithSubpath("registervm")
request := resource.Request(http.MethodPost, spec)
var task string
return task, c.Do(ctx, request, &task)
}

// VirtualMachineClassInfo https://developer.vmware.com/apis/vsphere-automation/v7.0U3/vcenter/data-structures/NamespaceManagement/VirtualMachineClasses/Info/
type VirtualMachineClassInfo struct {
ConfigStatus string `json:"config_status"`
Expand Down Expand Up @@ -799,6 +807,11 @@ type VirtualDevices struct {
VgpuDevices []VgpuDevice `json:"vgpu_devices,omitempty"`
}

// RegisterVMSpec https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/Namespaces_Instances_RegisterVMSpec/
type RegisterVMSpec struct {
VM string `json:"vm"`
}

// ListVmClasses https://developer.vmware.com/apis/vsphere-automation/v7.0U3/vcenter/api/vcenter/namespace-management/virtual-machine-classes/get/
func (c *Manager) ListVmClasses(ctx context.Context) ([]VirtualMachineClassInfo, error) {
resource := c.Resource(internal.VmClassesPath)
Expand Down
36 changes: 33 additions & 3 deletions vapi/namespace/simulator/simulator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2020-2022 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 @@ -35,6 +35,8 @@ import (
"github.com/vmware/govmomi/vapi/namespace"
vapi "github.com/vmware/govmomi/vapi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"

"github.com/vmware/govmomi/vapi/namespace/internal"
Expand Down Expand Up @@ -250,7 +252,14 @@ var namespacesMap = make(map[string]*namespace.NamespacesInstanceInfo)

func (h *Handler) namespaces(w http.ResponseWriter, r *http.Request) {
subpath := r.URL.Path[len(internal.NamespacesPath):]
subpath = strings.Replace(subpath, "/", "", -1)
subpath = strings.TrimPrefix(subpath, "/")
// TODO: move to 1.22's https://go.dev/blog/routing-enhancements
route := strings.Split(subpath, "/")
subpath = route[0]
action := ""
if len(route) > 1 {
action = route[1]
}

switch r.Method {
case http.MethodGet:
Expand Down Expand Up @@ -290,6 +299,27 @@ func (h *Handler) namespaces(w http.ResponseWriter, r *http.Request) {

vapi.ApiErrorNotFound(w)
case http.MethodPost:
if action == "registervm" {
var spec namespace.RegisterVMSpec
if !vapi.Decode(r, w, &spec) {
return
}

ref := types.ManagedObjectReference{Type: "VirtualMachine", Value: spec.VM}
task := types.CreateTask{Obj: ref}
key := &mo.Field{Path: "config.extraConfig", Key: "vmservice.virtualmachine.resource.yaml"}

vapi.StatusOK(w, vapi.RunTask(*h.URL, task, func(ctx context.Context, c *vim25.Client) error {
var vm mo.VirtualMachine
_ = property.DefaultCollector(c).RetrieveOne(ctx, task.Obj, []string{key.String()}, &vm)
if vm.Config == nil || len(vm.Config.ExtraConfig) == 0 {
return fmt.Errorf("%s %s not found", task.Obj, key)
}
return nil
}))
return
}

var spec namespace.NamespacesInstanceCreateSpec
if !vapi.Decode(r, w, &spec) {
return
Expand Down
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],
}
}
53 changes: 52 additions & 1 deletion vapi/simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,13 @@ func New(u *url.URL, r *simulator.Registry) ([]string, http.Handler) {
}

func (s *handler) withClient(f func(context.Context, *vim25.Client) error) error {
return WithClient(s.URL, f)
}

// WithClient creates invokes f with an authenticated vim25.Client.
func WithClient(u url.URL, f func(context.Context, *vim25.Client) error) error {
ctx := context.Background()
c, err := govmomi.NewClient(ctx, &s.URL, true)
c, err := govmomi.NewClient(ctx, &u, true)
if err != nil {
return err
}
Expand All @@ -203,6 +208,52 @@ func (s *handler) withClient(f func(context.Context, *vim25.Client) error) error
return f(ctx, c.Client)
}

// RunTask creates a Task with the given spec and sets the task state based on error returned by f.
func RunTask(u url.URL, spec types.CreateTask, f func(context.Context, *vim25.Client) error) string {
var id string

err := WithClient(u, func(ctx context.Context, c *vim25.Client) error {
spec.This = *c.ServiceContent.TaskManager
if spec.TaskTypeId == "" {
spec.TaskTypeId = "com.vmware.govmomi.simulator.test"
}
res, err := methods.CreateTask(ctx, c, &spec)
if err != nil {
return err
}

ref := res.Returnval.Task
task := object.NewTask(c, ref)
id = ref.Value + ":" + uuid.NewString()

if err = task.SetState(ctx, types.TaskInfoStateRunning, nil, nil); err != nil {
return err
}

var fault *types.LocalizedMethodFault
state := types.TaskInfoStateSuccess
if f != nil {
err = f(ctx, c)
}

if err != nil {
fault = &types.LocalizedMethodFault{
Fault: &types.SystemError{Reason: err.Error()},
LocalizedMessage: err.Error(),
}
state = types.TaskInfoStateError
}

return task.SetState(ctx, state, nil, fault)
})

if err != nil {
panic(err) // should not happen
}

return id
}

// HandleFunc wraps the given handler with authorization checks and passes to http.ServeMux.HandleFunc
func (s *handler) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) {
// Rest paths have been moved from /rest/* to /api/*. Account for both the legacy and new cases here.
Expand Down

0 comments on commit 9090fda

Please sign in to comment.