Skip to content

Commit b04198d

Browse files
committed
salt/states/boto3_elasticsearch.py: Deduplicated duplicate code into _check_return_value
1 parent 4499a08 commit b04198d

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

salt/states/boto3_elasticsearch.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,21 @@ def __virtual__():
8080
return __virtualname__
8181

8282

83+
def _check_return_value(ret):
84+
'''
85+
Helper function to check if the 'result' key of the return value has been
86+
properly set. This is to detect unexpected code-paths that would otherwise
87+
return a 'success'-y value but not actually be succesful.
88+
89+
:param dict ret: The returned value of a state function.
90+
'''
91+
if ret['result'] == 'oops':
92+
ret['result'] = False
93+
ret['comment'].append('An internal error has occurred: The result value was '
94+
'not properly changed.')
95+
return ret
96+
97+
8398
def present(
8499
name,
85100
elasticsearch_version=None,
@@ -363,11 +378,7 @@ def present(
363378
'changes:new:tags',
364379
res['changes']['new']
365380
)
366-
367-
if ret['result'] == 'oops':
368-
ret['result'] = False
369-
ret['comment'].append('An internal error has occurred: The result value was '
370-
'not properly changed.')
381+
ret = _check_return_value(ret)
371382
return ret
372383

373384

@@ -425,10 +436,7 @@ def absent(
425436
ret['result'] = True
426437
ret['comment'].append('Elasticsearch domain "{}" is already absent.'
427438
''.format(name))
428-
if ret['result'] == 'oops':
429-
ret['result'] = False
430-
ret['comment'].append('An internal error has occurred: The result value was '
431-
'not properly changed.')
439+
ret = _check_return_value(ret)
432440
return ret
433441

434442

@@ -562,10 +570,7 @@ def upgraded(
562570
''.format(name, elasticsearch_version))
563571
ret['changes'] = {'old': current_domain['ElasticsearchVersion'],
564572
'new': elasticsearch_version}
565-
if ret['result'] == 'oops':
566-
ret['result'] = False
567-
ret['comment'].append('An internal error has occurred: The result value was '
568-
'not properly changed.')
573+
ret = _check_return_value(ret)
569574
return ret
570575

571576

@@ -653,10 +658,7 @@ def latest(
653658
ret['comment'].append('Elasticsearch domain "{}" is already at its '
654659
'latest minor version {}.'
655660
''.format(name, current_version))
656-
if ret['result'] == 'oops':
657-
ret['result'] = False
658-
ret['comment'].append('An internal error has occurred: The result value was '
659-
'not properly changed.')
661+
ret = _check_return_value(ret)
660662
if ret['result'] and ret['changes'] and not minor_only:
661663
# Try and see if we can upgrade again
662664
res = latest(name, minor_only=minor_only, region=region, keyid=keyid, key=key, profile=profile)
@@ -748,8 +750,5 @@ def tagged(
748750
ret['result'] = True
749751
ret['comment'].append('Tags on Elasticsearch domain "{}" have been '
750752
'{}ed.'.format(name, 'replac' if replace else 'add'))
751-
if ret['result'] == 'oops':
752-
ret['result'] = False
753-
ret['comment'].append('An internal error has occurred: The result value was '
754-
'not properly changed.')
753+
ret = _check_return_value(ret)
755754
return ret

0 commit comments

Comments
 (0)