hiera-http 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/hiera/backend/http_backend.rb +109 -0
  2. metadata +77 -0
@@ -0,0 +1,109 @@
1
+ class Hiera
2
+ module Backend
3
+ class Http_backend
4
+
5
+ def initialize
6
+ require 'net/http'
7
+ @config = Config[:http]
8
+ end
9
+
10
+ def lookup(key, scope, order_override, resolution_type)
11
+
12
+ answer = nil
13
+
14
+ paths.insert(0, order_override) if order_override
15
+ paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) }
16
+
17
+ http = Net::HTTP.new(@config[:host], @config[:port])
18
+ http.read_timeout = @config[:http_read_timeout] || 10
19
+ http.open_timeout = @config[:http_connect_timeout] || 10
20
+
21
+
22
+ paths.each do |path|
23
+
24
+ Hiera.debug("[hiera-http]: Lookup #{key} from #{@config[:host]}:#{@config[:port]}#{path}")
25
+ httpreq = Net::HTTP::Get.new(path)
26
+
27
+ begin
28
+ httpres = http.request(httpreq)
29
+ rescue Exception => e
30
+ Hiera.warn("[hiera-http]: Net::HTTP threw exception #{e.message}")
31
+ raise Exception, e.message unless @config[:failure] == 'graceful'
32
+ next
33
+ end
34
+
35
+ unless httpres.kind_of?(Net::HTTPSuccess)
36
+ Hiera.debug("[hiera-http]: bad http response from #{@config[:host]}:#{@config[:port]}#{path}")
37
+ raise Exception, 'Bad HTTP response' unless @config[:failure] == 'graceful'
38
+ next
39
+ end
40
+
41
+ result = self.parse_response(key, httpres.body)
42
+ next unless result
43
+
44
+ parsed_result = Backend.parse_answer(result, scope)
45
+
46
+ case resolution_type
47
+ when :array
48
+ answer ||= []
49
+ answer << parsed_result
50
+ when :hash
51
+ answer ||= {}
52
+ answer = parsed_result.merge answer
53
+ else
54
+ answer = parsed_result
55
+ break
56
+ end
57
+ end
58
+ answer
59
+ end
60
+
61
+
62
+ def parse_response(key,answer)
63
+
64
+ return nil unless answer
65
+
66
+ Hiera.debug("[hiera-http]: Query returned data, parsing response as #{@config[:output] || 'plain'}")
67
+
68
+ case @config[:output]
69
+
70
+ when 'plain'
71
+ # When the output format is configured as plain we assume that if the
72
+ # endpoint URL returns an HTTP success then the contents of the response
73
+ # body is the value itself, or nil.
74
+ #
75
+ answer
76
+ when 'json'
77
+ # If JSON is specified as the output format, assume the output of the
78
+ # endpoint URL is a JSON document and return keypart that matched our
79
+ # lookup key
80
+ self.json_handler(key,answer)
81
+ when 'yaml'
82
+ # If YAML is specified as the output format, assume the output of the
83
+ # endpoint URL is a YAML document and return keypart that matched our
84
+ # lookup key
85
+ self.yaml_handler(key,answer)
86
+ else
87
+ answer
88
+ end
89
+ end
90
+
91
+ # Handlers
92
+ # Here we define specific handlers to parse the output of the http request
93
+ # and return a value. Currently we support YAML and JSON
94
+ #
95
+ def json_handler(key,answer)
96
+ require 'rubygems'
97
+ require 'json'
98
+ JSON.parse(answer)[key]
99
+ end
100
+
101
+ def yaml_handler(answer)
102
+ require 'yaml'
103
+ YAML.parse(answer)[key]
104
+ end
105
+
106
+ end
107
+ end
108
+ end
109
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig Dunn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hiera
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.1
46
+ description: Hiera backend for looking up data over HTTP APIs
47
+ email: craig@craigdunn.org
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/hiera/backend/http_backend.rb
53
+ homepage: http://github.com/crayfishx/hiera-http
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: HTTP backend for Hiera
77
+ test_files: []