Skip to content

Commit a8d80a2

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

File tree

1 file changed

+55
-78
lines changed

1 file changed

+55
-78
lines changed

manifests/volume.pp

Lines changed: 55 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,12 @@
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 (/^([^:]+):(.*)/) [strip($m[1]), strip($m[2]),]
82+
})
8183
} else {
82-
$_options = undef
84+
$_options = $options
8385
}
8486

8587
if $arbiter {
@@ -120,35 +122,22 @@
120122
}
121123

122124
# 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)
125+
# first we need to prefix each array element with the volume name
126+
# so that we match the gluster::volume::option title format of
127+
# volume:option
141128

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-
}
129+
# we need to ensure that these are applied AFTER the volume is created
130+
# but BEFORE the volume is started
131+
$new_volume_defaults = {
132+
require => Exec["gluster create volume ${title}"],
133+
before => Exec["gluster start volume ${title}"],
134+
}
150135

151-
create_resources(::gluster::volume::option, $hoh, $new_volume_defaults)
136+
$_options.each |$opt_name, $opt_value| {
137+
gluster::volume::option { "${title}:${opt_name}":
138+
value => $opt_value,
139+
* => $new_volume_defaults,
140+
}
152141
}
153142

154143
# don't forget to start the new volume!
@@ -226,48 +215,36 @@
226215
}
227216

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

232-
if $_current != $_options {
221+
if $current_options_keys != $provided_options_keys {
233222
#
234223
# either of $current_options or $_options may be empty.
235224
# we need to account for this situation
236225
#
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)
226+
$to_remove = difference($current_options_keys, $provided_options_keys)
227+
$to_add = difference($provided_options_keys, $current_options_keys)
228+
229+
# we have some options active that are not defined here. Remove them
230+
#
231+
# the syntax to remove ::gluster::volume::options is a little different
232+
# so build up the hash correctly
233+
#
234+
$to_remove.each |$opt_name| {
258235
if $remove_options {
259-
create_resources( ::gluster::volume::option, $remove )
236+
gluster::volume::option { "${title}:${opt_name}":
237+
ensure => absent,
238+
}
260239
} else {
261-
$remove_str = join( keys($remove), ', ' )
262-
notice("NOT REMOVING the following options for volume ${title}: ${remove_str}.")
240+
notice("NOT REMOVING the following option for volume ${title}: ${opt_name}.")
263241
}
264242
}
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 )
243+
# we have some options defined that are not active. Add them
244+
$to_add.each |$opt_name| {
245+
gluster::volume::option { "${title}:${opt_name}":
246+
value => $provided_options_keys[$opt_name],
247+
}
271248
}
272249
}
273250
} else {

0 commit comments

Comments
 (0)