github.com/vmware/govmomi@v0.43.0/vapi/cluster/cluster.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 cluster 18 19 import ( 20 "context" 21 "net/http" 22 "path" 23 24 "github.com/vmware/govmomi/vapi/rest" 25 "github.com/vmware/govmomi/vim25/mo" 26 "github.com/vmware/govmomi/vim25/types" 27 28 "github.com/vmware/govmomi/vapi/cluster/internal" 29 ) 30 31 // Manager extends rest.Client, adding cluster related methods. 32 type Manager struct { 33 *rest.Client 34 } 35 36 // NewManager creates a new Manager instance with the given client. 37 func NewManager(client *rest.Client) *Manager { 38 return &Manager{ 39 Client: client, 40 } 41 } 42 43 // CreateModule creates a new module in a vCenter cluster. 44 func (c *Manager) CreateModule(ctx context.Context, ref mo.Reference) (string, error) { 45 var s internal.CreateModule 46 s.Spec.ID = ref.Reference().Value 47 48 url := c.Resource(internal.ModulesPath) 49 var res string 50 return res, c.Do(ctx, url.Request(http.MethodPost, s), &res) 51 } 52 53 // DeleteModule deletes a specific module. 54 func (c *Manager) DeleteModule(ctx context.Context, id string) error { 55 url := c.Resource(internal.ModulesPath + "/" + id) 56 return c.Do(ctx, url.Request(http.MethodDelete), nil) 57 } 58 59 // ModuleSummary contains commonly used information about a module in a vCenter cluster. 60 type ModuleSummary struct { 61 Cluster string `json:"cluster"` 62 Module string `json:"module"` 63 } 64 65 // ModuleSummaryList is used to JSON encode/decode a ModuleSummary. 66 type ModuleSummaryList struct { 67 Summaries []ModuleSummary `json:"summaries"` 68 } 69 70 // ListModules returns information about the modules available in this vCenter server. 71 func (c *Manager) ListModules(ctx context.Context) ([]ModuleSummary, error) { 72 var res ModuleSummaryList 73 url := c.Resource(internal.ModulesPath) 74 return res.Summaries, c.Do(ctx, url.Request(http.MethodGet), &res) 75 } 76 77 func memberPath(id string) string { 78 return path.Join(internal.ModulesVMPath, id, "members") 79 } 80 81 // ListModuleMembers returns the virtual machines that are members of the module. 82 func (c *Manager) ListModuleMembers(ctx context.Context, id string) ([]types.ManagedObjectReference, error) { 83 var m internal.ModuleMembers 84 url := c.Resource(memberPath(id)) 85 err := c.Do(ctx, url.Request(http.MethodGet), &m) 86 if err != nil { 87 return nil, err 88 } 89 return m.AsReferences(), err 90 } 91 92 func (c *Manager) moduleMembers(ctx context.Context, action string, id string, vms ...mo.Reference) (bool, error) { 93 url := c.Resource(memberPath(id)).WithParam("action", action) 94 var m internal.ModuleMembers 95 for i := range vms { 96 m.VMs = append(m.VMs, vms[i].Reference().Value) 97 } 98 var res internal.Status 99 return res.Success, c.Do(ctx, url.Request(http.MethodPost, m), &res) 100 } 101 102 // AddModuleMembers adds virtual machines to the module. These virtual machines are required to be in the same vCenter cluster. 103 // Returns true if all vms are added, false if a vm is already a member of the module or not within the module's cluster. 104 func (c *Manager) AddModuleMembers(ctx context.Context, id string, vms ...mo.Reference) (bool, error) { 105 return c.moduleMembers(ctx, "add", id, vms...) 106 } 107 108 // RemoveModuleMembers removes virtual machines from the module. 109 // Returns true if all vms are removed, false if a vm is not a member of the module. 110 func (c *Manager) RemoveModuleMembers(ctx context.Context, id string, vms ...mo.Reference) (bool, error) { 111 return c.moduleMembers(ctx, "remove", id, vms...) 112 }