mirror of
https://github.com/ansible/awx.git
synced 2026-02-16 18:50:04 -03:30
Sort the asset groups to be imported by their dependency relationships
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import itertools
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@@ -5,6 +6,7 @@ import sys
|
|||||||
from awxkit import api, config
|
from awxkit import api, config
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
from awxkit.utils import to_str
|
from awxkit.utils import to_str
|
||||||
|
from awxkit.api.mixins import has_create
|
||||||
from awxkit.api.pages import Page
|
from awxkit.api.pages import Page
|
||||||
from awxkit.cli.format import FORMATTERS, format_response, add_authentication_arguments
|
from awxkit.cli.format import FORMATTERS, format_response, add_authentication_arguments
|
||||||
from awxkit.cli.utils import CustomRegistryMeta, cprint
|
from awxkit.cli.utils import CustomRegistryMeta, cprint
|
||||||
@@ -49,13 +51,13 @@ EXPORTABLE_RESOURCES = [
|
|||||||
'users',
|
'users',
|
||||||
'organizations',
|
'organizations',
|
||||||
'teams',
|
'teams',
|
||||||
# 'credential_types',
|
'credential_types',
|
||||||
# 'credentials',
|
'credentials',
|
||||||
# 'notification_templates',
|
'notification_templates',
|
||||||
# 'projects',
|
'projects',
|
||||||
# 'inventory',
|
'inventory',
|
||||||
# 'job_templates',
|
'job_templates',
|
||||||
# 'workflow_job_templates',
|
'workflow_job_templates',
|
||||||
]
|
]
|
||||||
|
|
||||||
NATURAL_KEYS = {
|
NATURAL_KEYS = {
|
||||||
@@ -188,6 +190,8 @@ class Import(CustomCommand):
|
|||||||
super(Import, self).__init__(*args, **kwargs)
|
super(Import, self).__init__(*args, **kwargs)
|
||||||
self._natural_key = {}
|
self._natural_key = {}
|
||||||
self._options = {}
|
self._options = {}
|
||||||
|
self._resource_page = {}
|
||||||
|
self._page_resource = {}
|
||||||
|
|
||||||
def get_by_natural_key(self, key, fetch=True):
|
def get_by_natural_key(self, key, fetch=True):
|
||||||
frozen_key = freeze(key)
|
frozen_key = freeze(key)
|
||||||
@@ -222,6 +226,9 @@ class Import(CustomCommand):
|
|||||||
|
|
||||||
def register_existing_assets(self, resource):
|
def register_existing_assets(self, resource):
|
||||||
endpoint = getattr(self.v2, resource)
|
endpoint = getattr(self.v2, resource)
|
||||||
|
self._resource_page[resource] = endpoint._create().__item_class__
|
||||||
|
self._page_resource[self._resource_page[resource]] = resource
|
||||||
|
|
||||||
options = endpoint.options().json['actions']['POST']
|
options = endpoint.options().json['actions']['POST']
|
||||||
self._options[resource] = options
|
self._options[resource] = options
|
||||||
|
|
||||||
@@ -268,13 +275,15 @@ class Import(CustomCommand):
|
|||||||
for resource in data:
|
for resource in data:
|
||||||
self.register_existing_assets(resource)
|
self.register_existing_assets(resource)
|
||||||
|
|
||||||
for resource, assets in data.items(): # FIXME: do a topological sort by dependencies
|
for page_cls in itertools.chain(*has_create.page_creation_order(*self._page_resource.keys())):
|
||||||
self.create_assets(resource, assets)
|
resource = self._page_resource[page_cls]
|
||||||
|
cprint("importing {}".format(resource), 'red', file=client.stderr)
|
||||||
|
self.create_assets(resource, data.get(resource, []))
|
||||||
|
|
||||||
# FIXME: should we delete existing unpatched assets?
|
# FIXME: should we delete existing unpatched assets?
|
||||||
|
|
||||||
for resource, assets in data.items():
|
# for resource, assets in data.items():
|
||||||
self.assign_related_assets(resource, assets)
|
# self.assign_related_assets(resource, assets)
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user