openstack_activeresource 0.1.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/.document +5 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +49 -0
- data/LICENSE.txt +14 -0
- data/README.rdoc +427 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/hot_fixes.rb +55 -0
- data/lib/locales/en.yml +5 -0
- data/lib/open_stack/base.rb +88 -0
- data/lib/open_stack/common.rb +56 -0
- data/lib/open_stack/glance/base.rb +39 -0
- data/lib/open_stack/glance/image.rb +33 -0
- data/lib/open_stack/glance.rb +27 -0
- data/lib/open_stack/keystone/admin/base.rb +41 -0
- data/lib/open_stack/keystone/admin/role.rb +35 -0
- data/lib/open_stack/keystone/admin/tenant.rb +104 -0
- data/lib/open_stack/keystone/admin/user.rb +98 -0
- data/lib/open_stack/keystone/admin/user_role.rb +34 -0
- data/lib/open_stack/keystone/admin.rb +32 -0
- data/lib/open_stack/keystone/public/auth.rb +141 -0
- data/lib/open_stack/keystone/public/base.rb +41 -0
- data/lib/open_stack/keystone/public/tenant.rb +64 -0
- data/lib/open_stack/keystone/public.rb +31 -0
- data/lib/open_stack/keystone.rb +27 -0
- data/lib/open_stack/nova/compute/base.rb +41 -0
- data/lib/open_stack/nova/compute/base_detail.rb +59 -0
- data/lib/open_stack/nova/compute/flavor.rb +56 -0
- data/lib/open_stack/nova/compute/floating_ip.rb +61 -0
- data/lib/open_stack/nova/compute/floating_ip_pool.rb +36 -0
- data/lib/open_stack/nova/compute/image.rb +76 -0
- data/lib/open_stack/nova/compute/key_pair.rb +47 -0
- data/lib/open_stack/nova/compute/network.rb +45 -0
- data/lib/open_stack/nova/compute/security_group.rb +117 -0
- data/lib/open_stack/nova/compute/server.rb +313 -0
- data/lib/open_stack/nova/compute/simple_tenant_usage.rb +128 -0
- data/lib/open_stack/nova/compute/volume_attachment.rb +98 -0
- data/lib/open_stack/nova/compute.rb +39 -0
- data/lib/open_stack/nova/volume/base.rb +41 -0
- data/lib/open_stack/nova/volume/volume.rb +77 -0
- data/lib/open_stack/nova/volume.rb +29 -0
- data/lib/open_stack/nova.rb +27 -0
- data/lib/open_stack.rb +40 -0
- data/lib/openstack_activeresource.rb +1 -0
- data/test/helper.rb +18 -0
- data/test/test_openstack-activeresource.rb +7 -0
- metadata +190 -0
@@ -0,0 +1,88 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
|
20
|
+
DATETIME_FORMAT="%Y-%m-%dT%H:%M:%S"
|
21
|
+
IPV4_REGEX= /\A(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\Z/
|
22
|
+
IPV4_CIDR_REGEX= /\A(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[1-2][0-9]|[0-9])\Z/
|
23
|
+
|
24
|
+
class Base < ActiveResource::Base
|
25
|
+
|
26
|
+
self.format = :json
|
27
|
+
self.timeout = 5
|
28
|
+
|
29
|
+
def self.headers
|
30
|
+
if defined?(@headers)
|
31
|
+
_headers = @headers
|
32
|
+
elsif self != OpenStack::Base && superclass.headers
|
33
|
+
_headers = superclass.headers
|
34
|
+
else
|
35
|
+
_headers = @headers || {}
|
36
|
+
end
|
37
|
+
|
38
|
+
if self.token.present?
|
39
|
+
_headers['X-Auth-Token'] = self.token.id
|
40
|
+
end
|
41
|
+
|
42
|
+
_headers
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def self.token=(token)
|
48
|
+
Thread.current[:open_stack_token] = token
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.token
|
52
|
+
Thread.current[:open_stack_token]
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
# Reopen ActiveResource::ClientError to add the proper parsing for OpenStack errors
|
60
|
+
class ActiveResource::ClientError < ActiveResource::ConnectionError
|
61
|
+
alias old_message message
|
62
|
+
alias old_to_s to_s
|
63
|
+
|
64
|
+
def message
|
65
|
+
decoded_error = decode_openstack_error
|
66
|
+
decoded_error.nil? ? old_message : decoded_error
|
67
|
+
rescue Exception => e
|
68
|
+
# Fallback to the original method
|
69
|
+
old_message
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_s
|
73
|
+
decoded_error = decode_openstack_error
|
74
|
+
decoded_error.nil? ? old_to_s : decoded_error
|
75
|
+
rescue Exception => e
|
76
|
+
# Fallback to the original method
|
77
|
+
old_to_s
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def decode_openstack_error
|
83
|
+
decoded_body = ActiveSupport::JSON.decode(self.response.body)
|
84
|
+
|
85
|
+
decoded_body[decoded_body.keys[0]]['message']
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
|
20
|
+
class Common < Base
|
21
|
+
|
22
|
+
# Overrides ActiveResource::Base::element_path to remove .<extension> from resources path
|
23
|
+
def self.element_path(id, prefix_options = {}, query_options = nil)
|
24
|
+
check_prefix_options(prefix_options)
|
25
|
+
|
26
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
27
|
+
"#{prefix(prefix_options)}#{collection_name}/#{URI.parser.escape id.to_s}#{query_string(query_options)}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Overrides ActiveResource::Base::collection_path to remove .<extension> from resources path
|
31
|
+
def self.collection_path(prefix_options = {}, query_options = nil)
|
32
|
+
check_prefix_options(prefix_options)
|
33
|
+
|
34
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
35
|
+
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Overrides ActiveResource::CustomMethods to remove .<extension>
|
39
|
+
def self.custom_method_collection_url(method_name, options = {})
|
40
|
+
prefix_options, query_options = split_options(options)
|
41
|
+
"#{prefix(prefix_options)}#{collection_name}/#{method_name}#{query_string(query_options)}"
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def custom_method_element_url(method_name, options = {})
|
47
|
+
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}#{self.class.__send__(:query_string, options)}"
|
48
|
+
end
|
49
|
+
|
50
|
+
def custom_method_new_element_url(method_name, options = {})
|
51
|
+
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}#{self.class.__send__(:query_string, options)}"
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Glance
|
20
|
+
|
21
|
+
class Base < OpenStack::Common
|
22
|
+
|
23
|
+
def self.site
|
24
|
+
if self == OpenStack::Glance::Base
|
25
|
+
Thread.current[:open_stack_glance_site]
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.site=(site)
|
32
|
+
super(site)
|
33
|
+
Thread.current[:open_stack_glance_site] = @site
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Glance
|
20
|
+
|
21
|
+
class Image < Base
|
22
|
+
schema do
|
23
|
+
attribute :name, :string
|
24
|
+
attribute :container_format, :string
|
25
|
+
attribute :disk_format, :string
|
26
|
+
attribute :checksum, :string
|
27
|
+
attribute :size, :integer
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
|
20
|
+
module Glance
|
21
|
+
extend ActiveSupport::Autoload
|
22
|
+
|
23
|
+
autoload :Base
|
24
|
+
autoload :Image
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
module Admin
|
21
|
+
|
22
|
+
class Base < OpenStack::Common
|
23
|
+
|
24
|
+
def self.site
|
25
|
+
if self == OpenStack::Keystone::Admin::Base
|
26
|
+
Thread.current[:open_stack_keystone_admin_site]
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.site=(site)
|
33
|
+
super(site)
|
34
|
+
Thread.current[:open_stack_keystone_admin_site] = @site
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
module Admin
|
21
|
+
|
22
|
+
class Role < Base
|
23
|
+
self.element_name = "OS-KSADM/role"
|
24
|
+
|
25
|
+
def self.find_by_name(name)
|
26
|
+
all.each { |role| return role if role.name == name }
|
27
|
+
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
module Admin
|
21
|
+
|
22
|
+
class Tenant < Base
|
23
|
+
|
24
|
+
schema do
|
25
|
+
attribute :enabled, :boolean
|
26
|
+
attribute :name, :string
|
27
|
+
attribute :description, :string
|
28
|
+
end
|
29
|
+
|
30
|
+
validates :enabled, :inclusion => {:in => [true, false]}
|
31
|
+
validates :name, :format => {:with => /\A\w[\w\s]+\w\Z/}
|
32
|
+
validates :description, :format => {:with => /\A[\w\s\.\-:@+,'"]+\Z/}
|
33
|
+
|
34
|
+
def self.find_every(options)
|
35
|
+
class_name = self.name.split('::').last.downcase
|
36
|
+
begin
|
37
|
+
case from = options[:from]
|
38
|
+
when Symbol
|
39
|
+
instantiate_collection(get(from, options[:params]))
|
40
|
+
when String
|
41
|
+
path = "#{from}#{query_string(options[:params])}"
|
42
|
+
instantiate_collection(format.decode(connection.get(path, headers).body)[class_name.pluralize] || [])
|
43
|
+
else
|
44
|
+
prefix_options, query_options = split_options(options[:params])
|
45
|
+
path = collection_path(prefix_options, query_options)
|
46
|
+
instantiate_collection((format.decode(connection.get(path, headers).body)[class_name.pluralize] || []), prefix_options)
|
47
|
+
end
|
48
|
+
rescue ActiveResource::ResourceNotFound
|
49
|
+
# Swallowing ResourceNotFound exceptions and return nil - as per
|
50
|
+
# ActiveRecord.
|
51
|
+
nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.find_by_name(name)
|
56
|
+
all.detect { |x| x.name == name }
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(params = {}, persisted = false)
|
60
|
+
super(params, persisted)
|
61
|
+
|
62
|
+
self.description = description
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def users(scope = :all)
|
67
|
+
User.find(scope, :params => {:tenant_id => self.id})
|
68
|
+
end
|
69
|
+
|
70
|
+
def user(id)
|
71
|
+
users(id)
|
72
|
+
end
|
73
|
+
|
74
|
+
def user_roles(user, scope = :all)
|
75
|
+
Role.find(scope, :params => {:tenant_id => self.id, :user_id => user.is_a?(User) ? user.id : user})
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_role_to_user(role, user)
|
79
|
+
role_id = role.is_a?(OpenStack::Keystone::Admin::Role) ? role.id : role
|
80
|
+
user_id = user.is_a?(OpenStack::Keystone::Admin::User) ? user.id : user
|
81
|
+
|
82
|
+
put("users/#{user_id}/roles/OS-KSADM/#{role_id}", {}, "null")
|
83
|
+
end
|
84
|
+
|
85
|
+
def delete_role_from_user(role, user)
|
86
|
+
role_id = role.is_a?(OpenStack::Keystone::Admin::Role) ? role.id : role
|
87
|
+
user_id = user.is_a?(OpenStack::Keystone::Admin::User) ? user.id : user
|
88
|
+
|
89
|
+
delete("users/#{user_id}/roles/OS-KSADM/#{role_id}")
|
90
|
+
end
|
91
|
+
|
92
|
+
def role(user)
|
93
|
+
users(user)
|
94
|
+
end
|
95
|
+
|
96
|
+
def description=(description)
|
97
|
+
@attributes[:description] = description.gsub /[^\w\s\.\-:@+,'"]/, '_' if description
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
module Admin
|
21
|
+
|
22
|
+
class User < Base
|
23
|
+
|
24
|
+
schema do
|
25
|
+
attribute :name, :string
|
26
|
+
attribute :password, :string
|
27
|
+
attribute :email, :string
|
28
|
+
attribute :enabled, :boolean
|
29
|
+
attribute :tenant_id, :string
|
30
|
+
end
|
31
|
+
|
32
|
+
validates :name, :presence => true
|
33
|
+
validates_length_of :name, :minimum => 4
|
34
|
+
validates_format_of :name, :with => /\A[0-9a-z_]+\Z/i
|
35
|
+
validates_format_of :password, :with => /(?=.*[\d\W])/, :message => :must_contain_at_least_one_digit_or_one_special_character
|
36
|
+
validates_length_of :password, :minimum => 8
|
37
|
+
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\Z/i
|
38
|
+
validates :enabled, :presence => true, :inclusion => {:in => [true, false]}
|
39
|
+
|
40
|
+
def initialize(attributes = {}, persisted = false)
|
41
|
+
attributes = attributes.with_indifferent_access
|
42
|
+
|
43
|
+
if attributes[:tenant].present?
|
44
|
+
attributes[:tenant_id] = attributes.delete(:tenant).id
|
45
|
+
end
|
46
|
+
|
47
|
+
if attributes[:tenantId].present?
|
48
|
+
attributes[:tenant_id] = attributes.delete(:tenantId)
|
49
|
+
end
|
50
|
+
|
51
|
+
super(attributes, persisted)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Overload ActiveRecord::encode method
|
55
|
+
# Custom encoding to deal with openstack API
|
56
|
+
def encode(options={})
|
57
|
+
to_encode = {
|
58
|
+
:user => {
|
59
|
+
:name => name,
|
60
|
+
:password => password,
|
61
|
+
:email => email,
|
62
|
+
:enabled => enabled
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
to_encode[:user][:tenantId] = tenant_id if @attributes[:tenant_id].present?
|
67
|
+
|
68
|
+
to_encode.send("to_#{self.class.format.extension}", options)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.all_by_tenant(tenant)
|
72
|
+
tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : tenant
|
73
|
+
|
74
|
+
all.select { |user| user.tenant_id == tenant_id }
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.find_by_tenant(id, tenant)
|
78
|
+
tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : tenant
|
79
|
+
|
80
|
+
user = self.find(id)
|
81
|
+
user.tenant_id == tenant_id ? user : nil
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.find_by_name(name)
|
85
|
+
all.each { |user| return user if user.name == name }
|
86
|
+
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
|
90
|
+
def roles(scope = :all)
|
91
|
+
OpenStack::Keystone::Admin::UserRole.find(scope, :params => { :tenant_id => self.tenant_id, :user_id => self.id })
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
module Admin
|
21
|
+
|
22
|
+
class UserRole < Base
|
23
|
+
self.element_name = "role"
|
24
|
+
|
25
|
+
def self.site
|
26
|
+
superclass.site + "tenants/:tenant_id/users/:user_id"
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# This file is part of the OpenStack-ActiveResource
|
2
|
+
#
|
3
|
+
# Copyright (C) 2012 Unidata S.p.A. (Davide Guerri - d.guerri@unidata.it)
|
4
|
+
#
|
5
|
+
# This program is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This program is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU General Public License
|
16
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module OpenStack
|
19
|
+
module Keystone
|
20
|
+
|
21
|
+
module Admin
|
22
|
+
extend ActiveSupport::Autoload
|
23
|
+
|
24
|
+
autoload :Base
|
25
|
+
autoload :Role
|
26
|
+
autoload :Tenant
|
27
|
+
autoload :User
|
28
|
+
autoload :UserRole
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|