Fix inv source org lookup, adjust unit tests (since org is now required for inv source module), update README, edit typos in test-related README

This commit is contained in:
beeankha
2020-09-02 22:05:25 -04:00
parent b78dea3e4b
commit 7764f1c1a5
7 changed files with 75 additions and 67 deletions

View File

@@ -131,6 +131,7 @@ options:
organization:
description:
- Name of the inventory source's inventory's organization.
required: True
type: str
extends_documentation_fragment: awx.awx.auth
'''
@@ -173,7 +174,7 @@ def main():
enabled_value=dict(),
host_filter=dict(),
credential=dict(),
organization=dict(),
organization=dict(required=True),
overwrite=dict(type='bool'),
overwrite_vars=dict(type='bool'),
custom_virtualenv=dict(),
@@ -196,29 +197,34 @@ def main():
name = module.params.get('name')
new_name = module.params.get('new_name')
inventory = module.params.get('inventory')
organization = module.params.get('organization')
source_script = module.params.get('source_script')
credential = module.params.get('credential')
source_project = module.params.get('source_project')
state = module.params.get('state')
new_fields = {}
organization_id = None
organization = module.params.get('organization')
if organization:
organization_id = module.get_one_by_name_or_id('organizations', organization)
# Attempt to look up inventory source based on the provided name and inventory ID
inventory_id = module.resolve_name_to_id('inventories', inventory)
inventory_source = module.get_one('inventory_sources', **{
org_id = module.resolve_name_to_id('organizations', organization)
inventory_object = module.get_one('inventories', **{
'data': {
'name': inventory,
'organization': org_id,
}
})
if not inventory_object:
module.fail_json(msg='The specified inventory was not found.')
inventory_source_object = module.get_one('inventory_sources', **{
'data': {
'name': name,
'inventory': inventory_id,
'inventory': inventory_object['id'],
}
})
if state == 'absent':
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(inventory_source)
module.delete_if_needed(inventory_source_object)
# Attempt to look up associated field items the user specified.
association_fields = {}
@@ -244,7 +250,7 @@ def main():
# Create the data that gets sent for create and update
inventory_source_fields = {
'name': new_name if new_name else name,
'inventory': inventory_id,
'inventory': inventory_object['id'],
}
# Attempt to look up the related items the user specified (these will fail the module if not found)
@@ -273,12 +279,12 @@ def main():
inventory_source_fields['source_vars'] = dumps(inventory_source_fields['source_vars'])
# Sanity check on arguments
if state == 'present' and not inventory_source and not inventory_source_fields['source']:
if state == 'present' and not inventory_source_object and not inventory_source_fields['source']:
module.fail_json(msg="If creating a new inventory source, the source param must be present")
# If the state was present we can let the module build or update the existing inventory_source, this will return on its own
# If the state was present we can let the module build or update the existing inventory_source_object, this will return on its own
module.create_or_update_if_needed(
inventory_source, inventory_source_fields,
inventory_source_object, inventory_source_fields,
endpoint='inventory_sources', item_type='inventory source',
associations=association_fields
)

View File

@@ -36,7 +36,7 @@ options:
description:
- Name of the inventory source's inventory's organization.
type: str
required: False
required: True
wait:
description:
- Wait for the job to complete.
@@ -91,7 +91,7 @@ def main():
argument_spec = dict(
inventory=dict(required=True),
inventory_source=dict(required=True),
organization=dict(),
organization=dict(required=True),
wait=dict(default=False, type='bool'),
interval=dict(default=1.0, type='float'),
timeout=dict(default=None, type='int'),
@@ -103,20 +103,31 @@ def main():
# Extract our parameters
inventory = module.params.get('inventory')
inventory_source = module.params.get('inventory_source')
organization = module.params.get('organization')
wait = module.params.get('wait')
interval = module.params.get('interval')
timeout = module.params.get('timeout')
new_fields = {}
organization_id = None
organization = module.params.get('organization')
if organization:
organization_id = module.get_one_by_name_or_id('organizations', organization)
org_id = module.resolve_name_to_id('organizations', organization)
inventory_object = module.get_one('inventories', **{
'data': {
'name': inventory,
'organization': org_id,
}
})
# Attempt to look up the inventory the user specified (these will fail the module if not found)
inventory_object = module.get_one_by_name_or_id('inventories', inventory)
# Return all inventory sources related to the specified inventory
inventory_source_object = module.get_one_by_name_or_id(inventory_object['related']['inventory_sources'], inventory_source)
if not inventory_object:
module.fail_json(msg='The specified inventory was not found.')
inventory_source_object = module.get_one('inventory_sources', **{
'data': {
'name': inventory_source,
'inventory': inventory_object['id'],
}
})
if not inventory_source_object:
module.fail_json(msg='The specified inventory source was not found.')
# Sync the inventory source(s)
inventory_source_update_results = module.post_endpoint(inventory_source_object['related']['update'], **{'data': {}})
@@ -124,12 +135,13 @@ def main():
if inventory_source_update_results['status_code'] != 202:
module.fail_json(msg="Failed to update inventory source, see response for details", **{'response': inventory_source_update_results})
inventory_source_update_results = module.wait_on_url(
url=inventory_source_update_results['json']['url'],
object_name=inventory_object,
object_type='inventory_update',
timeout=timeout, interval=interval
)
if wait:
inventory_source_update_results = module.wait_on_url(
url=inventory_source_update_results['json']['url'],
object_name=inventory_object,
object_type='inventory_update',
timeout=timeout, interval=interval
)
module.exit_json(**{
'changed': True,