|
| 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 configuration |
| 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 API |
| 18 | + BasePath = settings.BasePath + "/%s/configuration" |
| 19 | +) |
| 20 | + |
| 21 | +// Manager extends rest.Client, adding cluster configuration enablement related methods. |
| 22 | +type Manager struct { |
| 23 | + *rest.Client |
| 24 | +} |
| 25 | + |
| 26 | +// NewManager creates a new Manager instance with the given client. |
| 27 | +func NewManager(client *rest.Client) *Manager { |
| 28 | + return &Manager{ |
| 29 | + Client: client, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type Info struct { |
| 34 | + Config string `json:"config"` |
| 35 | + Metadata Metadata `json:"metadata"` |
| 36 | +} |
| 37 | + |
| 38 | +type Metadata struct { |
| 39 | + Id string `json:"id"` |
| 40 | +} |
| 41 | + |
| 42 | +type ExportResult struct { |
| 43 | + Config string `json:"config"` |
| 44 | +} |
| 45 | + |
| 46 | +type ImportSpec struct { |
| 47 | + Config string `json:"config"` |
| 48 | + Description *string `json:"description"` |
| 49 | +} |
| 50 | + |
| 51 | +type ApplySpec struct { |
| 52 | + Hosts *[]string `json:"hosts"` |
| 53 | + ApplyPolicySpec *ApplyPolicySpec `json:"apply_policy_spec"` |
| 54 | +} |
| 55 | + |
| 56 | +type ApplyPolicySpec struct { |
| 57 | + FailureAction *FailureAction `json:"failure_action"` |
| 58 | + PreRemediationPowerAction *string `json:"pre_remediation_power_action"` |
| 59 | + EnableQuickBoot *bool `json:"enable_quick_boot"` |
| 60 | + DisableDpm *bool `json:"disable_dpm"` |
| 61 | + DisableHac *bool `json:"disable_hac"` |
| 62 | + EvacuateOfflineVms *bool `json:"evacuate_offline_vms"` |
| 63 | + EnforceHclValidation *bool `json:"enforce_hcl_validation"` |
| 64 | + EnforceQuickPatch *bool `json:"enforce_quick_patch"` |
| 65 | + ParallelRemediationAction *ParallelRemediationAction `json:"parallel_remediation_action"` |
| 66 | + ConfigManagerPolicySpec *ConfigManagerPolicySpec `json:"config_manager_policy_spec"` |
| 67 | +} |
| 68 | + |
| 69 | +type FailureAction struct { |
| 70 | + Action string `json:"action"` |
| 71 | + RetryDelay int `json:"retry_delay"` |
| 72 | + RetryCount int `json:"retry_count"` |
| 73 | +} |
| 74 | + |
| 75 | +type ParallelRemediationAction struct { |
| 76 | + Enabled bool `json:"enabled"` |
| 77 | + MaxHosts int `json:"max_hosts"` |
| 78 | +} |
| 79 | + |
| 80 | +type ConfigManagerPolicySpec struct { |
| 81 | + SerialRemediation bool `json:"serial_remediation"` |
| 82 | +} |
| 83 | + |
| 84 | +// GetConfiguration returns the cluster configuration |
| 85 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 86 | +func (c *Manager) GetConfiguration(clusterId string) (Info, error) { |
| 87 | + path := c.Resource(fmt.Sprintf(BasePath, clusterId)) |
| 88 | + req := path.Request(http.MethodGet) |
| 89 | + var res Info |
| 90 | + return res, c.Do(context.Background(), req, &res) |
| 91 | +} |
| 92 | + |
| 93 | +// ExportConfiguration returns the cluster configuration |
| 94 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 95 | +func (c *Manager) ExportConfiguration(clusterId string) (ExportResult, error) { |
| 96 | + path := c.Resource(fmt.Sprintf(BasePath, clusterId)).WithAction("exportConfig") |
| 97 | + req := path.Request(http.MethodPost) |
| 98 | + var res ExportResult |
| 99 | + return res, c.Do(context.Background(), req, &res) |
| 100 | +} |
| 101 | + |
| 102 | +// ApplyConfiguration applies the current configuration to the provided hosts in the cluster |
| 103 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 104 | +func (c *Manager) ApplyConfiguration(clusterId string, spec ApplySpec) (string, error) { |
| 105 | + path := c.getBaseTransitionUrl(clusterId, "apply") |
| 106 | + req := path.Request(http.MethodPost, spec) |
| 107 | + var res string |
| 108 | + return res, c.Do(context.Background(), req, &res) |
| 109 | +} |
| 110 | + |
| 111 | +// CheckCompliance initiates a compliance check on the cluster |
| 112 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 113 | +func (c *Manager) CheckCompliance(clusterId string) (string, error) { |
| 114 | + path := c.getBaseTransitionUrl(clusterId, "checkCompliance") |
| 115 | + req := path.Request(http.MethodPost) |
| 116 | + var res string |
| 117 | + return res, c.Do(context.Background(), req, &res) |
| 118 | +} |
| 119 | + |
| 120 | +// Validate initiates a validation check on the pending cluster configuration |
| 121 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 122 | +func (c *Manager) Validate(clusterId string) (string, error) { |
| 123 | + path := c.getBaseTransitionUrl(clusterId, "validate") |
| 124 | + req := path.Request(http.MethodPost) |
| 125 | + var res string |
| 126 | + return res, c.Do(context.Background(), req, &res) |
| 127 | +} |
| 128 | + |
| 129 | +// Precheck initiates a precheck on the pending cluster configuration |
| 130 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 131 | +func (c *Manager) Precheck(clusterId string) (string, error) { |
| 132 | + path := c.getBaseTransitionUrl(clusterId, "precheck") |
| 133 | + req := path.Request(http.MethodPost) |
| 134 | + var res string |
| 135 | + return res, c.Do(context.Background(), req, &res) |
| 136 | +} |
| 137 | + |
| 138 | +// Import imports the provided configuration |
| 139 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration |
| 140 | +func (c *Manager) Import(clusterId string, spec ImportSpec) (string, error) { |
| 141 | + path := c.getBaseTransitionUrl(clusterId, "importConfig") |
| 142 | + req := path.Request(http.MethodPost, spec) |
| 143 | + var res string |
| 144 | + return res, c.Do(context.Background(), req, &res) |
| 145 | +} |
| 146 | + |
| 147 | +func (c *Manager) getBaseTransitionUrl(clusterId, action string) *rest.Resource { |
| 148 | + return c.Resource(fmt.Sprintf(BasePath, clusterId)).WithParam("action", action).WithParam("vmw-task", "true") |
| 149 | +} |
0 commit comments