Skip to content

Commit 26e0238

Browse files
committed
Make volume options a hash and fix options handling
1 parent 48ea44c commit 26e0238

File tree

1 file changed

+59
-78
lines changed

1 file changed

+59
-78
lines changed

manifests/volume.pp

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# @param bricks
1818
# an array of bricks to use for this volume
1919
# @param options
20-
# an array of volume options for the volume
20+
# an hash of volume options for the volume, also accepts array for BC
2121
# @param remove_options
2222
# whether to permit the removal of active options that are not defined for
2323
# this volume.
@@ -33,10 +33,10 @@
3333
# 'srv1.local:/export/brick2/brick',
3434
# 'srv2.local:/export/brick2/brick',
3535
# ],
36-
# options => [
37-
# 'server.allow-insecure: on',
38-
# 'nfs.ports-insecure: on',
39-
# ],
36+
# options => {
37+
# 'server.allow-insecure': on',
38+
# 'nfs.ports-insecure': 'on',
39+
# },
4040
# }
4141
#
4242
# @author Scott Merrill <[email protected]>
@@ -45,16 +45,16 @@
4545
define gluster::volume (
4646
Array[String, 1] $bricks,
4747

48-
Enum['present', 'absent'] $ensure = 'present',
49-
Boolean $force = false,
50-
Enum['tcp', 'rdma', 'tcp,rdma'] $transport = 'tcp',
51-
Boolean $rebalance = true,
52-
Boolean $heal = true,
53-
Boolean $remove_options = false,
54-
Optional[Array] $options = undef,
55-
Optional[Integer] $stripe = undef,
56-
Optional[Integer] $replica = undef,
57-
Optional[Integer] $arbiter = undef,
48+
Enum['present', 'absent'] $ensure = 'present',
49+
Boolean $force = false,
50+
Enum['tcp', 'rdma', 'tcp,rdma'] $transport = 'tcp',
51+
Boolean $rebalance = true,
52+
Boolean $heal = true,
53+
Boolean $remove_options = false,
54+
Variant[Array[String],Hash] $options = {},
55+
Optional[Integer] $stripe = undef,
56+
Optional[Integer] $replica = undef,
57+
Optional[Integer] $arbiter = undef,
5858
) {
5959
$_force = if $force {
6060
'force'
@@ -76,10 +76,16 @@
7676

7777
$_transport = "transport ${transport}"
7878

79-
if $options and ! empty( $options ) {
80-
$_options = sort( $options )
79+
if $options =~ Array {
80+
$_options = Hash($options.map |$item| {
81+
$m = $item.match(/^([^:]+):(.*)/)
82+
[
83+
strip($m[1]),
84+
strip($m[2])
85+
]
86+
})
8187
} else {
82-
$_options = undef
88+
$_options = $options
8389
}
8490

8591
if $arbiter {
@@ -120,35 +126,22 @@
120126
}
121127

122128
# if we have volume options, activate them now
123-
#
124-
# Note: $options is an array, but create_resources requires
125-
# a hash of hashes. We do some contortions to get the
126-
# array into the hash of hashes that looks like:
127-
#
128-
# option.name:
129-
# value: value
130-
#
131-
# Note 2: we're using the $_options variable, which contains the
132-
# sorted list of options.
133-
if $_options {
134-
# first we need to prefix each array element with the volume name
135-
# so that we match the gluster::volume::option title format of
136-
# volume:option
137-
$vol_opts = prefix( $_options, "${title}:" )
138-
# now we make some YAML, and then parse that to get a Puppet hash
139-
$yaml = join( regsubst( $vol_opts, ': ', ":\n value: ", 'G'), "\n")
140-
$hoh = parseyaml($yaml)
129+
# first we need to prefix each array element with the volume name
130+
# so that we match the gluster::volume::option title format of
131+
# volume:option
141132

142-
# safety check
143-
assert_type(Hash, $hoh)
144-
# we need to ensure that these are applied AFTER the volume is created
145-
# but BEFORE the volume is started
146-
$new_volume_defaults = {
147-
require => Exec["gluster create volume ${title}"],
148-
before => Exec["gluster start volume ${title}"],
149-
}
133+
# we need to ensure that these are applied AFTER the volume is created
134+
# but BEFORE the volume is started
135+
$new_volume_defaults = {
136+
require => Exec["gluster create volume ${title}"],
137+
before => Exec["gluster start volume ${title}"],
138+
}
150139

151-
create_resources(::gluster::volume::option, $hoh, $new_volume_defaults)
140+
$_options.each |$opt_name, $opt_value| {
141+
gluster::volume::option { "${title}:${opt_name}":
142+
value => $opt_value,
143+
* => $new_volume_defaults
144+
}
152145
}
153146

154147
# don't forget to start the new volume!
@@ -226,48 +219,36 @@
226219
}
227220

228221
# did the options change?
229-
$current_options_hash = pick(fact("gluster_volumes.${title}.options"), {})
230-
$_current = sort(join_keys_to_values($current_options_hash, ': '))
222+
$current_options_keys = sort(keys(pick(fact("gluster_volumes.${title}.options"), {})))
223+
$provided_options_keys = sort(keys($_options))
231224

232-
if $_current != $_options {
225+
if $current_options_keys != $provided_options_keys {
233226
#
234227
# either of $current_options or $_options may be empty.
235228
# we need to account for this situation
236229
#
237-
if is_array($_current) and is_array($_options) {
238-
$to_remove = difference($_current, $_options)
239-
$to_add = difference($_options, $_current)
240-
} else {
241-
if is_array($_current) {
242-
# $_options is not an array, so remove all currently set options
243-
$to_remove = $_current
244-
} elsif is_array($_options) {
245-
# $current_options is not an array, so add all our defined options
246-
$to_add = $_options
247-
}
248-
}
249-
if ! empty($to_remove) {
250-
# we have some options active that are not defined here. Remove them
251-
#
252-
# the syntax to remove ::gluster::volume::options is a little different
253-
# so build up the hash correctly
254-
#
255-
$remove_opts = prefix( $to_remove, "${title}:" )
256-
$remove_yaml = join( regsubst( $remove_opts, ': .+$', ":\n ensure: absent", 'G' ), "\n" )
257-
$remove = parseyaml($remove_yaml)
230+
$to_remove = difference($current_options_keys, $provided_options_keys)
231+
$to_add = difference($provided_options_keys, $current_options_keys)
232+
233+
# we have some options active that are not defined here. Remove them
234+
#
235+
# the syntax to remove ::gluster::volume::options is a little different
236+
# so build up the hash correctly
237+
#
238+
$to_remove.each |$opt_name| {
258239
if $remove_options {
259-
create_resources( ::gluster::volume::option, $remove )
240+
gluster::volume::option { "${title}:${opt_name}":
241+
ensure => absent
242+
}
260243
} else {
261-
$remove_str = join( keys($remove), ', ' )
262-
notice("NOT REMOVING the following options for volume ${title}: ${remove_str}.")
244+
notice("NOT REMOVING the following option for volume ${title}: ${opt_name}.")
263245
}
264246
}
265-
if ! empty($to_add) {
266-
# we have some options defined that are not active. Add them
267-
$add_opts = prefix( $to_add, "${title}:" )
268-
$add_yaml = join( regsubst( $add_opts, ': ', ":\n value: ", 'G' ), "\n" )
269-
$add = parseyaml($add_yaml)
270-
create_resources( ::gluster::volume::option, $add )
247+
# we have some options defined that are not active. Add them
248+
$to_add.each |$opt_name| {
249+
gluster::volume::option { "${title}:${opt_name}":
250+
value => $provided_options_keys[$opt_name]
251+
}
271252
}
272253
}
273254
} else {

0 commit comments

Comments
 (0)