Skip to content

Commit 226025b

Browse files
committed
Make volume options a hash and fix options handling
1 parent fdbf65a commit 226025b

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

6060
$_force = if $force {
@@ -77,10 +77,16 @@
7777

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

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

8692
if $arbiter {
@@ -121,35 +127,22 @@
121127
}
122128

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

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

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

155148
# don't forget to start the new volume!
@@ -229,48 +222,36 @@
229222
}
230223

231224
# did the options change?
232-
$current_options_hash = pick(fact("gluster_volumes.${title}.options"), {})
233-
$_current = sort(join_keys_to_values($current_options_hash, ': '))
225+
$current_options_keys = sort(keys(pick(fact("gluster_volumes.${title}.options"), {})))
226+
$provided_options_keys = sort(keys($_options))
234227

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

0 commit comments

Comments
 (0)