Skip to content

Commit

Permalink
Replace 'takeStringPointer', 'takeIntAddress', 'takeBoolPointer' with…
Browse files Browse the repository at this point in the history
… generic 'addrOf' (vmware#571)
  • Loading branch information
Didainius committed May 11, 2023
1 parent dabc2d8 commit de4d321
Show file tree
Hide file tree
Showing 43 changed files with 222 additions and 233 deletions.
2 changes: 2 additions & 0 deletions .changes/v2.21.0/571-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Internal - replaced 'takeStringPointer', 'takeIntAddress', 'takeBoolPointer' with generic 'addrOf'
[GH-571]
2 changes: 1 addition & 1 deletion govcd/access_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (vdc *Vdc) SetControlAccess(isSharedToEveryOne bool, everyoneAccessLevel st
}

accessControl.IsSharedToEveryone = true
accessControl.EveryoneAccessLevel = takeStringPointer(everyoneAccessLevel)
accessControl.EveryoneAccessLevel = &everyoneAccessLevel

} else { // Do configuration for individual users/groups
if len(accessSettings) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion govcd/access_control_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (vcd *TestVCD) testCatalogAccessControl(adminOrg *AdminOrg, catalog accessC
checkEmpty()
//globalSettings := types.ControlAccessParams{
// IsSharedToEveryone: true,
// EveryoneAccessLevel: takeStringPointer(types.ControlAccessReadWrite),
// EveryoneAccessLevel: addrOf(types.ControlAccessReadWrite),
// AccessSettings: nil,
//}
//err = testAccessControl(catalogName+" catalog global", catalog, globalSettings, globalSettings, true, check)
Expand Down
4 changes: 2 additions & 2 deletions govcd/access_control_vapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (vcd *TestVCD) Test_VappAccessControl(check *C) {

// Set access control to every user and group
allUsersSettings := types.ControlAccessParams{
EveryoneAccessLevel: takeStringPointer(types.ControlAccessReadOnly),
EveryoneAccessLevel: addrOf(types.ControlAccessReadOnly),
IsSharedToEveryone: true,
}

Expand All @@ -107,7 +107,7 @@ func (vcd *TestVCD) Test_VappAccessControl(check *C) {
check.Assert(err, IsNil)

allUsersSettings = types.ControlAccessParams{
EveryoneAccessLevel: takeStringPointer(types.ControlAccessReadWrite),
EveryoneAccessLevel: addrOf(types.ControlAccessReadWrite),
IsSharedToEveryone: true,
}
err = testAccessControl("vapp all users R/W", vapp, allUsersSettings, allUsersSettings, true, vappTenantContext, check)
Expand Down
4 changes: 2 additions & 2 deletions govcd/adminvdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (vdc *AdminVdc) RemoveStorageProfile(storageProfileName string) (Task, erro
Units: vdcStorageProfileDetails.Units,
Limit: vdcStorageProfileDetails.Limit,
Default: false,
Enabled: takeBoolPointer(false),
Enabled: addrOf(false),
ProviderVdcStorageProfile: &types.Reference{
HREF: vdcStorageProfileDetails.ProviderVdcStorageProfile.HREF,
},
Expand Down Expand Up @@ -561,7 +561,7 @@ func (vdc *AdminVdc) SetDefaultStorageProfile(storageProfileName string) error {
Units: vdcStorageProfileDetails.Units,
Limit: vdcStorageProfileDetails.Limit,
Default: true,
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
ProviderVdcStorageProfile: &types.Reference{
HREF: vdcStorageProfileDetails.ProviderVdcStorageProfile.HREF,
},
Expand Down
2 changes: 1 addition & 1 deletion govcd/adminvdc_nsxt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (vcd *TestVCD) Test_CreateNsxtOrgVdc(check *C) {
},
},
VdcStorageProfile: []*types.VdcStorageProfileConfiguration{&types.VdcStorageProfileConfiguration{
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
Units: "MB",
Limit: 1024,
Default: true,
Expand Down
8 changes: 4 additions & 4 deletions govcd/adminvdc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (vcd *TestVCD) Test_CreateOrgVdcWithFlex(check *C) {
},
},
VdcStorageProfile: []*types.VdcStorageProfileConfiguration{{
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
Units: "MB",
Limit: 1024,
Default: true,
Expand All @@ -101,7 +101,7 @@ func (vcd *TestVCD) Test_CreateOrgVdcWithFlex(check *C) {

if secondStorageProfileHref != "" {
vdcConfiguration.VdcStorageProfile = append(vdcConfiguration.VdcStorageProfile, &types.VdcStorageProfileConfiguration{
Enabled: takeBoolPointer(false),
Enabled: addrOf(false),
Units: "MB",
Limit: 1024,
Default: false,
Expand Down Expand Up @@ -195,7 +195,7 @@ func (vcd *TestVCD) Test_UpdateVdcFlex(check *C) {
check.Assert(err, IsNil)

err = adminVdc.AddStorageProfileWait(&types.VdcStorageProfileConfiguration{
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
Default: false,
Units: "MB",
ProviderVdcStorageProfile: &types.Reference{HREF: pvdcStorageProfile.HREF},
Expand Down Expand Up @@ -301,7 +301,7 @@ func (vcd *TestVCD) Test_VdcUpdateStorageProfile(check *C) {
Name: foundStorageProfile.Name,
Default: true,
Limit: 9081,
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
Units: "MB",
IopsSettings: nil,
ProviderVdcStorageProfile: &types.Reference{HREF: foundStorageProfile.ProviderVdcStorageProfile.HREF},
Expand Down
18 changes: 2 additions & 16 deletions govcd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,20 +694,6 @@ func addrOf[T any](variable T) *T {
return &variable
}

func takeBoolPointer(value bool) *bool {
return &value
}

// takeIntAddress is a helper that returns the address of an `int`
func takeIntAddress(x int) *int {
return &x
}

// takeStringPointer is a helper that returns the address of a `string`
func takeStringPointer(x string) *string {
return &x
}

// IsUuid returns true if the identifier is a bare UUID
func IsUuid(identifier string) bool {
reUuid := regexp.MustCompile(`^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$`)
Expand Down Expand Up @@ -858,8 +844,8 @@ func (client *Client) TestConnectionWithDefaults(subscriptionURL string) (bool,
testConnectionConfig := types.TestConnection{
Host: url.Hostname(),
Port: port,
Secure: takeBoolPointer(true), // Default value used by VCD UI
Timeout: 30, // Default value used by VCD UI
Secure: addrOf(true), // Default value used by VCD UI
Timeout: 30, // Default value used by VCD UI
}

testConnectionResult, err := client.TestConnection(testConnectionConfig)
Expand Down
6 changes: 3 additions & 3 deletions govcd/catalog_subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ func testSubscribedCatalog(testData subscriptionTestData, check *C) {

subscriptionPassword := "superUnknown"
err = fromCatalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
IsPublishedExternally: takeBoolPointer(true),
IsPublishedExternally: addrOf(true),
Password: subscriptionPassword,
IsCachedEnabled: takeBoolPointer(true),
PreserveIdentityInfoFlag: takeBoolPointer(true),
IsCachedEnabled: addrOf(true),
PreserveIdentityInfoFlag: addrOf(true),
})
check.Assert(err, IsNil)

Expand Down
18 changes: 9 additions & 9 deletions govcd/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,10 @@ func (vcd *TestVCD) Test_PublishToExternalOrganizations(check *C) {
AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())

err = adminCatalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
IsPublishedExternally: takeBoolPointer(true),
IsCachedEnabled: takeBoolPointer(true),
IsPublishedExternally: addrOf(true),
IsCachedEnabled: addrOf(true),
Password: "secretOrNot",
PreserveIdentityInfoFlag: takeBoolPointer(true),
PreserveIdentityInfoFlag: addrOf(true),
})
check.Assert(err, IsNil)
check.Assert(*adminCatalog.AdminCatalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
Expand All @@ -1002,10 +1002,10 @@ func (vcd *TestVCD) Test_PublishToExternalOrganizations(check *C) {
AddToCleanupList(catalogName, "catalog", vcd.config.VCD.Org, check.TestName())

err = catalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
IsPublishedExternally: takeBoolPointer(true),
IsCachedEnabled: takeBoolPointer(true),
IsPublishedExternally: addrOf(true),
IsCachedEnabled: addrOf(true),
Password: "secretOrNot",
PreserveIdentityInfoFlag: takeBoolPointer(true),
PreserveIdentityInfoFlag: addrOf(true),
})
check.Assert(err, IsNil)
check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
Expand All @@ -1014,10 +1014,10 @@ func (vcd *TestVCD) Test_PublishToExternalOrganizations(check *C) {
check.Assert(catalog.Catalog.PublishExternalCatalogParams.Password, Equals, "******")

err = catalog.PublishToExternalOrganizations(types.PublishExternalCatalogParams{
IsPublishedExternally: takeBoolPointer(true),
IsCachedEnabled: takeBoolPointer(false),
IsPublishedExternally: addrOf(true),
IsCachedEnabled: addrOf(false),
Password: "secretOrNot2",
PreserveIdentityInfoFlag: takeBoolPointer(false),
PreserveIdentityInfoFlag: addrOf(false),
})
check.Assert(err, IsNil)
check.Assert(*catalog.Catalog.PublishExternalCatalogParams.IsPublishedExternally, Equals, true)
Expand Down
17 changes: 9 additions & 8 deletions govcd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package govcd
import (
"errors"
"fmt"
"github.com/vmware/go-vcloud-director/v2/util"
"io"
"net/http"
"net/url"
Expand All @@ -18,6 +17,8 @@ import (
"strconv"
"time"

"github.com/vmware/go-vcloud-director/v2/util"

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

. "gopkg.in/check.v1"
Expand Down Expand Up @@ -663,7 +664,7 @@ func makeEmptyVm(vapp *VApp, name string) (*VM, error) {
SizeMb: int64(100),
BusNumber: 0,
UnitNumber: 0,
ThinProvisioned: takeBoolPointer(true),
ThinProvisioned: addrOf(true),
}
requestDetails := &types.RecomposeVAppParamsForEmptyVm{
CreateItem: &types.CreateItem{
Expand All @@ -672,11 +673,11 @@ func makeEmptyVm(vapp *VApp, name string) (*VM, error) {
Description: "created by makeEmptyVm",
GuestCustomizationSection: nil,
VmSpecSection: &types.VmSpecSection{
Modified: takeBoolPointer(true),
Modified: addrOf(true),
Info: "Virtual Machine specification",
OsType: "debian10Guest",
NumCpus: takeIntAddress(1),
NumCoresPerSocket: takeIntAddress(1),
NumCpus: addrOf(1),
NumCoresPerSocket: addrOf(1),
CpuResourceMhz: &types.CpuResourceMhz{Configured: 1},
MemoryResourceMb: &types.MemoryResourceMb{Configured: 512},
MediaSection: nil,
Expand Down Expand Up @@ -728,7 +729,7 @@ func spawnTestVdc(vcd *TestVCD, check *C, adminOrgName string) *Vdc {
},
},
VdcStorageProfile: []*types.VdcStorageProfileConfiguration{&types.VdcStorageProfileConfiguration{
Enabled: takeBoolPointer(true),
Enabled: addrOf(true),
Units: "MB",
Limit: 1024,
Default: true,
Expand All @@ -746,8 +747,8 @@ func spawnTestVdc(vcd *TestVCD, check *C, adminOrgName string) *Vdc {
IsEnabled: true,
IsThinProvision: true,
UsesFastProvisioning: true,
IsElastic: takeBoolPointer(true),
IncludeMemoryOverhead: takeBoolPointer(true),
IsElastic: addrOf(true),
IncludeMemoryOverhead: addrOf(true),
}

vdc, err := adminOrg.CreateOrgVdc(vdcConfiguration)
Expand Down
8 changes: 4 additions & 4 deletions govcd/edgegateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (egw *EdgeGateway) AddNATRuleAsync(ruleDetails NatRule) (Task, error) {
//construct new rule
natRule := &types.NatRule{
RuleType: ruleDetails.NatType,
IsEnabled: takeBoolPointer(true),
IsEnabled: addrOf(true),
Description: ruleDetails.Description,
GatewayNatRule: &types.GatewayNatRule{
Interface: &types.Reference{
Expand Down Expand Up @@ -632,7 +632,7 @@ func (egw *EdgeGateway) AddNATPortMappingWithUplink(network *types.OrgVDCNetwork
//add rule
natRule := &types.NatRule{
RuleType: natType,
IsEnabled: takeBoolPointer(true),
IsEnabled: addrOf(true),
GatewayNatRule: &types.GatewayNatRule{
Interface: &types.Reference{
HREF: uplinkRef,
Expand Down Expand Up @@ -866,7 +866,7 @@ func (egw *EdgeGateway) Create1to1Mapping(internal, external, description string
snat := &types.NatRule{
Description: description,
RuleType: "SNAT",
IsEnabled: takeBoolPointer(true),
IsEnabled: addrOf(true),
GatewayNatRule: &types.GatewayNatRule{
Interface: &types.Reference{
HREF: uplinkif,
Expand All @@ -885,7 +885,7 @@ func (egw *EdgeGateway) Create1to1Mapping(internal, external, description string
dnat := &types.NatRule{
Description: description,
RuleType: "DNAT",
IsEnabled: takeBoolPointer(true),
IsEnabled: addrOf(true),
GatewayNatRule: &types.GatewayNatRule{
Interface: &types.Reference{
HREF: uplinkif,
Expand Down
8 changes: 4 additions & 4 deletions govcd/edgegateway_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ func Test_getVnicIndexFromNetworkNameType(t *testing.T) {
hasError bool
expectedError error
}{
{"ExtNetwork", "my-ext-network", types.EdgeGatewayVnicTypeUplink, takeIntAddress(0), false, nil},
{"OrgNetwork", "my-vdc-int-net", types.EdgeGatewayVnicTypeInternal, takeIntAddress(1), false, nil},
{"WithSubinterfaces", "subinterfaced-net", types.EdgeGatewayVnicTypeSubinterface, takeIntAddress(10), false, nil},
{"WithSubinterfaces2", "subinterface2", types.EdgeGatewayVnicTypeSubinterface, takeIntAddress(11), false, nil},
{"ExtNetwork", "my-ext-network", types.EdgeGatewayVnicTypeUplink, addrOf(0), false, nil},
{"OrgNetwork", "my-vdc-int-net", types.EdgeGatewayVnicTypeInternal, addrOf(1), false, nil},
{"WithSubinterfaces", "subinterfaced-net", types.EdgeGatewayVnicTypeSubinterface, addrOf(10), false, nil},
{"WithSubinterfaces2", "subinterface2", types.EdgeGatewayVnicTypeSubinterface, addrOf(11), false, nil},
{"NonExistingUplink", "invalid-network-name", types.EdgeGatewayVnicTypeUplink, nil, true, ErrorEntityNotFound},
{"NonExistingInternal", "invalid-network-name", types.EdgeGatewayVnicTypeInternal, nil, true, ErrorEntityNotFound},
{"NonExistingSubinterface", "invalid-network-name", types.EdgeGatewayVnicTypeSubinterface, nil, true, ErrorEntityNotFound},
Expand Down
2 changes: 1 addition & 1 deletion govcd/global_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (client *Client) CreateGlobalRole(newGlobalRole *types.GlobalRole) (*Global
newGlobalRole.BundleKey = types.VcloudUndefinedKey
}
if newGlobalRole.PublishAll == nil {
newGlobalRole.PublishAll = takeBoolPointer(false)
newGlobalRole.PublishAll = addrOf(false)
}
returnGlobalRole := &GlobalRole{
GlobalRole: &types.GlobalRole{},
Expand Down
26 changes: 13 additions & 13 deletions govcd/nsxt_alb_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ func testAdvancedPoolConfig(check *C, edge *NsxtEdgeGateway, vcd *TestVCD, clien
Name: check.TestName() + "-Advanced",
GatewayRef: types.OpenApiReference{ID: edge.EdgeGateway.ID},
Algorithm: "FEWEST_SERVERS",
DefaultPort: takeIntAddress(8443),
GracefulTimeoutPeriod: takeIntAddress(1),
PassiveMonitoringEnabled: takeBoolPointer(true),
DefaultPort: addrOf(8443),
GracefulTimeoutPeriod: addrOf(1),
PassiveMonitoringEnabled: addrOf(true),
HealthMonitors: nil,
Members: []types.NsxtAlbPoolMember{
{
Enabled: true,
IpAddress: "1.1.1.1",
Port: 8400,
Ratio: takeIntAddress(2),
Ratio: addrOf(2),
},
{
Enabled: false,
Expand All @@ -95,17 +95,17 @@ func testAdvancedPoolConfig(check *C, edge *NsxtEdgeGateway, vcd *TestVCD, clien
poolConfigAdvancedUpdated := &types.NsxtAlbPool{
Name: poolConfigAdvanced.Name + "-Updated",
GatewayRef: types.OpenApiReference{ID: edge.EdgeGateway.ID},
Enabled: takeBoolPointer(false),
Enabled: addrOf(false),
Algorithm: "LEAST_LOAD",
GracefulTimeoutPeriod: takeIntAddress(0),
PassiveMonitoringEnabled: takeBoolPointer(false),
GracefulTimeoutPeriod: addrOf(0),
PassiveMonitoringEnabled: addrOf(false),
HealthMonitors: nil,
Members: []types.NsxtAlbPoolMember{
{
Enabled: true,
IpAddress: "1.1.1.1",
Port: 8300,
Ratio: takeIntAddress(3),
Ratio: addrOf(3),
},
{
Enabled: true,
Expand Down Expand Up @@ -138,9 +138,9 @@ func testPoolWithCertNoPrivateKey(check *C, vcd *TestVCD, edgeGatewayId string,
GatewayRef: types.OpenApiReference{ID: edgeGatewayId},
Algorithm: "FASTEST_RESPONSE",
CaCertificateRefs: []types.OpenApiReference{types.OpenApiReference{ID: createdCertificate.CertificateLibrary.Id}},
CommonNameCheckEnabled: takeBoolPointer(true),
CommonNameCheckEnabled: addrOf(true),
DomainNames: []string{"one", "two", "three"},
DefaultPort: takeIntAddress(1211),
DefaultPort: addrOf(1211),
}

testAlbPoolConfig(check, vcd, "CertificateWithNoPrivateKey", poolConfigWithCert, nil, client)
Expand Down Expand Up @@ -173,7 +173,7 @@ func testPoolWithCertAndPrivateKey(check *C, vcd *TestVCD, edgeGatewayId string,

Algorithm: "FASTEST_RESPONSE",
CaCertificateRefs: []types.OpenApiReference{types.OpenApiReference{ID: createdCertificate.CertificateLibrary.Id}},
DefaultPort: takeIntAddress(1211),
DefaultPort: addrOf(1211),
}

testAlbPoolConfig(check, vcd, "CertificateWithPrivateKey", poolConfigWithCertAndKey, nil, client)
Expand Down Expand Up @@ -288,8 +288,8 @@ func setupAlbPoolPrerequisites(check *C, vcd *TestVCD) (*NsxtAlbController, *Nsx
serviceEngineGroupAssignmentConfig := &types.NsxtAlbServiceEngineGroupAssignment{
GatewayRef: &types.OpenApiReference{ID: edge.EdgeGateway.ID},
ServiceEngineGroupRef: &types.OpenApiReference{ID: seGroup.NsxtAlbServiceEngineGroup.ID},
MaxVirtualServices: takeIntAddress(89),
MinVirtualServices: takeIntAddress(20),
MaxVirtualServices: addrOf(89),
MinVirtualServices: addrOf(20),
}

assignment, err := vcd.client.CreateAlbServiceEngineGroupAssignment(serviceEngineGroupAssignmentConfig)
Expand Down
Loading

0 comments on commit de4d321

Please sign in to comment.