Skip to content

Commit ac75427

Browse files
authored
Merge pull request #969 from tylerjl/rest-resources-int-datatypes
Rest resources int datatypes
2 parents 35de20e + 53c529c commit ac75427

File tree

17 files changed

+78
-63
lines changed

17 files changed

+78
-63
lines changed

lib/puppet/provider/elasticsearch_index/ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'puppet/provider/elastic_rest'
44

5+
require 'puppet_x/elastic/deep_to_i'
56
require 'puppet_x/elastic/deep_to_s'
67

78
Puppet::Type.type(:elasticsearch_index).provide(
@@ -10,7 +11,8 @@
1011
:metadata => :settings,
1112
:metadata_pipeline => [
1213
lambda { |data| data['settings'] },
13-
lambda { |data| Puppet_X::Elastic.deep_to_s data }
14+
lambda { |data| Puppet_X::Elastic.deep_to_s data },
15+
lambda { |data| Puppet_X::Elastic.deep_to_i data }
1416
],
1517
:api_uri => '_settings',
1618
:api_discovery_uri => '_all',

lib/puppet/provider/elasticsearch_license/shield.rb

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

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

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

lib/puppet/provider/elasticsearch_template/ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'puppet/provider/elastic_rest'
44

5+
require 'puppet_x/elastic/deep_to_i'
56
require 'puppet_x/elastic/deep_to_s'
67

78
Puppet::Type.type(:elasticsearch_template).provide(
@@ -10,7 +11,8 @@
1011
:api_uri => '_template',
1112
:metadata => :content,
1213
:metadata_pipeline => [
13-
lambda { |data| Puppet_X::Elastic.deep_to_s data }
14+
lambda { |data| Puppet_X::Elastic.deep_to_s data },
15+
lambda { |data| Puppet_X::Elastic.deep_to_i data }
1416
]
1517
) do
1618
desc 'A REST API based provider to manage Elasticsearch templates.'

lib/puppet/type/elasticsearch_index.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +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'
45
require 'puppet_x/elastic/deep_to_s'
56
require 'puppet_x/elastic/elasticsearch_rest_resource'
67

@@ -23,7 +24,7 @@ def insync?(is)
2324
end
2425

2526
munge do |value|
26-
Puppet_X::Elastic.deep_to_s(value)
27+
Puppet_X::Elastic.deep_to_i(Puppet_X::Elastic.deep_to_s(value))
2728
end
2829

2930
validate do |value|

lib/puppet/type/elasticsearch_license.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +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'
45
require 'puppet_x/elastic/deep_to_s'
56
require 'puppet_x/elastic/elasticsearch_rest_resource'
67

@@ -45,7 +46,7 @@ def should_to_s(newvalue)
4546
end
4647

4748
munge do |value|
48-
Puppet_X::Elastic.deep_to_s(value)
49+
Puppet_X::Elastic.deep_to_i(Puppet_X::Elastic.deep_to_s(value))
4950
end
5051
end
5152
end # of newtype

lib/puppet/type/elasticsearch_pipeline.rb

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

3+
require 'puppet_x/elastic/deep_to_i'
34
require 'puppet_x/elastic/deep_to_s'
45
require 'puppet_x/elastic/elasticsearch_rest_resource'
56

@@ -22,7 +23,7 @@
2223
end
2324

2425
munge do |value|
25-
Puppet_X::Elastic.deep_to_s(value)
26+
Puppet_X::Elastic.deep_to_i(Puppet_X::Elastic.deep_to_s(value))
2627
end
2728
end
2829
end # of newtype

lib/puppet/type/elasticsearch_template.rb

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

66
require 'puppet_x/elastic/deep_implode'
7+
require 'puppet_x/elastic/deep_to_i'
78
require 'puppet_x/elastic/deep_to_s'
89
require 'puppet_x/elastic/elasticsearch_rest_resource'
910

@@ -32,28 +33,31 @@
3233
# `in` and `should` states consistent if the user hasn't
3334
# provided any.
3435
#
35-
# We use deep_to_s to normalize hash values to strings, whether from
36-
# user-defined resources or when reading from the API.
36+
# The value is first stringified, then integers are parse out as
37+
# necessary, since the Elasticsearch API enforces some fields to be
38+
# integers.
3739
#
3840
# We also need to fully qualify index settings, since users
3941
# can define those with the index json key absent, but the API
4042
# always fully qualifies them.
41-
{ 'order' => '0', 'aliases' => {}, 'mappings' => {} }.merge(
42-
Puppet_X::Elastic.deep_to_s(
43-
value.tap do |val|
44-
if val.key? 'settings'
45-
val['settings']['index'] = {} unless val['settings'].key? 'index'
46-
(val['settings'].keys - ['index']).each do |setting|
47-
new_key = if setting.start_with? 'index.'
48-
setting[6..-1]
49-
else
50-
setting
51-
end
52-
val['settings']['index'][new_key] = \
53-
val['settings'].delete setting
43+
{ 'order' => 0, 'aliases' => {}, 'mappings' => {} }.merge(
44+
Puppet_X::Elastic.deep_to_i(
45+
Puppet_X::Elastic.deep_to_s(
46+
value.tap do |val|
47+
if val.key? 'settings'
48+
val['settings']['index'] = {} unless val['settings'].key? 'index'
49+
(val['settings'].keys - ['index']).each do |setting|
50+
new_key = if setting.start_with? 'index.'
51+
setting[6..-1]
52+
else
53+
setting
54+
end
55+
val['settings']['index'][new_key] = \
56+
val['settings'].delete setting
57+
end
5458
end
5559
end
56-
end
60+
)
5761
)
5862
)
5963
end

spec/fixtures/templates/post_6.0.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"index_patterns": [ "logstash-*" ],
3+
"version": 123,
34
"settings": {
45
"index": {
56
"refresh_interval": "5s",

spec/helpers/unit/type/elasticsearch_rest_shared_examples.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
end.not_to raise_error
6464
end
6565

66-
it 'should deeply stringify PSON-like values' do
66+
it 'should parse PSON-like values for certain types' do
6767
expect(described_class.new(
6868
:name => resource_name,
69-
meta_property => { 'key' => { 'value' => 0 } }
69+
meta_property => { 'key' => { 'value' => '0', 'other' => true } }
7070
)[meta_property]).to include(
71-
'key' => { 'value' => '0' }
71+
'key' => { 'value' => 0, 'other' => 'true' }
7272
)
7373
end
7474
end

0 commit comments

Comments
 (0)