Skip to content

Commit 94962bd

Browse files
committed
F #1550: Java and go api for optimization plans (#3511)
1 parent 1230680 commit 94962bd

File tree

3 files changed

+333
-1
lines changed

3 files changed

+333
-1
lines changed

src/oca/go/src/goca/cluster.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,39 @@ func (cc *ClusterController) RenameContext(ctx context.Context, newName string)
257257
_, err := cc.c.Client.CallContext(ctx, "one.cluster.rename", cc.ID, newName)
258258
return err
259259
}
260+
261+
// Optimize creates optimization plan for a cluster.
262+
func (cc *ClusterController) Optimize() error {
263+
return cc.OptimizeContext(context.Background())
264+
}
265+
266+
// OptimizeContext creates optimization plan for a cluster..
267+
// * ctx: context for cancelation
268+
func (cc *ClusterController) OptimizeContext(ctx context.Context) error {
269+
_, err := cc.c.Client.CallContext(ctx, "one.cluster.optimize", cc.ID)
270+
return err
271+
}
272+
273+
// PlanExecute executes optimization plan for cluster a cluster.
274+
func (cc *ClusterController) PlanExecute() error {
275+
return cc.PlanExecuteContext(context.Background())
276+
}
277+
278+
// PlanExecuteContext executes optimization plan for cluster a cluster.
279+
// * ctx: context for cancelation
280+
func (cc *ClusterController) PlanExecuteContext(ctx context.Context) error {
281+
_, err := cc.c.Client.CallContext(ctx, "one.cluster.planexecute", cc.ID)
282+
return err
283+
}
284+
285+
// PlanDelete renames a cluster.
286+
func (cc *ClusterController) PlanDelete() error {
287+
return cc.PlanDeleteContext(context.Background())
288+
}
289+
290+
// PlanDeleteContext renames a cluster.
291+
// * ctx: context for cancelation
292+
func (cc *ClusterController) PlanDeleteContext(ctx context.Context) error {
293+
_, err := cc.c.Client.CallContext(ctx, "one.cluster.plandelete", cc.ID)
294+
return err
295+
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* Copyright 2002-2025, OpenNebula Project, OpenNebula Systems */
3+
/* */
4+
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
5+
/* not use this file except in compliance with the License. You may obtain */
6+
/* a copy of the License at */
7+
/* */
8+
/* http://www.apache.org/licenses/LICENSE-2.0 */
9+
/* */
10+
/* Unless required by applicable law or agreed to in writing, software */
11+
/* distributed under the License is distributed on an "AS IS" BASIS, */
12+
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13+
/* See the License for the specific language governing permissions and */
14+
/* limitations under the License. */
15+
/*--------------------------------------------------------------------------- */
16+
17+
package goca
18+
19+
import (
20+
"math/rand"
21+
"time"
22+
"strconv"
23+
24+
"github.com/OpenNebula/one/src/oca/go/src/goca/schemas/cluster/keys"
25+
"github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
26+
"github.com/OpenNebula/one/src/oca/go/src/goca/errors"
27+
. "gopkg.in/check.v1"
28+
)
29+
30+
type ClusterSuite struct {
31+
clusterName string
32+
clusterID int
33+
}
34+
35+
var _ = Suite(&ClusterSuite{})
36+
37+
func (s *ClusterSuite) SetUpSuite(c *C) {
38+
rand.Seed(time.Now().UnixNano())
39+
}
40+
41+
func (s *ClusterSuite) SetUpTest(c *C) {
42+
// Create Cluster
43+
s.clusterName = "cluster_test_go" + strconv.Itoa(rand.Intn(1000))
44+
45+
id, err := testCtrl.Clusters().Create(s.clusterName)
46+
c.Assert(err, IsNil)
47+
s.clusterID = id
48+
}
49+
50+
func (s *ClusterSuite) TearDownTest(c *C) {
51+
// Delete Cluster
52+
clusterC := testCtrl.Cluster(s.clusterID)
53+
err := clusterC.Delete()
54+
55+
c.Assert(err, IsNil)
56+
}
57+
58+
func (s *ClusterSuite) TestGetByNameAndID(c *C) {
59+
// Get Cluster by ID
60+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
61+
62+
c.Assert(err, IsNil)
63+
c.Assert(cluster.ID, Equals, s.clusterID)
64+
c.Assert(cluster.Name, Equals, s.clusterName)
65+
66+
// Test value from Cluster template
67+
reservedCPU, err := cluster.Template.Get(keys.ReservedCPU)
68+
69+
c.Assert(err, IsNil)
70+
c.Assert(reservedCPU, Equals, "")
71+
72+
// Get Cluster by Name
73+
id, err := testCtrl.Clusters().ByName(s.clusterName)
74+
c.Assert(err, IsNil)
75+
c.Assert(id, Equals, s.clusterID)
76+
}
77+
78+
func (s *ClusterSuite) TestUpdate(c *C) {
79+
clusterC := testCtrl.Cluster(s.clusterID)
80+
err := clusterC.Update(`ATT1 = "VAL1"`, parameters.Merge)
81+
82+
c.Assert(err, IsNil)
83+
84+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
85+
c.Assert(err, IsNil)
86+
87+
att, err := cluster.Template.Get("ATT1")
88+
89+
c.Assert(err, IsNil)
90+
c.Assert(att, Equals, "VAL1")
91+
}
92+
93+
func (s *ClusterSuite) TestRename(c *C) {
94+
name := "new_name" + strconv.Itoa(rand.Intn(1000))
95+
clusterC := testCtrl.Cluster(s.clusterID)
96+
clusterC.Rename(name)
97+
98+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
99+
c.Assert(err, IsNil)
100+
c.Assert(cluster.Name, Equals, name);
101+
}
102+
103+
// Test add Host to Cluster
104+
func (s *ClusterSuite) TestAddDelHost(c *C) {
105+
// Create Host
106+
hostID, err := testCtrl.Hosts().Create(
107+
"dummy-cluster-test" + strconv.Itoa(rand.Intn(1000)),
108+
"dummy",
109+
"dummy",
110+
0)
111+
c.Assert(err, IsNil)
112+
113+
// Add Host to Cluster
114+
clusterC := testCtrl.Cluster(s.clusterID)
115+
err = clusterC.AddHost(hostID)
116+
c.Assert(err, IsNil)
117+
118+
// Get Cluster and check if Host was added to Cluster
119+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
120+
c.Assert(err, IsNil)
121+
c.Assert(len(cluster.Hosts.ID), Equals, 1)
122+
c.Assert(cluster.Hosts.ID[0], Equals, hostID)
123+
124+
// Delete Host from Cluster
125+
err = clusterC.DelHost(hostID)
126+
c.Assert(err, IsNil)
127+
128+
// Check if Host was added to Cluster
129+
cluster, err = testCtrl.Cluster(s.clusterID).Info()
130+
c.Assert(err, IsNil)
131+
c.Assert(len(cluster.Hosts.ID), Equals, 0)
132+
133+
// Delete Host
134+
err = testCtrl.Host(hostID).Delete()
135+
c.Assert(err, IsNil)
136+
}
137+
138+
// Test add Datastore to Cluster
139+
func (s *ClusterSuite) TestAddDelDatastore(c *C) {
140+
// Create Datastore
141+
dsTmpl := "NAME = go_cluster_ds" + strconv.Itoa(rand.Intn(1000)) + "\n" +
142+
"DS_MAD = dummy\n" +
143+
"TM_MAD = dummy\n" +
144+
"TYPE = BACKUP_DS\n"
145+
146+
dsID, err := testCtrl.Datastores().Create(dsTmpl, -1)
147+
c.Assert(err, IsNil)
148+
149+
// Add Datastore to Cluster
150+
clusterC := testCtrl.Cluster(s.clusterID)
151+
err = clusterC.AddDatastore(dsID)
152+
c.Assert(err, IsNil)
153+
154+
// Get Cluster and check if Datastore was added to Cluster
155+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
156+
c.Assert(err, IsNil)
157+
c.Assert(len(cluster.Datastores.ID), Equals, 1)
158+
c.Assert(cluster.Datastores.ID[0], Equals, dsID)
159+
160+
// Delete Datastore from Cluster
161+
err = clusterC.DelDatastore(dsID)
162+
c.Assert(err, IsNil)
163+
164+
// Check if Datastore was added to Cluster
165+
cluster, err = testCtrl.Cluster(s.clusterID).Info()
166+
c.Assert(err, IsNil)
167+
c.Assert(len(cluster.Datastores.ID), Equals, 0)
168+
169+
// Delete Datastore
170+
err = testCtrl.Datastore(dsID).Delete()
171+
c.Assert(err, IsNil)
172+
}
173+
174+
// Test add Virtual Network to Cluster
175+
func (s *ClusterSuite) TestAddDelVnet(c *C) {
176+
// Create Virtual Network
177+
vnTmpl := "NAME = go_cluster_vn" + strconv.Itoa(rand.Intn(1000)) + "\n" +
178+
"BRIDGE = vbr0\n" +
179+
"VN_MAD = dummy\n"
180+
181+
vnID, err := testCtrl.VirtualNetworks().Create(vnTmpl, -1)
182+
c.Assert(err, IsNil)
183+
184+
// Add Network to Cluster
185+
clusterC := testCtrl.Cluster(s.clusterID)
186+
err = clusterC.AddVnet(vnID)
187+
c.Assert(err, IsNil)
188+
189+
// Get Cluster and check if Network was added to Cluster
190+
cluster, err := testCtrl.Cluster(s.clusterID).Info()
191+
c.Assert(err, IsNil)
192+
c.Assert(len(cluster.Vnets.ID), Equals, 1)
193+
c.Assert(cluster.Vnets.ID[0], Equals, vnID)
194+
195+
// Delete Network from Cluster
196+
err = clusterC.DelVnet(vnID)
197+
c.Assert(err, IsNil)
198+
199+
// Check if Network was added to Cluster
200+
cluster, err = testCtrl.Cluster(s.clusterID).Info()
201+
c.Assert(err, IsNil)
202+
c.Assert(len(cluster.Vnets.ID), Equals, 0)
203+
204+
// Delete Network
205+
err = testCtrl.VirtualNetwork(vnID).Delete()
206+
c.Assert(err, IsNil)
207+
}
208+
209+
// Test optimization plan api
210+
func (s *ClusterSuite) TestPlanApi(c *C) {
211+
// Create Optimization Plan
212+
clusterC := testCtrl.Cluster(s.clusterID)
213+
214+
err := clusterC.Optimize()
215+
c.Assert(err, IsNil)
216+
217+
// Execute Optimization Plan - we don't have any optimization plan
218+
// Just test the API call exists
219+
err = clusterC.PlanExecute()
220+
c.Assert(err, NotNil)
221+
oneErr, _ := err.(*errors.ResponseError);
222+
c.Assert(int(oneErr.Code), Equals, errors.OneActionError)
223+
224+
// Delete Optimization Plan - we don't have any optimization plan
225+
// Just test the API call exists
226+
err = clusterC.PlanDelete()
227+
c.Assert(err, NotNil)
228+
oneErr, _ = err.(*errors.ResponseError);
229+
c.Assert(int(oneErr.Code), Equals, errors.OneActionError)
230+
}

src/oca/java/src/org/opennebula/client/cluster/Cluster.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class Cluster extends PoolElement{
3838
private static final String ADDVNET = METHOD_PREFIX + "addvnet";
3939
private static final String DELVNET = METHOD_PREFIX + "delvnet";
4040
private static final String RENAME = METHOD_PREFIX + "rename";
41+
private static final String OPTIMIZE = METHOD_PREFIX + "optimize";
42+
private static final String PLANEXECUTE = METHOD_PREFIX + "planexecute";
43+
private static final String PLANDELETE = METHOD_PREFIX + "plandelete";
4144

4245
/**
4346
* Creates a new Cluster representation.
@@ -218,7 +221,7 @@ public static OneResponse delVnet(Client client, int id, int vnetId)
218221
* Renames this Cluster.
219222
*
220223
* @param client XML-RPC Client.
221-
* @param id The image id of the target host we want to modify.
224+
* @param id The cluster id.
222225
* @param name New name for the Cluster
223226
* @return If successful the message contains the cluster id.
224227
*/
@@ -227,6 +230,39 @@ public static OneResponse rename(Client client, int id, String name)
227230
return client.call(RENAME, id, name);
228231
}
229232

233+
/**
234+
* Optimize Cluster
235+
*
236+
* @param client XML-RPC Client.
237+
* @param id The cluster id.
238+
*/
239+
public static OneResponse optimize(Client client, int id)
240+
{
241+
return client.call(OPTIMIZE, id);
242+
}
243+
244+
/**
245+
* Execute Plan
246+
*
247+
* @param client XML-RPC Client.
248+
* @param id The cluster id.
249+
*/
250+
public static OneResponse planExecute(Client client, int id)
251+
{
252+
return client.call(PLANEXECUTE, id);
253+
}
254+
255+
/**
256+
* Delete Plan
257+
*
258+
* @param client XML-RPC Client.
259+
* @param id The cluster id.
260+
*/
261+
public static OneResponse planDelete(Client client, int id)
262+
{
263+
return client.call(PLANDELETE, id);
264+
}
265+
230266
// =================================
231267
// Instanced object XML-RPC methods
232268
// =================================
@@ -354,6 +390,36 @@ public OneResponse rename(String name)
354390
return rename(client, id, name);
355391
}
356392

393+
/**
394+
* Optimize Cluster
395+
*
396+
* @return If an error occurs the error message contains the reason.
397+
*/
398+
public OneResponse optimize()
399+
{
400+
return optimize(client, id);
401+
}
402+
403+
/**
404+
* Execute Plan
405+
*
406+
* @return If an error occurs the error message contains the reason.
407+
*/
408+
public OneResponse planExecute()
409+
{
410+
return planExecute(client, id);
411+
}
412+
413+
/**
414+
* Delete Plan
415+
*
416+
* @return If an error occurs the error message contains the reason.
417+
*/
418+
public OneResponse planDelete()
419+
{
420+
return planDelete(client, id);
421+
}
422+
357423
// =================================
358424
// Helpers
359425
// =================================

0 commit comments

Comments
 (0)