github.com/vmware/govmomi@v0.43.0/vapi/vcenter/consumptiondomains/associations/associations.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 associations 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "net/http" 24 25 "github.com/vmware/govmomi/vapi/rest" 26 ) 27 28 const ( 29 basePath = "/api/vcenter/consumption-domains/zones/cluster" 30 associationsPath = basePath + "/%s/associations" 31 ) 32 33 // Manager extends rest.Client, adding vSphere Zone association related methods. 34 type Manager struct { 35 *rest.Client 36 } 37 38 // NewManager creates a new Manager instance with the given client. 39 func NewManager(client *rest.Client) *Manager { 40 return &Manager{ 41 Client: client, 42 } 43 } 44 45 // Status 46 // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_Cluster_Associations_Status 47 type Status struct { 48 Success bool `json:"success"` 49 } 50 51 // AddAssociations 52 // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=add/post 53 func (c *Manager) AddAssociations(zone string, cluster string) error { 54 path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "add") 55 req := path.Request(http.MethodPost, []string{cluster}) 56 var res Status 57 if err := c.Do(context.Background(), req, &res); err != nil { 58 return err 59 } 60 61 // This endpoint does not always return and error upon failure. 62 // In such cases we need to parse the response to figure out the actual status. 63 64 if !res.Success { 65 return errors.New("unable to add associations") 66 } 67 68 return nil 69 } 70 71 // RemoveAssociations 72 // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=remove/post/ 73 func (c *Manager) RemoveAssociations(zone string, cluster string) error { 74 path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "remove") 75 req := path.Request(http.MethodPost, []string{cluster}) 76 return c.Do(context.Background(), req, nil) 77 } 78 79 // GetAssociations 80 // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations/get/ 81 func (c *Manager) GetAssociations(zone string) ([]string, error) { 82 path := c.Resource(fmt.Sprintf(associationsPath, zone)) 83 req := path.Request(http.MethodGet) 84 var res []string 85 return res, c.Do(context.Background(), req, &res) 86 }