github.com/vmware/govmomi@v0.43.0/vapi/cluster/internal/internal.go (about)

     1  /*
     2  Copyright (c) 2020 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package internal
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/vmware/govmomi/view"
    23  	"github.com/vmware/govmomi/vim25"
    24  	"github.com/vmware/govmomi/vim25/mo"
    25  	"github.com/vmware/govmomi/vim25/types"
    26  )
    27  
    28  const (
    29  	// ModulesPath is rest endpoint for the Cluster Modules API
    30  	ModulesPath = "/vcenter/cluster/modules"
    31  	// ModulesVMPath is rest endpoint for the Cluster Modules Members API
    32  	ModulesVMPath = "/vcenter/cluster/modules/vm"
    33  )
    34  
    35  // Status is used for JSON encode/decode
    36  type Status struct {
    37  	Success bool `json:"success"`
    38  }
    39  
    40  // CreateModule is used for JSON encode/decode
    41  type CreateModule struct {
    42  	Spec struct {
    43  		ID string `json:"cluster"`
    44  	} `json:"spec"`
    45  }
    46  
    47  // ModuleMembers is used for JSON encode/decode
    48  type ModuleMembers struct {
    49  	VMs []string `json:"vms"`
    50  }
    51  
    52  // AsReferences converts the ModuleMembers.VM field to morefs
    53  func (m *ModuleMembers) AsReferences() []types.ManagedObjectReference {
    54  	refs := make([]types.ManagedObjectReference, 0, len(m.VMs))
    55  	for _, id := range m.VMs {
    56  		refs = append(refs, types.ManagedObjectReference{
    57  			Type:  "VirtualMachine",
    58  			Value: id,
    59  		})
    60  	}
    61  	return refs
    62  }
    63  
    64  // ClusterVM returns all VM references in the given cluster
    65  func ClusterVM(c *vim25.Client, cluster mo.Reference) ([]mo.Reference, error) {
    66  	ctx := context.Background()
    67  	kind := []string{"VirtualMachine"}
    68  
    69  	m := view.NewManager(c)
    70  	v, err := m.CreateContainerView(ctx, cluster.Reference(), kind, true)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	defer func() { _ = v.Destroy(ctx) }()
    75  
    76  	refs, err := v.Find(ctx, kind, nil)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	vms := make([]mo.Reference, 0, len(refs))
    82  	for i := range refs {
    83  		vms = append(vms, refs[i])
    84  	}
    85  
    86  	return vms, nil
    87  }