Skip to content

Commit c28918f

Browse files
authored
Merge pull request #958 from elastic/native-type-to-strings
Native type to strings
2 parents aed465c + ce8d817 commit c28918f

File tree

18 files changed

+128
-75
lines changed

18 files changed

+128
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Features
44

55
#### Fixes
6+
* REST-based resources are now coerced to string values to uniformly compare user defined values and Elasticsearch API responses.
67
* Resolve deprecation warnings related to use of the deprecated is_array() function.
78

89
## 6.3.0 (June 18, 2018)

lib/puppet/provider/elasticsearch_index/ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require 'puppet/provider/elastic_rest'
44

5-
require 'puppet_x/elastic/deep_to_i'
5+
require 'puppet_x/elastic/deep_to_s'
66

77
Puppet::Type.type(:elasticsearch_index).provide(
88
:ruby,
99
:parent => Puppet::Provider::ElasticREST,
1010
:metadata => :settings,
1111
:metadata_pipeline => [
1212
lambda { |data| data['settings'] },
13-
lambda { |data| Puppet_X::Elastic.deep_to_i data }
13+
lambda { |data| Puppet_X::Elastic.deep_to_s data }
1414
],
1515
:api_uri => '_settings',
1616
:api_discovery_uri => '_all',

lib/puppet/provider/elasticsearch_license/shield.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:parent => Puppet::Provider::ElasticREST,
77
:metadata => :content,
88
:metadata_pipeline => [
9-
lambda { |data| Puppet_X::Elastic.deep_to_i data }
9+
lambda { |data| Puppet_X::Elastic.deep_to_s data }
1010
],
1111
:api_uri => '_license',
1212
:query_string => {

lib/puppet/provider/elasticsearch_license/x-pack.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:parent => Puppet::Provider::ElasticREST,
99
:metadata => :content,
1010
:metadata_pipeline => [
11-
lambda { |data| Puppet_X::Elastic.deep_to_i data }
11+
lambda { |data| Puppet_X::Elastic.deep_to_s data }
1212
],
1313
:api_uri => '_xpack/license',
1414
:query_string => {

lib/puppet/provider/elasticsearch_template/ruby.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
require 'puppet/provider/elastic_rest'
44

5-
require 'puppet_x/elastic/deep_to_i'
5+
require 'puppet_x/elastic/deep_to_s'
66

77
Puppet::Type.type(:elasticsearch_template).provide(
88
:ruby,
99
:parent => Puppet::Provider::ElasticREST,
1010
:api_uri => '_template',
1111
:metadata => :content,
1212
:metadata_pipeline => [
13-
lambda { |data| Puppet_X::Elastic.deep_to_i data }
13+
lambda { |data| Puppet_X::Elastic.deep_to_s data }
1414
]
1515
) do
1616
desc 'A REST API based provider to manage Elasticsearch templates.'

lib/puppet/type/elasticsearch_index.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
22

33
require 'puppet_x/elastic/asymmetric_compare'
4-
require 'puppet_x/elastic/deep_to_i'
5-
require 'puppet_x/elastic/deep_implode'
4+
require 'puppet_x/elastic/deep_to_s'
65
require 'puppet_x/elastic/elasticsearch_rest_resource'
76

87
Puppet::Type.newtype(:elasticsearch_index) do
@@ -24,7 +23,7 @@ def insync?(is)
2423
end
2524

2625
munge do |value|
27-
Puppet_X::Elastic.deep_to_i(value)
26+
Puppet_X::Elastic.deep_to_s(value)
2827
end
2928

3029
validate do |value|

lib/puppet/type/elasticsearch_license.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
22

33
require 'puppet_x/elastic/asymmetric_compare'
4-
require 'puppet_x/elastic/deep_to_i'
5-
require 'puppet_x/elastic/deep_implode'
4+
require 'puppet_x/elastic/deep_to_s'
65
require 'puppet_x/elastic/elasticsearch_rest_resource'
76

87
Puppet::Type.newtype(:elasticsearch_license) do
@@ -46,7 +45,7 @@ def should_to_s(newvalue)
4645
end
4746

4847
munge do |value|
49-
Puppet_X::Elastic.deep_to_i(value)
48+
Puppet_X::Elastic.deep_to_s(value)
5049
end
5150
end
5251
end # of newtype

lib/puppet/type/elasticsearch_pipeline.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
22

3-
require 'puppet_x/elastic/deep_to_i'
4-
require 'puppet_x/elastic/deep_implode'
3+
require 'puppet_x/elastic/deep_to_s'
54
require 'puppet_x/elastic/elasticsearch_rest_resource'
65

76
Puppet::Type.newtype(:elasticsearch_pipeline) do
@@ -23,7 +22,7 @@
2322
end
2423

2524
munge do |value|
26-
Puppet_X::Elastic.deep_to_i(value)
25+
Puppet_X::Elastic.deep_to_s(value)
2726
end
2827
end
2928
end # of newtype

lib/puppet/type/elasticsearch_template.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'puppet/file_serving/metadata'
55

66
require 'puppet_x/elastic/deep_implode'
7-
require 'puppet_x/elastic/deep_to_i'
7+
require 'puppet_x/elastic/deep_to_s'
88
require 'puppet_x/elastic/elasticsearch_rest_resource'
99

1010
Puppet::Type.newtype(:elasticsearch_template) do
@@ -32,15 +32,14 @@
3232
# `in` and `should` states consistent if the user hasn't
3333
# provided any.
3434
#
35-
# We use deep_to_i to ensure any numeric values are properly
36-
# parsed, whether from user-defined resources or when reading
37-
# from the API.
35+
# We use deep_to_s to normalize hash values to strings, whether from
36+
# user-defined resources or when reading from the API.
3837
#
3938
# We also need to fully qualify index settings, since users
4039
# can define those with the index json key absent, but the API
4140
# always fully qualifies them.
42-
{ 'order' => 0, 'aliases' => {}, 'mappings' => {} }.merge(
43-
Puppet_X::Elastic.deep_to_i(
41+
{ 'order' => '0', 'aliases' => {}, 'mappings' => {} }.merge(
42+
Puppet_X::Elastic.deep_to_s(
4443
value.tap do |val|
4544
if val.key? 'settings'
4645
val['settings']['index'] = {} unless val['settings'].key? 'index'

lib/puppet_x/elastic/deep_to_s.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Puppet_X
2+
# Custom Elastic functions
3+
module Elastic
4+
# When given a hash, this method recurses deeply into all values to convert
5+
# any that aren't data structures into strings. This is necessary when
6+
# comparing results from Elasticsearch API calls, because values like
7+
# integers and booleans are in string form.
8+
def self.deep_to_s(obj)
9+
if obj.is_a? Array
10+
obj.map { |element| deep_to_s(element) }
11+
elsif obj.is_a? Hash
12+
obj.merge(obj) { |_key, val| deep_to_s(val) }
13+
elsif (not obj.is_a? String) and obj.respond_to? :to_s
14+
obj.to_s
15+
else
16+
obj
17+
end
18+
end
19+
end # of Elastic
20+
end # of Puppet_X

0 commit comments

Comments
 (0)