Skip to content

Commit 8a7208b

Browse files
author
Gavin Williams
committed
Add a sleep_interval param to the es_instance_conn_validator type.
This can be used to override the default sleep interval, for example on a slower system. Also updated default value from `2` seconds to `10` seconds. Finally, added a unit test for the type.
1 parent fbacf44 commit 8a7208b

File tree

3 files changed

+114
-13
lines changed

3 files changed

+114
-13
lines changed

lib/puppet/provider/es_instance_conn_validator/tcp_port.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,34 @@
22
require 'puppet/util/es_instance_validator'
33

44
# This file contains a provider for the resource type `es_instance_conn_validator`,
5-
# which validates the Elasticsearch instance connection by attempting an https connection.
5+
# which validates the Elasticsearch connection by attempting a tcp connection.
66

77
Puppet::Type.type(:es_instance_conn_validator).provide(:tcp_port) do
88
desc "A provider for the resource type `es_instance_conn_validator`,
99
which validates the connection by attempting an https
10-
connection to the Elasticsearch instance."
10+
connection to Elasticsearch."
1111

1212
def exists?
1313
start_time = Time.now
1414
timeout = resource[:timeout]
15+
sleep_interval = resource[:sleep]
1516

1617
success = validator.attempt_connection
1718

1819
while success == false && ((Time.now - start_time) < timeout)
19-
# It can take several seconds for the Elasticsearch instance to start up;
20+
# It can take several seconds for the Elasticsearch to start up;
2021
# especially on the first install. Therefore, our first connection attempt
21-
# may fail. Here we have somewhat arbitrarily chosen to retry every 2
22+
# may fail. Here we have somewhat arbitrarily chosen to retry every 10
2223
# seconds until the configurable timeout has expired.
23-
Puppet.debug('Failed to connect to the Elasticsearch instance; sleeping 2 seconds before retry')
24-
sleep 2
24+
Puppet.debug("Failed to connect to Elasticsearch; sleeping #{sleep_interval} seconds before retry")
25+
sleep sleep_interval
2526
success = validator.attempt_connection
2627
end
2728

2829
if success
29-
Puppet.debug("Connected to the ES instance in #{Time.now - start_time} seconds.")
30+
Puppet.debug("Connected to the Elasticsearch in #{Time.now - start_time} seconds.")
3031
else
31-
Puppet.notice("Failed to connect to the ES instance within timeout window of #{timeout} seconds; giving up.")
32+
Puppet.notice("Failed to connect to the Elasticsearch within timeout window of #{timeout} seconds; giving up.")
3233
end
3334

3435
success
@@ -38,7 +39,7 @@ def create
3839
# If `#create` is called, that means that `#exists?` returned false, which
3940
# means that the connection could not be established... so we need to
4041
# cause a failure here.
41-
raise Puppet::Error, "Unable to connect to ES instance ! (#{@validator.instance_server}:#{@validator.instance_port})"
42+
raise Puppet::Error, "Unable to connect to Elasticsearch! (#{@validator.instance_server}:#{@validator.instance_port})"
4243
end
4344

4445
private

lib/puppet/type/es_instance_conn_validator.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Puppet::Type.newtype(:es_instance_conn_validator) do
22
@doc = "Verify that a connection can be successfully established between a
3-
node and the Elasticsearch instance. It could potentially be used for other
4-
purposes such as monitoring."
3+
node and Elasticsearch. It could potentially be used for other purposes
4+
such as monitoring."
55

66
ensurable
77

@@ -10,7 +10,7 @@
1010
end
1111

1212
newparam(:server) do
13-
desc 'DNS name or IP address of the server where Elasticsearch instance should be running.'
13+
desc 'DNS name or IP address of the server where Elasticsearch should be running.'
1414
defaultto 'localhost'
1515
end
1616

@@ -20,7 +20,7 @@
2020
end
2121

2222
newparam(:timeout) do
23-
desc 'The max number of seconds that the validator should wait before giving up and deciding that the Elasticsearch instance is not running; defaults to 60 seconds.'
23+
desc 'The max number of seconds that the validator should wait before giving up and deciding that Elasticsearch is not running; defaults to 60 seconds.'
2424
defaultto 60
2525
validate do |value|
2626
# This will raise an error if the string is not convertible to an integer
@@ -30,4 +30,16 @@
3030
Integer(value)
3131
end
3232
end
33+
34+
newparam(:sleep_interval) do
35+
desc 'The number of seconds that the validator should wait before retrying the connection to Elasticsearch; defaults to 10 seconds.'
36+
defaultto 10
37+
validate do |value|
38+
# This will raise an error if the string is not convertible to an integer
39+
Integer(value)
40+
end
41+
munge do |value|
42+
Integer(value)
43+
end
44+
end
3345
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require_relative '../../helpers/unit/type/elasticsearch_rest_shared_examples'
2+
3+
describe Puppet::Type.type(:es_instance_conn_validator) do
4+
let(:resource_name) { 'conn-validator' }
5+
let(:conn_validator) do
6+
Puppet::Type.type(:es_instance_conn_validator)
7+
.new(name: resource_name)
8+
end
9+
10+
describe 'when validating attributes' do
11+
[:name, :server, :port, :timeout, :sleep_interval].each do |param|
12+
it "should have a #{param} parameter" do
13+
expect(described_class.attrtype(param)).to eq(:param)
14+
end
15+
end
16+
17+
[:ensure].each do |prop|
18+
it "should have a #{prop} property" do
19+
expect(described_class.attrtype(prop)).to eq(:property)
20+
end
21+
end
22+
23+
describe 'namevar validation' do
24+
it 'should have :name as its namevar' do
25+
expect(described_class.key_attributes).to eq([:name])
26+
end
27+
end
28+
end # describe when validating attributes
29+
30+
describe 'when validating values' do
31+
describe 'ensure' do
32+
it 'should support present as a value for ensure' do
33+
expect { described_class.new(
34+
:name => resource_name,
35+
:ensure => :present
36+
) }.to_not raise_error
37+
end
38+
39+
it 'should support absent as a value for ensure' do
40+
expect { described_class.new(
41+
:name => resource_name,
42+
:ensure => :absent
43+
) }.to_not raise_error
44+
end
45+
46+
it 'should not support other values' do
47+
expect { described_class.new(
48+
:name => resource_name,
49+
:ensure => :foo
50+
) }.to raise_error(Puppet::Error, /Invalid value/)
51+
end
52+
end # describe 'ensure'
53+
54+
describe 'timeout' do
55+
it "should support a numerical value" do
56+
conn_validator[:timeout] = 120
57+
expect(conn_validator[:timeout]).to eq(120)
58+
end
59+
60+
it "should have a default value of 60" do
61+
expect(conn_validator[:timeout]).to eq(60)
62+
end
63+
64+
it "should not support a non-numeric value" do
65+
expect do
66+
conn_validator[:timeout] = 'string'
67+
end.to raise_error(Puppet::Error, /invalid value/)
68+
end
69+
end # describe 'timeout'
70+
71+
describe 'sleep_interval' do
72+
it "should support a numerical value" do
73+
conn_validator[:sleep_interval] = 120
74+
expect(conn_validator[:sleep_interval]).to eq(120)
75+
end
76+
77+
it "should have a default value of 10" do
78+
expect(conn_validator[:sleep_interval]).to eq(10)
79+
end
80+
81+
it "should not support a non-numeric value" do
82+
expect do
83+
conn_validator[:sleep_interval] = 'string'
84+
end.to raise_error(Puppet::Error, /invalid value/)
85+
end
86+
end # describe 'sleep_interval
87+
end # describe 'when valdating values'
88+
end # of describe Puppet::Type

0 commit comments

Comments
 (0)