mirror of
https://github.com/ansible/awx.git
synced 2026-05-14 04:47:44 -02:30
Prioritize membership roles
since certain role grants will not be accepted by the api unless the user or team is part of the correct organization.
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import itertools
|
import itertools
|
||||||
import logging
|
import logging
|
||||||
import queue
|
|
||||||
|
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
@@ -231,38 +230,40 @@ class ApiV2(base.Base):
|
|||||||
if not S:
|
if not S:
|
||||||
continue
|
continue
|
||||||
if name == 'roles':
|
if name == 'roles':
|
||||||
self._roles.put((_page, S))
|
self._roles.append((_page, S))
|
||||||
else:
|
else:
|
||||||
self._related.put((_page, name, S))
|
self._related.append((_page, name, S))
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
def _assign_roles(self):
|
def _assign_role(self, endpoint, role):
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
_page, roles = self._roles.get_nowait()
|
|
||||||
self._roles.task_done()
|
|
||||||
role_endpoint = _page.json['related']['roles']
|
|
||||||
for role in roles:
|
|
||||||
if 'content_object' not in role:
|
if 'content_object' not in role:
|
||||||
continue # admin role
|
return
|
||||||
obj_page = self._cache.get_by_natural_key(role['content_object'])
|
obj_page = self._cache.get_by_natural_key(role['content_object'])
|
||||||
if obj_page is not None:
|
if obj_page is None:
|
||||||
|
return
|
||||||
role_page = obj_page.get_object_role(role['name'], by_name=True)
|
role_page = obj_page.get_object_role(role['name'], by_name=True)
|
||||||
try:
|
try:
|
||||||
role_endpoint.post({'id': role_page['id']})
|
endpoint.post({'id': role_page['id']})
|
||||||
except exc.NoContent: # desired exception on successful (dis)association
|
except exc.NoContent: # desired exception on successful (dis)association
|
||||||
pass
|
pass
|
||||||
else:
|
|
||||||
pass # admin role
|
def _assign_membership(self):
|
||||||
except queue.Empty:
|
for _page, roles in self._roles:
|
||||||
break
|
role_endpoint = _page.json['related']['roles']
|
||||||
|
for role in roles:
|
||||||
|
if role['name'] == 'Member':
|
||||||
|
self._assign_role(role_endpoint, role)
|
||||||
|
|
||||||
|
def _assign_roles(self):
|
||||||
|
for _page, roles in self._roles:
|
||||||
|
role_endpoint = _page.json['related']['roles']
|
||||||
|
for role in roles:
|
||||||
|
if role['name'] != 'Member':
|
||||||
|
self._assign_role(role_endpoint, role)
|
||||||
|
|
||||||
def _assign_related(self):
|
def _assign_related(self):
|
||||||
while True:
|
for _page, name, related_set in self._related:
|
||||||
try:
|
|
||||||
_page, name, related_set = self._related.get_nowait()
|
|
||||||
self._related.task_done()
|
|
||||||
endpoint = _page.related[name]
|
endpoint = _page.related[name]
|
||||||
if isinstance(related_set, dict): # Relateds that are just json blobs, e.g. survey_spec
|
if isinstance(related_set, dict): # Relateds that are just json blobs, e.g. survey_spec
|
||||||
endpoint.post(related_set)
|
endpoint.post(related_set)
|
||||||
@@ -293,13 +294,11 @@ class ApiV2(base.Base):
|
|||||||
self._import_list(endpoint, related_set)
|
self._import_list(endpoint, related_set)
|
||||||
|
|
||||||
# FIXME: deal with pruning existing relations that do not match the import set
|
# FIXME: deal with pruning existing relations that do not match the import set
|
||||||
except queue.Empty:
|
|
||||||
break
|
|
||||||
|
|
||||||
def import_assets(self, data):
|
def import_assets(self, data):
|
||||||
self._cache = page.PageCache()
|
self._cache = page.PageCache()
|
||||||
self._related = queue.Queue()
|
self._related = []
|
||||||
self._roles = queue.Queue()
|
self._roles = []
|
||||||
|
|
||||||
changed = False
|
changed = False
|
||||||
|
|
||||||
@@ -312,6 +311,7 @@ class ApiV2(base.Base):
|
|||||||
# FIXME: should we delete existing unpatched assets?
|
# FIXME: should we delete existing unpatched assets?
|
||||||
|
|
||||||
self._assign_related()
|
self._assign_related()
|
||||||
|
self._assign_membership()
|
||||||
self._assign_roles()
|
self._assign_roles()
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
|
|||||||
Reference in New Issue
Block a user