18
18
from django .db .utils import Error , IntegrityError
19
19
from requests import HTTPError
20
20
21
- from ansible_base .rbac .models .role import AssignmentBase , RoleDefinition , RoleTeamAssignment , RoleUserAssignment
22
21
from ansible_base .resource_registry .models import Resource , ResourceType
23
22
from ansible_base .resource_registry .models .service_identifier import service_id
24
23
from ansible_base .resource_registry .registry import get_registry
25
24
from ansible_base .resource_registry .rest_client import ResourceAPIClient , get_resource_server_client
26
25
27
26
logger = logging .getLogger ('ansible_base.resources_api.tasks.sync' )
28
27
28
+ _is_rbac_installed = 'ansible_base.rbac' in settings .INSTALLED_APPS
29
+
29
30
30
31
class ManifestNotFound (HTTPError ):
31
32
"""Raise when server returns 404 for a manifest"""
@@ -139,7 +140,9 @@ def fetch_manifest(
139
140
return [ManifestItem (** row ) for row in csv_reader ]
140
141
141
142
142
- def get_ansible_id_or_pk (assignment : AssignmentBase ) -> str :
143
+ def get_ansible_id_or_pk (assignment ) -> str :
144
+ if not _is_rbac_installed :
145
+ raise RuntimeError ("get_ansible_id_or_pk requires ansible_base.rbac to be installed" )
143
146
# For object-scoped assignments, try to get the object's ansible_id
144
147
if assignment .content_type .model in ('organization' , 'team' ):
145
148
object_resource = Resource .objects .filter (object_id = assignment .object_id , content_type__model = assignment .content_type .model ).first ()
@@ -153,7 +156,9 @@ def get_ansible_id_or_pk(assignment: AssignmentBase) -> str:
153
156
return str (ansible_id_or_pk )
154
157
155
158
156
- def get_content_object (role_definition : RoleDefinition , assignment_tuple : AssignmentTuple ) -> Any :
159
+ def get_content_object (role_definition , assignment_tuple : AssignmentTuple ) -> Any :
160
+ if not _is_rbac_installed :
161
+ raise RuntimeError ("get_content_object requires ansible_base.rbac to be installed" )
157
162
content_object = None
158
163
if role_definition .content_type .model in ('organization' , 'team' ):
159
164
object_resource = Resource .objects .get (ansible_id = assignment_tuple .ansible_id_or_pk )
@@ -167,6 +172,8 @@ def get_content_object(role_definition: RoleDefinition, assignment_tuple: Assign
167
172
168
173
def get_remote_assignments (api_client : ResourceAPIClient ) -> set [AssignmentTuple ]:
169
174
"""Fetch remote assignments from the resource server and convert to tuples."""
175
+ if not _is_rbac_installed :
176
+ raise RuntimeError ("get_remote_assignments requires ansible_base.rbac to be installed" )
170
177
assignments = set ()
171
178
172
179
# Fetch user assignments with pagination
@@ -238,6 +245,10 @@ def get_remote_assignments(api_client: ResourceAPIClient) -> set[AssignmentTuple
238
245
239
246
def get_local_assignments () -> set [AssignmentTuple ]:
240
247
"""Get local assignments and convert to tuples."""
248
+ if not _is_rbac_installed :
249
+ raise RuntimeError ("get_local_assignments requires ansible_base.rbac to be installed" )
250
+ from ansible_base .rbac .models .role import RoleUserAssignment , RoleTeamAssignment
251
+
241
252
assignments = set ()
242
253
243
254
# Get user assignments
@@ -294,6 +305,10 @@ def get_local_assignments() -> set[AssignmentTuple]:
294
305
295
306
def delete_local_assignment (assignment_tuple : AssignmentTuple ) -> bool :
296
307
"""Delete a local assignment based on the tuple."""
308
+ if not _is_rbac_installed :
309
+ raise RuntimeError ("delete_local_assignment requires ansible_base.rbac to be installed" )
310
+ from ansible_base .rbac .models .role import RoleDefinition
311
+
297
312
try :
298
313
role_definition = RoleDefinition .objects .get (name = assignment_tuple .role_definition_name )
299
314
@@ -320,6 +335,10 @@ def delete_local_assignment(assignment_tuple: AssignmentTuple) -> bool:
320
335
321
336
def create_local_assignment (assignment_tuple : AssignmentTuple ) -> bool :
322
337
"""Create a local assignment based on the tuple."""
338
+ if not _is_rbac_installed :
339
+ raise RuntimeError ("create_local_assignment requires ansible_base.rbac to be installed" )
340
+ from ansible_base .rbac .models .role import RoleDefinition
341
+
323
342
try :
324
343
role_definition = RoleDefinition .objects .get (name = assignment_tuple .role_definition_name )
325
344
@@ -694,6 +713,10 @@ def _sync_assignments(self):
694
713
if not self .sync_assignments :
695
714
return
696
715
716
+ if not _is_rbac_installed :
717
+ self .write (">>> Skipping role assignments sync (rbac not installed)" )
718
+ return
719
+
697
720
self .write (">>> Syncing role assignments" )
698
721
699
722
try :
0 commit comments