mirror of
https://github.com/ansible/awx.git
synced 2026-02-22 21:46:00 -03:30
Make more use of the functionality of Page in get_assets
and related methods. Also, rename get_resource_options -> get_options and enhance_asset -> serialize_asset.
This commit is contained in:
@@ -197,7 +197,7 @@ class Export(CustomCommand):
|
|||||||
# 3) the resource flag is used with an argument, and the attr will be that argument's value
|
# 3) the resource flag is used with an argument, and the attr will be that argument's value
|
||||||
resources.add_argument('--{}'.format(resource), nargs='?', const='')
|
resources.add_argument('--{}'.format(resource), nargs='?', const='')
|
||||||
|
|
||||||
def get_resource_options(self, endpoint):
|
def get_options(self, endpoint):
|
||||||
return endpoint.options().json['actions']['POST']
|
return endpoint.options().json['actions']['POST']
|
||||||
|
|
||||||
def register_natural_key(self, asset):
|
def register_natural_key(self, asset):
|
||||||
@@ -231,32 +231,31 @@ class Export(CustomCommand):
|
|||||||
from .options import pk_or_name
|
from .options import pk_or_name
|
||||||
|
|
||||||
pk = pk_or_name(self.v2, resource, value)
|
pk = pk_or_name(self.v2, resource, value)
|
||||||
results = endpoint.get(id=pk).json['results']
|
results = endpoint.get(id=pk).results
|
||||||
else:
|
else:
|
||||||
results = endpoint.get(all_pages=True).json['results']
|
results = endpoint.get(all_pages=True).results
|
||||||
|
|
||||||
for asset in results:
|
for asset in results:
|
||||||
self.register_natural_key(asset)
|
self.register_natural_key(asset.json)
|
||||||
|
|
||||||
options = self.get_resource_options(endpoint)
|
options = self.get_options(endpoint)
|
||||||
return [self.enhance_asset(endpoint, asset, options) for asset in results]
|
return [self.serialize_asset(asset, options) for asset in results]
|
||||||
|
|
||||||
def enhance_asset(self, endpoint, asset, options):
|
def serialize_asset(self, asset, options):
|
||||||
fields = {
|
fields = {
|
||||||
key: asset[key] for key in options
|
key: asset.json[key] for key in options
|
||||||
if key in asset and options[key]['type'] != 'id'
|
if key in asset.json and options[key]['type'] != 'id'
|
||||||
}
|
}
|
||||||
|
|
||||||
fk_fields = {
|
fk_fields = {
|
||||||
key: self.get_natural_key(url=asset['related'][key]) for key in options
|
key: self.get_natural_key(url=asset.related[key].endpoint) for key in options
|
||||||
if key in asset and options[key]['type'] == 'id'
|
if key in asset.json and options[key]['type'] == 'id'
|
||||||
}
|
}
|
||||||
|
|
||||||
related = {}
|
related = {}
|
||||||
for k, v in asset['related'].items():
|
for k, related_endpoint in asset.related.items():
|
||||||
if k != 'roles':
|
if k != 'roles':
|
||||||
continue
|
continue
|
||||||
related_endpoint = TentativePage(v, endpoint.connection)
|
|
||||||
data = related_endpoint.get(all_pages=True).json
|
data = related_endpoint.get(all_pages=True).json
|
||||||
if 'results' in data:
|
if 'results' in data:
|
||||||
related[k] = [self.get_natural_key(asset=x) for x in data['results']]
|
related[k] = [self.get_natural_key(asset=x) for x in data['results']]
|
||||||
|
|||||||
Reference in New Issue
Block a user