|
| 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 enablement |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + "github.com/vmware/govmomi/vapi/esx/settings" |
| 13 | + "github.com/vmware/govmomi/vapi/rest" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // BasePath The base endpoint for the clusters configuration enablement API |
| 18 | + BasePath = settings.BasePath + "/%s/enablement" |
| 19 | + Configuration = BasePath + "/configuration" |
| 20 | + Transition = Configuration + "/transition" |
| 21 | +) |
| 22 | + |
| 23 | +type FileSpec struct { |
| 24 | + Config string `json:"config"` |
| 25 | + Filename string `json:"filename"` |
| 26 | +} |
| 27 | + |
| 28 | +// Manager extends rest.Client, adding cluster configuration enablement related methods. |
| 29 | +type Manager struct { |
| 30 | + *rest.Client |
| 31 | +} |
| 32 | + |
| 33 | +// NewManager creates a new Manager instance with the given client. |
| 34 | +func NewManager(client *rest.Client) *Manager { |
| 35 | + return &Manager{ |
| 36 | + Client: client, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// EnableClusterConfiguration enables cluster configuration profiles |
| 41 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 42 | +func (c *Manager) EnableClusterConfiguration(clusterId string) (string, error) { |
| 43 | + path := c.getBaseTransitionUrl(clusterId, "enable") |
| 44 | + req := path.Request(http.MethodPost, nil) |
| 45 | + var res string |
| 46 | + return res, c.Do(context.Background(), req, &res) |
| 47 | +} |
| 48 | + |
| 49 | +// ImportFromReferenceHost imports the configuration of an existing ESXi host |
| 50 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 51 | +func (c *Manager) ImportFromReferenceHost(clusterId, hostId string) (string, error) { |
| 52 | + path := c.getBaseTransitionUrl(clusterId, "importFromHost") |
| 53 | + req := path.Request(http.MethodPost, hostId) |
| 54 | + var res string |
| 55 | + return res, c.Do(context.Background(), req, &res) |
| 56 | +} |
| 57 | + |
| 58 | +// ImportFromFile imports the configuration in the provided json string |
| 59 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 60 | +func (c *Manager) ImportFromFile(clusterId string, spec FileSpec) (string, error) { |
| 61 | + path := c.getBaseTransitionUrl(clusterId, "importFromFile") |
| 62 | + req := path.Request(http.MethodPost, spec) |
| 63 | + var res string |
| 64 | + return res, c.Do(context.Background(), req, &res) |
| 65 | +} |
| 66 | + |
| 67 | +// ValidateConfiguration performs server-side validation of the pending cluster configuration |
| 68 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 69 | +func (c *Manager) ValidateConfiguration(clusterId string) (string, error) { |
| 70 | + path := c.getBaseTransitionUrl(clusterId, "validateConfig") |
| 71 | + req := path.Request(http.MethodPost, nil) |
| 72 | + var res string |
| 73 | + return res, c.Do(context.Background(), req, &res) |
| 74 | +} |
| 75 | + |
| 76 | +// RunPrecheck performs server-side pre-checks of the pending cluster configuration |
| 77 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 78 | +func (c *Manager) RunPrecheck(clusterId string) (string, error) { |
| 79 | + path := c.getBaseTransitionUrl(clusterId, "precheck") |
| 80 | + req := path.Request(http.MethodPost, nil) |
| 81 | + var res string |
| 82 | + return res, c.Do(context.Background(), req, &res) |
| 83 | +} |
| 84 | + |
| 85 | +// Cancel deletes the pending cluster configuration |
| 86 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 87 | +func (c *Manager) Cancel(clusterId string) (string, error) { |
| 88 | + path := c.Resource(fmt.Sprintf(Transition, clusterId)).WithParam("action", "cancel") |
| 89 | + req := path.Request(http.MethodPost, nil) |
| 90 | + var res string |
| 91 | + return res, c.Do(context.Background(), req, &res) |
| 92 | +} |
| 93 | + |
| 94 | +// GetClusterConfigurationStatus returns the status of the current pending cluster configuration |
| 95 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/enablement/configuration/transition |
| 96 | +func (c *Manager) GetClusterConfigurationStatus(clusterId string) (string, error) { |
| 97 | + path := c.Resource(fmt.Sprintf(Transition, clusterId)) |
| 98 | + req := path.Request(http.MethodGet) |
| 99 | + var res string |
| 100 | + return res, c.Do(context.Background(), req, &res) |
| 101 | +} |
| 102 | + |
| 103 | +func (c *Manager) getBaseTransitionUrl(clusterId, action string) *rest.Resource { |
| 104 | + return c.Resource(fmt.Sprintf(Transition, clusterId)).WithParam("action", action).WithParam("vmw-task", "true") |
| 105 | +} |
0 commit comments