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

Add support for managing VDC group #410

Merged
merged 50 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
d05517b
add main implementation
vbauzys Aug 25, 2020
3faee0a
git push origin master
vbauzys Aug 25, 2020
a497ca1
git push origin master
vbauzys Sep 3, 2020
71d524d
git push origin master
vbauzys Sep 22, 2020
879296d
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Oct 21, 2020
02a4d81
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Oct 21, 2020
b365d6c
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Oct 27, 2020
808752b
removed not needed after merge
vbauzys Oct 29, 2020
29c17ae
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 5, 2020
9073403
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 12, 2020
9efad14
Merge branch 'vmware:master' into master
vbauzys Oct 11, 2021
dabfd82
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 8, 2021
be532a2
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Nov 10, 2021
3b0ef91
Add VDC group implementation and tests
vbauzys Nov 23, 2021
6cf856a
git push origin masterMerge branch 'master' of github.com:vmware/go-v…
vbauzys Nov 23, 2021
a0978f6
Merge branch 'master' into vdc-group
vbauzys Nov 23, 2021
d56a766
remove not needed code
vbauzys Nov 23, 2021
13be693
add tentative test
vbauzys Nov 23, 2021
d787844
Add fix for roles.go
vbauzys Nov 24, 2021
8a13bc8
add tests for org Admin
vbauzys Nov 24, 2021
d04220e
add methods to handle default DFW policy
vbauzys Nov 25, 2021
023bd63
improve test
vbauzys Nov 25, 2021
144ef18
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Nov 25, 2021
5cf4292
Merge branch 'master' into vdc-group
vbauzys Nov 25, 2021
b272e23
Added changelog
vbauzys Nov 25, 2021
e1c7aa8
Made Update more strict
vbauzys Nov 30, 2021
ae46aa3
improve test
vbauzys Dec 2, 2021
ac0e396
add improvment
vbauzys Dec 6, 2021
08471e7
add improvements
vbauzys Dec 8, 2021
8c0217f
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Dec 8, 2021
0cc57de
Merge branch 'master' into vdc-group
vbauzys Dec 8, 2021
285e489
add comments
vbauzys Dec 8, 2021
5c3b4f1
add comments
vbauzys Dec 8, 2021
4e124e7
add comments
vbauzys Dec 8, 2021
b030d62
add fix appeared after refactoring
vbauzys Dec 9, 2021
8f4ebc5
add lost note
vbauzys Dec 9, 2021
2531ea0
add fix reference
vbauzys Dec 10, 2021
1c1bf2d
improve comment
vbauzys Dec 10, 2021
ffd6be1
added improvements after review
vbauzys Dec 10, 2021
9ef6234
added error which returns which VCD were not found as candidate VDCs
vbauzys Dec 14, 2021
380a7c4
small improvement
vbauzys Dec 14, 2021
d47ca40
small fix
vbauzys Dec 14, 2021
5ba6326
add func comment
vbauzys Dec 15, 2021
9ca9513
added review fixes
vbauzys Dec 16, 2021
8041eb1
renamed functions to GetAllVdcGroupCandidates and GetAllNsxtVdcGroupC…
vbauzys Dec 16, 2021
2a3d319
removed changing rights to checking if rights are ready in env. Usual…
vbauzys Dec 17, 2021
b888b60
improve comment
vbauzys Dec 17, 2021
29e6957
improve comment
vbauzys Dec 17, 2021
05ac73e
Merge branch 'master' of github.com:vmware/go-vcloud-director
vbauzys Dec 17, 2021
d08e7c9
Merge branch 'master' into vdc-group
vbauzys Dec 17, 2021
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
2 changes: 2 additions & 0 deletions .changes/v2.14.0/410-features.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Added types `VdcGroup`, `types.VdcGroup`, `types.ParticipatingOrgVdcs`, `types.CandidateVdc`, `types.DfwPolicies` and `types.DefaultPolicy` for handling VDC groups with corresponding
methods `adminOrg.CreateNsxtVdcGroup`, `adminOrg.CreateVdcGroup`, `adminOrg.GetAllNsxtVdcGroupCandidates`, `adminOrg.GetAllVdcGroupCandidates`, `adminOrg.GetAllVdcGroups`, `adminOrg.GetVdcGroupByName`, `adminOrg.GetVdcGroupById`, `vdcGroup.Update`, `vdcGroup.GenericUpdate`, `vdcGroup.Delete`, `vdcGroup.DisableDefaultPolicy`, `vdcGroup.EnableDefaultPolicy`, `vdcGroup.GetDfwPolicies`, `vdcGroup.DeActivateDfw`, `vdcGroup.ActivateDfw`, `vdcGroup.UpdateDefaultDfwPolicies`, `vdcGroup.UpdateDfwPolicies` [GH-410]
2 changes: 2 additions & 0 deletions govcd/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ At least one of the following tags should be defined:
* user: Runs user related tests
* vapp: Runs vapp related tests
* vdc: Runs vdc related tests
* vdcGroup: Runs vdc group related tests
* certificate Runs certificate related tests
* vm: Runs vm related tests

Examples:
Expand Down
4 changes: 2 additions & 2 deletions govcd/api_vcd_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build api || openapi || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || search || nsxv || nsxt || auth || affinity || role || alb || certificate || ALL
// +build api openapi functional catalog vapp gateway network org query extnetwork task vm vdc system disk lb lbAppRule lbAppProfile lbServerPool lbServiceMonitor lbVirtualServer user search nsxv nsxt auth affinity role alb certificate ALL
//go:build api || openapi || functional || catalog || vapp || gateway || network || org || query || extnetwork || task || vm || vdc || system || disk || lb || lbAppRule || lbAppProfile || lbServerPool || lbServiceMonitor || lbVirtualServer || user || search || nsxv || nsxt || auth || affinity || role || alb || certificate || vdcGroup || ALL
// +build api openapi functional catalog vapp gateway network org query extnetwork task vm vdc system disk lb lbAppRule lbAppProfile lbServerPool lbServiceMonitor lbVirtualServer user search nsxv nsxt auth affinity role alb certificate vdcGroup ALL

/*
* Copyright 2021 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
Expand Down
36 changes: 17 additions & 19 deletions govcd/certificate_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ package govcd

import (
"fmt"
"net/url"
"strings"

"github.com/vmware/go-vcloud-director/v2/types/v56"
"net/url"
)

// Certificate is a structure defining a certificate in VCD
Expand Down Expand Up @@ -186,42 +184,42 @@ func (adminOrg *AdminOrg) GetAllCertificatesFromLibrary(queryParameters url.Valu
// https://github.com/golang/go/issues/4013
// https://github.com/czos/goamz/pull/11/files
func getCertificateFromLibraryByName(client *Client, name string, additionalHeader map[string]string) (*Certificate, error) {
var params = url.Values{}

slowSearch := false
versionWithNoBug, err := client.VersionEqualOrGreater("10.3", 3)
slowSearch, params, err := shouldDoSlowSearch("alias", name, client)
if err != nil {
return nil, err
}
if (!versionWithNoBug && (strings.Contains(name, ",") || strings.Contains(name, ";"))) ||
strings.Contains(name, " ") || strings.Contains(name, "+") || strings.Contains(name, "*") {
slowSearch = true
} else {
params.Set("filter", fmt.Sprintf("alias==%s", url.QueryEscape(name)))
params.Set("filterEncoded", "true")
}

var foundCertificates []*Certificate
certificates, err := getAllCertificateFromLibrary(client, params, additionalHeader)
if err != nil {
return nil, err
}
if len(certificates) == 0 {
return nil, ErrorEntityNotFound
}
foundCertificates = append(foundCertificates, certificates[0])

if slowSearch {
foundCertificates = nil
for _, certificate := range certificates {
if certificate.CertificateLibrary.Alias == name {
return certificate, nil
foundCertificates = append(foundCertificates, certificate)
}
}
return nil, ErrorEntityNotFound
if len(foundCertificates) == 0 {
return nil, ErrorEntityNotFound
}
if len(foundCertificates) > 1 {
return nil, fmt.Errorf("more than one certificate found with name '%s'", name)
}
}

if len(certificates) > 1 {
return nil, fmt.Errorf("more than one certificate found with name '%s'", name)
if len(certificates) > 1 && !slowSearch {
{
return nil, fmt.Errorf("more than one certificate found with name '%s'", name)
}
}
return certificates[0], nil
return foundCertificates[0], nil
}

// GetCertificateFromLibraryByName retrieves certificate from certificate library by given name
Expand Down
10 changes: 0 additions & 10 deletions govcd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,16 +858,6 @@ func extractIdsFromOpenApiReferences(refs []types.OpenApiReference) []string {
return resultStrings
}

// contains checks if a slice contains element
func contains(s []string, element string) bool {
for _, a := range s {
if a == element {
return true
}
}
return false
}

// checkSkipWhenApiToken skips the test if the connection was established using an API token
func (vcd *TestVCD) checkSkipWhenApiToken(check *C) {
if vcd.client.Client.UsingAccessToken {
Expand Down
2 changes: 1 addition & 1 deletion govcd/nsxt_firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (vcd *TestVCD) Test_NsxtFirewall(check *C) {
check.Assert(len(fwCreated.NsxtFirewallRuleContainer.UserDefinedRules), Equals, len(randomizedFwRuleDefs))
definedAppPortProfileIds := extractIdsFromOpenApiReferences(randomizedFwRuleDefs[index].ApplicationPortProfiles)
for _, appPortProfile := range fwCreated.NsxtFirewallRuleContainer.UserDefinedRules[index].ApplicationPortProfiles {
check.Assert(contains(definedAppPortProfileIds, appPortProfile.ID), Equals, true)
check.Assert(contains(appPortProfile.ID, definedAppPortProfileIds), Equals, true)
}
}

Expand Down
25 changes: 25 additions & 0 deletions govcd/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,28 @@ func copyUrlRef(in *url.URL) *url.URL {
}
return newUrlRef
}

// shouldDoSlowSearch returns true if query isn't working or added needed params if returns false.
// When the name contains commas, semicolons or asterisks, the encoding is rejected by the API in VCD 10.2 version.
// For this reason, when one or more commas, semicolons or asterisks are present we run the search brute force,
// by fetching all and comparing the name. Yet, this is not needed anymore in VCD 10.3 version.
// Also, url.QueryEscape as well as url.Values.Encode() both encode the space as a + character. So we use
// search brute force too. Reference to issue:
// https://github.com/golang/go/issues/4013
// https://github.com/czos/goamz/pull/11/files
func shouldDoSlowSearch(filterKey, name string, client *Client) (bool, url.Values, error) {
var params = url.Values{}
slowSearch := false
versionWithNoBug, err := client.VersionEqualOrGreater("10.3", 2)
if err != nil {
return false, params, err
}
if (!versionWithNoBug && (strings.Contains(name, ",") || strings.Contains(name, ";"))) ||
strings.Contains(name, " ") || strings.Contains(name, "+") || strings.Contains(name, "*") {
slowSearch = true
} else {
params.Set("filter", fmt.Sprintf(filterKey+"==%s", url.QueryEscape(name)))
params.Set("filterEncoded", "true")
}
return slowSearch, params, err
}
4 changes: 4 additions & 0 deletions govcd/openapi_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ var endpointMinApiVersions = map[string]string{
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnel: "34.0", // VCD 10.1+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnelConnectionProperties: "34.0", // VCD 10.1+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointIpSecVpnTunnelStatus: "34.0", // VCD 10.1+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcGroups: "35.0", // VCD 10.2+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcGroupsCandidateVdcs: "35.0", // VCD 10.2+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcGroupsDfwPolicies: "35.0", // VCD 10.2+
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcGroupsDfwDefaultPolicies: "35.0", // VCD 10.2+

// NSX-T ALB (Advanced/AVI Load Balancer) support was introduced in 10.2
types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointAlbController: "35.0", // VCD 10.2+
Expand Down
10 changes: 7 additions & 3 deletions govcd/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,17 @@ func removeRightsFromRole(client *Client, roleType, name, id, endpoint string, r
for _, rr := range removeRights {
if cr.ID == rr.ID {
foundToRemove[cr.Name] = true
} else {
// If the right is not in the list to be removed, we add it to the input (to be preserved) list
input.Values = append(input.Values, types.OpenApiReference{Name: cr.Name, ID: cr.ID})
}
}
}

for _, cr := range currentRights {
_, found := foundToRemove[cr.Name]
if !found {
input.Values = append(input.Values, types.OpenApiReference{Name: cr.Name, ID: cr.ID})
}
}

// Check that all the items to be removed were found in the current rights list
notFoundNames := ""
for name, found := range foundToRemove {
Expand Down
2 changes: 1 addition & 1 deletion govcd/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,6 @@ func (vcd *TestVCD) TestQueryAllVdcs(check *C) {
fmt.Printf("# Checking result contains all known VDCs (%s).", strings.Join((knownVdcs), ", "))
}
for _, knownVdcName := range knownVdcs {
check.Assert(contains(foundVdcNames, knownVdcName), Equals, true)
check.Assert(contains(knownVdcName, foundVdcNames), Equals, true)
}
}
10 changes: 10 additions & 0 deletions govcd/tenant_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,13 @@ func getTenantContextFromHeader(header map[string]string) *TenantContext {
}
return nil
}

// getTenantContext retrieves the tenant context for a VdcGroup
func (vdcGroup *VdcGroup) getTenantContext() (*TenantContext, error) {
org := vdcGroup.parent

if org == nil {
return nil, fmt.Errorf("VDC group %s has no parent", vdcGroup.VdcGroup.Name)
}
return org.tenantContext()
}
Loading