Enable passing of integer value for pool-set action

Enable passing of an integer to the pool-set function.  Due to how juju
appears to parse things on the command line, setting type to string
causes it to fail to accept '3', "3" or 3 as a string.  Only "'3'"
works.  However, if we remove the type from actions.yaml and do the
validation in the charm, any value can be passed.

Depends-On: I6081c23af61fd5e872982ff477b0a5cb27141d11

Change-Id: Idf3468d9ae28dafc09c86f08b7f8c6470a665b7a
Closes-Bug: #1838650
This commit is contained in:
Alex Kavanagh 2020-07-02 14:56:41 +01:00
parent 74ad5886a9
commit b34d85ee81
3 changed files with 19 additions and 3 deletions

View File

@ -256,7 +256,10 @@ pool-set:
type: string
description: "Any valid Ceph key from http://docs.ceph.com/docs/master/rados/operations/pools/#set-pool-values"
value:
type: string
# LP: #1838650 - unfortunately, Juju appears to consider '3' on the
# command line as not being a string, and has to be quoted as "'3'". So,
# we actually let the charm do the verification, and let any value
# through here.
description: "The value to set"
required:
- key

View File

@ -32,7 +32,8 @@ if __name__ == '__main__':
'value': value}
try:
handle_set_pool_value(service='admin', request=request)
# Bug: #1838650 -- force coercion to an int for the value if required.
handle_set_pool_value(service='admin', request=request, coerce=True)
except CalledProcessError as e:
log(str(e))
action_fail("Setting pool key: {} and value: {} failed with "

View File

@ -540,11 +540,13 @@ def handle_remove_cache_tier(request, service):
pool.remove_cache_tier(cache_pool=cache_pool)
def handle_set_pool_value(request, service):
def handle_set_pool_value(request, service, coerce=False):
"""Sets an arbitrary pool value.
:param request: dict of request operations and params
:param service: The ceph client to run the command under.
:param coerce: Try to parse/coerce the value into the correct type.
Used by the action code that only gets Str from Juju
:returns: dict. exit-code and reason if not 0
"""
# Set arbitrary pool values
@ -558,6 +560,16 @@ def handle_set_pool_value(request, service):
# Get the validation method
validator_params = POOL_KEYS[params['key']]
# BUG: #1838650 - the function needs to try to coerce the value param to
# the type required for the validator to pass. Note, if this blows, then
# the param isn't parsable to the correct type.
if coerce:
try:
params['value'] = validator_params[0](params['value'])
except ValueError:
raise RuntimeError("Value {} isn't of type {}"
.format(params['value'], validator_params[0]))
# end of BUG: #1838650
if len(validator_params) == 1:
# Validate that what the user passed is actually legal per Ceph's rules
validator(params['value'], validator_params[0])