Skip to content

Commit

Permalink
Bind v1 and v3 namespaces if created.
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 Sep 11, 2023
1 parent a642bee commit 14db56a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
47 changes: 30 additions & 17 deletions galaxy_ng/app/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
class LegacyNamespacesSerializer(serializers.ModelSerializer):

summary_fields = serializers.SerializerMethodField()
# date_joined = serializers.SerializerMethodField()
# active = serializers.SerializerMethodField()
name = serializers.SerializerMethodField()
# full_name = serializers.SerializerMethodField()
url = serializers.SerializerMethodField()
avatar_url = serializers.SerializerMethodField()
related = serializers.SerializerMethodField()

class Meta:
model = LegacyNamespace
Expand All @@ -25,12 +23,17 @@ class Meta:
'created',
'modified',
'name',
# 'full_name',
# 'date_joined',
'avatar_url',
# 'active'
'related',
]

def get_related(self, obj):
return {
'provider_namespaces': None,
'content': None,
'owners': f'/api/v1/namespaces/{obj.id}/owners/',
}

def get_name(self, obj):
if hasattr(obj, 'name'):
return obj.name
Expand All @@ -40,20 +43,24 @@ def get_name(self, obj):
def get_url(self, obj):
return ''

# def get_full_name(self, obj):
# return ''

def get_date_joined(self, obj):
return obj.created

def get_summary_fields(self, obj):
owners = obj.owners.all()
owners = [{'id': x.id, 'username': x.username} for x in owners]
return {'owners': owners}

# TODO: What does this actually mean?
# def get_active(self, obj):
# return True
# link the v1 namespace to the v3 namespace so that users
# don't need to query the database to figure it out.
providers = []
if obj.namespace:
providers.append({
'id': obj.namespace.id,
'name': obj.namespace.name,
'pulp_href': f"/api/pulp/api/v3/pulp_ansible/namespaces/{obj.namespace.id}/",
})

return {'owners': owners, 'provider_namespaces': providers}

def get_avatar_url(self, obj):
url = f'https://github.com/{obj.name}.png'
Expand Down Expand Up @@ -233,17 +240,23 @@ def get_summary_fields(self, obj):
versions = obj.full_metadata.get('versions', [])
dependencies = obj.full_metadata.get('dependencies', [])
tags = obj.full_metadata.get('tags', [])

provider_ns = None
if obj.namespace and obj.namespace.namespace:
provider_ns = {
'id': obj.namespace.namespace.id,
'name': obj.namespace.namespace.name,
'pulp_href': f"/api/pulp/api/v3/pulp_ansible/namespaces/{obj.namespace.namespace.id}/"
}

return {
'dependencies': dependencies,
'namespace': {
'id': obj.namespace.id,
'name': obj.namespace.name,
'avatar_url': f'https://github.com/{obj.namespace.name}.png'
},
'provider_namespace': {
'id': obj.namespace.id,
'name': obj.namespace.name
},
'provider_namespace': provider_ns,
'repository': {
'name': obj.name,
'original_name': obj.full_metadata.get('github_repo')
Expand Down
11 changes: 9 additions & 2 deletions galaxy_ng/social/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ def do_auth(self, access_token, *args, **kwargs):
auth_response = self.strategy.authenticate(*args, **kwargs)

# create a legacynamespace?
legacy_namespace, _ = self._ensure_legacynamespace(login)
legacy_namespace, legacy_created = self._ensure_legacynamespace(login)

# define namespace, validate and create ...
namespace_name = self.transform_namespace_name(login)
print(f'NAMESPACE NAME: {namespace_name}')
if self.validate_namespace_name(namespace_name):

# Need user for group and rbac binding
Expand All @@ -74,7 +75,12 @@ def do_auth(self, access_token, *args, **kwargs):
group, _ = self._ensure_group(namespace_name, user)

# create a v3 namespace?
namespace, _ = self._ensure_namespace(namespace_name, user, group)
v3_namespace, v3_created = self._ensure_namespace(namespace_name, user, group)

# bind the v3 namespace to the v1 namespace
if legacy_created and v3_created:
legacy_namespace.namespace = v3_namespace
legacy_namespace.save()

return auth_response

Expand Down Expand Up @@ -111,6 +117,7 @@ def _ensure_namespace(self, name, user, group):

with transaction.atomic():
namespace, created = Namespace.objects.get_or_create(name=name)
print(f'NAMESPACE:{namespace} CREATED:{created}')
owners = rbac.get_v3_namespace_owners(namespace)
if created or not owners:
# Binding by user breaks the UI workflow ...
Expand Down

0 comments on commit 14db56a

Please sign in to comment.