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