21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'manifests/watch.pp', line 21
define consul::watch (
Optional $args = undef,
Optional $datacenter = undef,
Enum['present', 'absent'] $ensure = present,
Optional $event_name = undef,
Optional $handler = undef,
Optional $key = undef,
Optional $keyprefix = undef,
Optional[Boolean] $passingonly = undef,
Optional $service = undef,
Optional $service_tag = undef,
Optional $state = undef,
Optional $token = undef,
Optional $type = undef,
) {
include consul
$id = $title
$basic_hash = {
'type' => $type,
'args' => $args,
'handler' => $handler,
'datacenter' => $datacenter,
'token' => $token,
}
if (versioncmp($consul::version, '0.4.0') < 0) {
fail('Watches are only supported in Consul 0.4.0 and above')
}
if (! $handler and ! $args) {
fail('All watch conditions must have a handler or args list defined')
}
if ($handler and $args) {
fail('Watch conditions cannot have both a handler and args list defined')
}
if (! $type ) {
fail('All watch conditions must have a type defined')
}
case $type {
'key': {
if (! $key ) {
fail('key is required for watch type [key]')
}
$type_hash = {
key => $key,
}
}
'keyprefix': {
if (! $keyprefix ) {
fail('keyprefix is required for watch type of [keyprefix]')
}
$type_hash = {
prefix => $keyprefix,
}
}
'service': {
if (! $service ) {
fail('service is required for watch type of [service]')
}
$type_hash = {
service => $service,
tag => $service_tag,
passingonly => $passingonly,
}
}
'checks': {
$type_hash = {
service => $service,
state => $state,
}
}
'event': {
$type_hash = {
name => $event_name,
}
}
/(nodes|services)/: {
$type_hash = {}
}
default: {
fail("${type} is an unrecogonized watch type that is not supported currently")
}
}
$merged_hash = $basic_hash + $type_hash
$watch_hash = {
watches => [$merged_hash.filter |$key, $val| { $val =~ NotUndef }],
}
file { "${consul::config_dir}/watch_${id}.json":
ensure => $ensure,
owner => $consul::user_real,
group => $consul::group_real,
mode => $consul::config_mode,
content => consul::sorted_json($watch_hash, $consul::pretty_config, $consul::pretty_config_indent),
notify => Class['consul::reload_service'],
}
}
|