jerakia-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 899404e16dd30ec067c873af83e69b51f7643f37
4
+ data.tar.gz: fe2ac9e0f68a145578d9eaabfef449b50b16e2f9
5
+ SHA512:
6
+ metadata.gz: a2f6761ce0df466ed40958289e0b44130e1cb44f497d2348d08bf64bef9a2a39a3e7ee8c651f58d1685d25ad9b026a1cad5152dfa53f0f214b3a3f99a6870795
7
+ data.tar.gz: 7c5cd46e0ce57593c02c6fbae1cab23c0b61d49fc8493be78f90e1ac679693356e36c16d77e7e0d9a629124c60c7a57c74be677078155a83b5fde79b24562d9b
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'jerakia/client/cli'
7
+
8
+ Jerakia::Client::CLI.start(ARGV)
@@ -0,0 +1,76 @@
1
+ require 'rest-client'
2
+ require 'jerakia/client/error'
3
+ require 'jerakia/client/version'
4
+ require 'jerakia/client/lookup'
5
+ require 'jerakia/client/scope'
6
+ require 'jerakia/client/token'
7
+ require 'uri'
8
+ require 'json'
9
+
10
+ class Jerakia
11
+ class Client
12
+ include Jerakia::Client::Lookup
13
+ include Jerakia::Client::Scope
14
+
15
+ attr_reader :config
16
+ def initialize(opts={})
17
+ @config = default_config.merge(
18
+ opts.reject { |k,v| v.nil? }
19
+ )
20
+ @token = nil
21
+ end
22
+
23
+ def default_config
24
+ {
25
+ :host => 'localhost',
26
+ :port => 9843,
27
+ :api => 'v1',
28
+ :proto => 'http',
29
+ }
30
+ end
31
+
32
+ def default_lookup_options
33
+ {
34
+ :policy => :default,
35
+ :lookup_type => :first,
36
+ }
37
+ end
38
+
39
+ def token
40
+ @token ||= @config[:token]
41
+ @token ||= Jerakia::Client::Token.load_from_file
42
+ raise Jerakia::Client::Error, "No authorization token available" if @token.nil?
43
+ @token
44
+ end
45
+
46
+ def url_address
47
+ "#{@config[:proto]}://#{@config[:host]}:#{@config[:port]}"
48
+ end
49
+
50
+
51
+ def get(url_path, params={})
52
+ headers = { 'X-Authentication' => token }
53
+ uri_params = '?' + URI.encode_www_form(params)
54
+ url = url_address + url_path + uri_params
55
+ begin
56
+ response = RestClient.get(url, headers)
57
+ rescue RestClient::Unauthorized => e
58
+ raise Jerakia::Client::AuthorizationError, "Request not authorized"
59
+ end
60
+ return JSON.parse(response.body)
61
+ end
62
+
63
+ def put(url_path, params)
64
+ headers = { 'X-Authentication' => token }
65
+ url = url_address + url_path
66
+ begin
67
+ response = RestClient.put(url, params.to_json, headers)
68
+ rescue RestClient::Unauthorized => e
69
+ raise Jerakia::Client::AuthorizationError, "Request not authorized"
70
+ end
71
+ return JSON.parse(response.body)
72
+ end
73
+ end
74
+ end
75
+
76
+
@@ -0,0 +1,17 @@
1
+ require 'thor'
2
+ require 'jerakia/client'
3
+ require 'jerakia/client/cli/lookup'
4
+
5
+ class Jerakia
6
+ class Client
7
+ class CLI < Thor
8
+ include Jerakia::Client::CLI::Lookup
9
+
10
+ desc 'version', 'Version information'
11
+ def version
12
+ puts Jerakia::Client::VERSION
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,113 @@
1
+ require 'json'
2
+ require 'yaml'
3
+
4
+ class Jerakia
5
+ class Client
6
+ class CLI < Thor
7
+ module Lookup
8
+ def self.included(thor)
9
+ thor.class_eval do
10
+ desc 'lookup [KEY]', 'Lookup [KEY] with Jerakia'
11
+
12
+ option :host,
13
+ aliases: :H,
14
+ type: :string,
15
+ default: nil,
16
+ desc: 'Hostname or IP to connect to'
17
+
18
+ option :port,
19
+ aliases: :P,
20
+ type: :string,
21
+ default: nil,
22
+ desc: 'Port to connect to'
23
+
24
+ option :token,
25
+ aliases: :T,
26
+ type: :string,
27
+ default: nil,
28
+ desc: "Token to use for authorization, if not provided a jerakia.yaml will be searched for (see docs)"
29
+
30
+ option :api,
31
+ aliases: :a,
32
+ type: :string,
33
+ default: nil,
34
+ desc: 'API Version to implement (see docs)'
35
+
36
+ option :policy,
37
+ aliases: :p,
38
+ type: :string,
39
+ default: 'default',
40
+ desc: 'Lookup policy'
41
+ option :namespace,
42
+ aliases: :n,
43
+ type: :string,
44
+ default: '',
45
+ desc: 'Lookup namespace'
46
+ option :type,
47
+ aliases: :t,
48
+ type: :string,
49
+ default: 'first',
50
+ desc: 'Lookup type'
51
+ option :scope,
52
+ aliases: :s,
53
+ type: :string,
54
+ desc: 'Scope handler',
55
+ default: 'metadata'
56
+ option :scope_options,
57
+ type: :hash,
58
+ desc: 'Key/value pairs to be passed to the scope handler'
59
+ option :merge_type,
60
+ aliases: :m,
61
+ type: :string,
62
+ default: 'array',
63
+ desc: 'Merge type'
64
+ option :verbose,
65
+ aliases: :v,
66
+ type: :boolean,
67
+ desc: 'Print verbose information'
68
+ option :debug,
69
+ aliases: :D,
70
+ type: :boolean,
71
+ desc: 'Debug information to console, implies --log-level debug'
72
+
73
+ option :output,
74
+ aliases: :o,
75
+ type: :string,
76
+ default: 'json',
77
+ desc: 'Output format, yaml or json'
78
+
79
+ def lookup(key)
80
+ client = Jerakia::Client.new({
81
+ :host => options[:host],
82
+ :port => options[:port],
83
+ :api => options[:api],
84
+ :token => options[:token],
85
+ })
86
+
87
+ lookup_opts = {
88
+ :namespace => options[:namespace].split(/::/),
89
+ :policy => options[:policy].to_sym,
90
+ :lookup_type => options[:type].to_sym,
91
+ :merge => options[:merge_type].to_sym,
92
+ :metadata => options[:metadata] || {},
93
+ :scope => options[:scope].to_sym,
94
+ :scope_options => options[:scope_options],
95
+ :use_schema => options[:schema]
96
+ }
97
+ response = client.lookup(key, lookup_opts)
98
+ answer = response['payload']
99
+ case options[:output]
100
+ when 'json'
101
+ puts answer.to_json
102
+ when 'yaml'
103
+ puts answer.to_yaml
104
+ else
105
+ raise Jerakia::Client::Error, "Unknown output type #{options[:output]}"
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,9 @@
1
+ class Jerakia
2
+ class Client
3
+ class Error < RuntimeError
4
+ end
5
+
6
+ class AuthorizationError < RuntimeError
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class Jerakia
2
+ class Client
3
+ module Lookup
4
+ def lookup(key, params)
5
+ return get("/v1/lookup/#{key}", params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ class Jerakia
2
+ class Client
3
+ module Scope
4
+
5
+ def send_scope(realm, identifier, scope)
6
+ put("/v1/scope/#{realm}/#{identifier}", scope)
7
+ end
8
+
9
+ def get_scope_uuid(realm, identifier)
10
+ get("/v1/scope/#{realm}/#{identifier}/uuid")
11
+ end
12
+
13
+ def get_scope(realm, identifier)
14
+ get("/v1/scope/#{realm}/#{identifier}")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'yaml'
2
+ class Jerakia
3
+ class Client
4
+ class Token
5
+ class << self
6
+ def load_from_file
7
+ [
8
+ File.join(ENV['HOME'], ".jerakia", "jerakia.yaml"),
9
+ "/etc/jerakia/jerakia.yaml",
10
+ ]. each do |file|
11
+ if File.exists?(file)
12
+ config = YAML.load(File.read(file))
13
+ return config['client_token'] if config['client_token']
14
+ end
15
+ end
16
+ return nil
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ class Jerakia
2
+ class Client
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jerakia-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Craig Dunn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ description: CLI and ruby client libraries for Jerakia Server
28
+ email:
29
+ executables:
30
+ - jerakia-client
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/jerakia-client
35
+ - lib/jerakia/client.rb
36
+ - lib/jerakia/client/cli.rb
37
+ - lib/jerakia/client/cli/lookup.rb
38
+ - lib/jerakia/client/error.rb
39
+ - lib/jerakia/client/lookup.rb
40
+ - lib/jerakia/client/scope.rb
41
+ - lib/jerakia/client/token.rb
42
+ - lib/jerakia/client/version.rb
43
+ homepage: http://jerakia.io
44
+ licenses:
45
+ - Apache 2.0
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 2.5.1
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: CLI and ruby client libraries for Jerakia Server
67
+ test_files: []