Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api bump from v33.0 to v35.0 #434

Merged
merged 19 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changes/v2.15.0/434-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add environment variable `GOVCD_API_VERSION` so API version can be set manually [GH-434]
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions .changes/v2.15.0/434-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Bump Default API Version to V35.0 [GH-434]
2 changes: 2 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ While running tests, the following environment variables can be used:
* `VCD_TOKEN` : specifies the authorization token to use instead of username/password
(Use `./scripts/get_token.sh` to retrieve one)
* `GOVCD_KEEP_TEST_OBJECTS` will skip deletion of objects created during tests.
* `GOVCD_API_VERSION` allows to select the API version to use. This must be used **for testing purposes only** as the SDK
has been tested to use certain version of the API. Using this environment variable may lead to unexpected failures.

When both the environment variable and the command line option are possible, the environment variable gets evaluated first.

Expand Down
18 changes: 16 additions & 2 deletions govcd/api_vcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package govcd
import (
"crypto/tls"
"fmt"
semver "github.com/hashicorp/go-version"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -102,10 +104,22 @@ func (vcdClient *VCDClient) vcdCloudApiAuthorize(user, pass, org string) (*http.
// NewVCDClient initializes VMware vCloud Director client with reasonable defaults.
// It accepts functions of type VCDClientOption for adjusting defaults.
func NewVCDClient(vcdEndpoint url.URL, insecure bool, options ...VCDClientOption) *VCDClient {
minVcdApiVersion := "35.0" // supported by 10.2+
userDefinedApiVersion := os.Getenv("GOVCD_API_VERSION")
if userDefinedApiVersion != "" {
_, err := semver.NewVersion(userDefinedApiVersion)
if err != nil {
// We do not have error in return of this function signature.
// To avoid breaking API the only thing we can do is panic.
panic(fmt.Sprintf("unable to initialize VCD client from environment variable GOVCD_API_VERSION. Version '%s' is not valid: %s", userDefinedApiVersion, err))
}
minVcdApiVersion = userDefinedApiVersion
}

// Setting defaults
vcdClient := &VCDClient{
Client: Client{
APIVersion: "33.0", // supported by 10.0+
APIVersion: minVcdApiVersion, // supported by 10.2+
// UserAgent cannot embed exact version by default because this is source code and is supposed to be used by programs,
// but any client can customize or disable it at all using WithHttpUserAgent() configuration options function.
UserAgent: "go-vcloud-director",
Expand All @@ -130,7 +144,7 @@ func NewVCDClient(vcdEndpoint url.URL, insecure bool, options ...VCDClientOption
if err != nil {
// We do not have error in return of this function signature.
// To avoid breaking API the only thing we can do is panic.
panic(fmt.Sprintf("unable to initialize vCD client: %s", err))
panic(fmt.Sprintf("unable to initialize VCD client: %s", err))
}
}
return vcdClient
Expand Down
66 changes: 2 additions & 64 deletions govcd/api_vcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ func (vcd *TestVCD) SetUpSuite(check *C) {
// creates a new VApp for vapp tests
if !skipVappCreation && config.VCD.Network.Net1 != "" && config.VCD.StorageProfile.SP1 != "" &&
config.VCD.Catalog.Name != "" && config.VCD.Catalog.CatalogItem != "" {
vcd.vapp, err = vcd.createTestVapp(TestSetUpSuite)
// deployVappForTest replaces the old createTestVapp() because it was using bad implemented method vdc.ComposeVApp
vcd.vapp, err = deployVappForTest(vcd, TestSetUpSuite)
// If no vApp is created, we skip all vApp tests
if err != nil {
fmt.Printf("%s\n", err)
Expand Down Expand Up @@ -1611,69 +1612,6 @@ func TestVCDClient_Authenticate(t *testing.T) {
}
}

func (vcd *TestVCD) createTestVapp(name string) (*VApp, error) {
// ========================= issue#252 ==================================
// TODO: To be enabled when issue#252 is resolved.
// Allows re-using a pre-created vApp
// existingVapp, err := vcd.vdc.GetVAppByName(name, false)
// if err == nil {
// fmt.Printf("vApp %s already exists. Skipping creation\n",name)
// return existingVapp, nil
// }
// ======================================================================
// Populate OrgVDCNetwork
var networks []*types.OrgVDCNetwork
net, err := vcd.vdc.GetOrgVdcNetworkByName(vcd.config.VCD.Network.Net1, false)
if err != nil {
return nil, fmt.Errorf("error finding network : %s, err: %s", vcd.config.VCD.Network.Net1, err)
}
networks = append(networks, net.OrgVDCNetwork)
// Populate Catalog
cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
if err != nil || cat == nil {
return nil, fmt.Errorf("error finding catalog : %s", err)
}
// Populate Catalog Item
catitem, err := cat.GetCatalogItemByName(vcd.config.VCD.Catalog.CatalogItem, false)
if err != nil {
return nil, fmt.Errorf("error finding catalog item : %s", err)
}
// Get VAppTemplate
vAppTemplate, err := catitem.GetVAppTemplate()
if err != nil {
return nil, fmt.Errorf("error finding vapptemplate : %s", err)
}
// Get StorageProfileReference
storageProfileRef, err := vcd.vdc.FindStorageProfileReference(vcd.config.VCD.StorageProfile.SP1)
if err != nil {
return nil, fmt.Errorf("error finding storage profile: %s", err)
}
// Compose VApp
task, err := vcd.vdc.ComposeVApp(networks, vAppTemplate, storageProfileRef, name, "description", true)
if err != nil {
return nil, fmt.Errorf("error composing vapp: %s", err)
}
// After a successful creation, the entity is added to the cleanup list.
// If something fails after this point, the entity will be removed
AddToCleanupList(name, "vapp", "", "createTestVapp")
err = task.WaitTaskCompletion()
if err != nil {
return nil, fmt.Errorf("error composing vapp: %s", err)
}
// Get VApp
vapp, err := vcd.vdc.GetVAppByName(name, true)
if err != nil {
return nil, fmt.Errorf("error getting vapp: %s", err)
}

err = vapp.BlockWhileStatus("UNRESOLVED", vapp.client.MaxRetryTimeout)
if err != nil {
return nil, fmt.Errorf("error waiting for created test vApp to have working state: %s", err)
}

return vapp, err
}

func Test_splitParent(t *testing.T) {
type args struct {
parent string
Expand Down
50 changes: 36 additions & 14 deletions govcd/common_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build api || auth || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || role || nsxv || nsxt || openapi || affinity || search || ALL
// +build api auth functional catalog vapp gateway network org query extnetwork task vm vdc system disk lb lbAppRule lbAppProfile lbServerPool lbServiceMonitor lbVirtualServer user role nsxv nsxt openapi affinity search ALL
//go:build api || auth || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || role || nsxv || nsxt || openapi || affinity || search || alb || certificate || vdcGroup || metadata || ALL
// +build api auth functional catalog vapp gateway network org query extnetwork task vm vdc system disk lb lbAppRule lbAppProfile lbServerPool lbServiceMonitor lbVirtualServer user role nsxv nsxt openapi affinity search alb certificate vdcGroup metadata ALL

/*
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
Expand Down Expand Up @@ -261,15 +261,14 @@ func isTcpPortOpen(host, port string, timeout int) bool {

}

// moved from vapp_test.go
func createVappForTest(vcd *TestVCD, vappName string) (*VApp, error) {
// deployVappForTest aims to replace createVappForTest
func deployVappForTest(vcd *TestVCD, vappName string) (*VApp, error) {
// Populate OrgVDCNetwork
var networks []*types.OrgVDCNetwork
net, err := vcd.vdc.GetOrgVdcNetworkByName(vcd.config.VCD.Network.Net1, false)
if err != nil {
return nil, fmt.Errorf("error finding network : %s", err)
}
networks = append(networks, net.OrgVDCNetwork)

// Populate Catalog
Didainius marked this conversation as resolved.
Show resolved Hide resolved
cat, err := vcd.org.GetCatalogByName(vcd.config.VCD.Catalog.Name, false)
if err != nil || cat == nil {
Expand All @@ -290,22 +289,45 @@ func createVappForTest(vcd *TestVCD, vappName string) (*VApp, error) {
if err != nil {
return nil, fmt.Errorf("error finding storage profile: %s", err)
}
// Compose VApp
task, err := vcd.vdc.ComposeVApp(networks, vAppTemplate, storageProfileRef, vappName, "description", true)

// Create empty vApp
vapp, err := vcd.vdc.CreateRawVApp(vappName, "description")
if err != nil {
return nil, fmt.Errorf("error composing vapp: %s", err)
return nil, fmt.Errorf("error creating vapp: %s", err)
}

// After a successful creation, the entity is added to the cleanup list.
// If something fails after this point, the entity will be removed
AddToCleanupList(vappName, "vapp", "", "createTestVapp")
err = task.WaitTaskCompletion()

// Create vApp networking
vAppNetworkConfig, err := vapp.AddOrgNetwork(&VappNetworkSettings{}, net.OrgVDCNetwork, false)
if err != nil {
return nil, fmt.Errorf("error creating vApp network. %s", err)
}

// Create VM with only one NIC connected to vapp_net
networkConnectionSection := &types.NetworkConnectionSection{
PrimaryNetworkConnectionIndex: 0,
}

netConn := &types.NetworkConnection{
Network: vAppNetworkConfig.NetworkConfig[0].NetworkName,
IsConnected: true,
NetworkConnectionIndex: 0,
IPAddressAllocationMode: types.IPAllocationModePool,
}

networkConnectionSection.NetworkConnection = append(networkConnectionSection.NetworkConnection, netConn)

task, err := vapp.AddNewVMWithStorageProfile("test_vm", vAppTemplate, networkConnectionSection, &storageProfileRef, true)
if err != nil {
return nil, fmt.Errorf("error composing vapp: %s", err)
return nil, fmt.Errorf("error creating the VM: %s", err)
}
// Get VApp
vapp, err := vcd.vdc.GetVAppByName(vappName, true)

err = task.WaitTaskCompletion()
if err != nil {
return nil, fmt.Errorf("error getting vapp: %s", err)
return nil, fmt.Errorf("error while waiting for the VM to be created %s", err)
}

err = vapp.BlockWhileStatus("UNRESOLVED", vapp.client.MaxRetryTimeout)
Expand Down
4 changes: 3 additions & 1 deletion govcd/nsxt_firewall_group_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func createNsxtRoutedNetwork(check *C, vcd *TestVCD, vdc *Vdc, edgeGatewayId str
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName() + "routed-net",
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeRouted,

Expand Down
12 changes: 12 additions & 0 deletions govcd/openapi_org_network_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ func (vdc *Vdc) UpdateOpenApiOrgVdcNetworkDhcp(orgNetworkId string, orgVdcNetwor
client: vdc.client,
}

// From v35.0 onwards, if orgVdcNetworkDhcpConfig.LeaseTime or orgVdcNetworkDhcpConfig.Mode are not explicitly
// passed, the API doesn't use any defaults returning an error. Previous API versions were setting
// LeaseTime to 86400 seconds and Mode to EDGE if these values were not supplied. These two conditional
// address the situation.
if orgVdcNetworkDhcpConfig.LeaseTime == nil {
orgVdcNetworkDhcpConfig.LeaseTime = takeIntAddress(86400)
}

if len(orgVdcNetworkDhcpConfig.Mode) == 0 {
orgVdcNetworkDhcpConfig.Mode = "EDGE"
}

err = vdc.client.OpenApiPutItem(minimumApiVersion, urlRef, nil, orgVdcNetworkDhcpConfig, orgNetDhcpResponse.OpenApiOrgVdcNetworkDhcp, nil)
if err != nil {
return nil, fmt.Errorf("error updating Org VDC network DHCP configuration: %s", err)
Expand Down
24 changes: 18 additions & 6 deletions govcd/openapi_org_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkIsolated(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeIsolated,
Subnets: types.OrgVdcNetworkSubnets{
Expand Down Expand Up @@ -65,7 +67,9 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkRouted(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeRouted,

Expand Down Expand Up @@ -130,7 +134,9 @@ func (vcd *TestVCD) Test_NsxtOrgVdcNetworkImported(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.nsxtVdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeOpaque,
// BackingNetworkId contains NSX-T logical switch ID for Imported networks
Expand Down Expand Up @@ -170,7 +176,9 @@ func (vcd *TestVCD) Test_NsxvOrgVdcNetworkIsolated(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeIsolated,
Subnets: types.OrgVdcNetworkSubnets{
Expand Down Expand Up @@ -213,7 +221,9 @@ func (vcd *TestVCD) Test_NsxvOrgVdcNetworkRouted(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeRouted,

Expand Down Expand Up @@ -275,7 +285,9 @@ func (vcd *TestVCD) Test_NsxvOrgVdcNetworkDirect(check *C) {
orgVdcNetworkConfig := &types.OpenApiOrgVdcNetwork{
Name: check.TestName(),
Description: check.TestName() + "-description",
OrgVdc: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

// On v35.0 orgVdc is not supported anymore. Using ownerRef instead.
OwnerRef: &types.OpenApiReference{ID: vcd.vdc.Vdc.ID},

NetworkType: types.OrgVdcNetworkTypeDirect,
ParentNetwork: &types.OpenApiReference{ID: externalNetwork.ExternalNetwork.ID},
Expand Down
2 changes: 1 addition & 1 deletion govcd/vapp_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (vcd *TestVCD) Test_UpdateNetworkFirewallRules(check *C) {
func (vcd *TestVCD) prepareVappWithVappNetwork(check *C, vappName, orgVdcNetworkName string) (*VApp, string, *types.NetworkConfigSection, error) {
fmt.Printf("Running: %s\n", check.TestName())

vapp, err := createVappForTest(vcd, vappName)
vapp, err := deployVappForTest(vcd, vappName)
check.Assert(err, IsNil)
check.Assert(vapp, NotNil)

Expand Down
Loading