hiera-mysql-json-backend-jruby 1.0.1 → 1.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: 1381e6d0d6dfa969f329d587deb3a9869753c085
4
- data.tar.gz: 647d1444436daca4231edc66f3f88a6fd74fbb57
3
+ metadata.gz: 5c96d6e30cfeffa3fa425bfe7346b80d22a1f77b
4
+ data.tar.gz: 88938c126d8489901ee3a5fd883c52840bdd18a1
5
5
  SHA512:
6
- metadata.gz: c508f1dd2c1e670f4b038ab631ba468c2ef00e6d2612ba112a176feaca914466af4e0e53504e39d70fc67adb8a2371c4288686c0e567e5ce699e5f0c3df8e77e
7
- data.tar.gz: 0f3d413504875afcdbe6466856899b7962dce3a0308ba1b48b93ebad68266a83144167f11592d344621e2bf32557b6f244b51ce3181877dcec6252f48222762f
6
+ metadata.gz: 81d60c95edcfecca8cec2cb6d90be52cc301ebd6a44a975b98a29924ae42218911699efef5fc8d2d9794db7f9420faf2e7563c9e265f45669a5635991db511d6
7
+ data.tar.gz: 593f3b3910733414be38f96c2fd3e15812ce3f577c1cf88e6d7a7b9fa61248e1acd1e0c11a53a0352167693fee0aa5b4f31451dbd814afe3182c4f466a38bf18
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "hiera-mysql-json-backend-jruby"
5
- gem.version = "1.0.1"
5
+ gem.version = "1.1.0"
6
6
  gem.authors = ["Hostnet"]
7
7
  gem.email = ["opensource@hostnet.nl"]
8
8
  gem.description = %q{Alternative MySQL backend with json support for hiera}
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "hiera-mysql-json-backend"
5
- gem.version = "1.0.1"
5
+ gem.version = "1.1.0"
6
6
  gem.authors = ["Hostnet"]
7
7
  gem.email = ["opensource@hostnet.nl"]
8
8
  gem.description = %q{Alternative MySQL backend with json support for hiera}
@@ -18,7 +18,25 @@ class Hiera
18
18
 
19
19
  @cache = cache || Filecache.new
20
20
 
21
- Hiera.debug("Hiera mysql_json initialized")
21
+ Hiera.debug('Hiera mysql_json initialized')
22
+ end
23
+
24
+ def should_lookup?(constraints, scope)
25
+ return true unless constraints.is_a?(Hash)
26
+ should_lookup = false
27
+ constraints.each do |item, matchers|
28
+ next unless scope.exist?(item.to_s)
29
+ if scope[item.to_s] =~ compile_regexes(matchers)
30
+ should_lookup = true
31
+ break
32
+ end
33
+ end
34
+ should_lookup
35
+ end
36
+
37
+ def compile_regexes(regexes)
38
+ res = regexes.map { |re| Regexp.compile(re) }
39
+ Regexp.union(res)
22
40
  end
23
41
 
24
42
  def lookup(key, scope, order_override, resolution_type)
@@ -28,9 +46,11 @@ class Hiera
28
46
  # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
29
47
  results = nil
30
48
 
31
- Hiera.debug("looking up #{key} in MySQL2 Backend")
49
+ Hiera.debug("looking up #{key} in mysql_json Backend")
32
50
  Hiera.debug("resolution type is #{resolution_type}")
33
51
 
52
+ return nil unless should_lookup?(Config[:mysql_json][:only_for], scope)
53
+
34
54
  Backend.datasources(scope, order_override) do |source|
35
55
  Hiera.debug("Looking for data source #{source}")
36
56
  sqlfile = Backend.datafile(:mysql_json, scope, source, "sql") || next
@@ -41,20 +61,20 @@ class Hiera
41
61
  end
42
62
 
43
63
  mysql_config = data.fetch(:dbconfig, {})
44
- mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql2][:host] || 'localhost'
45
- mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql2][:user]
46
- mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql2][:pass]
47
- mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql2][:port] || '3306'
48
- mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql2][:database]
64
+ mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql_json][:host] || 'localhost'
65
+ mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql_json][:user]
66
+ mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql_json][:pass]
67
+ mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql_json][:port] || '3306'
68
+ mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql_json][:database]
49
69
 
50
70
  connection_hash = {
51
- :host => mysql_host,
52
- :username => mysql_user,
53
- :password => mysql_pass,
54
- :database => mysql_database,
55
- :port => mysql_port,
56
- :reconnect => true}
57
-
71
+ host: mysql_host,
72
+ username: mysql_user,
73
+ password: mysql_pass,
74
+ database: mysql_database,
75
+ port: mysql_port,
76
+ reconnect: true
77
+ }
58
78
 
59
79
  Hiera.debug("data #{data.inspect}")
60
80
  next if data.empty?
@@ -70,6 +90,7 @@ class Hiera
70
90
  begin
71
91
  new_answer = JSON.parse(sql_results[0]['value'])
72
92
  rescue
93
+ raise Exception, "JSON parse error for key '#{key}'." unless Config[:mysql_json][:ignore_json_parse_errors]
73
94
  Hiera.debug("Miserable failure while looking for #{key}.")
74
95
  next
75
96
  end
@@ -88,19 +109,18 @@ class Hiera
88
109
  break
89
110
  end
90
111
  end
91
- return results
112
+ results
92
113
  end
93
114
 
94
-
95
115
  def query(connection_hash, query)
96
116
  Hiera.debug("Executing SQL Query: #{query}")
97
117
 
98
- data=[]
99
- mysql_host=connection_hash[:host]
100
- mysql_user=connection_hash[:username]
101
- mysql_pass=connection_hash[:password]
102
- mysql_database=connection_hash[:database]
103
- mysql_port=connection_hash[:port]
118
+ data = []
119
+ mysql_host = connection_hash[:host]
120
+ mysql_user = connection_hash[:username]
121
+ mysql_pass = connection_hash[:password]
122
+ mysql_database = connection_hash[:database]
123
+ mysql_port = connection_hash[:port]
104
124
 
105
125
  if defined?(JRUBY_VERSION)
106
126
  Jdbc::MySQL.load_driver
@@ -109,7 +129,7 @@ class Hiera
109
129
  props.set_property :user, mysql_user
110
130
  props.set_property :password, mysql_pass
111
131
 
112
- conn = com.mysql.jdbc.Driver.new.connect(url,props)
132
+ conn = com.mysql.jdbc.Driver.new.connect(url, props)
113
133
  stmt = conn.create_statement
114
134
 
115
135
  res = stmt.execute_query(query)
@@ -118,7 +138,7 @@ class Hiera
118
138
 
119
139
  Hiera.debug("Mysql Query returned #{numcols} rows")
120
140
 
121
- while ( res.next ) do
141
+ while res.next
122
142
  if numcols < 2
123
143
  Hiera.debug("Mysql value : #{res.getString(1)}")
124
144
  data << res.getString(1)
@@ -144,8 +164,7 @@ class Hiera
144
164
  end
145
165
  end
146
166
 
147
- return data
148
-
167
+ data
149
168
  end
150
169
  end
151
170
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-mysql-json-backend-jruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hostnet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-17 00:00:00.000000000 Z
11
+ date: 2017-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jdbc-mysql