onering-client 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
data/bin/onering CHANGED
@@ -35,11 +35,12 @@ end
35
35
  subcommander.version = ::Gem.loaded_specs['onering-client'].version.to_s
36
36
  subcommander.desc = ::Gem.loaded_specs['onering-client'].description
37
37
 
38
- #subcommander.opt :server, '-s', '--server', 'Specify the Onering server URL'
39
-
40
38
  subcommand :devices, "Operations related to Onering's assets database" do |devices|
41
39
  api = Onering::API::Devices
42
- api.connect ENV['ONERING_URL']
40
+ api.connect({
41
+ :host => ENV['ONERING_URL'],
42
+ :pemfile => ENV['ONERING_PEM']
43
+ })
43
44
 
44
45
  def _field(action, field, value, opts={})
45
46
  rv = []
@@ -137,7 +138,60 @@ subcommand :devices, "Operations related to Onering's assets database" do |devic
137
138
  begin
138
139
  json = ::JSON.load(STDIN.read)
139
140
  raise "Input document must specify an ID" if sc[:args].empty? and not json['id']
140
-
141
+
142
+ print_format(api.save((sc[:args].first || json['id']), json))
143
+ rescue Exception => e
144
+ STDERR.puts "#{e.class.name}: #{e.message}"
145
+ exit 1
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+
153
+ subcommand :users, "Manage Onering users" do |users|
154
+ api = Onering::API::Auth
155
+ api.connect({
156
+ :host => ENV['ONERING_URL'],
157
+ :pemfile => ENV['ONERING_PEM']
158
+ })
159
+
160
+ # SHOW
161
+ users.subcommand :show, "Print out a single user by ID" do |sc|
162
+ sc.usage = "onering users show ID"
163
+
164
+ sc.exec do
165
+ id = sc[:args].first
166
+ print_format(api.get(:users, id))
167
+ end
168
+ end
169
+
170
+ # LIST
171
+ users.subcommand :list, "List users" do |sc|
172
+ sc.usage = "onering users list FIELD"
173
+ sc.opt :as_txt, '-t', '--as-text', "Return the results as text"
174
+
175
+ sc.exec do
176
+ field = sc[:args].first
177
+ filter = sc[:filter]
178
+
179
+ print_format(api.list(:users, field, {
180
+ :filter => filter
181
+ }), (sc[:as_txt] ? :text : nil))
182
+ end
183
+ end
184
+
185
+ # SAVE
186
+ users.subcommand :save, "Creates or updates a new device in Onering, reading a JSON document from standard input" do |sc|
187
+ sc.usage = "cat user.json | onering users save [ID]"
188
+
189
+ sc.exec do
190
+ unless STDIN.tty?
191
+ begin
192
+ json = ::JSON.load(STDIN.read)
193
+ raise "Input document must specify an ID" if sc[:args].empty? and not json['id']
194
+
141
195
  print_format(api.save((sc[:args].first || json['id']), json))
142
196
  rescue Exception => e
143
197
  STDERR.puts "#{e.class.name}: #{e.message}"
data/lib/onering.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  $: << File.expand_path(File.dirname(__FILE__))
2
2
  require 'onering/api'
3
3
  require 'onering/plugins/devices'
4
+ require 'onering/plugins/authentication'
data/lib/onering/api.rb CHANGED
@@ -1,8 +1,9 @@
1
- require 'net/http'
1
+ require 'net/https'
2
2
  require 'uri'
3
3
  require 'json'
4
4
  require 'yaml'
5
5
  require 'addressable/uri'
6
+ require 'deep_merge'
6
7
 
7
8
  module Onering
8
9
  module API
@@ -13,29 +14,60 @@ module Onering
13
14
  end
14
15
 
15
16
  class Base
16
- DEFAULT_BASE="http://onering"
17
+ DEFAULT_BASE="https://onering"
17
18
  DEFAULT_PATH="/api"
18
- DEFAULT_OPTIONS={}
19
+ DEFAULT_OPTIONS_FILE=[
20
+ "./cli.yml",
21
+ "~/.onering/cli.yml",
22
+ "/etc/onering/cli.yml"
23
+ ]
24
+
25
+ DEFAULT_CLIENT_PEM=[
26
+ "./client.pem",
27
+ "~/.onering/client.pem",
28
+ "/etc/onering/client.pem"
29
+ ]
19
30
 
20
31
  class<<self
32
+ def connect(options={})
33
+ # list all existing config files from least specific to most
34
+ @_configfiles = ([options[:config]] + DEFAULT_OPTIONS_FILE).compact.select{|i|
35
+ File.exists?(File.expand_path(i))
36
+ }.reverse
37
+
38
+ # merge all config files with more-specific values overriding less-specific ones
39
+ @_config = {}
40
+ @_configfiles.each do |i|
41
+ c = YAML.load(File.read(File.expand_path(i))) rescue {}
42
+ @_config.deep_merge!(c)
43
+ end
21
44
 
22
- def connect(host=nil)
23
- if host.is_a?(URI)
24
- @_uri = host
25
- elsif host.is_a?(String)
26
- @_uri = Addressable::URI.parse("#{host}/#{DEFAULT_PATH}")
45
+ if options[:host].is_a?(URI)
46
+ @_uri = options[:host]
47
+ elsif options[:host].is_a?(String)
48
+ @_uri = Addressable::URI.parse("#{options[:host]}/#{DEFAULT_PATH}")
27
49
  else
28
- @_uri = Addressable::URI.parse("#{DEFAULT_BASE}/#{DEFAULT_PATH}")
50
+ @_uri = Addressable::URI.parse("#{@_config['url'] || DEFAULT_BASE}/#{@_config['apiroot'] || DEFAULT_PATH}")
29
51
  end
30
52
 
31
-
32
53
  if @_uri
33
- @_http = Net::HTTP.new(@_uri.host, @_uri.port)
54
+ @_pemfile = ([options[:pemfile], @_config['pemfile']]+DEFAULT_CLIENT_PEM).compact.select{|i|
55
+ File.exists?(File.expand_path(i))
56
+ }.first
57
+
58
+ if @_pemfile
59
+ @_pem = File.read(File.expand_path(@_pemfile))
60
+ @_http = Net::HTTP.new(@_uri.host, (@_uri.port || 443))
61
+ @_http.use_ssl = true
62
+ @_http.cert = OpenSSL::X509::Certificate.new(@_pem)
63
+ @_http.key = OpenSSL::PKey::RSA.new(@_pem)
64
+ @_http.verify_mode = OpenSSL::SSL::VERIFY_PEER
65
+ end
34
66
  end
35
67
  end
36
68
 
37
69
  def request(endpoint, options={})
38
- options = DEFAULT_OPTIONS.merge(options)
70
+ options = @_config.merge(options)
39
71
  request = nil
40
72
 
41
73
  uri = Addressable::URI.parse("#{@_uri.to_s}/#{endpoint}")
@@ -0,0 +1,39 @@
1
+
2
+ module Onering
3
+ module API
4
+ class Auth < Base
5
+ class<<self
6
+ def _check_type(type)
7
+ raise "Invalid authentication module object '#{type}'" unless %w{
8
+ users groups capabilities
9
+ }.include?(type.to_s)
10
+ end
11
+
12
+ def get(type, id='current')
13
+ _check_type(type)
14
+ request("#{type}/#{id}")
15
+ end
16
+
17
+ def list(type, field='id', options={
18
+ :unique => true,
19
+ :sort => true,
20
+ :filter => nil
21
+ })
22
+ _check_type(type)
23
+ rv = request("#{type}/list").collect{|i| i[field.to_s] }
24
+ rv = rv.uniq if options[:unique]
25
+ rv = rv.sort if options[:sort]
26
+ rv
27
+ end
28
+
29
+ def save(type, id, data)
30
+ _check_type(type)
31
+ request("#{type}/#{id}", {
32
+ :method => :post,
33
+ :data => data
34
+ })
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -3,10 +3,6 @@ module Onering
3
3
  module API
4
4
  class Devices < Base
5
5
  class<<self
6
- def connect(host=nil)
7
- super(host)
8
- end
9
-
10
6
  def get(id)
11
7
  request("devices/#{id}")
12
8
  end
metadata CHANGED
@@ -1,96 +1,83 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: onering-client
3
- version: !ruby/object:Gem::Version
4
- hash: 63
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.17
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 16
10
- version: 0.0.16
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Gary Hetzel
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-01-29 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: subcommander
16
+ requirement: &22628180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
22
23
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: *22628180
25
+ - !ruby/object:Gem::Dependency
26
+ name: deep_merge
27
+ requirement: &22627760 !ruby/object:Gem::Requirement
24
28
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
32
33
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: addressable
36
34
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
35
+ version_requirements: *22627760
36
+ - !ruby/object:Gem::Dependency
37
+ name: addressable
38
+ requirement: &22627280 !ruby/object:Gem::Requirement
38
39
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
46
44
  type: :runtime
47
- version_requirements: *id002
45
+ prerelease: false
46
+ version_requirements: *22627280
48
47
  description: A Ruby wrapper for Onering
49
48
  email: ghetzel@outbrain.com
50
- executables:
49
+ executables:
51
50
  - onering
52
51
  extensions: []
53
-
54
52
  extra_rdoc_files: []
55
-
56
- files:
53
+ files:
57
54
  - lib/onering.rb
58
55
  - lib/onering/api.rb
59
56
  - lib/onering/plugins/devices.rb
57
+ - lib/onering/plugins/authentication.rb
60
58
  - bin/onering
61
59
  homepage: https://github.com/outbrain/onering-ruby
62
60
  licenses: []
63
-
64
61
  post_install_message:
65
62
  rdoc_options: []
66
-
67
- require_paths:
63
+ require_paths:
68
64
  - lib
69
- required_ruby_version: !ruby/object:Gem::Requirement
65
+ required_ruby_version: !ruby/object:Gem::Requirement
70
66
  none: false
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 0
77
- version: "0"
78
- required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
72
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
87
77
  requirements: []
88
-
89
78
  rubyforge_project:
90
- rubygems_version: 1.8.15
79
+ rubygems_version: 1.8.11
91
80
  signing_key:
92
81
  specification_version: 3
93
82
  summary: Onering client API
94
83
  test_files: []
95
-
96
- has_rdoc: