Skip to content

Commit

Permalink
Lint fixes and more social logging.
Browse files Browse the repository at this point in the history
No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner committed Oct 3, 2023
1 parent c400e25 commit 3328674
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
1 change: 0 additions & 1 deletion galaxy_ng/app/api/v1/viewsets/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)



class LegacyRolesSyncViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, LegacyTasksMixin):
"""Load roles from an upstream v1 source."""

Expand Down
75 changes: 55 additions & 20 deletions galaxy_ng/social/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,26 @@ def do_auth(self, access_token, *args, **kwargs):
v3_namespace, v3_namespace_created = \
self.handle_v3_namespace(auth_response, email, login, gid)

print('-' * 100)
print(f'{login} v3 namespace: {v3_namespace}')
print('-' * 100)
logger.info(
f'v3 namespace {v3_namespace} created:{v3_namespace_created}'
+ f' for github login {login}'
)

# create a legacynamespace and bind to the v3 namespace?
if v3_namespace:
legacy_namespace, legacy_namespace_created = \
self._ensure_legacynamespace(login, v3_namespace)
logger.info(
f'legacy namespace {legacy_namespace} created:{legacy_namespace_created}'
+ f' for github login {login}'
)

return auth_response

def handle_v3_namespace(self, session_user, session_email, session_login, github_id):

logger.debug(
f'HANDLING V3 NAMESPACE session_user:{session_user}'
logger.info(
f'handle_v3_namespace(1): session_user:{session_user}'
+ f' session_email:{session_email} session_login:{session_login}'
)

Expand All @@ -86,19 +91,23 @@ def handle_v3_namespace(self, session_user, session_email, session_login, github
# first make the namespace name ...
namespace_name = self.transform_namespace_name(session_login)

logger.debug(f'TRANSFORMED NAME: {namespace_name}')
print(f'TRANSFORMED NAME: {namespace_name}')
logger.info(
f'handle_v3_namespace(2): session_login:{session_login}'
+ f' transformed_name:{namespace_name}'
)

if not self.validate_namespace_name(namespace_name):
logger.debug(f'DID NOT VALIDATE NAMESPACE NAME: {namespace_name}')
print(f'DID NOT VALIDATE NAMESPACE NAME: {namespace_name}')
logger.info(
f'handle_v3_namespace(3): session_login:{session_login}'
+ f' transformed_name:{namespace_name} validated:False')
return False, False

# does the namespace already exist?
found_namespace = Namespace.objects.filter(name=namespace_name).first()

logger.debug(f'FOUND NAMESPACE: {found_namespace}')
print(f'FOUND NAMESPACE: {found_namespace}')
logger.info(
f'handle_v3_namespace(4): session_login:{session_login}'
+ f' transformed_name:{namespace_name} found:{found_namespace}'
)

# is it owned by this userid?
if found_namespace:
Expand All @@ -107,39 +116,65 @@ def handle_v3_namespace(self, session_user, session_email, session_login, github
logger.debug(f'FOUND EXISTING OWNERS: {owners}')

if session_user in owners:
logger.info(
f'handle_v3_namespace(5): session_login:{session_login}'
f' is owner of {found_namespace}'
)
return found_namespace, False
else:
logger.info(
f'handle_v3_namespace(6): session_login:{session_login}'
+ f' is NOT owner of {found_namespace}'
)

# FIXME - make one from the transformed name?
if not found_namespace:
namespace, namespace_created = self._ensure_namespace(namespace_name, session_user)
logger.info(
f'handle_v3_namespace(7): session_login:{session_login}'
+ f' created:{namespace_created} namespace:{namespace}'
)
return namespace, namespace_created

# short circuit if the user does own at least one namespace ...
owned_namespaces = rbac.get_owned_v3_namespaces(session_user)
logger.debug(f'FOUND USER OWNED NAMESPACES: {owned_namespaces}')
print(f'FOUND USER OWNED NAMESPACES: {owned_namespaces}')
logger.info(
f'handle_v3_namespace(8): session_login:{session_login} owns {owned_namespaces}'
)

if owned_namespaces:
# does one resemble the desired namespace name?
owned_namespaces = sorted(owned_namespaces)
for ns in owned_namespaces:
if ns.name.startswith(namespace_name):
logger.debug(f'MATCHED NS OWNED BY {session_user}: {ns} {ns.name}')
logger.info(
f'handle_v3_namespace(9): session_login:{session_login} returning {ns}'
)
return ns, False

logger.info(
f'handle_v3_namespace(10): session_login:{session_login}'
+ f' no candidates among {owned_namespaces}'
)
return None, False

# should always have a namespace ...
if found_namespace:
logger.debug(
f'GENERATING A NEW NAMESPACE NAME SINCE USER DOES NOT OWN {found_namespace}'
)
namespace_name = self.generate_available_namespace_name(session_login, github_id)
logger.debug(f'FINAL NAMESPACE NAME {namespace_name}')
logger.info(
f'handle_v3_namespace(11): session_login:{session_login}'
+ f' new namespace name {namespace_name}'
)

# create a v3 namespace?
namespace, namespace_created = self._ensure_namespace(namespace_name, session_user)
logger.info(
f'handle_v3_namespace(12): session_login:{session_login}'
+ f' namespace:{namespace} created:{namespace_created}'
)

owned = rbac.get_owned_v3_namespaces(session_user)
logger.debug(f'NS OWNED BY {session_user}: {owned}')
logger.info(f'handle_v3_namespace(13): session_login:{session_login} owns {owned}')

return namespace, namespace_created

Expand Down
6 changes: 5 additions & 1 deletion galaxy_ng/tests/integration/api/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ def test_me_social_with_v1_synced_user(ansible_config):
)

# v1 sync the user's roles and namespace ...
pargs = json.dumps({"github_user": username, "limit": 1, "baseurl": "https://old-galaxy.ansible.com"}).encode('utf-8')
pargs = json.dumps({
"github_user": username,
"limit": 1,
"baseurl": "https://old-galaxy.ansible.com"
}).encode('utf-8')
resp = admin_client('/api/v1/sync/', method='POST', args=pargs)
wait_for_v1_task(resp=resp, api_client=admin_client)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ def test_rbac_utils_get_owned_v3_namespaces(ansible_config):
def test_community_tools_urls(ansible_config):
pass


@pytest.mark.deployment_community
def test_social_auth_no_duplicated_namespaces(ansible_config):

Expand Down

0 comments on commit 3328674

Please sign in to comment.