yao 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e696fbc7834e739c27a29deb49679d3460773cb
4
- data.tar.gz: 250225f1088c806b3f5dbd35fca145cabf8ef666
3
+ metadata.gz: ba36ff08aa0365694cdd8b503c8f281326757bdd
4
+ data.tar.gz: 5eb339c8efba8d3120407cbca1be572ca7caef2f
5
5
  SHA512:
6
- metadata.gz: 3a8560868dd98e7a2a9d9175691b9508c73ecaa4fe5b8bcef63d7471dc5d1decafba99249b07a0c09a86a47a5a5644a1e36658251c2250529994f8e81ca411fa
7
- data.tar.gz: 23bb94097a85315a46770eeb87fddcdbc119916d4adb1da78079ea1ab4218899d1852f9a56e18ca4ecce0f8cd353a474be791d4d35fb1045ba6c4ea85feef605
6
+ metadata.gz: c6c1910d273332de80491414bf07000ba0197b661e834bbd21c389af91b7ee4efe070719ef1a0da4945301d4122ef57acbd349215deb5020a4705ca0c8b25b5c
7
+ data.tar.gz: 7740df0df2806e24a2ba621185abacca35c87df2187d367d304de70b1a96847eb20616adb3ceb50a9e81c6513d1dcc05c2e85715d3d434afe8aad21cff10392c
data/lib/yao/client.rb CHANGED
@@ -8,24 +8,33 @@ module Yao
8
8
  module Client
9
9
  class ClientSet
10
10
  def initialize
11
- @pool = {}
11
+ @pool = {}
12
+ @admin_pool = {}
12
13
  end
13
- attr_reader :pool
14
+ attr_reader :pool, :admin_pool
14
15
 
15
16
  %w(default compute network image metering volume orchestration identity).each do |type|
16
17
  define_method(type) do
17
18
  self.pool[type]
18
19
  end
20
+
21
+ define_method("#{type}_admin") do
22
+ self.admin_pool[type]
23
+ end
19
24
  end
20
25
 
21
26
  def register_endpoints(endpoints, token: nil)
22
- endpoints.each_pair do |type, endpoint|
27
+ endpoints.each_pair do |type, urls|
23
28
  # XXX: neutron just have v2.0 API and endpoint may not have version prefix
24
- if type == "network" && URI.parse(endpoint).path == "/"
25
- endpoint = File.join(endpoint, "v2.0")
29
+ if type == "network"
30
+ urls = urls.map {|public_or_admin, url|
31
+ url = URI.parse(url).path == "/" ? File.join(url, "v2.0") : url
32
+ [public_or_admin, url]
33
+ }.to_h
26
34
  end
27
35
 
28
- self.pool[type] = Yao::Client.gen_client(endpoint, token: token)
36
+ self.pool[type] = Yao::Client.gen_client(urls[:public_url], token: token)
37
+ self.admin_pool[type] = Yao::Client.gen_client(urls[:admin_url], token: token)
29
38
  end
30
39
  end
31
40
  end
@@ -66,7 +75,7 @@ module Yao
66
75
 
67
76
  def reset_client(new_endpoint=nil)
68
77
  set = ClientSet.new
69
- set.register_endpoints("default" => (new_endpoint || Yao.config.endpoint))
78
+ set.register_endpoints("default" => {public_url: new_endpoint || Yao.config.endpoint})
70
79
  self.default_client = set
71
80
  end
72
81
  end
@@ -23,24 +23,60 @@ module Yao::Resources
23
23
  @service
24
24
  end
25
25
 
26
+ def admin=(bool)
27
+ @admin = bool
28
+ end
29
+
30
+ def return_single_on_querying=(bool)
31
+ @return_single_on_querying = bool
32
+ end
33
+
34
+ def resources_path
35
+ @resources_path || resources_name
36
+ end
37
+
38
+ def resources_path=(path)
39
+ @resources_path = path.sub(%r!^\/!, "")
40
+ end
41
+
26
42
  def client
27
- Yao.default_client.pool[service]
43
+ if @admin
44
+ Yao.default_client.admin_pool[service]
45
+ else
46
+ Yao.default_client.pool[service]
47
+ end
48
+ end
49
+
50
+ def as_member(&blk)
51
+ if @admin
52
+ @admin = false
53
+ result = yield(blk)
54
+ @admin = true
55
+ result
56
+ else
57
+ yield blk
58
+ end
28
59
  end
29
60
 
30
61
  # restful methods
31
62
  def list(query={})
32
- return_resources(GET(resources_name, query).body[resources_name_in_json])
63
+ json = GET(resources_path, query).body
64
+ if @return_single_on_querying && !query.empty?
65
+ return_resource(json[resource_name_in_json])
66
+ else
67
+ return_resources(json[resources_name_in_json])
68
+ end
33
69
  end
34
70
 
35
71
  def list_detail(query={})
36
- return_resources(GET([resources_name, "detail"].join("/"), query).body[resources_name_in_json])
72
+ return_resources(GET([resources_path, "detail"].join("/"), query).body[resources_name_in_json])
37
73
  end
38
74
 
39
75
  def get(id_or_permalink, query={})
40
76
  res = if id_or_permalink =~ /^https?:\/\//
41
77
  GET(id_or_permalink, query)
42
78
  else
43
- GET([resources_name, id_or_permalink].join("/"), query)
79
+ GET([resources_path, id_or_permalink].join("/"), query)
44
80
  end
45
81
  return_resource(res.body[resource_name_in_json])
46
82
  end
@@ -50,7 +86,7 @@ module Yao::Resources
50
86
  params = {
51
87
  resource_name_in_json => resource_params
52
88
  }
53
- res = POST(resources_name) do |req|
89
+ res = POST(resources_path) do |req|
54
90
  req.body = params.to_json
55
91
  req.headers['Content-Type'] = 'application/json'
56
92
  end
@@ -61,7 +97,7 @@ module Yao::Resources
61
97
  params = {
62
98
  resource_name_in_json => resource_params
63
99
  }
64
- res = PUT([resources_name, id].join("/")) do |req|
100
+ res = PUT([resources_path, id].join("/")) do |req|
65
101
  req.body = params.to_json
66
102
  req.headers['Content-Type'] = 'application/json'
67
103
  end
@@ -69,7 +105,7 @@ module Yao::Resources
69
105
  end
70
106
 
71
107
  def destroy(id)
72
- res = DELETE([resources_name, id].join("/"))
108
+ res = DELETE([resources_path, id].join("/"))
73
109
  res.body
74
110
  end
75
111
 
@@ -0,0 +1,11 @@
1
+ module Yao::Resources
2
+ class Role < Base
3
+ friendly_attributes :name, :description, :id
4
+
5
+ self.service = "identity"
6
+ self.resource_name = "role"
7
+ self.resources_name = "roles"
8
+ self.resources_path = "/OS-KSADM/roles"
9
+ self.admin = true
10
+ end
11
+ end
@@ -5,5 +5,17 @@ module Yao::Resources
5
5
  self.service = "identity"
6
6
  self.resource_name = "tenant"
7
7
  self.resources_name = "tenants"
8
+ self.admin = true
9
+
10
+ class << self
11
+ def get_by_name(name)
12
+ self.get("", name: name)
13
+ end
14
+ alias find_by_name get_by_name
15
+
16
+ def accessible
17
+ as_member { self.list }
18
+ end
19
+ end
8
20
  end
9
21
  end
@@ -0,0 +1,20 @@
1
+ module Yao::Resources
2
+ class User < Base
3
+ friendly_attributes :name, :email, :enabled, :id
4
+
5
+ alias enabled? enabled
6
+
7
+ self.service = "identity"
8
+ self.resource_name = "user"
9
+ self.resources_name = "users"
10
+ self.admin = true
11
+ self.return_single_on_querying = true
12
+
13
+ class << self
14
+ def get_by_name(name)
15
+ self.list(name: name)
16
+ end
17
+ alias find_by_name get_by_name
18
+ end
19
+ end
20
+ end
data/lib/yao/resources.rb CHANGED
@@ -15,7 +15,8 @@ module Yao
15
15
  autoload :Port, "yao/resources/port"
16
16
  autoload :Tenant, "yao/resources/tenant"
17
17
  autoload :Host, "yao/resources/host"
18
-
18
+ autoload :User, "yao/resources/user"
19
+ autoload :Role, "yao/resources/role"
19
20
  end
20
21
 
21
22
  def self.const_missing(name)
data/lib/yao/token.rb CHANGED
@@ -47,16 +47,15 @@ module Yao
47
47
  return unless _endpoints
48
48
 
49
49
  _endpoints.each do |endpoint_data|
50
- key = endpoint_data["type"]
51
- value = if d = endpoint_data["endpoints"].first
52
- d["publicURL"]
53
- else
54
- nil
55
- end
56
- if value
57
- @endpoints[key] = value
58
- end
50
+ type = endpoint_data["type"]
51
+ endpoint = endpoint_data["endpoints"].first
52
+ urls = {}
53
+ urls[:public_url] = endpoint["publicURL"] if endpoint["publicURL"]
54
+ urls[:admin_url] = endpoint["adminURL"] if endpoint["adminURL"]
55
+
56
+ @endpoints[type] = urls
59
57
  end
58
+
60
59
  Yao.default_client.register_endpoints(@endpoints, token: self)
61
60
  end
62
61
  end
data/lib/yao/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yao
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yao
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio, KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-23 00:00:00.000000000 Z
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -182,11 +182,13 @@ files:
182
182
  - lib/yao/resources/network.rb
183
183
  - lib/yao/resources/port.rb
184
184
  - lib/yao/resources/restfully_accessible.rb
185
+ - lib/yao/resources/role.rb
185
186
  - lib/yao/resources/security_group.rb
186
187
  - lib/yao/resources/security_group_rule.rb
187
188
  - lib/yao/resources/server.rb
188
189
  - lib/yao/resources/subnet.rb
189
190
  - lib/yao/resources/tenant.rb
191
+ - lib/yao/resources/user.rb
190
192
  - lib/yao/server_error.rb
191
193
  - lib/yao/token.rb
192
194
  - lib/yao/version.rb