1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Check argument choices according to type value

This commit is contained in:
Hiroaki Nakamura 2016-06-30 01:59:37 +09:00 committed by Matt Clay
parent bb69e7030c
commit 22957e79f6

View file

@ -317,7 +317,6 @@ except ImportError:
# LXD_ANSIBLE_STATES is a map of states that contain values of methods used
# when a particular state is evoked.
LXD_ANSIBLE_STATES = {
'present': '', # TODO: Separate state for profile
'started': '_started',
'stopped': '_stopped',
'restarted': '_restarted',
@ -325,6 +324,11 @@ LXD_ANSIBLE_STATES = {
'frozen': '_frozen'
}
# PROFILE_STATES is a list for states supported for type=profiles
PROFILES_STATES = [
'present', 'absent'
]
# ANSIBLE_LXD_STATES is a map of states of lxd containers to the Ansible
# lxc_container module state parameter value.
ANSIBLE_LXD_STATES = {
@ -361,8 +365,13 @@ class LxdContainerManagement(object):
self.name = self.module.params['name']
self.type = self.module.params['type']
self._build_config()
# TODO: check state value according to type
self.state = self.module.params['state']
if self.type == 'container':
self._check_argument_choices('state', self.state, LXD_ANSIBLE_STATES.keys())
elif self.type == 'profile':
self._check_argument_choices('state', self.state, PROFILES_STATES)
self.new_name = self.module.params.get('new_name', None)
self.timeout = self.module.params['timeout']
self.wait_for_ipv4_addresses = self.module.params['wait_for_ipv4_addresses']
@ -383,6 +392,12 @@ class LxdContainerManagement(object):
self.logs = []
self.actions = []
def _check_argument_choices(self, name, value, choices):
if value not in choices:
choices_str=",".join([str(c) for c in choices])
msg="value of %s must be one of: %s, got: %s" % (name, choices_str, value)
self.module.fail_json(msg=msg)
def _build_config(self):
self.config = {}
for attr in CONFIG_PARAMS[self.type]:
@ -767,7 +782,7 @@ def main():
type='dict',
),
state=dict(
choices=LXD_ANSIBLE_STATES.keys(),
choices=list(set(LXD_ANSIBLE_STATES.keys()) | set(PROFILES_STATES)),
default='started'
),
timeout=dict(