Skip to content

Commit

Permalink
feat: vSphere Zones API bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Stoyan Zhelyazkov <[email protected]>
  • Loading branch information
spacegospod committed May 20, 2024
1 parent 218b261 commit 1edfbe3
Show file tree
Hide file tree
Showing 5 changed files with 517 additions and 0 deletions.
86 changes: 86 additions & 0 deletions vapi/vcenter/consumptiondomains/associations/associations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package associations

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/vmware/govmomi/vapi/rest"
)

const (
basePath = "/api/vcenter/consumption-domains/zones/cluster"
associationsPath = basePath + "/%s/associations"
)

// Manager extends rest.Client, adding vSphere Zone association related methods.
type Manager struct {
*rest.Client
}

// NewManager creates a new Manager instance with the given client.
func NewManager(client *rest.Client) *Manager {
return &Manager{
Client: client,
}
}

// Status
// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/data-structures/ConsumptionDomains_Zones_Cluster_Associations_Status
type Status struct {
Success bool `json:"success"`
}

// AddAssociations
// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=add/post
func (c *Manager) AddAssociations(zone string, cluster string) error {
path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "add")
req := path.Request(http.MethodPost, []string{cluster})
var res Status
if err := c.Do(context.Background(), req, &res); err != nil {
return err
}

// This endpoint does not always return and error upon failure.
// In such cases we need to parse the response to figure out the actual status.

if !res.Success {
return errors.New("unable to add associations")
}

return nil
}

// RemoveAssociations
// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations__action=remove/post/
func (c *Manager) RemoveAssociations(zone string, cluster string) error {
path := c.Resource(fmt.Sprintf(associationsPath, zone)).WithParam("action", "remove")
req := path.Request(http.MethodPost, []string{cluster})
return c.Do(context.Background(), req, nil)
}

// GetAssociations
// https://developer.broadcom.com/xapis/vsphere-automation-api/latest/vcenter/api/vcenter/consumption-domains/zones/cluster/zone/associations/get/
func (c *Manager) GetAssociations(zone string) ([]string, error) {
path := c.Resource(fmt.Sprintf(associationsPath, zone))
req := path.Request(http.MethodGet)
var res []string
return res, c.Do(context.Background(), req, &res)
}
133 changes: 133 additions & 0 deletions vapi/vcenter/consumptiondomains/consumptiondomains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package consumptiondomains_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/associations"
"github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/zones"
"github.com/vmware/govmomi/vim25"

_ "github.com/vmware/govmomi/vapi/simulator"
_ "github.com/vmware/govmomi/vapi/vcenter/consumptiondomains/simulator"
)

func TestConsumptionDomains(t *testing.T) {
simulator.Test(func(ctx context.Context, vc *vim25.Client) {
rc := rest.NewClient(vc)

err := rc.Login(ctx, simulator.DefaultLogin)
if err != nil {
t.Fatal(err)
}

zm := zones.NewManager(rc)

// Create a zone
zoneId, err := zm.CreateZone(zones.CreateSpec{
Zone: "test-zone-1",
Description: "placeholder description",
})

if err != nil {
t.Error(err)
}

// List all zones and find the one created earlier
zonesList, err := zm.ListZones()

if err != nil {
t.Error(err)
}

assert.Equal(t, 1, len(zonesList))
assert.Equal(t, "test-zone-1", zonesList[0].Zone)
assert.Equal(t, "placeholder description", zonesList[0].Info.Description)

// Query zone by ID
zone, err := zm.GetZone(zoneId)

if err != nil {
t.Error(err)
}

assert.Equal(t, "placeholder description", zone.Description)

am := associations.NewManager(rc)

// Create a cluster association
err = am.AddAssociations(zoneId, "domain-c9")

if err != nil {
t.Error(err)
}

// Query the associations for the test zone
assc, err := am.GetAssociations(zoneId)

if err != nil {
t.Error(err)
}

assert.Equal(t, 1, len(assc))
assert.Equal(t, "domain-c9", assc[0])

// Delete the cluster association
err = am.RemoveAssociations(zoneId, "domain-c9")

if err != nil {
t.Error(err)
}

// Verify that the association is removed
assc, err = am.GetAssociations(zoneId)

if err != nil {
t.Error(err)
}

assert.Equal(t, 0, len(assc))

// Delete the zone
err = zm.DeleteZone(zoneId)

if err != nil {
t.Error(err)
}

// Verify the zone is removed
zone, err = zm.GetZone(zoneId)

if err == nil {
t.Error(err)
}

zonesList, err = zm.ListZones()

if err != nil {
t.Error(err)
}

assert.Equal(t, 0, len(zonesList))
})
}
Loading

0 comments on commit 1edfbe3

Please sign in to comment.