github.com/vmware/govmomi@v0.51.0/vapi/vcenter/consumptiondomains/associations/associations.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 associations
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"fmt"
    11  	"net/http"
    12  
    13  	"github.com/vmware/govmomi/vapi/rest"
    14  )
    15  
    16  const (
    17  	basePath         = "/api/vcenter/consumption-domains/zones/cluster"
    18  	associationsPath = basePath + "/%s/associations"
    19  )
    20  
    21  // Manager extends rest.Client, adding vSphere Zone association 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  // Status
    34  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_Cluster_Associations_Status
    35  type Status struct {
    36  	Success bool `json:"success"`
    37  }
    38  
    39  // AddAssociations
    40  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=add/post
    41  func (c *Manager) AddAssociations(zone string, cluster string) error {
    42  	path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "add")
    43  	req := path.Request(http.MethodPost, []string{cluster})
    44  	var res Status
    45  	if err := c.Do(context.Background(), req, &res); err != nil {
    46  		return err
    47  	}
    48  
    49  	// This endpoint does not always return and error upon failure.
    50  	// In such cases we need to parse the response to figure out the actual status.
    51  
    52  	if !res.Success {
    53  		return errors.New("unable to add associations")
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  // RemoveAssociations
    60  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=remove/post/
    61  func (c *Manager) RemoveAssociations(zone string, cluster string) error {
    62  	path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "remove")
    63  	req := path.Request(http.MethodPost, []string{cluster})
    64  	return c.Do(context.Background(), req, nil)
    65  }
    66  
    67  // GetAssociations
    68  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations/get/
    69  func (c *Manager) GetAssociations(zone string) ([]string, error) {
    70  	path := c.Resource(fmt.Sprintf(associationsPath, zone))
    71  	req := path.Request(http.MethodGet)
    72  	var res []string
    73  	return res, c.Do(context.Background(), req, &res)
    74  }