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