github.com/vmware/govmomi@v0.43.0/vapi/vcenter/consumptiondomains/zones/zones.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 zones
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"github.com/vmware/govmomi/vapi/rest"
    24  )
    25  
    26  const (
    27  	basePath = "/api/vcenter/consumption-domains/zones"
    28  )
    29  
    30  // Manager extends rest.Client, adding vSphere Zone related methods.
    31  type Manager struct {
    32  	*rest.Client
    33  }
    34  
    35  // NewManager creates a new Manager instance with the given client.
    36  func NewManager(client *rest.Client) *Manager {
    37  	return &Manager{
    38  		Client: client,
    39  	}
    40  }
    41  
    42  // CreateSpec
    43  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_CreateSpec
    44  type CreateSpec struct {
    45  	Zone        string `json:"zone"`
    46  	Description string `json:"description"`
    47  }
    48  
    49  // ZoneInfo
    50  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_Info
    51  type ZoneInfo struct {
    52  	Description string `json:"description"`
    53  }
    54  
    55  // ListItem
    56  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_ListItem
    57  type ListItem struct {
    58  	Zone string   `json:"zone"`
    59  	Info ZoneInfo `json:"info"`
    60  }
    61  
    62  // ListResult
    63  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_ListResult
    64  type ListResult struct {
    65  	Items []ListItem `json:"items"`
    66  }
    67  
    68  // CreateZone creates a vSphere Zone. Returns the identifier of the new zone and an error if the operation fails
    69  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/post
    70  func (c *Manager) CreateZone(spec CreateSpec) (string, error) {
    71  	path := c.Resource(basePath)
    72  	req := path.Request(http.MethodPost, spec)
    73  	var res string
    74  	return res, c.Do(context.Background(), req, &res)
    75  }
    76  
    77  // DeleteZone deletes a vSphere Zone
    78  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/zone/delete
    79  func (c *Manager) DeleteZone(zone string) error {
    80  	path := c.Resource(basePath).WithSubpath(zone)
    81  	req := path.Request(http.MethodDelete)
    82  	return c.Do(context.Background(), req, nil)
    83  }
    84  
    85  // GetZone returns the details of a vSphere Zone
    86  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/zone/get
    87  func (c *Manager) GetZone(zone string) (ZoneInfo, error) {
    88  	path := c.Resource(basePath).WithSubpath(zone)
    89  	req := path.Request(http.MethodGet)
    90  	var res ZoneInfo
    91  	return res, c.Do(context.Background(), req, &res)
    92  }
    93  
    94  // ListZones returns the details of all vSphere Zones
    95  // https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/get
    96  func (c *Manager) ListZones() ([]ListItem, error) {
    97  	path := c.Resource(basePath)
    98  	req := path.Request(http.MethodGet)
    99  	var res ListResult
   100  
   101  	if err := c.Do(context.Background(), req, &res); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	return res.Items, nil
   106  }