mirror of
https://github.com/ansible/awx.git
synced 2026-05-06 17:07:36 -02:30
Refactor Host manager and dynamic Inventory tests and update
validation/serialization
This commit is contained in:
@@ -45,6 +45,8 @@ from awx.main.fields import ImplicitRoleField
|
||||
from awx.main.utils import (
|
||||
get_type_for_model, get_model_for_type, timestamp_apiformat,
|
||||
camelcase_to_underscore, getattrd, parse_yaml_or_json)
|
||||
from awx.main.utils.filters import DynamicFilter
|
||||
|
||||
from awx.main.validators import vars_validate_or_raise
|
||||
|
||||
from awx.conf.license import feature_enabled
|
||||
@@ -1113,7 +1115,7 @@ class InventorySerializer(BaseSerializerWithVariables):
|
||||
|
||||
class Meta:
|
||||
model = Inventory
|
||||
fields = ('*', 'organization', 'variables', 'has_active_failures',
|
||||
fields = ('*', 'organization', 'kind', 'host_filter', 'variables', 'has_active_failures',
|
||||
'total_hosts', 'hosts_with_active_failures', 'total_groups',
|
||||
'groups_with_active_failures', 'has_inventory_sources',
|
||||
'total_inventory_sources', 'inventory_sources_with_failures')
|
||||
@@ -1145,6 +1147,17 @@ class InventorySerializer(BaseSerializerWithVariables):
|
||||
ret['organization'] = None
|
||||
return ret
|
||||
|
||||
def validate(self, attrs):
|
||||
kind = attrs.get('kind', 'standard')
|
||||
if kind == 'dynamic':
|
||||
host_filter = attrs.get('host_filter')
|
||||
if host_filter is not None:
|
||||
try:
|
||||
DynamicFilter().query_from_string(host_filter)
|
||||
except RuntimeError, e:
|
||||
raise models.base.ValidationError(e)
|
||||
return super(InventorySerializer, self).validate(attrs)
|
||||
|
||||
|
||||
class InventoryDetailSerializer(InventorySerializer):
|
||||
|
||||
|
||||
@@ -72,6 +72,8 @@ from awx.main.utils import * # noqa
|
||||
from awx.main.utils import (
|
||||
callback_filter_out_ansible_extra_vars
|
||||
)
|
||||
from awx.main.utils.filters import DynamicFilter
|
||||
|
||||
from awx.api.permissions import * # noqa
|
||||
from awx.api.renderers import * # noqa
|
||||
from awx.api.serializers import * # noqa
|
||||
@@ -79,7 +81,6 @@ from awx.api.metadata import RoleMetadata
|
||||
from awx.main.consumers import emit_channel_notification
|
||||
from awx.main.models.unified_jobs import ACTIVE_STATES
|
||||
from awx.main.scheduler.tasks import run_job_complete
|
||||
from awx.main.querysets import DynamicFilterQuerySet
|
||||
|
||||
logger = logging.getLogger('awx.api.views')
|
||||
|
||||
@@ -1764,10 +1765,10 @@ class HostList(ListCreateAPIView):
|
||||
capabilities_prefetch = ['inventory.admin']
|
||||
|
||||
def get_queryset(self):
|
||||
qs = DynamicFilterQuerySet(HostList, using=self._db)
|
||||
qs = super(HostList, self).get_queryset()
|
||||
filter_string = self.request.query_params.get('host_filter', None)
|
||||
if filter_string:
|
||||
filter_q = qs.query_from_string(filter_string)
|
||||
filter_q = DynamicFilter.query_from_string(filter_string)
|
||||
qs = qs.filter(filter_q)
|
||||
return qs
|
||||
|
||||
|
||||
Reference in New Issue
Block a user