github.com/vmware/govmomi@v0.43.0/vapi/esx/settings/clusters/clusters.go (about) 1 /* 2 Copyright (c) 2024-2024 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 clusters 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 "strings" 24 25 "github.com/vmware/govmomi/vapi/rest" 26 ) 27 28 const ( 29 basePath = "/api/esx/settings" 30 // SoftwareDraftsPath The endpoint for the software drafts API 31 SoftwareDraftsPath = basePath + "/clusters/%s/software/drafts" 32 // SoftwareComponentsPath The endpoint for retrieving the custom components in a software draft 33 SoftwareComponentsPath = SoftwareDraftsPath + "/%s/software/components" 34 // BaseImagePath The endpoint for retrieving the base image of a software draft 35 BaseImagePath = SoftwareDraftsPath + "/%s/software/base-image" 36 // SoftwareEnablementPath The endpoint for retrieving the vLCM status (enabled/disabled) of a cluster 37 SoftwareEnablementPath = basePath + "/clusters/%s/enablement/software" 38 ) 39 40 // Manager extends rest.Client, adding Software Drafts related methods. 41 type Manager struct { 42 *rest.Client 43 } 44 45 // NewManager creates a new Manager instance with the given client. 46 func NewManager(client *rest.Client) *Manager { 47 return &Manager{ 48 Client: client, 49 } 50 } 51 52 // SettingsClustersSoftwareDraftsMetadata is a type mapping for 53 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Software/Drafts/Metadata/ 54 type SettingsClustersSoftwareDraftsMetadata struct { 55 CreationTime string `json:"creation_time"` 56 Owner string `json:"owner"` 57 Status string `json:"status"` 58 } 59 60 // SettingsBaseImageDetails is a type mapping for 61 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/BaseImageDetails/ 62 type SettingsBaseImageDetails struct { 63 DisplayName string `json:"display_name"` 64 DisplayVersion string `json:"display_version"` 65 ReleaseDate string `json:"release_date"` 66 } 67 68 // SettingsBaseImageInfo is a type mapping for 69 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/BaseImageInfo/ 70 type SettingsBaseImageInfo struct { 71 Version string `json:"version"` 72 Details SettingsBaseImageDetails `json:"details"` 73 } 74 75 // SettingsComponentDetails is a type mapping for 76 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/ComponentDetails/ 77 type SettingsComponentDetails struct { 78 DisplayName string `json:"display_name"` 79 Vendor string `json:"vendor"` 80 } 81 82 // SettingsComponentInfo is a type mapping for 83 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/ComponentInfo/ 84 type SettingsComponentInfo struct { 85 Version string `json:"version"` 86 Details SettingsComponentDetails `json:"details"` 87 } 88 89 // SettingsSolutionComponentSpec is a type mapping for 90 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/SolutionComponentSpec/ 91 type SettingsSolutionComponentSpec struct { 92 Component string `json:"component"` 93 } 94 95 // SettingsSolutionComponentDetails is a type mapping for 96 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/SolutionComponentDetails/ 97 type SettingsSolutionComponentDetails struct { 98 Component string `json:"component"` 99 DisplayName string `json:"display_name"` 100 Vendor string `json:"vendor"` 101 DisplayVersion string `json:"display_version,omitempty"` 102 } 103 104 // SettingsSolutionDetails is a type mapping for 105 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/SolutionDetails/ 106 type SettingsSolutionDetails struct { 107 DisplayName string `json:"display_name"` 108 DisplayVersion string `json:"display_version"` 109 } 110 111 // SettingsSolutionInfo is a type mapping for 112 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/SolutionInfo/ 113 type SettingsSolutionInfo struct { 114 Version string `json:"version"` 115 Components []SettingsSolutionComponentSpec `json:"components"` 116 Details SettingsSolutionDetails `json:"details"` 117 } 118 119 // SettingsAddOnDetails is a type mapping for 120 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/AddOnDetails/ 121 type SettingsAddOnDetails struct { 122 DisplayName string `json:"display_name"` 123 DisplayVersion string `json:"display_version"` 124 Vendor string `json:"vendor"` 125 } 126 127 // SettingsAddOnInfo is a type mapping for 128 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/AddOnInfo/ 129 type SettingsAddOnInfo struct { 130 Name string `json:"name"` 131 Version string `json:"version"` 132 Details SettingsAddOnDetails `json:"details,omitempty"` 133 } 134 135 // SettingsHardwareSupportPackageInfo is a type mapping for 136 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/HardwareSupportPackageInfo/ 137 type SettingsHardwareSupportPackageInfo struct { 138 Pkg string `json:"pkg"` 139 Version string `json:"version"` 140 } 141 142 // SettingsHardwareSupportInfo is a type mapping for 143 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/HardwareSupportInfo/ 144 type SettingsHardwareSupportInfo struct { 145 Packages map[string]SettingsHardwareSupportPackageInfo `json:"packages"` 146 } 147 148 // SettingsSoftwareInfo is a type mapping for 149 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/SoftwareInfo/ 150 type SettingsSoftwareInfo struct { 151 BaseImage SettingsBaseImageInfo `json:"base_image"` 152 Components map[string]SettingsComponentInfo `json:"components"` 153 Solutions map[string]SettingsSolutionInfo `json:"solutions"` 154 AddOn SettingsAddOnInfo `json:"add_on,omitempty"` 155 HardwareSupport SettingsHardwareSupportInfo `json:"hardware_support,omitempty"` 156 } 157 158 // SettingsClustersSoftwareDraftsInfo is a type mapping for 159 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Software/Drafts/Info/ 160 type SettingsClustersSoftwareDraftsInfo struct { 161 Metadata SettingsClustersSoftwareDraftsMetadata `json:"metadata"` 162 Software SettingsSoftwareInfo `json:"software"` 163 } 164 165 // SoftwareComponentsUpdateSpec is a type mapping for 166 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Software/Drafts/Software/Components/UpdateSpec/ 167 type SoftwareComponentsUpdateSpec struct { 168 ComponentsToDelete []string `json:"components_to_delete,omitempty"` 169 ComponentsToSet map[string]string `json:"components_to_set,omitempty"` 170 } 171 172 // SettingsClustersSoftwareDraftsCommitSpec is a type mapping for 173 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Software/Drafts/CommitSpec/ 174 type SettingsClustersSoftwareDraftsCommitSpec struct { 175 Message string `json:"message,omitempty"` 176 } 177 178 // SettingsBaseImageSpec is a type mapping for 179 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/BaseImageSpec/ 180 type SettingsBaseImageSpec struct { 181 Version string `json:"version"` 182 } 183 184 // EnableSoftwareManagementSpec is a type mapping for 185 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Enablement/Software/EnableSpec/ 186 type EnableSoftwareManagementSpec struct { 187 SkipSoftwareCheck bool `json:"skip_software_check"` 188 } 189 190 // SoftwareManagementInfo is a type mapping for 191 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/data-structures/Settings/Clusters/Enablement/Software/Info/ 192 type SoftwareManagementInfo struct { 193 Enabled bool `json:"enabled"` 194 } 195 196 // ListSoftwareDrafts retrieves the software drafts for a cluster 197 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/get/ 198 func (c *Manager) ListSoftwareDrafts(clusterId string, owners *[]string) (map[string]SettingsClustersSoftwareDraftsMetadata, error) { 199 path := c.Resource(fmt.Sprintf(SoftwareDraftsPath, clusterId)) 200 201 if owners != nil && len(*owners) > 0 { 202 path = path.WithParam("owners", strings.Join(*owners, ",")) 203 } 204 205 req := path.Request(http.MethodGet) 206 var res map[string]SettingsClustersSoftwareDraftsMetadata 207 return res, c.Do(context.Background(), req, &res) 208 } 209 210 // CreateSoftwareDraft creates a software draft on the provided cluster 211 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/post/ 212 func (c *Manager) CreateSoftwareDraft(clusterId string) (string, error) { 213 path := c.Resource(fmt.Sprintf(SoftwareDraftsPath, clusterId)) 214 req := path.Request(http.MethodPost) 215 var res string 216 return res, c.Do(context.Background(), req, &res) 217 } 218 219 // DeleteSoftwareDraft removes the specified draft 220 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/delete/ 221 func (c *Manager) DeleteSoftwareDraft(clusterId, draftId string) error { 222 path := c.Resource(fmt.Sprintf(SoftwareDraftsPath, clusterId)).WithSubpath(draftId) 223 req := path.Request(http.MethodDelete) 224 return c.Do(context.Background(), req, nil) 225 } 226 227 // GetSoftwareDraft returns the set of components in the specified draft 228 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/get/ 229 func (c *Manager) GetSoftwareDraft(clusterId, draftId string) (SettingsClustersSoftwareDraftsInfo, error) { 230 path := c.Resource(fmt.Sprintf(SoftwareDraftsPath, clusterId)).WithSubpath(draftId) 231 req := path.Request(http.MethodGet) 232 var res SettingsClustersSoftwareDraftsInfo 233 return res, c.Do(context.Background(), req, &res) 234 } 235 236 // CommitSoftwareDraft closes and applies the specified draft 237 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draftactioncommitvmw-tasktrue/post/ 238 func (c *Manager) CommitSoftwareDraft(clusterId, draftId string, spec SettingsClustersSoftwareDraftsCommitSpec) (string, error) { 239 path := c.Resource(fmt.Sprintf(SoftwareDraftsPath, clusterId)).WithSubpath(draftId).WithParam("action", "commit").WithParam("vmw-task", "true") 240 req := path.Request(http.MethodPost, spec) 241 var res string 242 return res, c.Do(context.Background(), req, &res) 243 } 244 245 // ListSoftwareDraftComponents returns all components in the specified draft 246 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/software/components/get/ 247 func (c *Manager) ListSoftwareDraftComponents(clusterId, draftId string) (map[string]SettingsComponentInfo, error) { 248 path := c.Resource(fmt.Sprintf(SoftwareComponentsPath, clusterId, draftId)) 249 req := path.Request(http.MethodGet) 250 var res map[string]SettingsComponentInfo 251 return res, c.Do(context.Background(), req, &res) 252 } 253 254 // GetSoftwareDraftComponent returns a component from the specified draft 255 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/software/components/component/get/ 256 func (c *Manager) GetSoftwareDraftComponent(clusterId, draftId, component string) (SettingsComponentInfo, error) { 257 path := c.Resource(fmt.Sprintf(SoftwareComponentsPath, clusterId, draftId)).WithSubpath(component) 258 req := path.Request(http.MethodGet) 259 var res SettingsComponentInfo 260 return res, c.Do(context.Background(), req, &res) 261 } 262 263 // UpdateSoftwareDraftComponents updates the set of components in the specified draft 264 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/software/components/patch/ 265 func (c *Manager) UpdateSoftwareDraftComponents(clusterId, draftId string, spec SoftwareComponentsUpdateSpec) error { 266 path := c.Resource(fmt.Sprintf(SoftwareComponentsPath, clusterId, draftId)) 267 req := path.Request(http.MethodPatch, spec) 268 return c.Do(context.Background(), req, nil) 269 } 270 271 // RemoveSoftwareDraftComponents removes a component from the specified draft 272 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/software/components/component/delete/ 273 func (c *Manager) RemoveSoftwareDraftComponents(clusterId, draftId, component string) error { 274 path := c.Resource(fmt.Sprintf(SoftwareComponentsPath, clusterId, draftId)).WithSubpath(component) 275 req := path.Request(http.MethodDelete) 276 return c.Do(context.Background(), req, nil) 277 } 278 279 // GetSoftwareDraftBaseImage retrieves the ESXi image version on the specified draft 280 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/software/drafts/draft/software/base-image/get 281 func (c *Manager) GetSoftwareDraftBaseImage(clusterId, draftId string) (SettingsBaseImageInfo, error) { 282 path := c.Resource(fmt.Sprintf(BaseImagePath, clusterId, draftId)) 283 req := path.Request(http.MethodGet) 284 var res SettingsBaseImageInfo 285 return res, c.Do(context.Background(), req, &res) 286 } 287 288 // SetSoftwareDraftBaseImage sets the ESXi image version on the specified draft 289 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/settings/clusters.software.drafts.software.base_image/put 290 func (c *Manager) SetSoftwareDraftBaseImage(clusterId, draftId, version string) error { 291 path := c.Resource(fmt.Sprintf(BaseImagePath, clusterId, draftId)) 292 req := path.Request(http.MethodPut, SettingsBaseImageSpec{Version: version}) 293 return c.Do(context.Background(), req, nil) 294 } 295 296 // EnableSoftwareManagement enables vLCM on the cluster 297 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/settings/clusters.enablement.software/put 298 func (c *Manager) EnableSoftwareManagement(clusterId string, skipCheck bool) (string, error) { 299 path := c.Resource(fmt.Sprintf(SoftwareEnablementPath, clusterId)).WithParam("vmw-task", "true") 300 req := path.Request(http.MethodPut, EnableSoftwareManagementSpec{SkipSoftwareCheck: skipCheck}) 301 var res string 302 return res, c.Do(context.Background(), req, &res) 303 } 304 305 // GetSoftwareManagement checks whether vLCM is enabled on the cluster 306 // https://developer.vmware.com/apis/vsphere-automation/latest/esx/api/esx/settings/clusters/cluster/enablement/software/get/ 307 func (c *Manager) GetSoftwareManagement(clusterId string) (SoftwareManagementInfo, error) { 308 path := c.Resource(fmt.Sprintf(SoftwareEnablementPath, clusterId)) 309 req := path.Request(http.MethodGet) 310 var res SoftwareManagementInfo 311 return res, c.Do(context.Background(), req, &res) 312 }