webee 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Sergio Rubio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Installation
2
+
3
+ gem install webee
4
+
5
+ # Usage
6
+
7
+ $: << '../lib'
8
+ require 'webee'
9
+ require 'md5'
10
+
11
+ WeBee::Api.user = ENV['user'] || 'admin'
12
+ WeBee::Api.password = ENV['pass'] || 'xabiquo'
13
+ WeBee::Api.url = 'http://abiquo/api'
14
+
15
+ #
16
+ # List all datacenters available
17
+ #
18
+ WeBee::Datacenter.all.each do |dc|
19
+ puts dc.name
20
+ end
21
+
22
+
23
+ #
24
+ # Return the first datacenter whose name matches BCN (matched via Regexp)
25
+ #
26
+ dc = WeBee::Datacenter.find_by_name('BCN').first
27
+ puts dc.name
28
+
29
+ #
30
+ # List Datacenter Racks
31
+ #
32
+ dc.racks.each do |r|
33
+ puts r.name
34
+ end
35
+
36
+ #
37
+ # List all the enterprises
38
+ #
39
+ WeBee::Enterprise.all.each do |e|
40
+ puts e.name
41
+ end
42
+
43
+ #
44
+ # Find an enterprise named QA
45
+ #
46
+ qa_ent = WeBee::Enterprise.find_by_name('QA')
47
+
48
+ #
49
+ # List enterprise 'QA' users
50
+ #
51
+ qa_ent.users.each do |u|
52
+ puts u.name
53
+ end
54
+
55
+
56
+ #
57
+ # Create a new user in QA enterprise
58
+ #
59
+ user = qa_ent.create_user :name => 'Sergio Rubio',
60
+ :role => WeBee::UserRole.cloud_admin,
61
+ :password => MD5.hexdigest('rubiojr'),
62
+ :surname => 'rubiojr', #optional
63
+ :email => 'rubiojr@apient.com',
64
+ :locale => 'en_US',
65
+ :nick => 'rubiojr',
66
+ :active => 'true' #optional
67
+
68
+ # delete the user
69
+ user.delete
70
+
71
+ #
72
+ # Create a new datacenter
73
+ # FIXME: not implemented in WeBee
74
+ #
75
+ # WeBee::Datacenter.create :name => 'Test DC'
76
+
77
+ #
78
+ # List WeBee::OVFPackage available in enterprise QA
79
+ #
80
+ qa_ent.ovf_packages.each do |pkg|
81
+ puts "#{pkg.product_name} (#{pkg.category_name})"
82
+ end
83
+
84
+ #
85
+ # Create a new Rack
86
+ #
87
+ dc = WeBee::Datacenter.find_by_name('BCN').first
88
+ rack = WeBee::Rack.create dc.datacenter_id,
89
+ :name => 'rack_1',
90
+ :ha_enabled => false,
91
+ :vlan_min_id => 100,
92
+ :vlan_max_id => 2048,
93
+ :vlan_per_vdc_expected => 6,
94
+ :nrsq => 80
95
+
96
+ #
97
+ # Delete the rack
98
+ # FIXME: Not supported in Abiquo right now
99
+ #
100
+ #rack.delete
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ $:<< 'lib'
3
+ require 'webee'
4
+ require 'rake'
5
+
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
9
+ gem.name = "webee"
10
+ gem.homepage = "http://github.com/rubiojr/webee"
11
+ gem.license = "MIT"
12
+ gem.summary = %Q{Abiquo API Ruby Implementation}
13
+ gem.description = %Q{Abiquo API Ruby Implementation}
14
+ gem.email = "sergio@rubio.name"
15
+ gem.authors = ["Sergio Rubio"]
16
+ gem.version = WeBee::VERSION
17
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
18
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
19
+ gem.add_runtime_dependency 'sax-machine'
20
+ gem.add_runtime_dependency 'rest-client'
21
+ gem.add_runtime_dependency 'nokogiri'
22
+ gem.add_runtime_dependency 'activesupport'
23
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ task :default => :build
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "webee #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/bin/webee ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'webee'
5
+ require 'webee/command'
6
+ require 'mixlib/cli'
7
+ require 'pp'
8
+
9
+ config = YAML.load_file("#{ENV['HOME']}/.webeerc")
10
+
11
+ WeBee::Api.user = config[:user]
12
+ WeBee::Api.password = config[:password]
13
+ WeBee::Api.url = config[:url]
14
+
15
+ if not WeBee::Api.test_auth
16
+ $stderr.puts 'API Access Error. Check configuration.'
17
+ exit 1
18
+ end
19
+
20
+
21
+ cmd = ARGV.shift
22
+ if cmd.nil?
23
+ $stderr.puts "No command specified."
24
+ WeBee::Command.list_all.each do |cmd|
25
+ WeBee::Command.print_help cmd
26
+ end
27
+ end
28
+
29
+ if not WeBee::Command.list_all.find { |c| c == cmd }
30
+ $stderr.puts "Invalid command: #{cmd}"
31
+ else
32
+ WeBee::Command.execute cmd
33
+ end
@@ -0,0 +1,94 @@
1
+ $: << '../lib'
2
+ require 'webee'
3
+ require 'md5'
4
+
5
+ WeBee::Api.user = ENV['user'] || 'admin'
6
+ WeBee::Api.password = ENV['pass'] || 'xabiquo'
7
+ WeBee::Api.url = 'http://abiquo/api'
8
+
9
+ #
10
+ # List all datacenters available
11
+ #
12
+ WeBee::Datacenter.all.each do |dc|
13
+ puts dc.name
14
+ end
15
+
16
+
17
+ #
18
+ # Return the first datacenter whose name matches BCN (matched via Regexp)
19
+ #
20
+ dc = WeBee::Datacenter.find_by_name('BCN').first
21
+ puts dc.name
22
+
23
+ #
24
+ # List Datacenter Racks
25
+ #
26
+ dc.racks.each do |r|
27
+ puts r.name
28
+ end
29
+
30
+ #
31
+ # List all the enterprises
32
+ #
33
+ WeBee::Enterprise.all.each do |e|
34
+ puts e.name
35
+ end
36
+
37
+ #
38
+ # Find an enterprise named QA
39
+ #
40
+ qa_ent = WeBee::Enterprise.find_by_name('QA')
41
+
42
+ #
43
+ # List enterprise 'QA' users
44
+ #
45
+ qa_ent.users.each do |u|
46
+ puts u.name
47
+ end
48
+
49
+
50
+ #
51
+ # Create a new user in QA enterprise
52
+ #
53
+ user = qa_ent.create_user :name => 'Sergio Rubio',
54
+ :role => WeBee::UserRole.cloud_admin,
55
+ :password => MD5.hexdigest('rubiojr'),
56
+ :surname => 'rubiojr', #optional
57
+ :email => 'rubiojr@apient.com',
58
+ :locale => 'en_US',
59
+ :nick => 'rubiojr',
60
+ :active => 'true' #optional
61
+
62
+ # delete the user
63
+ user.delete
64
+
65
+ #
66
+ # Create a new datacenter
67
+ # FIXME: not implemented in WeBee
68
+ #
69
+ # WeBee::Datacenter.create :name => 'Test DC'
70
+
71
+ #
72
+ # List WeBee::OVFPackage available in enterprise QA
73
+ #
74
+ qa_ent.ovf_packages.each do |pkg|
75
+ puts "#{pkg.product_name} (#{pkg.category_name})"
76
+ end
77
+
78
+ #
79
+ # Create a new Rack
80
+ #
81
+ dc = WeBee::Datacenter.find_by_name('BCN').first
82
+ rack = WeBee::Rack.create dc.datacenter_id,
83
+ :name => 'rack_1',
84
+ :ha_enabled => false,
85
+ :vlan_min_id => 100,
86
+ :vlan_max_id => 2048,
87
+ :vlan_per_vdc_expected => 6,
88
+ :nrsq => 80
89
+
90
+ #
91
+ # Delete the rack
92
+ # FIXME: Not supported in Abiquo right now
93
+ #
94
+ #rack.delete
data/lib/webee.rb ADDED
@@ -0,0 +1,320 @@
1
+ require 'rubygems'
2
+ require 'rest_client'
3
+ require 'sax-machine'
4
+ require 'nokogiri'
5
+ require 'active_support/core_ext/hash'
6
+ require 'uri'
7
+
8
+ module WeBee
9
+
10
+ VERSION = '0.1'
11
+
12
+ module RestResource
13
+
14
+ module ClassMethods
15
+
16
+ def find(id)
17
+ self.parse(RestClient.get(Api.url + "#{@resource_path}/#{id}"))
18
+ end
19
+
20
+ def all(options = {})
21
+ col = []
22
+ doc = Nokogiri.parse(RestClient.get(Api.url + "#{@resource_path}"))
23
+ pages = doc.search('//totalSize').text.to_i / 25
24
+ pages = 1 if pages == 0
25
+ 1.upto(pages) do |p|
26
+ doc.search("//#{@resource_name}").each do |node|
27
+ col << self.parse(node.to_s)
28
+ end
29
+ end
30
+ col
31
+ end
32
+
33
+ def resource_path(path)
34
+ @resource_path = path
35
+ end
36
+
37
+ def resource_name(name)
38
+ @resource_name = name
39
+ end
40
+
41
+ end
42
+
43
+ def self.included(base)
44
+ base.extend(ClassMethods)
45
+ end
46
+
47
+ end
48
+
49
+
50
+ class Api
51
+ class << self
52
+ attr_accessor :user, :password, :url, :port
53
+
54
+ def url=(url)
55
+ @url = build_url(url)
56
+ end
57
+
58
+ private
59
+ def build_url(url)
60
+ port ||= 80
61
+ uri = URI.parse(url)
62
+ "http://#{user}:#{password}@#{uri.host}:#{uri.port}#{uri.path}"
63
+ end
64
+ end
65
+
66
+ def self.test_auth
67
+ begin
68
+ RestClient.get Api.url
69
+ rescue Exception
70
+ return false
71
+ end
72
+ true
73
+ end
74
+
75
+ end
76
+
77
+ class OVFPackage
78
+ include SAXMachine
79
+ include RestResource
80
+
81
+ element :link
82
+ element :categoryName, :as => :category_name
83
+ element :description
84
+ element :diskFormatTypeUri, :as => :disk_format_type_uri
85
+ element :diskSizeMb, :as => :dist_size_mb
86
+ element :productName, :as => :product_name
87
+ element :id, :as => :resource_id
88
+ element :url
89
+
90
+
91
+ #
92
+ # enterprise can be either 1 or an Enterprise object
93
+ #
94
+ def self.all(enterprise = 1)
95
+ if enterprise.is_a? Enterprise
96
+ enterprise_id = enterprise.resource_id
97
+ else
98
+ enterprise_id = enterprise
99
+ end
100
+ items = []
101
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/enterprises/#{enterprise_id}/appslib/ovfpackages", :content_type => :xml))
102
+ doc.search('//ovfPackage').each do |node|
103
+ items << OVFPackage.parse(node.to_s)
104
+ end
105
+ items
106
+ end
107
+
108
+ def self.find(enterprise_id = 1, package_id = 1)
109
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/enterprises/#{enterprise_id}/appslib/ovfpackages/#{package_id}"))
110
+ OVFPackage.parse(doc.root.to_s)
111
+ end
112
+ end
113
+
114
+ class Datacenter
115
+ include SAXMachine
116
+ include RestResource
117
+
118
+ resource_path '/admin/datacenters'
119
+ resource_name 'datacenter'
120
+
121
+ element :id, :as => :datacenter_id
122
+ element :name
123
+ element :ramSoft
124
+ element :ramHard
125
+ element :cpuSoft
126
+ element :cpuHard
127
+ element :storageSoft
128
+ element :storageHard
129
+ element :repositorySoft
130
+ element :repositoryHard
131
+ element :publicIPSoft
132
+ element :publicIPHard
133
+ element :hdSoft
134
+ element :hdHard
135
+ element :vlanSoft
136
+ element :vlanHard
137
+ element :location
138
+
139
+ def self.create(attributes)
140
+ xml = attributes.to_xml(:root => 'datacenter')
141
+ res = RestClient.post(Api.url + '/admin/datacenters', xml, :content_type => :xml, :accept => :xml)
142
+ Datacenter.parse(res)
143
+ end
144
+
145
+ #
146
+ # Return all the Datacenters matching name
147
+ #
148
+ def self.find_by_name(name, options = {})
149
+ Datacenter.all.find_all { |dc| dc.name =~ /#{name}/ }
150
+ end
151
+
152
+ def racks
153
+ items = []
154
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/datacenters/#{@datacenter_id}/racks", :accept => :xml))
155
+ doc.search
156
+ doc.search('//rack').each do |node|
157
+ items << Rack.parse(node.to_s)
158
+ end
159
+ items
160
+ end
161
+
162
+ end
163
+
164
+ class Rack
165
+ include SAXMachine
166
+
167
+ attr_accessor :datacenter_id
168
+
169
+ element :id, :as => :rack_id
170
+ element :longDescription, :as => :long_description
171
+ element :name
172
+ element :nrsq
173
+ element :shortDescription, :as => :short_description
174
+ element :vlanIdMax, :as => :vlan_id_max
175
+ element :vlanIdMin, :as => :vlan_id_min
176
+ element :vlanPerVdcExpected, :as => :vlan_per_vdc_expected
177
+ element :vlansIdAvoided, :as => :vlan_id_avoided
178
+ element :haEnabled, :as => :ha_enabled
179
+
180
+ def self.create(datacenter_id, attributes)
181
+ xml = attributes.to_xml(:root => 'rack')
182
+ res = RestClient.post(Api.url + "/admin/datacenters/#{datacenter_id}/racks", xml, :content_type => :xml, :accept => :xml)
183
+ r = Rack.parse(res)
184
+ r.datacenter_id = datacenter_id
185
+ r
186
+ end
187
+
188
+ def delete
189
+ RestClient.delete(Api.url + "/admin/datacenters/#{datacenter_id}/racks/#{rack_id}", :content_type => :xml)
190
+ end
191
+
192
+ end
193
+
194
+ class Enterprise
195
+ include SAXMachine
196
+ include RestResource
197
+
198
+ resource_path '/admin/enterprises'
199
+ resource_name 'enterprise'
200
+
201
+ element :id, :as => :resource_id
202
+ element :name
203
+
204
+ def delete
205
+ RestClient.delete(Api.url + "/admin/enterprises/#{resource_id}")
206
+ end
207
+
208
+ #
209
+ # may raise Exception if recuest is not successful
210
+ #
211
+ def self.create(attributes = {})
212
+ xml = attributes.to_xml(:root => 'enterprise')
213
+ res = RestClient.post(Api.url + '/admin/enterprises', xml, :content_type => :xml, :accept => :xml)
214
+ Enterprise.parse(res)
215
+ end
216
+
217
+ def self.find_by_name(name, options = {})
218
+ Enterprise.all(options).find { |e| e.name =~ /#{name}/ }
219
+ end
220
+
221
+ def ovf_packages
222
+ WeBee::OVFPackage.all(resource_id)
223
+ end
224
+
225
+ def users
226
+ col = []
227
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/enterprises/#{resource_id}/users"))
228
+ doc.search('//user').each do |node|
229
+ col << User.parse(node.to_s)
230
+ end
231
+ col
232
+ end
233
+
234
+ def create_user(attributes)
235
+ attributes[:enterprise] = self
236
+ User.create attributes
237
+ end
238
+
239
+ end
240
+
241
+ class User
242
+ include SAXMachine
243
+
244
+ attr_accessor :enterprise_id
245
+ element :name
246
+ element :surname
247
+ element :email
248
+ element :description
249
+ element :nick
250
+ element :active
251
+ element :id, :as => :user_id
252
+
253
+ #
254
+ # May raise exception if request is not successful
255
+ #
256
+ def self.create(attributes)
257
+ if attributes[:role]
258
+ role = "<link rel='role' href='#{attributes[:role]}'/>"
259
+ attributes.delete :role
260
+ end
261
+ eid = attributes[:enterprise].resource_id
262
+ attributes.delete :enterprise
263
+ xml = attributes.to_xml(:root => 'user')
264
+ xml = xml.gsub('</user>', "#{role}</user>")
265
+ res = RestClient.post(Api.url + "/admin/enterprises/#{eid}/users", xml, :content_type => :xml, :accept => :xml)
266
+ user = User.parse(res)
267
+ user.enterprise_id = eid
268
+ user
269
+ end
270
+
271
+ def delete
272
+ RestClient.delete(Api.url + "/admin/enterprises/#{enterprise_id}/users/#{user_id}")
273
+ end
274
+
275
+ def self.find(user_id, enterprise)
276
+ if enterprise.is_a? Enterprise
277
+ enterprise_id = enterprise.resource_id
278
+ else
279
+ enterprise_id = enterprise
280
+ end
281
+ User.parse(RestClient.get(Api.url + "/admin/enterprises/#{enterprise_id}/users/#{user_id}"))
282
+ end
283
+
284
+ def self.all(enterprise = nil)
285
+ if enterprise.is_a? Enterprise
286
+ enterprise_id = enterprise.resource_id
287
+ else
288
+ enterprise_id = enterprise
289
+ end
290
+ if enterprise.nil?
291
+ enterprise_id = '_'
292
+ end
293
+ u = []
294
+ doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/enterprises/#{enterprise_id}/users"))
295
+ doc.search('//user').each do |node|
296
+ u << User.parse(node.to_s)
297
+ end
298
+ u
299
+ end
300
+
301
+ end
302
+
303
+ class UserRole
304
+
305
+ def self.cloud_admin
306
+ Api.url + '/admin/roles/1'
307
+ end
308
+
309
+ def self.user
310
+ Api.url + '/admin/roles/2'
311
+ end
312
+
313
+ def self.enterprise_admin
314
+ Api.url + '/admin/roles/3'
315
+ end
316
+
317
+ end
318
+
319
+ end
320
+
@@ -0,0 +1,35 @@
1
+ module WeBee
2
+ class Command
3
+
4
+ def self.list_all
5
+ cmds = []
6
+ Dir[File.dirname(__FILE__) + '/commands/*.rb'].each do |cmd|
7
+ cmds << File.basename(cmd, '.rb')
8
+ end
9
+ cmds
10
+ end
11
+
12
+ def initialize(args)
13
+ @argv = args
14
+ end
15
+
16
+ def help
17
+ end
18
+
19
+ def self.print_help(cmd)
20
+ require "#{File.dirname(__FILE__)}/commands/#{cmd}.rb"
21
+ klass = WeBee::Commands.const_get cmd.capitalize
22
+ obj = klass.new(ARGV)
23
+ obj.help
24
+ end
25
+
26
+ def self.execute(cmd)
27
+ require "#{File.dirname(__FILE__)}/commands/#{cmd}.rb"
28
+ klass = WeBee::Commands.const_get cmd.capitalize
29
+ obj = klass.new(ARGV)
30
+ obj.parse_options
31
+ obj.run
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,44 @@
1
+ module WeBee
2
+ module Commands
3
+ class Appslib < Command
4
+ require 'mixlib/cli'
5
+ include Mixlib::CLI
6
+ option :help,
7
+ :short => "-h",
8
+ :long => "--help",
9
+ :description => "Show this message",
10
+ :on => :tail,
11
+ :boolean => true,
12
+ :show_options => true,
13
+ :exit => 0
14
+
15
+ option :enterprise,
16
+ :long => '--enterprise ID/NAME',
17
+ :default => 1
18
+
19
+
20
+ def run
21
+ actions = %w{list-ovfs}
22
+ if not actions.include? ARGV.first
23
+ $stderr.puts "Invalid action.\n\nAVAILABLE ACTIONS: #{actions.join(' ')}\n\n"
24
+ return
25
+ else
26
+ #if config[:name].nil?
27
+ # $stderr.puts 'Invalid Enterprise name.'
28
+ # $stderr.puts opt_parser.help
29
+ # return
30
+ #end
31
+ begin
32
+ ::WeBee::OVFPackage.all.each do |p|
33
+ puts p.productName
34
+ end
35
+ rescue Exception => e
36
+ puts e.message
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,41 @@
1
+ module WeBee
2
+ module Commands
3
+ class Enterprise < Command
4
+ require 'mixlib/cli'
5
+ include Mixlib::CLI
6
+ option :help,
7
+ :short => "-h",
8
+ :long => "--help",
9
+ :description => "Show this message",
10
+ :on => :tail,
11
+ :boolean => true,
12
+ :show_options => true,
13
+ :exit => 0
14
+
15
+ option :name,
16
+ :long => '--name NAME'
17
+
18
+ def run
19
+ actions = %w{create}
20
+ if not actions.include? ARGV.first
21
+ $stderr.puts "Invalid action.\n\nAVAILABLE ACTIONS: #{actions.join(' ')}\n\n"
22
+ return
23
+ else
24
+ if config[:name].nil?
25
+ $stderr.puts 'Invalid Enterprise name.'
26
+ $stderr.puts opt_parser.help
27
+ return
28
+ end
29
+
30
+ begin
31
+ ::WeBee::Enterprise.create :name => config[:name]
32
+ rescue Exception => e
33
+ puts e.message
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,114 @@
1
+ module WeBee
2
+ module Commands
3
+ class User < Command
4
+ require 'mixlib/cli'
5
+ include Mixlib::CLI
6
+ option :help,
7
+ :short => "-h",
8
+ :long => "--help",
9
+ :description => "Show this message",
10
+ :on => :tail,
11
+ :boolean => true,
12
+ :show_options => true,
13
+ :exit => 0
14
+
15
+ option :name,
16
+ :long => '--name NAME'
17
+
18
+ option :surname,
19
+ :long => '--surname SURNAME'
20
+
21
+ option :email,
22
+ :long => '--email EMAIL'
23
+
24
+ option :description,
25
+ :long => '--description DESC',
26
+ :default => ''
27
+
28
+ option :nick,
29
+ :long => '--nick NICK'
30
+
31
+ option :active,
32
+ :long => '--active VAL',
33
+ :default => 1
34
+
35
+ option :enterprise,
36
+ :long => '--enterprise ENT',
37
+ :default => 1
38
+
39
+ option :locale,
40
+ :long => '--locale LOCALE',
41
+ :default => 'en_US'
42
+
43
+ option :password,
44
+ :long => '--password PASS'
45
+
46
+ option :role,
47
+ :long => '--role ROLE',
48
+ :default => 'cloud-admin'
49
+
50
+ def run
51
+ roles_map = {
52
+ 'cloud-admin' => WeBee::UserRole.cloud_admin,
53
+ 'enterprise-admin' => WeBee::UserRole.enterprise_admin,
54
+ 'user' => WeBee::UserRole.user
55
+ }
56
+
57
+ actions = %w{create}
58
+ if not actions.include? ARGV.first
59
+ $stderr.puts "Invalid action.\n\nAVAILABLE ACTIONS: #{actions.join(' ')}\n\n"
60
+ return
61
+ else
62
+ if config[:name].nil?
63
+ $stderr.puts 'Invalid user name.'
64
+ $stderr.puts opt_parser.help
65
+ return
66
+ end
67
+ if config[:surname].nil?
68
+ $stderr.puts 'Invalid surname.'
69
+ $stderr.puts opt_parser.help
70
+ return
71
+ end
72
+ if config[:email].nil?
73
+ $stderr.puts 'Invalid email.'
74
+ $stderr.puts opt_parser.help
75
+ return
76
+ end
77
+ if config[:nick].nil?
78
+ $stderr.puts 'Invalid nick.'
79
+ $stderr.puts opt_parser.help
80
+ return
81
+ end
82
+ if config[:password].nil?
83
+ $stderr.puts 'Invalid password.'
84
+ $stderr.puts opt_parser.help
85
+ return
86
+ end
87
+ role = roles_map[config[:role]]
88
+ if role.nil?
89
+ $stderr.puts "Invalid role.\n\nAVAILABLE_ROLES: cloud-admin enterprise-admin user\n\n"
90
+ $stderr.puts opt_parser.help
91
+ return
92
+ end
93
+
94
+
95
+ begin
96
+ ::WeBee::User.create :name => config[:name],
97
+ :surname => config[:surname],
98
+ :email => config[:email],
99
+ :description => config[:description],
100
+ :nick => config[:nick],
101
+ :password => config[:password],
102
+ :role => role,
103
+ :locale => config[:locale],
104
+ :enterprise => Enterprise.find(config[:enterprise]),
105
+ :active => config[:active]
106
+ rescue Exception => e
107
+ puts e.message
108
+ end
109
+ end
110
+ end
111
+
112
+ end
113
+ end
114
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'webee'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestWebee < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webee
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Sergio Rubio
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-29 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: sax-machine
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rest-client
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: nokogiri
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :runtime
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: activesupport
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ type: :runtime
74
+ version_requirements: *id004
75
+ description: Abiquo API Ruby Implementation
76
+ email: sergio@rubio.name
77
+ executables:
78
+ - webee
79
+ extensions: []
80
+
81
+ extra_rdoc_files:
82
+ - LICENSE.txt
83
+ - README.md
84
+ files:
85
+ - .document
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/webee
90
+ - examples/basics.rb
91
+ - lib/webee.rb
92
+ - lib/webee/command.rb
93
+ - lib/webee/commands/appslib.rb
94
+ - lib/webee/commands/enterprise.rb
95
+ - lib/webee/commands/user.rb
96
+ - test/helper.rb
97
+ - test/test_webee.rb
98
+ homepage: http://github.com/rubiojr/webee
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.5
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Abiquo API Ruby Implementation
131
+ test_files: []
132
+