lib/yao/resources/role.rb in yao-0.7.0 vs lib/yao/resources/role.rb in yao-0.8.0

- old
+ new

@@ -3,46 +3,101 @@ friendly_attributes :name, :description, :id self.service = "identity" self.resource_name = "role" self.resources_name = "roles" - self.resources_path = "/OS-KSADM/roles" self.admin = true class << self - def get_by_name(name) - self.list.find {|role| role.name == name } + + # override Yao::Resources::RestfullyAccessible#find_by_name + # This is workaround of keystone versioning v2.0/v3. + # @return [Array<Yao::Resources::Role>] + def find_by_name(name, query={}) + if api_version_v2? + list.select{|r| r.name == name} + else + super + end end - alias find_by_name get_by_name + # override Yao::Resources::RestfullyAccessible#resources_path + # This is workaround of keystone versioning v2.0/v3. + # @return [String] + def resources_path + if api_version_v2? + "OS-KSADM/roles" + else + resources_name + end + end + + # @param user_name [String] + # @param on [String] + # @return [Array<Yao::Resources::Role>] def list_for_user(user_name, on:) - user = Yao::User.get_by_name(user_name) - tenant = Yao::Tenant.get_by_name(on) - path = ["tenants", tenant.id, "users", user.id, "roles"].join("/") + user = Yao::User.get(user_name) + tenant = if api_version_v2? + Yao::Tenant.find_by_name(on) + else + Yao::Project.get(on) + end - with_resources_path(path) { self.list } + res = GET(path_for_role_resource(tenant, user)) + resources_from_json(res.body) end + # @param role_name [String] + # @param to: [String] + # @param on: [String] + # @return [Faraday::Response] def grant(role_name, to:, on:) - role = Yao::Role.get_by_name(role_name) - user = Yao::User.get_by_name(to) - tenant = Yao::Tenant.get_by_name(on) + role = Yao::Role.get(role_name) + user = Yao::User.get(to) + tenant = if api_version_v2? + Yao::Tenant.find_by_name(on) + else + Yao::Project.get(on) + end - PUT path_for_grant_revoke(tenant, user, role) + # response is "204 Not Content" + PUT(path_for_role_resource(tenant, user, role)) end + # @param role_name [String] + # @param from: [String] + # @param on: [String] + # @return [Faraday::Response] def revoke(role_name, from:, on:) - role = Yao::Role.get_by_name(role_name) - user = Yao::User.get_by_name(from) - tenant = Yao::Tenant.get_by_name(on) + role = Yao::Role.get(role_name) + user = Yao::User.get(from) + tenant = if api_version_v2? + Yao::Tenant.find_by_name(on) + else + Yao::Project.get(on) + end - DELETE path_for_grant_revoke(tenant, user, role) + # response is "204 Not Content" + DELETE(path_for_role_resource(tenant, user, role)) end private - def path_for_grant_revoke(tenant, user, role) - ["tenants", tenant.id, "users", user.id, "roles", "OS-KSADM", role.id].join("/") + # workaround of keystone versioning v2.0/v3 + # @return [Bool] + def api_version_v2? + client.url_prefix.to_s =~ /v2\.0/ + end + + def path_for_role_resource(tenant, user, role = nil) + if api_version_v2? + paths = ["tenants", tenant.id, "users", user.id, "roles"] + paths += ["OS-KSADM", role.id] if role + else + paths = ["projects", tenant.id, "users", user.id, "roles"] + paths << role.id if role + end + paths.join("/") end end end end