oca 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/OpenNebula.rb ADDED
@@ -0,0 +1,144 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
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
+ begin # require 'rubygems'
18
+ require 'rubygems'
19
+ rescue Exception
20
+ end
21
+
22
+ require 'xmlrpc/client'
23
+ require 'digest/sha1'
24
+ require 'rexml/document'
25
+ require 'pp'
26
+
27
+ require 'OpenNebula/XMLUtils'
28
+ require 'OpenNebula/VirtualMachine'
29
+ require 'OpenNebula/VirtualMachinePool'
30
+ require 'OpenNebula/VirtualNetwork'
31
+ require 'OpenNebula/VirtualNetworkPool'
32
+ require 'OpenNebula/Image'
33
+ require 'OpenNebula/ImagePool'
34
+ require 'OpenNebula/User'
35
+ require 'OpenNebula/UserPool'
36
+ require 'OpenNebula/Host'
37
+ require 'OpenNebula/HostPool'
38
+ require 'OpenNebula/Template'
39
+ require 'OpenNebula/TemplatePool'
40
+ require 'OpenNebula/Group'
41
+ require 'OpenNebula/GroupPool'
42
+ require 'OpenNebula/Acl'
43
+ require 'OpenNebula/AclPool'
44
+
45
+ module OpenNebula
46
+
47
+ # -------------------------------------------------------------------------
48
+ # The Error Class represents a generic error in the OpenNebula
49
+ # library. It contains a readable representation of the error.
50
+ # Any function in the OpenNebula module will return an Error
51
+ # object in case of error.
52
+ # -------------------------------------------------------------------------
53
+ class Error
54
+ attr_reader :message
55
+
56
+ # +message+ a description of the error
57
+ def initialize(message=nil)
58
+ @message=message
59
+ end
60
+
61
+ def to_str()
62
+ @message
63
+ end
64
+ end
65
+
66
+ # -------------------------------------------------------------------------
67
+ # Returns true if the object returned by a method of the OpenNebula
68
+ # library is an Error
69
+ # -------------------------------------------------------------------------
70
+ def self.is_error?(value)
71
+ value.class==OpenNebula::Error
72
+ end
73
+
74
+ # -------------------------------------------------------------------------
75
+ # The client class, represents the connection with the core and handles the
76
+ # xml-rpc calls.
77
+ # -------------------------------------------------------------------------
78
+ class Client
79
+ attr_accessor :one_auth
80
+
81
+ begin
82
+ require 'xmlparser'
83
+ XMLPARSER=true
84
+ rescue LoadError
85
+ XMLPARSER=false
86
+ end
87
+
88
+ def initialize(secret=nil, endpoint=nil, hash=true)
89
+ if secret
90
+ one_secret = secret
91
+ elsif ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and File.file?(ENV["ONE_AUTH"])
92
+ one_secret=File.read(ENV["ONE_AUTH"])
93
+ elsif File.file?(ENV["HOME"]+"/.one/one_auth")
94
+ one_secret=File.read(ENV["HOME"]+"/.one/one_auth")
95
+ else
96
+ raise "ONE_AUTH file not present"
97
+ end
98
+
99
+ tokens = one_secret.chomp.split(':')
100
+
101
+ if tokens.length > 2
102
+ @one_auth = one_secret
103
+ elsif tokens.length == 2
104
+ if hash
105
+ pass = Digest::SHA1.hexdigest(tokens[1])
106
+ else
107
+ pass = tokens[1]
108
+ end
109
+ @one_auth = "#{tokens[0]}:#{pass}"
110
+ else
111
+ raise "Authorization file malformed"
112
+ end
113
+
114
+ if endpoint
115
+ @one_endpoint=endpoint
116
+ elsif ENV["ONE_XMLRPC"]
117
+ @one_endpoint=ENV["ONE_XMLRPC"]
118
+ else
119
+ @one_endpoint="http://localhost:2633/RPC2"
120
+ end
121
+
122
+ @server=XMLRPC::Client.new2(@one_endpoint)
123
+ end
124
+
125
+ def call(action, *args)
126
+
127
+ if XMLPARSER
128
+ @server.set_parser(XMLRPC::XMLParser::XMLStreamParser.new)
129
+ end
130
+
131
+ begin
132
+ response = @server.call_async("one."+action, @one_auth, *args)
133
+
134
+ if response[0] == false
135
+ Error.new(response[1])
136
+ else
137
+ response[1] #response[1..-1]
138
+ end
139
+ rescue Exception => e
140
+ Error.new(e.message)
141
+ end
142
+ end
143
+ end
144
+ end
data/lib/oca.rb ADDED
@@ -0,0 +1 @@
1
+ require 'OpenNebula.rb'
@@ -0,0 +1,82 @@
1
+ $: << '../' \
2
+ << './'
3
+
4
+ require 'OpenNebula'
5
+ require 'helpers/MockClient'
6
+
7
+ module OpenNebula
8
+
9
+ describe "Host using NOKOGIRI" do
10
+ before(:all) do
11
+ NOKOGIRI=true
12
+
13
+ client = MockClient.new()
14
+ @host_pool = HostPool.new(client)
15
+ end
16
+
17
+ it "should update the HOST_POOL info" do
18
+ rc = @host_pool.info()
19
+ rc.nil?.should eql(true)
20
+ end
21
+
22
+ it "should iterate the HOST_POOL elements and get info from them" do
23
+ rc = @host_pool.each{ |host|
24
+ host.class.to_s.should eql("OpenNebula::Host")
25
+ if host.id == 0
26
+ host.name.should eql('dummyhost')
27
+ host['STATE'].should eql('2')
28
+ host['IM_MAD'].should eql('im_dummy')
29
+ host['HOST_SHARE/MEM_USAGE'].should eql('1572864')
30
+ host['HOST_SHARE/CPU_USAGE'].should eql('300')
31
+ host['HOST_SHARE/FREE_MEM'].should eql('16777216')
32
+ host['HOST_SHARE/RUNNING_VMS'].should eql('3')
33
+ elsif host.id == 1
34
+ host.name.should eql('thost')
35
+ host['STATE'].should eql('2')
36
+ host['IM_MAD'].should eql('im_dummy')
37
+ host['HOST_SHARE/MEM_USAGE'].should eql('0')
38
+ host['HOST_SHARE/CPU_USAGE'].should eql('0')
39
+ host['HOST_SHARE/FREE_MEM'].should eql('16777216')
40
+ host['HOST_SHARE/RUNNING_VMS'].should eql('0')
41
+ end
42
+ }
43
+ end
44
+ end
45
+
46
+ describe "Host using REXML" do
47
+ before(:all) do
48
+ NOKOGIRI=false
49
+
50
+ client = MockClient.new()
51
+ @host_pool = HostPool.new(client)
52
+ end
53
+
54
+ it "should update the HOST_POOL info" do
55
+ rc = @host_pool.info()
56
+ rc.nil?.should eql(true)
57
+ end
58
+
59
+ it "should iterate the HOST_POOL elements and get info from them" do
60
+ rc = @host_pool.each{ |host|
61
+ host.class.to_s.should eql("OpenNebula::Host")
62
+ if host.id == 0
63
+ host.name.should eql('dummyhost')
64
+ host['STATE'].should eql('2')
65
+ host['IM_MAD'].should eql('im_dummy')
66
+ host['HOST_SHARE/MEM_USAGE'].should eql('1572864')
67
+ host['HOST_SHARE/CPU_USAGE'].should eql('300')
68
+ host['HOST_SHARE/FREE_MEM'].should eql('16777216')
69
+ host['HOST_SHARE/RUNNING_VMS'].should eql('3')
70
+ elsif host.id == 1
71
+ host.name.should eql('thost')
72
+ host['STATE'].should eql('2')
73
+ host['IM_MAD'].should eql('im_dummy')
74
+ host['HOST_SHARE/MEM_USAGE'].should eql('0')
75
+ host['HOST_SHARE/CPU_USAGE'].should eql('0')
76
+ host['HOST_SHARE/FREE_MEM'].should eql('16777216')
77
+ host['HOST_SHARE/RUNNING_VMS'].should eql('0')
78
+ end
79
+ }
80
+ end
81
+ end
82
+ end
data/test/Host_spec.rb ADDED
@@ -0,0 +1,220 @@
1
+ $: << '../' \
2
+ << './'
3
+
4
+ require 'OpenNebula'
5
+ require 'helpers/MockClient'
6
+
7
+ module OpenNebula
8
+
9
+ describe "Host using NOKOGIRI" do
10
+ before(:all) do
11
+ NOKOGIRI=true
12
+
13
+ @xml = Host.build_xml(7)
14
+
15
+ client = MockClient.new()
16
+ @host = Host.new(@xml,client)
17
+ end
18
+
19
+ it "should create a Nokogiri Node" do
20
+ @xml.class.to_s.should eql('Nokogiri::XML::NodeSet')
21
+ end
22
+
23
+ it "should allocate the new HOST" do
24
+ @host.allocate(nil,nil,nil,nil)
25
+
26
+ @host.id.should eql(7)
27
+ end
28
+
29
+ it "should update the HOST info" do
30
+ @host.info()
31
+
32
+ @host.id.should eql(7)
33
+ @host.name.should eql('dummyhost')
34
+ @host.state.should eql(2)
35
+ @host.state_str.should eql('MONITORED')
36
+ @host.short_state_str.should eql('on')
37
+ end
38
+
39
+ it "should enable the HOST" do
40
+ rc = @host.enable()
41
+
42
+ rc.should eql(nil)
43
+ end
44
+
45
+ it "should disable the HOST" do
46
+ rc = @host.disable()
47
+
48
+ rc.should eql(nil)
49
+ end
50
+
51
+ it "should delete the HOST" do
52
+ rc = @host.delete()
53
+
54
+ rc.should eql(nil)
55
+ end
56
+
57
+ it "should access an attribute using []" do
58
+ @host['ID'].should eql('7')
59
+ @host['NAME'].should eql('dummyhost')
60
+ @host['STATE'].should eql('2')
61
+ @host['IM_MAD'].should eql('im_dummy')
62
+ @host['LAST_MON_TIME'].should eql('1277733596')
63
+ @host['HOST_SHARE/MEM_USAGE'].should eql('1572864')
64
+ @host['HOST_SHARE/CPU_USAGE'].should eql('300')
65
+ @host['HOST_SHARE/FREE_CPU'].should eql('800')
66
+ @host['HOST_SHARE/RUNNING_VMS'].should eql('3')
67
+ @host['TEMPLATE/CPUSPEED'].should eql('2.2GHz')
68
+ @host['TEMPLATE/HYPERVISOR'].should eql('dummy')
69
+ @host['TEMPLATE/TOTALMEMORY'].should eql('16777216')
70
+ end
71
+ end
72
+
73
+ describe "Host using REXML" do
74
+ before(:all) do
75
+ NOKOGIRI=false
76
+
77
+ @xml = Host.build_xml(7)
78
+
79
+ client = MockClient.new()
80
+ @host = Host.new(@xml,client)
81
+ end
82
+
83
+ it "should create a REXML Element" do
84
+ @xml.class.to_s.should eql('REXML::Element')
85
+ end
86
+
87
+ it "should allocate the new HOST" do
88
+ @host.allocate(nil,nil,nil,nil)
89
+
90
+ @host.id.should eql(7)
91
+ end
92
+
93
+ it "should update the HOST info" do
94
+ @host.info()
95
+
96
+ @host.id.should eql(7)
97
+ @host.name.should eql('dummyhost')
98
+ @host.state.should eql(2)
99
+ @host.state_str.should eql('MONITORED')
100
+ @host.short_state_str.should eql('on')
101
+ end
102
+
103
+ it "should enable the HOST" do
104
+ rc = @host.enable()
105
+
106
+ rc.should eql(nil)
107
+ end
108
+
109
+ it "should disable the HOST" do
110
+ rc = @host.disable()
111
+
112
+ rc.should eql(nil)
113
+ end
114
+
115
+ it "should delete the HOST" do
116
+ rc = @host.delete()
117
+
118
+ rc.should eql(nil)
119
+ end
120
+
121
+ it "should access an attribute using []" do
122
+ @host['ID'].should eql('7')
123
+ @host['NAME'].should eql('dummyhost')
124
+ @host['STATE'].should eql('2')
125
+ @host['IM_MAD'].should eql('im_dummy')
126
+ @host['LAST_MON_TIME'].should eql('1277733596')
127
+ @host['HOST_SHARE/MEM_USAGE'].should eql('1572864')
128
+ @host['HOST_SHARE/CPU_USAGE'].should eql('300')
129
+ @host['HOST_SHARE/FREE_CPU'].should eql('800')
130
+ @host['HOST_SHARE/RUNNING_VMS'].should eql('3')
131
+ @host['TEMPLATE/CPUSPEED'].should eql('2.2GHz')
132
+ @host['TEMPLATE/HYPERVISOR'].should eql('dummy')
133
+ @host['TEMPLATE/TOTALMEMORY'].should eql('16777216')
134
+ end
135
+ end
136
+
137
+
138
+ describe "Host using NOKOGIRI without id" do
139
+ before(:all) do
140
+ NOKOGIRI=true
141
+
142
+ @xml = Host.build_xml()
143
+
144
+ client = MockClient.new()
145
+ @host = Host.new(@xml,client)
146
+ end
147
+
148
+ it "should create a Nokogiri Node" do
149
+ @xml.class.to_s.should eql('Nokogiri::XML::NodeSet')
150
+ end
151
+
152
+ it "should get Error getting info" do
153
+ rc = @host.info()
154
+
155
+ OpenNebula.is_error?(rc).should eql(true)
156
+ @host.id.should eql(nil)
157
+ @host.name.should eql(nil)
158
+ end
159
+
160
+ it "should enable the HOST" do
161
+ rc = @host.enable()
162
+
163
+ OpenNebula.is_error?(rc).should eql(true)
164
+ end
165
+
166
+ it "should disable the HOST" do
167
+ rc = @host.disable()
168
+
169
+ OpenNebula.is_error?(rc).should eql(true)
170
+ end
171
+
172
+ it "should get Error deleting the HOST" do
173
+ rc = @host.delete()
174
+
175
+ OpenNebula.is_error?(rc).should eql(true)
176
+ end
177
+ end
178
+
179
+ describe "Host using REXML without id" do
180
+ before(:all) do
181
+ NOKOGIRI=false
182
+
183
+ @xml = Host.build_xml()
184
+
185
+ client = MockClient.new()
186
+ @host = Host.new(@xml,client)
187
+ end
188
+
189
+ it "should create a REXML Element" do
190
+ @xml.class.to_s.should eql('REXML::Element')
191
+ end
192
+
193
+ it "should get Error getting info" do
194
+ rc = @host.info()
195
+
196
+ OpenNebula.is_error?(rc).should eql(true)
197
+ @host.id.should eql(nil)
198
+ @host.name.should eql(nil)
199
+ end
200
+
201
+ it "should enable the HOST" do
202
+ rc = @host.enable()
203
+
204
+ OpenNebula.is_error?(rc).should eql(true)
205
+ end
206
+
207
+ it "should disable the HOST" do
208
+ rc = @host.disable()
209
+
210
+ OpenNebula.is_error?(rc).should eql(true)
211
+ end
212
+
213
+ it "should get Error deleting the HOST" do
214
+ rc = @host.delete()
215
+
216
+ OpenNebula.is_error?(rc).should eql(true)
217
+ end
218
+ end
219
+
220
+ end
@@ -0,0 +1,66 @@
1
+ $: << '../' \
2
+ << './'
3
+
4
+ require 'OpenNebula'
5
+ require 'helpers/MockClient'
6
+
7
+ module OpenNebula
8
+
9
+ describe "User using NOKOGIRI" do
10
+ before(:all) do
11
+ NOKOGIRI=true
12
+
13
+ client = MockClient.new()
14
+ @user_pool = UserPool.new(client)
15
+ end
16
+
17
+ it "should update the USER_POOL info" do
18
+ rc = @user_pool.info()
19
+ rc.nil?.should eql(true)
20
+ end
21
+
22
+ it "should iterate the USER_POOL elements and get info from them" do
23
+ rc = @user_pool.each{ |user|
24
+ user.class.to_s.should eql("OpenNebula::User")
25
+ if user.id == 0
26
+ user.name.should eql('oneadmin')
27
+ user['PASSWORD'].should eql('f13a1234833436f71ab846572d251c0d40391e72')
28
+ user['ENABLED'].should eql('1')
29
+ elsif user.id == 1
30
+ user.name.should eql('dan')
31
+ user['PASSWORD'].should eql('d22a12348334v33f71ba846572d25250d40701e72')
32
+ user['ENABLED'].should eql('0')
33
+ end
34
+ }
35
+ end
36
+ end
37
+
38
+ describe "User using REXML" do
39
+ before(:all) do
40
+ NOKOGIRI=false
41
+
42
+ client = MockClient.new()
43
+ @user_pool = UserPool.new(client)
44
+ end
45
+
46
+ it "should update the USER_POOL info" do
47
+ rc = @user_pool.info()
48
+ rc.nil?.should eql(true)
49
+ end
50
+
51
+ it "should iterate the USER_POOL elements and get info from them" do
52
+ rc = @user_pool.each{ |user|
53
+ user.class.to_s.should eql("OpenNebula::User")
54
+ if user.id == 0
55
+ user.name.should eql('oneadmin')
56
+ user['PASSWORD'].should eql('f13a1234833436f71ab846572d251c0d40391e72')
57
+ user['ENABLED'].should eql('1')
58
+ elsif user.id == 1
59
+ user.name.should eql('dan')
60
+ user['PASSWORD'].should eql('d22a12348334v33f71ba846572d25250d40701e72')
61
+ user['ENABLED'].should eql('0')
62
+ end
63
+ }
64
+ end
65
+ end
66
+ end
data/test/User_spec.rb ADDED
@@ -0,0 +1,146 @@
1
+ $: << '../' \
2
+ << './'
3
+
4
+ require 'OpenNebula'
5
+ require 'helpers/MockClient'
6
+
7
+ module OpenNebula
8
+
9
+ describe "User using NOKOGIRI" do
10
+ before(:all) do
11
+ NOKOGIRI=true
12
+
13
+ @xml = User.build_xml(3)
14
+
15
+ client = MockClient.new()
16
+ @user = User.new(@xml,client)
17
+ end
18
+
19
+ it "should create a Nokogiri Node" do
20
+ @xml.class.to_s.should eql('Nokogiri::XML::NodeSet')
21
+ end
22
+
23
+ it "should allocate the new USER" do
24
+ @user.allocate(nil,nil)
25
+
26
+ @user.id.should eql(3)
27
+ end
28
+
29
+ it "should update the USER info" do
30
+ @user.info()
31
+
32
+ @user.id.should eql(3)
33
+ @user.name.should eql('dan')
34
+ end
35
+
36
+ it "should delete the USER" do
37
+ rc = @user.delete()
38
+
39
+ rc.should eql(nil)
40
+ end
41
+
42
+ it "should access an attribute using []" do
43
+ @user['ID'].should eql('3')
44
+ @user['NAME'].should eql('dan')
45
+ @user['PASSWORD'].should eql('d22a12348334v33f71ba846572d25250d40701e72')
46
+ @user['ENABLED'].should eql('0')
47
+ end
48
+ end
49
+
50
+ describe "User using REXML" do
51
+ before(:all) do
52
+ NOKOGIRI=false
53
+
54
+ @xml = User.build_xml(3)
55
+
56
+ client = MockClient.new()
57
+ @user = User.new(@xml,client)
58
+ end
59
+
60
+ it "should create a REXML Element" do
61
+ @xml.class.to_s.should eql('REXML::Element')
62
+ end
63
+
64
+ it "should allocate the new USER" do
65
+ @user.allocate(nil,nil)
66
+
67
+ @user.id.should eql(3)
68
+ end
69
+
70
+ it "should update the USER info" do
71
+ @user.info()
72
+
73
+ @user.id.should eql(3)
74
+ @user.name.should eql('dan')
75
+ end
76
+
77
+ it "should delete the USER" do
78
+ rc = @user.delete()
79
+
80
+ rc.should eql(nil)
81
+ end
82
+
83
+ it "should access an attribute using []" do
84
+ @user['ID'].should eql('3')
85
+ @user['NAME'].should eql('dan')
86
+ @user['PASSWORD'].should eql('d22a12348334v33f71ba846572d25250d40701e72')
87
+ @user['ENABLED'].should eql('0')
88
+ end
89
+ end
90
+
91
+
92
+ describe "User using NOKOGIRI without id" do
93
+ before(:all) do
94
+ NOKOGIRI=true
95
+
96
+ @xml = User.build_xml()
97
+
98
+ client = MockClient.new()
99
+ @user = User.new(@xml,client)
100
+ end
101
+
102
+ it "should create a Nokogiri Node" do
103
+ @xml.class.to_s.should eql('Nokogiri::XML::NodeSet')
104
+ end
105
+
106
+ it "should get Error getting info" do
107
+ rc = @user.info()
108
+
109
+ OpenNebula.is_error?(rc).should eql(true)
110
+ end
111
+
112
+ it "should get Error deleting the USER" do
113
+ rc = @user.delete()
114
+
115
+ OpenNebula.is_error?(rc).should eql(true)
116
+ end
117
+ end
118
+
119
+ describe "User using REXML without id" do
120
+ before(:all) do
121
+ NOKOGIRI=false
122
+
123
+ @xml = User.build_xml()
124
+
125
+ client = MockClient.new()
126
+ @user = User.new(@xml,client)
127
+ end
128
+
129
+ it "should create a REXML Element" do
130
+ @xml.class.to_s.should eql('REXML::Element')
131
+ end
132
+
133
+ it "should get Error getting info" do
134
+ rc = @user.info()
135
+
136
+ OpenNebula.is_error?(rc).should eql(true)
137
+ end
138
+
139
+ it "should get Error deleting the USER" do
140
+ rc = @user.delete()
141
+
142
+ OpenNebula.is_error?(rc).should eql(true)
143
+ end
144
+ end
145
+
146
+ end