mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 01:57:35 -03:30
added awx collection support for bulk api
return more context for bulk host create
now return list of minimal info about host objects
[
{
"name": "lakjdsafoiaweirnladlk",
"enabled": true,
"instance_id": "",
"description": "",
"variables": "",
"id": 4593,
"url": "/api/v2/hosts/4593/",
"inventory": "/api/v2/inventories/1/"
}
]
Updated tests, but needed to work around some weird behavior with
sqlite. Apparently it behaves differently around assigning ID's to the
result of bulk_create and that is messed up my use of `reverse` to look
up the url of the hosts
This commit is contained in:
parent
34834252ff
commit
ce5270434c
@ -2065,8 +2065,17 @@ class BulkHostCreateSerializer(serializers.Serializer):
|
||||
|
||||
# This actually updates the cached "total_hosts" field on the inventory
|
||||
update_inventory_computed_fields.delay(validated_data['inventory'].id)
|
||||
ids = [item.id for item in result]
|
||||
return {"created": ids, "url": InventorySerializer().get_related(validated_data['inventory'])['hosts']}
|
||||
return_keys = [k for k in BulkHostSerializer().fields.keys()] + ['id']
|
||||
return_data = []
|
||||
for r in result:
|
||||
item = {k: getattr(r, k) for k in return_keys}
|
||||
if not settings.IS_TESTING_MODE:
|
||||
# sqlite acts different with bulk_create -- it doesn't return the id of the objects
|
||||
# to get it, you have to do an additional query, which is not useful for our tests
|
||||
item['url'] = reverse('api:host_detail', kwargs={'pk': r.id})
|
||||
item['inventory'] = reverse('api:inventory_detail', kwargs={'pk': validated_data['inventory'].id})
|
||||
return_data.append(item)
|
||||
return return_data
|
||||
|
||||
|
||||
class GroupTreeSerializer(GroupSerializer):
|
||||
|
||||
@ -47,7 +47,7 @@ def test_bulk_host_create_num_queries(organization, inventory, post, get, user,
|
||||
hosts = [{'name': uuid4()} for i in range(num_hosts)]
|
||||
with withAssertNumQueriesLessThan(num_queries):
|
||||
bulk_host_create_response = post(reverse('api:bulk_host_create'), {'inventory': inventory.id, 'hosts': hosts}, u, expect=201).data
|
||||
assert bulk_host_create_response['created'] == len(hosts), f"unexpected number of hosts created for user {u}"
|
||||
assert len(bulk_host_create_response) == len(hosts), f"unexpected number of hosts created for user {u}"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@ -80,7 +80,7 @@ def test_bulk_host_create_rbac(organization, inventory, post, get, user):
|
||||
bulk_host_create_response = post(
|
||||
reverse('api:bulk_host_create'), {'inventory': inventory.id, 'hosts': [{'name': f'foobar-{indx}'}]}, u, expect=201
|
||||
).data
|
||||
assert bulk_host_create_response['created'] == 1, f"unexpected number of hosts created for user {u}"
|
||||
assert len(bulk_host_create_response) == 1, f"unexpected number of hosts created for user {u}"
|
||||
|
||||
for indx, u in enumerate([member, auditor, use_inv_member]):
|
||||
bulk_host_create_response = post(
|
||||
@ -135,8 +135,7 @@ def test_bulk_job_launch_no_org_assigned(job_template, organization, inventory,
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bulk_job_launch_multiple_org_assigned(job_template, organization, inventory, project, credential, post, get,
|
||||
user):
|
||||
def test_bulk_job_launch_multiple_org_assigned(job_template, organization, inventory, project, credential, post, get, user):
|
||||
'''
|
||||
if I am part of multiple organization...
|
||||
and if I do not provide org at the launch time
|
||||
@ -156,8 +155,7 @@ def test_bulk_job_launch_multiple_org_assigned(job_template, organization, inven
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bulk_job_launch_specific_org(job_template, organization, inventory, project, credential, post, get,
|
||||
user):
|
||||
def test_bulk_job_launch_specific_org(job_template, organization, inventory, project, credential, post, get, user):
|
||||
'''
|
||||
if I am part of multiple organization...
|
||||
and if I provide org at the launch time
|
||||
@ -172,12 +170,12 @@ def test_bulk_job_launch_specific_org(job_template, organization, inventory, pro
|
||||
jt.save()
|
||||
jt.execute_role.members.add(normal_user)
|
||||
bulk_job_launch_response = post(
|
||||
reverse('api:bulk_job_launch'), {'name': 'Bulk Job Launch', 'jobs': [{'unified_job_template': jt.id}], 'organization': org1.id}, normal_user, expect=201
|
||||
reverse('api:bulk_job_launch'), {'name': 'Bulk Job Launch', 'jobs': [{'unified_job_template': jt.id}], 'organization': org1.id}, normal_user, expect=201
|
||||
).data
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_bulk_job_launch_inventory_no_access(job_template, organization, inventory, project, credential, post, get,
|
||||
user):
|
||||
def test_bulk_job_launch_inventory_no_access(job_template, organization, inventory, project, credential, post, get, user):
|
||||
'''
|
||||
if I don't have access to the inventory...
|
||||
and if I try to use it at the launch time
|
||||
@ -192,5 +190,5 @@ def test_bulk_job_launch_inventory_no_access(job_template, organization, invento
|
||||
inv = Inventory.objects.create(name='inv1', organization=org2)
|
||||
jt.execute_role.members.add(normal_user)
|
||||
bulk_job_launch_response = post(
|
||||
reverse('api:bulk_job_launch'), {'name': 'Bulk Job Launch', 'jobs': [{'unified_job_template': jt.id, 'inventory': inv.id}]}, normal_user, expect=400
|
||||
reverse('api:bulk_job_launch'), {'name': 'Bulk Job Launch', 'jobs': [{'unified_job_template': jt.id, 'inventory': inv.id}]}, normal_user, expect=400
|
||||
).data
|
||||
|
||||
@ -11,11 +11,13 @@ from datetime import timedelta
|
||||
|
||||
|
||||
if "pytest" in sys.modules:
|
||||
IS_TESTING_MODE = True
|
||||
from unittest import mock
|
||||
|
||||
with mock.patch('__main__.__builtins__.dir', return_value=[]):
|
||||
import ldap
|
||||
else:
|
||||
IS_TESTING_MODE = False
|
||||
import ldap
|
||||
|
||||
|
||||
|
||||
73
awx_collection/plugins/modules/bulk_host_create.py
Normal file
73
awx_collection/plugins/modules/bulk_host_create.py
Normal file
@ -0,0 +1,73 @@
|
||||
#!/usr/bin/python
|
||||
# coding: utf-8 -*-
|
||||
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: bulk_host_create
|
||||
author: "Seth Foster (@fosterseth)"
|
||||
short_description: Bulk host create in Automation Platform Controller
|
||||
description:
|
||||
- Single-request bulk host creation in Automation Platform Controller.
|
||||
- Designed to efficiently add many hosts to an inventory.
|
||||
options:
|
||||
hosts:
|
||||
description:
|
||||
- List of hosts to add to inventory.
|
||||
required: True
|
||||
type: str
|
||||
inventory:
|
||||
description:
|
||||
- Inventory the hosts should be made a member of.
|
||||
required: True
|
||||
type: str
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Bulk host create
|
||||
bulk_host_create:
|
||||
inventory: 1
|
||||
hosts:
|
||||
- name: foobar.org
|
||||
- name: 127.0.0.1
|
||||
'''
|
||||
|
||||
from ..module_utils.controller_api import ControllerAPIModule
|
||||
import json
|
||||
|
||||
def main():
|
||||
# Any additional arguments that are not fields of the item can be added here
|
||||
argument_spec = dict(
|
||||
hosts=dict(required=True, type='list'),
|
||||
inventory=dict(),
|
||||
)
|
||||
|
||||
# Create a module for ourselves
|
||||
module = ControllerAPIModule(argument_spec=argument_spec)
|
||||
|
||||
# Extract our parameters
|
||||
inventory = module.params.get('inventory')
|
||||
hosts = module.params.get('hosts')
|
||||
|
||||
# Launch the jobs
|
||||
result = module.post_endpoint("bulk/host_create", data={"inventory": inventory, "hosts": hosts})
|
||||
|
||||
if result['status_code'] != 201:
|
||||
module.fail_json(msg="Failed to create hosts, see response for details", response=result)
|
||||
|
||||
module.json_output['changed'] = True
|
||||
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
163
awx_collection/plugins/modules/bulk_job_launch.py
Normal file
163
awx_collection/plugins/modules/bulk_job_launch.py
Normal file
@ -0,0 +1,163 @@
|
||||
#!/usr/bin/python
|
||||
# coding: utf-8 -*-
|
||||
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: bulk_job_launch
|
||||
author: "Seth Foster (@fosterseth)"
|
||||
short_description: Bulk job launch in Automation Platform Controller
|
||||
description:
|
||||
- Single-request bulk job launch in Automation Platform Controller.
|
||||
- The result is flat workflow, each job specified in the parameter jobs results in a workflow job node.
|
||||
- Any options specified at the top level will inherited by the launched jobs (if prompt on launch is enabled for those fields).
|
||||
- Designed to efficiently start many jobs at once.
|
||||
options:
|
||||
jobs:
|
||||
description:
|
||||
- List of jobs to create.
|
||||
- Any promptable field on unified_job_template can be provided as a field on the list item (e.g. limit).
|
||||
required: True
|
||||
type: list
|
||||
name:
|
||||
description:
|
||||
- The name of the bulk job that is created
|
||||
required: False
|
||||
type: str
|
||||
organization:
|
||||
description:
|
||||
- If not provided, will use the organization the user is in.
|
||||
- Required if the user belongs to more than one organization.
|
||||
- Affects who can see the resulting bulk job.
|
||||
type: str
|
||||
inventory:
|
||||
description:
|
||||
- Inventory to use for the jobs ran within the bulk job, only used if prompt for inventory is set.
|
||||
type: str
|
||||
limit:
|
||||
description:
|
||||
- Limit to use for the I(job_template).
|
||||
type: str
|
||||
scm_branch:
|
||||
description:
|
||||
- A specific branch of the SCM project to run the template on.
|
||||
- This is only applicable if your project allows for branch override.
|
||||
type: str
|
||||
extra_vars:
|
||||
description:
|
||||
- Any extra vars required to launch the job.
|
||||
type: dict
|
||||
job_tags:
|
||||
description:
|
||||
- A comma-separated list of playbook tags to specify what parts of the playbooks should be executed.
|
||||
type: str
|
||||
skip_tags:
|
||||
description:
|
||||
- A comma-separated list of playbook tags to skip certain tasks or parts of the playbooks to be executed.
|
||||
type: str
|
||||
wait:
|
||||
description:
|
||||
- Wait for the workflow to complete.
|
||||
default: True
|
||||
type: bool
|
||||
interval:
|
||||
description:
|
||||
- The interval to request an update from the controller.
|
||||
required: False
|
||||
default: 2
|
||||
type: float
|
||||
timeout:
|
||||
description:
|
||||
- If waiting for the workflow to complete this will abort after this
|
||||
amount of seconds
|
||||
type: int
|
||||
extends_documentation_fragment: awx.awx.auth
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
job_info:
|
||||
description: dictionary containing information about the workflow executed
|
||||
returned: If workflow launched
|
||||
type: dict
|
||||
'''
|
||||
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Launch bulk jobs
|
||||
bulk_job_launch:
|
||||
name: My Bulk Job Launch
|
||||
jobs:
|
||||
- unified_job_template: 7
|
||||
- unified_job_template: 10
|
||||
limit: foo
|
||||
limit: bar
|
||||
inventory: 1 # only affects job templates with prompt on launch enabled for inventory
|
||||
|
||||
- name: Launch bulk jobs with lookup plugin
|
||||
bulk_job_launch:
|
||||
name: My Bulk Job Launch
|
||||
jobs:
|
||||
- unified_job_template: 7
|
||||
- unified_job_template: "{{ lookup('awx.awx.controller_api', 'job_templates', query_params={'name': 'Demo Job Template'}, return_ids=True) }}"
|
||||
'''
|
||||
|
||||
from ..module_utils.controller_api import ControllerAPIModule
|
||||
import json
|
||||
|
||||
def main():
|
||||
# Any additional arguments that are not fields of the item can be added here
|
||||
argument_spec = dict(
|
||||
jobs=dict(required=True, type='list'),
|
||||
name=dict(),
|
||||
organization=dict(),
|
||||
inventory=dict(),
|
||||
limit=dict(),
|
||||
scm_branch=dict(),
|
||||
extra_vars=dict(type='dict'),
|
||||
job_tags=dict(),
|
||||
skip_tags=dict(),
|
||||
wait=dict(required=False, default=True, type='bool'),
|
||||
interval=dict(required=False, default=2.0, type='float'),
|
||||
timeout=dict(required=False, default=None, type='int'),
|
||||
)
|
||||
|
||||
# Create a module for ourselves
|
||||
module = ControllerAPIModule(argument_spec=argument_spec)
|
||||
|
||||
# Extract our parameters
|
||||
name = module.params.get('name')
|
||||
wait = module.params.get('wait')
|
||||
timeout = module.params.get('timeout')
|
||||
interval = module.params.get('interval')
|
||||
jobs = module.params.get('jobs')
|
||||
|
||||
# Launch the jobs
|
||||
result = module.post_endpoint("bulk/job_launch", data={"jobs": jobs})
|
||||
|
||||
if result['status_code'] != 201:
|
||||
module.fail_json(msg="Failed to launch bulk jobs, see response for details", response=result)
|
||||
|
||||
module.json_output['changed'] = True
|
||||
module.json_output['id'] = result['json']['id']
|
||||
module.json_output['status'] = result['json']['status']
|
||||
# This is for backwards compatability
|
||||
module.json_output['job_info'] = {'id': result['json']['id']}
|
||||
|
||||
if not wait:
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
# Invoke wait function
|
||||
module.wait_on_url(url=result['json']['url'], object_name=name, object_type='Bulk Job Launch', timeout=timeout, interval=interval)
|
||||
|
||||
module.exit_json(**module.json_output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
x
Reference in New Issue
Block a user