1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
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 #
8
# http://www.apache.org/licenses/LICENSE-2.0 #
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
#--------------------------------------------------------------------------- #
18
require 'OpenNebula/Pool'
21
class Cluster < PoolElement
22
#######################################################################
23
# Constants and Class Methods
24
#######################################################################
27
:info => "cluster.info",
28
:allocate => "cluster.allocate",
29
:delete => "cluster.delete",
30
:addhost => "cluster.addhost",
31
:delhost => "cluster.delhost",
32
:adddatastore => "cluster.adddatastore",
33
:deldatastore => "cluster.deldatastore",
34
:addvnet => "cluster.addvnet",
35
:delvnet => "cluster.delvnet"
38
# Creates a Cluster description with just its identifier
39
# this method should be used to create plain Cluster objects.
40
# +id+ the id of the host
43
# cluster = Cluster.new(Cluster.build_xml(3),rpc_client)
45
def Cluster.build_xml(pe_id=nil)
47
cluster_xml = "<CLUSTER><ID>#{pe_id}</ID></CLUSTER>"
49
cluster_xml = "<CLUSTER></CLUSTER>"
52
XMLElement.build_xml(cluster_xml,'CLUSTER')
56
def initialize(xml, client)
60
#######################################################################
61
# XML-RPC Methods for the Cluster Object
62
#######################################################################
64
# Retrieves the information of the given Cluster.
66
super(CLUSTER_METHODS[:info], 'CLUSTER')
69
# Allocates a new Cluster in OpenNebula
71
# +clustername+ A string containing the name of the Cluster.
72
def allocate(clustername)
73
super(CLUSTER_METHODS[:allocate], clustername)
78
super(CLUSTER_METHODS[:delete])
81
# Adds a Host to this Cluster
82
# @param hid [Integer] Host ID
83
# @return [nil, OpenNebula::Error] nil in case of success, Error
86
return Error.new('ID not defined') if !@pe_id
88
rc = @client.call(CLUSTER_METHODS[:addhost], @pe_id, hid)
89
rc = nil if !OpenNebula.is_error?(rc)
94
# Deletes a Host from this Cluster
95
# @param hid [Integer] Host ID
96
# @return [nil, OpenNebula::Error] nil in case of success, Error
99
return Error.new('ID not defined') if !@pe_id
101
rc = @client.call(CLUSTER_METHODS[:delhost], @pe_id, hid)
102
rc = nil if !OpenNebula.is_error?(rc)
107
# Adds a Datastore to this Cluster
108
# @param ds_id [Integer] Datastore ID
109
# @return [nil, OpenNebula::Error] nil in case of success, Error
111
def adddatastore(ds_id)
112
return Error.new('ID not defined') if !@pe_id
114
rc = @client.call(CLUSTER_METHODS[:adddatastore], @pe_id, ds_id)
115
rc = nil if !OpenNebula.is_error?(rc)
120
# Deletes a Datastore from this Cluster
121
# @param ds_id [Integer] Datastore ID
122
# @return [nil, OpenNebula::Error] nil in case of success, Error
124
def deldatastore(ds_id)
125
return Error.new('ID not defined') if !@pe_id
127
rc = @client.call(CLUSTER_METHODS[:deldatastore], @pe_id, ds_id)
128
rc = nil if !OpenNebula.is_error?(rc)
133
# Adds a VNet to this Cluster
134
# @param vnet_id [Integer] VNet ID
135
# @return [nil, OpenNebula::Error] nil in case of success, Error
138
return Error.new('ID not defined') if !@pe_id
140
rc = @client.call(CLUSTER_METHODS[:addvnet], @pe_id, vnet_id)
141
rc = nil if !OpenNebula.is_error?(rc)
146
# Deletes a VNet from this Cluster
147
# @param vnet_id [Integer] VNet ID
148
# @return [nil, OpenNebula::Error] nil in case of success, Error
151
return Error.new('ID not defined') if !@pe_id
153
rc = @client.call(CLUSTER_METHODS[:delvnet], @pe_id, vnet_id)
154
rc = nil if !OpenNebula.is_error?(rc)
159
# ---------------------------------------------------------------------
160
# Helpers to get information
161
# ---------------------------------------------------------------------
163
# Returns whether or not the host with 'id' is part of this cluster
164
# @param id [Integer|Array] host ID
165
# @return [Boolean] true if found
166
def contains_host?(id)
167
contains_resource?('HOSTS/ID', id)
170
# Returns an array with the numeric host ids
171
# @return [Array<Integer>]
175
self.each("HOSTS/ID") do |id|
176
array << id.text.to_i
182
# Returns whether or not the datastore with 'id' is part of this cluster
183
# @param id [Integer|Array] datastore ID
184
# @return [Boolean] true if found
185
def contains_datastore?(id)
186
contains_resource?('DATASTORES/ID', id)
189
# Returns an array with the numeric datastore ids
190
# @return [Array<Integer>]
194
self.each("DATASTORES/ID") do |id|
195
array << id.text.to_i
201
# Returns whether or not the vnet with 'id' is part of this cluster
202
# @param id [Integer|Arrray] vnet ID
203
# @return [Boolean] true if found
204
def contains_vnet?(id)
205
contains_resource?('VNETS/ID', id)
208
# Returns an array with the numeric vnet ids
209
# @return [Array<Integer>]
213
self.each("VNETS/ID") do |id|
214
array << id.text.to_i
222
def contains_resource?(xpath, id)
223
id_array = retrieve_elements(xpath)
225
return false if id_array.nil?
227
id = [id] if id.class != Array
230
return false if !id_array.include?(i.to_s)