mirror of
https://github.com/ZwareBear/awx.git
synced 2026-03-19 23:33:37 -05:00
Make the bulk endpoint templates work in API browser
Various fixes - Don't skip checking resource RBAC permissions for admins Necessary to handle bad input, e.g. providing a unified_job_template id that doesn't exit - In awxkit, only "walk" if we get 'url' in the result - Bulk host create should return url pointing to inventory, not inventory/hosts dont do org check for superuser
This commit is contained in:
committed by
Elijah DeLee
parent
9358d59f20
commit
7cb16ef91d
@@ -2077,7 +2077,7 @@ class BulkHostCreateSerializer(serializers.Serializer):
|
||||
item['url'] = reverse('api:host_detail', kwargs={'pk': r.id})
|
||||
item['inventory'] = reverse('api:inventory_detail', kwargs={'pk': validated_data['inventory'].id})
|
||||
host_data.append(item)
|
||||
return_data['url'] = reverse('api:inventory_hosts_list', kwargs={'pk': validated_data['inventory'].id})
|
||||
return_data['url'] = reverse('api:inventory_detail', kwargs={'pk': validated_data['inventory'].id})
|
||||
return_data['hosts'] = host_data
|
||||
return return_data
|
||||
|
||||
@@ -4640,8 +4640,6 @@ class BulkJobLaunchSerializer(BaseSerializer):
|
||||
if 'instance_groups' in job:
|
||||
[requested_use_instance_groups.add(instance_group) for instance_group in job['instance_groups']]
|
||||
|
||||
# If we are not a superuser, check we have permissions
|
||||
if request and not request.user.is_superuser:
|
||||
self.check_organization_permission(attrs, request)
|
||||
self.check_unified_job_permission(request, requested_ujts)
|
||||
if requested_use_inventories or 'inventory' in attrs:
|
||||
@@ -4688,7 +4686,6 @@ class BulkJobLaunchSerializer(BaseSerializer):
|
||||
nodes = []
|
||||
node_m2m_objects = {}
|
||||
node_m2m_object_types_to_through_model = {
|
||||
|
||||
'credentials': WorkflowJobNode.credentials.through,
|
||||
'labels': WorkflowJobNode.labels.through,
|
||||
'instance_groups': WorkflowJobNode.instance_groups.through,
|
||||
@@ -4759,6 +4756,7 @@ class BulkJobLaunchSerializer(BaseSerializer):
|
||||
# validate Organization
|
||||
# - If the orgs is not set, set it to the org of the launching user
|
||||
# - If the user is part of multiple orgs, throw a validation error saying user is part of multiple orgs, please provide one
|
||||
if not request.user.is_superuser:
|
||||
if 'organization' not in attrs or attrs['organization'] == None or attrs['organization'] == '':
|
||||
if Organization.accessible_pk_qs(request.user, 'read_role').count() == 1:
|
||||
for tup in Organization.accessible_pk_qs(request.user, 'read_role').all():
|
||||
@@ -4786,6 +4784,7 @@ class BulkJobLaunchSerializer(BaseSerializer):
|
||||
if requested_ujts - allowed_ujts:
|
||||
not_allowed = requested_ujts - allowed_ujts
|
||||
raise serializers.ValidationError(_(f"Unified Job Templates {not_allowed} not found or you don't have permissions to access it"))
|
||||
|
||||
def check_inventory_permission(self, attrs, request, requested_use_inventories):
|
||||
accessible_use_inventories = {tup[0] for tup in Inventory.accessible_pk_qs(request.user, 'use_role')}
|
||||
if requested_use_inventories - accessible_use_inventories:
|
||||
|
||||
@@ -4,21 +4,17 @@ This endpoint allows the client to create multiple hosts and associate them with
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
{
|
||||
"inventory": 1,
|
||||
"hosts": [
|
||||
{
|
||||
"inventory": 1,
|
||||
"hosts": [
|
||||
{"name": "example1.com", "variables": "ansible_connection: local"},
|
||||
{"name": "example2.com"}
|
||||
]
|
||||
|
||||
}
|
||||
```
|
||||
]
|
||||
}
|
||||
|
||||
Return data:
|
||||
|
||||
```commandline
|
||||
{
|
||||
{
|
||||
"url": "/api/v2/inventories/3/hosts/",
|
||||
"hosts": [
|
||||
{
|
||||
@@ -42,5 +38,4 @@ Return data:
|
||||
"inventory": "/api/v2/inventories/3/"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
}
|
||||
|
||||
@@ -4,13 +4,10 @@ This endpoint allows the client to launch multiple UnifiedJobTemplates at a time
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
{
|
||||
"name": "my bulk job",
|
||||
"jobs": [
|
||||
{
|
||||
"name": "my bulk job",
|
||||
"jobs": [
|
||||
{"unified_job_template": 7, "inventory": 2},
|
||||
{"unified_job_template": 7, "credentials": [3]}
|
||||
]
|
||||
|
||||
}
|
||||
```
|
||||
]
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ from rest_framework.reverse import reverse
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
|
||||
from awx.main.models import UnifiedJob, Host
|
||||
from awx.api.generics import (
|
||||
GenericAPIView,
|
||||
APIView,
|
||||
|
||||
@@ -15,7 +15,10 @@ page.register_page([resources.bulk, (resources.bulk, 'get')], Bulk)
|
||||
class BulkJobLaunch(base.Base):
|
||||
def post(self, payload={}):
|
||||
result = self.connection.post(self.endpoint, payload)
|
||||
if 'url' in result.json():
|
||||
return self.walk(result.json()['url'])
|
||||
else:
|
||||
return self.page_identity(result, request_json={})
|
||||
|
||||
|
||||
page.register_page(resources.bulk_job_launch, BulkJobLaunch)
|
||||
|
||||
Reference in New Issue
Block a user