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

Improve error messages on missing required variables.

This commit is contained in:
Vincent Viallet 2013-06-21 21:41:00 +08:00
parent c124411c36
commit 7b0b9016b5

View file

@ -237,18 +237,11 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
# Any create step triggers a job that need to be waited for. # Any create step triggers a job that need to be waited for.
if not servers: if not servers:
new_server = True for arg in ('name', 'plan', 'distribution', 'datacenter'):
# TODO - improve if not eval(arg):
if not name: module.fail_json(msg='%s is required for active state' % arg)
module.fail_json(msg='%s is required for active state' % 'name')
if not plan:
module.fail_json(msg='%s is required for active state' % 'plan')
if not distribution:
module.fail_json(msg='%s is required for active state' % 'distribution')
if not datacenter:
module.fail_json(msg='%s is required for active state' % 'datacenter')
# Create linode entity # Create linode entity
new_server = True
try: try:
res = api.linode_create(DatacenterID=datacenter, PlanID=plan, res = api.linode_create(DatacenterID=datacenter, PlanID=plan,
PaymentTerm=payment_term) PaymentTerm=payment_term)
@ -261,16 +254,11 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE']) module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
if not disks: if not disks:
new_server = True for arg in ('name', 'linode_id', 'distribution'):
# TODO - improve if not eval(arg):
if not name: module.fail_json(msg='%s is required for active state' % arg)
module.fail_json(msg='%s is required for active state' % 'name')
if not linode_id:
module.fail_json(msg='%s is required for active state' % 'linode_id')
if not distribution:
module.fail_json(msg='%s is required for active state' % 'distribution')
# Create disks (1 from distrib, 1 for SWAP) # Create disks (1 from distrib, 1 for SWAP)
new_server = True
try: try:
if not password: if not password:
# Password is required on creation, if not provided generate one # Password is required on creation, if not provided generate one
@ -299,14 +287,9 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE']) module.fail_json(msg = '%s' % e.value[0]['ERRORMESSAGE'])
if not configs: if not configs:
new_server = True for arg in ('name', 'linode_id', 'distribution'):
# TODO - improve if not eval(arg):
if not name: module.fail_json(msg='%s is required for active state' % arg)
module.fail_json(msg='%s is required for active state' % 'name')
if not linode_id:
module.fail_json(msg='%s is required for active state' % 'linode_id')
if not distribution:
module.fail_json(msg='%s is required for active state' % 'distribution')
# Check architecture # Check architecture
for distrib in api.avail_distributions(): for distrib in api.avail_distributions():
@ -337,6 +320,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
disks_list = ','.join(disks_id) disks_list = ','.join(disks_id)
# Create config # Create config
new_server = True
try: try:
api.linode_config_create(LinodeId=linode_id, KernelId=kernel_id, api.linode_config_create(LinodeId=linode_id, KernelId=kernel_id,
Disklist=disks_list, Label='%s config' % name) Disklist=disks_list, Label='%s config' % name)
@ -388,14 +372,15 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
if new_server and not ssh_pub_key: if new_server and not ssh_pub_key:
instance['password'] = password instance['password'] = password
instances.append(instance) instances.append(instance)
elif state in ('stopped'): elif state in ('stopped'):
if not name: for arg in ('name', 'linode_id'):
module.fail_json(msg='%s is required for stopped state' % 'name') if not eval(arg):
if not linode_id: module.fail_json(msg='%s is required for active state' % arg)
module.fail_json(msg='%s is required for stopped state' % 'linode_id')
if not servers: if not servers:
module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id)) module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id))
for server in servers: for server in servers:
instance = getInstanceDetails(api, server) instance = getInstanceDetails(api, server)
if server['STATUS'] != 2: if server['STATUS'] != 2:
@ -408,14 +393,15 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
else: else:
instance['status'] = 'Stopped' instance['status'] = 'Stopped'
instances.append(instance) instances.append(instance)
elif state in ('restarted'): elif state in ('restarted'):
if not name: for arg in ('name', 'linode_id'):
module.fail_json(msg='%s is required for restarted state' % 'name') if not eval(arg):
if not linode_id: module.fail_json(msg='%s is required for active state' % arg)
module.fail_json(msg='%s is required for restarted state' % 'linode_id')
if not servers: if not servers:
module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id)) module.fail_json(msg = 'Server %s (lid: %s) not found' % (name, linode_id))
for server in servers: for server in servers:
instance = getInstanceDetails(api, server) instance = getInstanceDetails(api, server)
try: try:
@ -425,6 +411,7 @@ def linodeServers(module, api, state, name, plan, distribution, datacenter, lino
instance['status'] = 'Restarting' instance['status'] = 'Restarting'
changed = True changed = True
instances.append(instance) instances.append(instance)
elif state in ('absent', 'deleted'): elif state in ('absent', 'deleted'):
for server in servers: for server in servers:
instance = getInstanceDetails(api, server) instance = getInstanceDetails(api, server)