mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 18:09:57 -03:30
refactor API v2 version detection and feature inclusion
This commit is contained in:
parent
e65ef35acf
commit
80df1523b0
@ -1195,7 +1195,6 @@ class HostSerializer(BaseSerializerWithVariables):
|
||||
ad_hoc_commands = self.reverse('api:host_ad_hoc_commands_list', kwargs={'pk': obj.pk}),
|
||||
ad_hoc_command_events = self.reverse('api:host_ad_hoc_command_events_list', kwargs={'pk': obj.pk}),
|
||||
fact_versions = self.reverse('api:host_fact_versions_list', kwargs={'pk': obj.pk}),
|
||||
ansible_facts = self.reverse('api:host_ansible_facts_detail', kwargs={'pk': obj.pk}),
|
||||
))
|
||||
if obj.inventory:
|
||||
res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk})
|
||||
@ -1203,6 +1202,10 @@ class HostSerializer(BaseSerializerWithVariables):
|
||||
res['last_job'] = self.reverse('api:job_detail', kwargs={'pk': obj.last_job.pk})
|
||||
if obj.last_job_host_summary:
|
||||
res['last_job_host_summary'] = self.reverse('api:job_host_summary_detail', kwargs={'pk': obj.last_job_host_summary.pk})
|
||||
if self.version > 1:
|
||||
res.update(dict(
|
||||
ansible_facts = self.reverse('api:host_ansible_facts_detail', kwargs={'pk': obj.pk}),
|
||||
))
|
||||
return res
|
||||
|
||||
def get_summary_fields(self, obj):
|
||||
@ -1291,17 +1294,9 @@ class GroupSerializer(BaseSerializerWithVariables):
|
||||
'total_hosts', 'hosts_with_active_failures', 'total_groups',
|
||||
'groups_with_active_failures', 'has_inventory_sources')
|
||||
|
||||
@property
|
||||
def V1(self):
|
||||
request = self.context.get('request')
|
||||
# TODO: use the better version-getter after merged with other branches
|
||||
if request and request.version == 'v1':
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def show_capabilities(self): # TODO: consolidate in 3.3
|
||||
if self.V1:
|
||||
if self.version == 1:
|
||||
return ['copy', 'edit', 'start', 'schedule', 'delete']
|
||||
else:
|
||||
return ['copy', 'edit', 'delete']
|
||||
@ -1316,7 +1311,7 @@ class GroupSerializer(BaseSerializerWithVariables):
|
||||
|
||||
def get_summary_fields(self, obj): # TODO: remove in 3.3
|
||||
summary_fields = super(GroupSerializer, self).get_summary_fields(obj)
|
||||
if self.V1:
|
||||
if self.version == 1:
|
||||
try:
|
||||
inv_src = obj.deprecated_inventory_source
|
||||
summary_fields['inventory_source'] = {}
|
||||
@ -1342,7 +1337,7 @@ class GroupSerializer(BaseSerializerWithVariables):
|
||||
inventory_sources = self.reverse('api:group_inventory_sources_list', kwargs={'pk': obj.pk}),
|
||||
ad_hoc_commands = self.reverse('api:group_ad_hoc_commands_list', kwargs={'pk': obj.pk}),
|
||||
))
|
||||
if self.V1: # TODO: remove in 3.3
|
||||
if self.version == 1: # TODO: remove in 3.3
|
||||
try:
|
||||
res['inventory_source'] = self.reverse('api:inventory_source_detail',
|
||||
kwargs={'pk': obj.deprecated_inventory_source.pk})
|
||||
@ -1354,7 +1349,7 @@ class GroupSerializer(BaseSerializerWithVariables):
|
||||
|
||||
def create(self, validated_data): # TODO: remove in 3.3
|
||||
instance = super(GroupSerializer, self).create(validated_data)
|
||||
if self.V1:
|
||||
if self.version == 1: # TODO: remove in 3.3
|
||||
InventorySource.objects.create(deprecated_group=instance, inventory=instance.inventory)
|
||||
return instance
|
||||
|
||||
@ -1552,7 +1547,7 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt
|
||||
if obj.last_update:
|
||||
res['last_update'] = self.reverse('api:inventory_update_detail',
|
||||
kwargs={'pk': obj.last_update.pk})
|
||||
if self.V1: # TODO: remove in 3.3
|
||||
if self.version == 1: # TODO: remove in 3.3
|
||||
res['group'] = None
|
||||
if obj.deprecated_group:
|
||||
res['group'] = self.reverse('api:group_detail', kwargs={'pk': obj.deprecated_group.pk})
|
||||
@ -1560,13 +1555,13 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt
|
||||
|
||||
def get_fields(self): # TODO: remove in 3.3
|
||||
fields = super(InventorySourceSerializer, self).get_fields()
|
||||
if not self.V1:
|
||||
if self.version > 1:
|
||||
fields.pop('group', None)
|
||||
return fields
|
||||
|
||||
def get_summary_fields(self, obj): # TODO: remove in 3.3
|
||||
summary_fields = super(InventorySourceSerializer, self).get_summary_fields(obj)
|
||||
if self.V1 and obj.deprecated_group_id:
|
||||
if self.version == 1 and obj.deprecated_group_id:
|
||||
g = obj.deprecated_group
|
||||
summary_fields['group'] = {}
|
||||
for field in SUMMARIZABLE_FK_FIELDS['group']:
|
||||
@ -1580,13 +1575,6 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt
|
||||
return obj.deprecated_group.id
|
||||
return None
|
||||
|
||||
@property
|
||||
def V1(self): # TODO: use the better version-getter after merged with other branches
|
||||
request = self.context.get('request')
|
||||
if request and request.version == 'v1':
|
||||
return True
|
||||
return False
|
||||
|
||||
def build_relational_field(self, field_name, relation_info):
|
||||
field_class, field_kwargs = super(InventorySourceSerializer, self).build_relational_field(field_name, relation_info)
|
||||
# SCM Project and inventory are read-only unless creating a new inventory.
|
||||
|
||||
@ -117,7 +117,6 @@ host_urls = patterns('awx.api.views',
|
||||
#url(r'^(?P<pk>[0-9]+)/single_fact/$', 'host_single_fact_view'),
|
||||
url(r'^(?P<pk>[0-9]+)/fact_versions/$', 'host_fact_versions_list'),
|
||||
url(r'^(?P<pk>[0-9]+)/fact_view/$', 'host_fact_compare_view'),
|
||||
url(r'^(?P<pk>[0-9]+)/ansible_facts/$', 'host_ansible_facts_detail'),
|
||||
)
|
||||
|
||||
group_urls = patterns('awx.api.views',
|
||||
@ -386,6 +385,7 @@ v1_urls = patterns('awx.api.views',
|
||||
v2_urls = patterns('awx.api.views',
|
||||
url(r'^$', 'api_v2_root_view'),
|
||||
url(r'^credential_types/', include(credential_type_urls)),
|
||||
url(r'^hosts/(?P<pk>[0-9]+)/ansible_facts/$', 'host_ansible_facts_detail'),
|
||||
)
|
||||
|
||||
urlpatterns = patterns('awx.api.views',
|
||||
|
||||
@ -1783,6 +1783,7 @@ class HostAnsibleFactsDetail(RetrieveAPIView):
|
||||
model = Host
|
||||
serializer_class = AnsibleFactsSerializer
|
||||
new_in_320 = True
|
||||
new_in_api_v2 = True
|
||||
|
||||
|
||||
class InventoryHostsList(SubListCreateAttachDetachAPIView):
|
||||
@ -2050,7 +2051,7 @@ class GroupDetail(RetrieveUpdateDestroyAPIView):
|
||||
obj = self.get_object()
|
||||
if not request.user.can_access(self.model, 'delete', obj):
|
||||
raise PermissionDenied()
|
||||
if self.request.version == 'v1': # TODO: deletion of automatic inventory_source, remove in 3.3
|
||||
if get_request_version(request) == 1: # TODO: deletion of automatic inventory_source, remove in 3.3
|
||||
try:
|
||||
obj.deprecated_inventory_source.delete()
|
||||
except Group.deprecated_inventory_source.RelatedObjectDoesNotExist:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user