Skip to content

Commit 2c1c04a

Browse files
authored
Change owner to VdcComputePolicy functions (#468)
* Change owner to VdcComputePolicy functions * Add client.CreateVdcComputePolicy * Add client.GetVdcComputePolicyById * Add client.GetAllVdcComputePolicies * Deprecate org.GetVdcComputePolicyById * Deprecate adminOrg.GetVdcComputePolicyById * Deprecate org.GetAllVdcComputePolicies * Deprecate adminOrg.GetAllVdcComputePolicies * Deprecate org.CreateVdcComputePolicy Signed-off-by: Giuseppe Maxia <gmaxia@vmware.com>
1 parent 011d36c commit 2c1c04a

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

.changes/v2.16.0/468-improvements.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Add methods `client.CreateVdcComputePolicy`, `client.GetVdcComputePolicyById`, `client.GetAllVdcComputePolicies` [GH-468]
2+
* Deprecate `org.GetVdcComputePolicyById`, `adminOrg.GetVdcComputePolicyById` [GH-468]
3+
* Deprecate `org.GetAllVdcComputePolicies`, `adminOrg.GetAllVdcComputePolicies`, `org.CreateVdcComputePolicy` [GH-468]

govcd/api_vcd_test.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -1529,16 +1529,7 @@ func (vcd *TestVCD) removeLeftoverEntities(entity CleanupEntity) {
15291529
return
15301530

15311531
case "vdcComputePolicy":
1532-
if entity.Parent == "" {
1533-
vcd.infoCleanup("removeLeftoverEntries: [ERROR] No ORG provided for vdcComputePolicy '%s'\n", entity.Name)
1534-
return
1535-
}
1536-
org, err := vcd.client.GetAdminOrgByName(entity.Parent)
1537-
if err != nil {
1538-
vcd.infoCleanup(notFoundMsg, "org", entity.Parent)
1539-
return
1540-
}
1541-
policy, err := org.GetVdcComputePolicyById(entity.Name)
1532+
policy, err := vcd.client.Client.GetVdcComputePolicyById(entity.Name)
15421533
if policy == nil || err != nil {
15431534
vcd.infoCleanup(notFoundMsg, "vdcComputePolicy", entity.Name)
15441535
return

govcd/vdccomputepolicy.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package govcd
22

33
/*
4-
* Copyright 2020 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
4+
* Copyright 2022 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
55
*/
66

77
import (
88
"fmt"
9-
"github.com/vmware/go-vcloud-director/v2/types/v56"
10-
"github.com/vmware/go-vcloud-director/v2/util"
119
"net/http"
1210
"net/url"
11+
12+
"github.com/vmware/go-vcloud-director/v2/types/v56"
13+
"github.com/vmware/go-vcloud-director/v2/util"
1314
)
1415

1516
// In UI called VM sizing policy. In API VDC compute policy
@@ -20,11 +21,18 @@ type VdcComputePolicy struct {
2021
}
2122

2223
// GetVdcComputePolicyById retrieves VDC compute policy by given ID
24+
func (client *Client) GetVdcComputePolicyById(id string) (*VdcComputePolicy, error) {
25+
return getVdcComputePolicyById(client, id)
26+
}
27+
28+
// GetVdcComputePolicyById retrieves VDC compute policy by given ID
29+
// Deprecated: use client.GetVdcComputePolicyById
2330
func (org *AdminOrg) GetVdcComputePolicyById(id string) (*VdcComputePolicy, error) {
2431
return getVdcComputePolicyById(org.client, id)
2532
}
2633

2734
// GetVdcComputePolicyById retrieves VDC compute policy by given ID
35+
// Deprecated: use client.GetVdcComputePolicyById
2836
func (org *Org) GetVdcComputePolicyById(id string) (*VdcComputePolicy, error) {
2937
return getVdcComputePolicyById(org.client, id)
3038
}
@@ -63,12 +71,20 @@ func getVdcComputePolicyById(client *Client, id string) (*VdcComputePolicy, erro
6371

6472
// GetAllVdcComputePolicies retrieves all VDC compute policies using OpenAPI endpoint. Query parameters can be supplied to perform additional
6573
// filtering
74+
func (client *Client) GetAllVdcComputePolicies(queryParameters url.Values) ([]*VdcComputePolicy, error) {
75+
return getAllVdcComputePolicies(client, queryParameters)
76+
}
77+
78+
// GetAllVdcComputePolicies retrieves all VDC compute policies using OpenAPI endpoint. Query parameters can be supplied to perform additional
79+
// filtering
80+
// Deprecated: use client.GetAllVdcComputePolicies
6681
func (org *AdminOrg) GetAllVdcComputePolicies(queryParameters url.Values) ([]*VdcComputePolicy, error) {
6782
return getAllVdcComputePolicies(org.client, queryParameters)
6883
}
6984

7085
// GetAllVdcComputePolicies retrieves all VDC compute policies using OpenAPI endpoint. Query parameters can be supplied to perform additional
7186
// filtering
87+
// Deprecated: use client.GetAllVdcComputePolicies
7288
func (org *Org) GetAllVdcComputePolicies(queryParameters url.Values) ([]*VdcComputePolicy, error) {
7389
return getAllVdcComputePolicies(org.client, queryParameters)
7490
}
@@ -107,24 +123,30 @@ func getAllVdcComputePolicies(client *Client, queryParameters url.Values) ([]*Vd
107123
}
108124

109125
// CreateVdcComputePolicy creates a new VDC Compute Policy using OpenAPI endpoint
126+
// Deprecated: use client.CreateVdcComputePolicy
110127
func (org *AdminOrg) CreateVdcComputePolicy(newVdcComputePolicy *types.VdcComputePolicy) (*VdcComputePolicy, error) {
128+
return org.client.CreateVdcComputePolicy(newVdcComputePolicy)
129+
}
130+
131+
// CreateVdcComputePolicy creates a new VDC Compute Policy using OpenAPI endpoint
132+
func (client *Client) CreateVdcComputePolicy(newVdcComputePolicy *types.VdcComputePolicy) (*VdcComputePolicy, error) {
111133
endpoint := types.OpenApiPathVersion1_0_0 + types.OpenApiEndpointVdcComputePolicies
112-
minimumApiVersion, err := org.client.checkOpenApiEndpointCompatibility(endpoint)
134+
minimumApiVersion, err := client.checkOpenApiEndpointCompatibility(endpoint)
113135
if err != nil {
114136
return nil, err
115137
}
116138

117-
urlRef, err := org.client.OpenApiBuildEndpoint(endpoint)
139+
urlRef, err := client.OpenApiBuildEndpoint(endpoint)
118140
if err != nil {
119141
return nil, err
120142
}
121143

122144
returnVdcComputePolicy := &VdcComputePolicy{
123145
VdcComputePolicy: &types.VdcComputePolicy{},
124-
client: org.client,
146+
client: client,
125147
}
126148

127-
err = org.client.OpenApiPostItem(minimumApiVersion, urlRef, nil, newVdcComputePolicy, returnVdcComputePolicy.VdcComputePolicy, nil)
149+
err = client.OpenApiPostItem(minimumApiVersion, urlRef, nil, newVdcComputePolicy, returnVdcComputePolicy.VdcComputePolicy, nil)
128150
if err != nil {
129151
return nil, fmt.Errorf("error creating VDC compute policy: %s", err)
130152
}

govcd/vdccomputepolicy_test.go

+25-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// +build vdc functional openapi ALL
33

44
/*
5-
* Copyright 2020 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
5+
* Copyright 2022 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
66
*/
77

88
package govcd
@@ -21,29 +21,26 @@ func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
2121
check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
2222
}
2323

24-
org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
25-
check.Assert(err, IsNil)
26-
check.Assert(org, NotNil)
27-
24+
client := &vcd.client.Client
2825
// Step 1 - Create a new VDC compute policies
2926
newComputePolicy := &VdcComputePolicy{
30-
client: org.client,
27+
client: client,
3128
VdcComputePolicy: &types.VdcComputePolicy{
3229
Name: check.TestName() + "_empty",
3330
Description: "Empty policy created by test",
3431
},
3532
}
3633

37-
createdPolicy, err := org.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
34+
createdPolicy, err := client.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
3835
check.Assert(err, IsNil)
3936

40-
AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", vcd.org.Org.Name, "Test_VdcComputePolicies")
37+
AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
4138

4239
check.Assert(createdPolicy.VdcComputePolicy.Name, Equals, newComputePolicy.VdcComputePolicy.Name)
4340
check.Assert(createdPolicy.VdcComputePolicy.Description, Equals, newComputePolicy.VdcComputePolicy.Description)
4441

4542
newComputePolicy2 := &VdcComputePolicy{
46-
client: org.client,
43+
client: client,
4744
VdcComputePolicy: &types.VdcComputePolicy{
4845
Name: check.TestName(),
4946
Description: "Not Empty policy created by test",
@@ -60,10 +57,10 @@ func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
6057
},
6158
}
6259

63-
createdPolicy2, err := org.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
60+
createdPolicy2, err := client.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
6461
check.Assert(err, IsNil)
6562

66-
AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", vcd.org.Org.Name, "Test_VdcComputePolicies")
63+
AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
6764

6865
check.Assert(createdPolicy2.VdcComputePolicy.Name, Equals, newComputePolicy2.VdcComputePolicy.Name)
6966
check.Assert(*createdPolicy2.VdcComputePolicy.CPUSpeed, Equals, 100)
@@ -84,7 +81,7 @@ func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
8481
check.Assert(updatedPolicy.VdcComputePolicy, DeepEquals, createdPolicy2.VdcComputePolicy)
8582

8683
// Step 3 - Get all VDC compute policies
87-
allExistingPolicies, err := org.GetAllVdcComputePolicies(nil)
84+
allExistingPolicies, err := client.GetAllVdcComputePolicies(nil)
8885
check.Assert(err, IsNil)
8986
check.Assert(allExistingPolicies, NotNil)
9087

@@ -95,12 +92,12 @@ func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
9592
queryParams := url.Values{}
9693
queryParams.Add("filter", "id=="+onePolicy.VdcComputePolicy.ID)
9794

98-
expectOnePolicyResultById, err := org.GetAllVdcComputePolicies(queryParams)
95+
expectOnePolicyResultById, err := client.GetAllVdcComputePolicies(queryParams)
9996
check.Assert(err, IsNil)
10097
check.Assert(len(expectOnePolicyResultById) == 1, Equals, true)
10198

10299
// Step 2.2 - retrieve
103-
exactItem, err := org.GetVdcComputePolicyById(onePolicy.VdcComputePolicy.ID)
100+
exactItem, err := client.GetVdcComputePolicyById(onePolicy.VdcComputePolicy.ID)
104101
check.Assert(err, IsNil)
105102

106103
check.Assert(err, IsNil)
@@ -115,13 +112,13 @@ func (vcd *TestVCD) Test_VdcComputePolicies(check *C) {
115112
err = createdPolicy.Delete()
116113
check.Assert(err, IsNil)
117114
// Step 5 - try to read deleted VDC computed policy should end up with error 'ErrorEntityNotFound'
118-
deletedPolicy, err := org.GetVdcComputePolicyById(createdPolicy.VdcComputePolicy.ID)
115+
deletedPolicy, err := client.GetVdcComputePolicyById(createdPolicy.VdcComputePolicy.ID)
119116
check.Assert(ContainsNotFound(err), Equals, true)
120117
check.Assert(deletedPolicy, IsNil)
121118

122119
err = createdPolicy2.Delete()
123120
check.Assert(err, IsNil)
124-
deletedPolicy2, err := org.GetVdcComputePolicyById(createdPolicy2.VdcComputePolicy.ID)
121+
deletedPolicy2, err := client.GetVdcComputePolicyById(createdPolicy2.VdcComputePolicy.ID)
125122
check.Assert(ContainsNotFound(err), Equals, true)
126123
check.Assert(deletedPolicy2, IsNil)
127124
}
@@ -131,6 +128,7 @@ func (vcd *TestVCD) Test_SetAssignedComputePolicies(check *C) {
131128
check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
132129
}
133130

131+
client := &vcd.client.Client
134132
org, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
135133
check.Assert(err, IsNil)
136134
check.Assert(org, NotNil)
@@ -145,29 +143,29 @@ func (vcd *TestVCD) Test_SetAssignedComputePolicies(check *C) {
145143
client: org.client,
146144
VdcComputePolicy: &types.VdcComputePolicy{
147145
Name: check.TestName() + "1",
148-
Description: "Policy created by Test_SetVdcComputePolicies",
146+
Description: "Policy created by Test_SetAssignedComputePolicies",
149147
CoresPerSocket: takeIntAddress(1),
150148
CPUReservationGuarantee: takeFloatAddress(0.26),
151149
CPULimit: takeIntAddress(200),
152150
},
153151
}
154-
createdPolicy, err := org.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
152+
createdPolicy, err := client.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
155153
check.Assert(err, IsNil)
156-
AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", vcd.org.Org.Name, "Test_VdcComputePolicies")
154+
AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
157155

158156
newComputePolicy2 := &VdcComputePolicy{
159157
client: org.client,
160158
VdcComputePolicy: &types.VdcComputePolicy{
161159
Name: check.TestName() + "2",
162-
Description: "Policy created by Test_SetVdcComputePolicies",
160+
Description: "Policy created by Test_SetAssignedComputePolicies",
163161
CoresPerSocket: takeIntAddress(2),
164162
CPUReservationGuarantee: takeFloatAddress(0.52),
165163
CPULimit: takeIntAddress(400),
166164
},
167165
}
168-
createdPolicy2, err := org.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
166+
createdPolicy2, err := client.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
169167
check.Assert(err, IsNil)
170-
AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", vcd.org.Org.Name, "Test_VdcComputePolicies")
168+
AddToCleanupList(createdPolicy2.VdcComputePolicy.ID, "vdcComputePolicy", "", check.TestName())
171169

172170
// Get default compute policy
173171
allAssignedComputePolicies, err := adminVdc.GetAllAssignedVdcComputePolicies(nil)
@@ -200,4 +198,9 @@ func (vcd *TestVCD) Test_SetAssignedComputePolicies(check *C) {
200198

201199
_, err = adminVdc.SetAssignedComputePolicies(policyReferences)
202200
check.Assert(err, IsNil)
201+
202+
err = createdPolicy.Delete()
203+
check.Assert(err, IsNil)
204+
err = createdPolicy2.Delete()
205+
check.Assert(err, IsNil)
203206
}

govcd/vm_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1490,10 +1490,10 @@ func (vcd *TestVCD) Test_AddNewEmptyVMWithVmComputePolicyAndUpdate(check *C) {
14901490
vcd.infoCleanup(notFoundMsg, "vdc", vcd.vdc.Vdc.Name)
14911491
}
14921492

1493-
createdPolicy, err := adminOrg.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
1493+
createdPolicy, err := adminOrg.client.CreateVdcComputePolicy(newComputePolicy.VdcComputePolicy)
14941494
check.Assert(err, IsNil)
14951495

1496-
createdPolicy2, err := adminOrg.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
1496+
createdPolicy2, err := adminOrg.client.CreateVdcComputePolicy(newComputePolicy2.VdcComputePolicy)
14971497
check.Assert(err, IsNil)
14981498

14991499
AddToCleanupList(createdPolicy.VdcComputePolicy.ID, "vdcComputePolicy", vcd.org.Org.Name, "Test_AddNewEmptyVMWithVmComputePolicyAndUpdate")

0 commit comments

Comments
 (0)