openstack_activeresource 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/hot_fixes.rb CHANGED
@@ -17,8 +17,9 @@
17
17
 
18
18
  # Reopens ActiveResource::Base to fix "ActiveResource nested resources not being persisted"
19
19
  # See https://github.com/rails/rails/pull/3107
20
- module ActiveResource
20
+ module ActiveResource #:nodoc:
21
21
  class Base
22
+
22
23
  def load(attributes, remove_root = false)
23
24
  raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
24
25
  @prefix_options, attributes = split_options(attributes)
@@ -49,7 +50,9 @@ module ActiveResource
49
50
  value.duplicable? ? value.dup : value
50
51
  end
51
52
  end
53
+
52
54
  self
53
55
  end
56
+
54
57
  end
55
58
  end
@@ -26,6 +26,7 @@ module OpenStack
26
26
  self.format = :json
27
27
  self.timeout = 5
28
28
 
29
+ # Set the X-Auth-Token header if the OpenStack authentication token is present
29
30
  def self.headers
30
31
  if defined?(@headers)
31
32
  _headers = @headers
@@ -44,11 +45,15 @@ module OpenStack
44
45
 
45
46
  protected
46
47
 
48
+ # Set the authentication token
47
49
  def self.token=(token)
50
+ # Trying to be thread safe here...
48
51
  Thread.current[:open_stack_token] = token.is_a?(OpenStack::Keystone::Public::Auth::Token) ? token.id : token
49
52
  end
50
53
 
54
+ # Get the current authentication token
51
55
  def self.token
56
+ # Trying to be thread safe here...
52
57
  Thread.current[:open_stack_token]
53
58
  end
54
59
 
@@ -19,13 +19,20 @@ module OpenStack
19
19
  module Keystone
20
20
  module Admin
21
21
 
22
+ # An OpenStack Role ("admin view")
23
+ #
24
+ # ==== Attributes
25
+ # * +name+ - The name of this role
22
26
  class Role < Base
23
27
  self.element_name = "OS-KSADM/role"
24
28
 
25
- def self.find_by_name(name)
26
- all.each { |role| return role if role.name == name }
29
+ schema do
30
+ attribute :name, :string
31
+ end
27
32
 
28
- nil
33
+ # List Roles with a given name
34
+ def self.find_by_name(name)
35
+ all.detect { |role| role.name == name }
29
36
  end
30
37
 
31
38
  end
@@ -19,6 +19,12 @@ module OpenStack
19
19
  module Keystone
20
20
  module Admin
21
21
 
22
+ # An OpenStack Tenant ("admin view")
23
+ #
24
+ # ==== Attributes
25
+ # * +name+ - The name of this tenant
26
+ # * +description+ - A description of this tenant
27
+ # * +enabled+ - True if this tenant is enabled
22
28
  class Tenant < Base
23
29
 
24
30
  schema do
@@ -31,7 +37,13 @@ module OpenStack
31
37
  validates :name, :format => {:with => /\A\w[\w\s]+\w\Z/}
32
38
  validates :description, :format => {:with => /\A[\w\s\.\-:@+,'"]+\Z/}
33
39
 
34
- def self.find_every(options)
40
+ def initialize(params = {}, persisted = false) #:notnew:
41
+ super(params, persisted)
42
+
43
+ self.description = description
44
+ end
45
+
46
+ def self.find_every(options) #:nodoc:
35
47
  class_name = self.name.split('::').last.downcase
36
48
  begin
37
49
  case from = options[:from]
@@ -52,29 +64,46 @@ module OpenStack
52
64
  end
53
65
  end
54
66
 
67
+ # List of tenant with a given name
68
+ #
69
+ # ==== Attributes
70
+ # * +name+ - A string
55
71
  def self.find_by_name(name)
56
72
  all.detect { |x| x.name == name }
57
73
  end
58
74
 
59
- def initialize(params = {}, persisted = false)
60
- super(params, persisted)
61
-
62
- self.description = description
63
- end
64
-
65
-
75
+ # List of Users (instances of OpenStack::Keystone::Admin::User) in this tenant
76
+ #
77
+ # ==== Attributes
78
+ # * +scope+ - An ActiveResource scope (defaults to :all)
66
79
  def users(scope = :all)
67
80
  User.find(scope, :params => {:tenant_id => self.id})
68
81
  end
69
82
 
83
+ # Returns the instance of OpenStack::Keystone::Admin::User with the given id
84
+ #
85
+ # ==== Attributes
86
+ # * +id+ - A string
70
87
  def user(id)
71
88
  users(id)
72
89
  end
73
90
 
91
+ # List if roles in this tenant for a given instance of OpenStack::Keystone::Admin::User or user id
92
+ #
93
+ # ==== Attributes
94
+ # * +user+ - A string
95
+ # * +scope+ - An ActiveResource scope (defaults to :all)
74
96
  def user_roles(user, scope = :all)
75
- Role.find(scope, :params => {:tenant_id => self.id, :user_id => user.is_a?(User) ? user.id : user})
97
+ user_id = user.is_a?(OpenStack::Keystone::Admin::User) ? user.id : user
98
+
99
+ Role.find(scope, :params => {:tenant_id => self.id, :user_id => user_id})
76
100
  end
77
101
 
102
+ # Adds a role to a user in this tenant
103
+ #
104
+ # ==== Attributes
105
+ # * +role+ - Instance of OpenStack::Keystone::Admin::Role or a role id
106
+ # * +user+ - Instance of OpenStack::Keystone::Admin::User or a user id
78
107
  def add_role_to_user(role, user)
79
108
  role_id = role.is_a?(OpenStack::Keystone::Admin::Role) ? role.id : role
80
109
  user_id = user.is_a?(OpenStack::Keystone::Admin::User) ? user.id : user
@@ -82,6 +111,11 @@ module OpenStack
82
111
  put("users/#{user_id}/roles/OS-KSADM/#{role_id}", {}, "null")
83
112
  end
84
113
 
114
+ # Removes a role to a user in this tenant
115
+ #
116
+ # ==== Attributes
117
+ # * +role+ - Instance of OpenStack::Keystone::Admin::Role or a role id
118
+ # * +user+ - Instance of OpenStack::Keystone::Admin::User or a user id
85
119
  def delete_role_from_user(role, user)
86
120
  role_id = role.is_a?(OpenStack::Keystone::Admin::Role) ? role.id : role
87
121
  user_id = user.is_a?(OpenStack::Keystone::Admin::User) ? user.id : user
@@ -89,10 +123,7 @@ module OpenStack
89
123
  delete("users/#{user_id}/roles/OS-KSADM/#{role_id}")
90
124
  end
91
125
 
92
- def role(user)
93
- users(user)
94
- end
95
-
126
+ # Returns a filtered description for this tenant
96
127
  def description=(description)
97
128
  @attributes[:description] = description.gsub /[^\w\s\.\-:@+,'"]/, '_' if description
98
129
 
@@ -19,6 +19,14 @@ module OpenStack
19
19
  module Keystone
20
20
  module Admin
21
21
 
22
+ # An OpenStack User ("admin view")
23
+ #
24
+ # ==== Attributes
25
+ # * +name+ - The name of this user
26
+ # * +password+ - Password (possibly encrypted) of this user
27
+ # * +email+ - E-mail address of this user
28
+ # * +enabled+ - True if this user is enabled
29
+ # * +tenant_id+ - Default (i.e. primary) tenant for this user
22
30
  class User < Base
23
31
 
24
32
  schema do
@@ -37,7 +45,7 @@ module OpenStack
37
45
  validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\Z/i
38
46
  validates :enabled, :presence => true, :inclusion => {:in => [true, false]}
39
47
 
40
- def initialize(attributes = {}, persisted = false)
48
+ def initialize(attributes = {}, persisted = false) #:notnew:
41
49
  attributes = attributes.with_indifferent_access
42
50
 
43
51
  if attributes[:tenant].present?
@@ -51,9 +59,8 @@ module OpenStack
51
59
  super(attributes, persisted)
52
60
  end
53
61
 
54
- # Overload ActiveRecord::encode method
55
- # Custom encoding to deal with openstack API
56
- def encode(options={})
62
+ # Overloads ActiveRecord::encode method
63
+ def encode(options={}) #:nodoc: Custom encoding to deal with openstack API
57
64
  to_encode = {
58
65
  :user => {
59
66
  :name => name,
@@ -68,12 +75,21 @@ module OpenStack
68
75
  to_encode.send("to_#{self.class.format.extension}", options)
69
76
  end
70
77
 
78
+ # List of users in a given tenant
79
+ #
80
+ # ==== Attributes
81
+ # * +tenant+ - An instance of OpenStack::Keystone::Admin::Tenant or a tenant id
71
82
  def self.all_by_tenant(tenant)
72
83
  tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : tenant
73
84
 
74
85
  all.select { |user| user.tenant_id == tenant_id }
75
86
  end
76
87
 
88
+ # Find a user in a given tenant
89
+ #
90
+ # ==== Attributes
91
+ # * +id+ - The user id
92
+ # * +tenant+ - An instance of OpenStack::Keystone::Admin::Tenant or a tenant id
77
93
  def self.find_by_tenant(id, tenant)
78
94
  tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : tenant
79
95
 
@@ -81,17 +97,28 @@ module OpenStack
81
97
  user.tenant_id == tenant_id ? user : nil
82
98
  end
83
99
 
100
+ # List of user with a given name
101
+ #
102
+ # ==== Attributes
103
+ # * +name+ - A string
84
104
  def self.find_by_name(name)
85
105
  all.detect { |user| user.name == name }
86
106
  end
87
-
107
+
108
+ # The primary (default) tenant (i.e. an instance of OpenStack::Keystone::Admin::Tenant) associated with this user
88
109
  def tenant
89
110
  OpenStack::Keystone::Admin::Tenant.find tenant_id
90
111
  end
91
112
 
113
+ # File role(s) (i.e. instances of OpenStack::Keystone::Admin::UserRole) for this user in a given tenant
114
+ #
115
+ # ==== Attributes
116
+ # * +scope+ - The ActiveResource scope (defaults to :all)
117
+ # * +tenant+ - An optional instance of OpenStack::Keystone::Admin::Tenant (or a tenant id). Defaults to the primary tenant for this user
92
118
  def roles(scope = :all, tenant = nil)
93
119
  tenant_id = tenant.is_a?(OpenStack::Keystone::Admin::Tenant) ? tenant.id : (tenant || self.tenant_id)
94
- OpenStack::Keystone::Admin::UserRole.find(scope, :params => { :tenant_id => tenant_id, :user_id => self.id })
120
+
121
+ OpenStack::Keystone::Admin::UserRole.find(scope, :params => {:tenant_id => tenant_id, :user_id => self.id})
95
122
  end
96
123
 
97
124
  end
@@ -19,10 +19,25 @@ module OpenStack
19
19
  module Keystone
20
20
  module Admin
21
21
 
22
+ # User Role ("admin view") (\*Warning:* incomplete)
23
+ #
24
+ # ==== Attributes
25
+ # * +name+ - The name of the Role
26
+ # * +description+ - A description of the role
22
27
  class UserRole < Base
23
28
  self.element_name = "role"
24
29
  self.site = superclass.site + "tenants/:tenant_id/users/:user_id"
25
30
 
31
+ schema do
32
+ attribute :name, :string
33
+ attribute :description, :string
34
+ end
35
+
36
+ # Return the associated instance of OpenStack::Keystone::Admin::Role
37
+ def role
38
+ OpenStack::Keystone::Admin::Role.find(self.id) if persisted?
39
+ end
40
+
26
41
  end
27
42
 
28
43
  end
@@ -19,6 +19,7 @@ module OpenStack
19
19
  module Keystone
20
20
  module Public
21
21
 
22
+ # End user authentication
22
23
  class Auth < Base
23
24
  self.element_name = "token"
24
25
 
@@ -31,7 +32,7 @@ module OpenStack
31
32
  validates :username, :presence => true, :unless => Proc.new { token.present? }
32
33
  validates :password, :presence => true, :unless => Proc.new { token.present? }
33
34
 
34
- def initialize(attributes = {}, persisted = false)
35
+ def initialize(attributes = {}, persisted = false) #:notnew:
35
36
  attributes[:username] ||= ""
36
37
  attributes[:password] ||= ""
37
38
 
@@ -44,9 +45,8 @@ module OpenStack
44
45
  super(attributes, persisted)
45
46
  end
46
47
 
47
- # Overload ActiveRecord::encode method
48
- # Custom encoding to deal with openstack API
49
- def encode(options={})
48
+ # Overloads ActiveRecord::encode method
49
+ def encode(options={}) #:nodoc: Custom encoding to deal with openstack API
50
50
  to_encode = {}
51
51
  if token.present?
52
52
  to_encode[:auth] = {
@@ -68,26 +68,33 @@ module OpenStack
68
68
  to_encode.send("to_#{self.class.format.extension}", options)
69
69
  end
70
70
 
71
- # Catch some exceptions to perform "remote validation" of this resource
72
- def save
71
+ def save #:nodoc: Catch some exceptions to perform "remote validation" of this resource
73
72
  super
74
73
  rescue ActiveResource::UnauthorizedAccess
75
74
  errors.add :password, I18n.t(:is_invalid)
76
75
  return false
77
76
  end
78
77
 
78
+ # Returns the service catalog for current authentication
79
79
  def service_catalog
80
80
  @attributes[:serviceCatalog].is_a?(Array) ? @attributes[:serviceCatalog] : []
81
81
  end
82
82
 
83
+ # Returns the OpenStack::Keystone::Public::Auth::Token instance for current authentication
83
84
  def token
84
85
  @attributes[:token]
85
86
  end
86
87
 
88
+ # Returns the token_id (string) for current authentication
87
89
  def token_id
88
90
  token.id if token.present?
89
91
  end
90
92
 
93
+ # Returns the list of endpoint for current authentication and for a given endpoint_type and region
94
+ #
95
+ # ==== Attributes
96
+ # * +endpoint_type+ - The type of endpoint. Currently valid values are: "Compute", "Volume"
97
+ # * +region+ - Restrict the search to given a region (can be omitted)
91
98
  def endpoints_for(endpoint_type, region=nil)
92
99
  return [] unless service_catalog.present?
93
100
 
@@ -105,18 +112,24 @@ module OpenStack
105
112
  endpoints
106
113
  end
107
114
 
115
+ # Returns the first endpoint for current authentication and for a given endpoint_type and region
116
+ #
117
+ # ==== Attributes
118
+ # * +endpoint_type+ - The type of endpoint. Currently valid values are: "Compute", "Volume"
119
+ # * +region+ - Restrict the search to given a region (can be omitted)
108
120
  def endpoint_for(endpoint_type, region=nil)
109
121
  endpoints_for(endpoint_type, region)[0]
110
122
  end
111
123
 
112
124
  end
113
125
 
126
+ # Authentication Token
114
127
  class Auth::Token < Base
115
128
  schema do
116
129
  attribute :expires, :string
117
130
  end
118
131
 
119
- def initialize(attributes = {}, persisted = false)
132
+ def initialize(attributes = {}, persisted = false) #:notnew:
120
133
  attributes = attributes.with_indifferent_access
121
134
  new_attributes = {
122
135
  :id => attributes[:id],
@@ -126,10 +139,12 @@ module OpenStack
126
139
  super(new_attributes, persisted)
127
140
  end
128
141
 
142
+ # Expiration date and time for this token
129
143
  def expires_at
130
144
  DateTime.strptime(attributes[:expires], OpenStack::DATETIME_FORMAT)
131
145
  end
132
146
 
147
+ # True if the token is expired
133
148
  def expired?
134
149
  DateTime.strptime(attributes[:expires], OpenStack::DATETIME_FORMAT) < DateTime.now.utc
135
150
  end
@@ -21,6 +21,7 @@ module OpenStack
21
21
 
22
22
  class Base < OpenStack::Common
23
23
 
24
+ # Get the Keystone endpoint assigned to OpenStack::Keystone::Public classes
24
25
  def self.site
25
26
  if self == OpenStack::Keystone::Public::Base
26
27
  Thread.current[:open_stack_keystone_public_site]
@@ -29,6 +30,7 @@ module OpenStack
29
30
  end
30
31
  end
31
32
 
33
+ # Set the Keystone endpoint assigned to OpenStack::Keystone::Public classes
32
34
  def self.site=(site)
33
35
  super(site)
34
36
  Thread.current[:open_stack_keystone_public_site] = @site
@@ -19,6 +19,12 @@ module OpenStack
19
19
  module Keystone
20
20
  module Public
21
21
 
22
+ # An OpenStack Tenant ("public view")
23
+ #
24
+ # ==== Attributes
25
+ # * +name+ - The name of the tenant
26
+ # * +description+ - A description of the tenant
27
+ # * +enabled+ - True if the tenant is enabled
22
28
  class Tenant < Base
23
29
 
24
30
  schema do
@@ -28,7 +34,7 @@ module OpenStack
28
34
 
29
35
  end
30
36
 
31
- def self.find_every(options)
37
+ def self.find_every(options) #:nodoc:
32
38
  class_name = self.name.split('::').last.downcase
33
39
  begin
34
40
  case from = options[:from]
@@ -49,12 +55,20 @@ module OpenStack
49
55
  end
50
56
  end
51
57
 
58
+ # List of tenants with a given name (accessible by the current authenticated user...)
59
+ #
60
+ # ==== Attributes
61
+ # * +name+ - A string
52
62
  def self.find_by_name(name)
53
- self.all.each { |tenant| return tenant if tenant.name == name }
63
+ all.detect { |tenant| tenant.name == name }
54
64
  end
55
65
 
66
+ # The first tenant with a given name (accessible by the current authenticated user...)
67
+ #
68
+ # ==== Attributes
69
+ # * +name+ - A string
56
70
  def self.find_all_by_name(name)
57
- self.all.reject! { |tenant| tenant.name != name }
71
+ all.reject! { |tenant| tenant.name != name }
58
72
  end
59
73
 
60
74
  end