|
| 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 drafts |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + "github.com/vmware/govmomi/vapi/esx/settings/clusters/configuration" |
| 13 | + "github.com/vmware/govmomi/vapi/rest" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // BasePath The base endpoint for the clusters configuration API |
| 18 | + BasePath = configuration.BasePath + "/drafts" |
| 19 | + DraftPath = BasePath + "/%s" |
| 20 | +) |
| 21 | + |
| 22 | +// Manager extends rest.Client, adding cluster configuration drafts related methods. |
| 23 | +type Manager struct { |
| 24 | + *rest.Client |
| 25 | +} |
| 26 | + |
| 27 | +// NewManager creates a new Manager instance with the given client. |
| 28 | +func NewManager(client *rest.Client) *Manager { |
| 29 | + return &Manager{ |
| 30 | + Client: client, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type Draft struct { |
| 35 | + ID string `json:"id"` |
| 36 | + State string `json:"state"` |
| 37 | +} |
| 38 | + |
| 39 | +type CreateSpec struct { |
| 40 | + Config string `json:"config"` |
| 41 | + ReferenceHost string `json:"reference_host"` |
| 42 | +} |
| 43 | + |
| 44 | +type ApplySpec struct { |
| 45 | + PolicySpec ApplyPolicySpec `json:"apply_policy_spec"` |
| 46 | +} |
| 47 | + |
| 48 | +type ApplyPolicySpec struct { |
| 49 | + FailureAction *FailureAction `json:"failure_action"` |
| 50 | + PreRemediationPowerAction *string `json:"pre_remediation_power_action"` |
| 51 | + EnableQuickBoot *bool `json:"enable_quick_boot"` |
| 52 | + DisableDpm *bool `json:"disable_dpm"` |
| 53 | + DisableHac *bool `json:"disable_hac"` |
| 54 | + EvacuateOfflineVms *bool `json:"evacuate_offline_vms"` |
| 55 | + EnforceHclValidation *bool `json:"enforce_hcl_validation"` |
| 56 | + EnforceQuickPatch *bool `json:"enforce_quick_patch"` |
| 57 | + ParallelRemediationAction *ParallelRemediationAction `json:"parallel_remediation_action"` |
| 58 | + ConfigManagerPolicySpec *ConfigManagerPolicySpec `json:"config_manager_policy_spec"` |
| 59 | +} |
| 60 | + |
| 61 | +type FailureAction struct { |
| 62 | + Action string `json:"action"` |
| 63 | + RetryDelay int `json:"retry_delay"` |
| 64 | + RetryCount int `json:"retry_count"` |
| 65 | +} |
| 66 | + |
| 67 | +type ParallelRemediationAction struct { |
| 68 | + Enabled bool `json:"enabled"` |
| 69 | + MaxHosts int `json:"max_hosts"` |
| 70 | +} |
| 71 | + |
| 72 | +type ConfigManagerPolicySpec struct { |
| 73 | + SerialRemediation bool `json:"serial_remediation"` |
| 74 | +} |
| 75 | + |
| 76 | +type UpdateSpec struct { |
| 77 | + Config string `json:"config"` |
| 78 | +} |
| 79 | + |
| 80 | +type ImportSpec struct { |
| 81 | + Host string `json:"host"` |
| 82 | +} |
| 83 | + |
| 84 | +// ListDrafts returns all active drafts |
| 85 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 86 | +func (c *Manager) ListDrafts(clusterId string) (map[string]Draft, error) { |
| 87 | + path := c.Resource(fmt.Sprintf(BasePath, clusterId)) |
| 88 | + req := path.Request(http.MethodGet) |
| 89 | + var res map[string]Draft |
| 90 | + return res, c.Do(context.Background(), req, &res) |
| 91 | +} |
| 92 | + |
| 93 | +// GetDraft returns a draft by its ID |
| 94 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 95 | +func (c *Manager) GetDraft(clusterId, draftId string) (Draft, error) { |
| 96 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)) |
| 97 | + req := path.Request(http.MethodGet) |
| 98 | + var res Draft |
| 99 | + return res, c.Do(context.Background(), req, &res) |
| 100 | +} |
| 101 | + |
| 102 | +// DeleteDraft deletes a draft |
| 103 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 104 | +func (c *Manager) DeleteDraft(clusterId, draftId string) (Draft, error) { |
| 105 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)) |
| 106 | + req := path.Request(http.MethodDelete) |
| 107 | + var res Draft |
| 108 | + return res, c.Do(context.Background(), req, &res) |
| 109 | +} |
| 110 | + |
| 111 | +// CreateDraft creates a draft with the provided configuration |
| 112 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 113 | +func (c *Manager) CreateDraft(clusterId string, spec CreateSpec) error { |
| 114 | + path := c.Resource(fmt.Sprintf(BasePath, clusterId)) |
| 115 | + req := path.Request(http.MethodPost, spec) |
| 116 | + return c.Do(context.Background(), req, nil) |
| 117 | +} |
| 118 | + |
| 119 | +// ApplyDraft commits the draft with the specified ID |
| 120 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 121 | +func (c *Manager) ApplyDraft(clusterId, draftId string) error { |
| 122 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)).WithAction("apply") |
| 123 | + req := path.Request(http.MethodPost) |
| 124 | + return c.Do(context.Background(), req, nil) |
| 125 | +} |
| 126 | + |
| 127 | +// UpdateDraft updates the configuration of the draft with the specified ID |
| 128 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 129 | +func (c *Manager) UpdateDraft(clusterId, draftId string, spec UpdateSpec) error { |
| 130 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)).WithAction("update") |
| 131 | + req := path.Request(http.MethodPost, spec) |
| 132 | + return c.Do(context.Background(), req, nil) |
| 133 | +} |
| 134 | + |
| 135 | +// ImportFromHost sets a reference host to use as the source for the draft configuration |
| 136 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 137 | +func (c *Manager) ImportFromHost(clusterId, draftId string, spec ImportSpec) (string, error) { |
| 138 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)).WithAction("importFromHost").WithParam("vmw-task", "true") |
| 139 | + req := path.Request(http.MethodPost, spec) |
| 140 | + var res string |
| 141 | + return res, c.Do(context.Background(), req, &res) |
| 142 | +} |
| 143 | + |
| 144 | +// Precheck runs pre-checks for the provided draft on the specified cluster |
| 145 | +// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/api/esx/settings/clusters/cluster/configuration/drafts |
| 146 | +func (c *Manager) Precheck(clusterId, draftId string) (string, error) { |
| 147 | + path := c.Resource(fmt.Sprintf(DraftPath, clusterId, draftId)).WithAction("precheck").WithParam("vmw-task", "true") |
| 148 | + req := path.Request(http.MethodPost) |
| 149 | + var res string |
| 150 | + return res, c.Do(context.Background(), req, &res) |
| 151 | +} |
0 commit comments