code-stye Yapf: 5 files updated
data/interfaces/appr.py endpoints/appr/cnr_backend.py endpoints/appr/registry.py endpoints/appr/test/test_api.py endpoints/appr/test/test_registry.py
This commit is contained in:
parent
578f87f94c
commit
599ce0de54
6 changed files with 112 additions and 130 deletions
|
@ -7,7 +7,6 @@ import cnr.semver
|
|||
from cnr.exception import raise_package_not_found, raise_channel_not_found
|
||||
from six import add_metaclass
|
||||
|
||||
|
||||
from app import storage, authentication
|
||||
from data import model, oci_model
|
||||
from data.database import Tag, Manifest, MediaType, Blob, Repository, Channel
|
||||
|
@ -30,31 +29,23 @@ class ChannelView(namedtuple('ChannelView', ['name', 'current'])):
|
|||
"""
|
||||
|
||||
|
||||
class ApplicationSummaryView(namedtuple('ApplicationSummaryView', ['name',
|
||||
'namespace',
|
||||
'visibility',
|
||||
'default',
|
||||
'manifests',
|
||||
'channels',
|
||||
'releases',
|
||||
'updated_at',
|
||||
'created_at'])):
|
||||
class ApplicationSummaryView(
|
||||
namedtuple('ApplicationSummaryView', [
|
||||
'name', 'namespace', 'visibility', 'default', 'manifests', 'channels', 'releases',
|
||||
'updated_at', 'created_at'
|
||||
])):
|
||||
""" ApplicationSummaryView is an aggregated view of an application repository.
|
||||
"""
|
||||
|
||||
|
||||
class ApplicationManifest(namedtuple('ApplicationManifest', ['mediaType',
|
||||
'digest',
|
||||
'content'])):
|
||||
class ApplicationManifest(namedtuple('ApplicationManifest', ['mediaType', 'digest', 'content'])):
|
||||
""" ApplicationManifest embed the BlobDescriptor and some metadata around it.
|
||||
An ApplicationManifest is content-addressable.
|
||||
"""
|
||||
|
||||
|
||||
class ApplicationRelease(namedtuple('ApplicationRelease', ['release',
|
||||
'name',
|
||||
'created_at',
|
||||
'manifest'])):
|
||||
class ApplicationRelease(
|
||||
namedtuple('ApplicationRelease', ['release', 'name', 'created_at', 'manifest'])):
|
||||
""" The ApplicationRelease associates an ApplicationManifest to a repository and release.
|
||||
"""
|
||||
|
||||
|
@ -221,12 +212,15 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
releases = [t.name for t in repo.tag_set_prefetch]
|
||||
if not releases:
|
||||
continue
|
||||
available_releases = [str(x) for x in sorted(cnr.semver.versions(releases, False),
|
||||
reverse=True)]
|
||||
available_releases = [
|
||||
str(x) for x in sorted(cnr.semver.versions(releases, False), reverse=True)
|
||||
]
|
||||
channels = None
|
||||
if with_channels:
|
||||
channels = [ChannelView(name=chan.name, current=chan.linked_tag.name)
|
||||
for chan in oci_model.channel.get_repo_channels(repo)]
|
||||
channels = [
|
||||
ChannelView(name=chan.name, current=chan.linked_tag.name)
|
||||
for chan in oci_model.channel.get_repo_channels(repo)
|
||||
]
|
||||
|
||||
app_name = _join_package_name(repo.namespace_user.username, repo.name)
|
||||
manifests = self.list_manifests(app_name, available_releases[0])
|
||||
|
@ -239,8 +233,7 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
manifests=manifests,
|
||||
releases=available_releases,
|
||||
updated_at=_timestamp_to_iso(repo.tag_set_prefetch[-1].lifetime_start),
|
||||
created_at=_timestamp_to_iso(repo.tag_set_prefetch[0].lifetime_start),
|
||||
)
|
||||
created_at=_timestamp_to_iso(repo.tag_set_prefetch[0].lifetime_start),)
|
||||
views.append(view)
|
||||
return views
|
||||
|
||||
|
@ -270,9 +263,10 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
Todo:
|
||||
* Filter results with readeable reposistory for the user (including visibilitys)
|
||||
"""
|
||||
return [_join_package_name(r.namespace_user.username, r.name)
|
||||
for r in model.repository.get_app_search(lookup=query, username=username, limit=50)]
|
||||
|
||||
return [
|
||||
_join_package_name(r.namespace_user.username, r.name)
|
||||
for r in model.repository.get_app_search(lookup=query, username=username, limit=50)
|
||||
]
|
||||
|
||||
def list_releases(self, package_name, media_type=None):
|
||||
""" Return the list of all releases of an Application
|
||||
|
@ -307,21 +301,15 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
created_at = _timestamp_to_iso(tag.lifetime_start)
|
||||
|
||||
blob_descriptor = BlobDescriptor(digest=_strip_sha256_header(blob.digest),
|
||||
mediaType=blob.media_type.name,
|
||||
size=blob.size, urls=[])
|
||||
mediaType=blob.media_type.name, size=blob.size, urls=[])
|
||||
|
||||
app_manifest = ApplicationManifest(digest=manifest.digest, mediaType=manifest.media_type.name,
|
||||
content=blob_descriptor)
|
||||
app_manifest = ApplicationManifest(
|
||||
digest=manifest.digest, mediaType=manifest.media_type.name, content=blob_descriptor)
|
||||
|
||||
app_release = ApplicationRelease(release=tag.name,
|
||||
created_at=created_at,
|
||||
name=package_name,
|
||||
app_release = ApplicationRelease(release=tag.name, created_at=created_at, name=package_name,
|
||||
manifest=app_manifest)
|
||||
return app_release
|
||||
except (Tag.DoesNotExist,
|
||||
Manifest.DoesNotExist,
|
||||
Blob.DoesNotExist,
|
||||
Repository.DoesNotExist,
|
||||
except (Tag.DoesNotExist, Manifest.DoesNotExist, Blob.DoesNotExist, Repository.DoesNotExist,
|
||||
MediaType.DoesNotExist):
|
||||
raise_package_not_found(package_name, release, media_type)
|
||||
|
||||
|
@ -330,14 +318,10 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
path = cnrblob.upload_url(cnrblob.digest)
|
||||
locations = storage.preferred_locations
|
||||
storage.stream_write(locations, path, fp, 'application/x-gzip')
|
||||
db_blob = oci_model.blob.get_or_create_blob(cnrblob.digest,
|
||||
cnrblob.size,
|
||||
content_media_type,
|
||||
db_blob = oci_model.blob.get_or_create_blob(cnrblob.digest, cnrblob.size, content_media_type,
|
||||
locations)
|
||||
return BlobDescriptor(mediaType=content_media_type,
|
||||
digest=_strip_sha256_header(db_blob.digest),
|
||||
size=db_blob.size,
|
||||
urls=[])
|
||||
digest=_strip_sha256_header(db_blob.digest), size=db_blob.size, urls=[])
|
||||
|
||||
def create_release(self, package, user, visibility, force=False):
|
||||
""" Add an app-release to a repository
|
||||
|
@ -349,8 +333,8 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
repo = model.repository.get_or_create_repository(ns, name, user, visibility=visibility,
|
||||
repo_kind='application')
|
||||
tag_name = package.release
|
||||
oci_model.release.create_app_release(repo, tag_name, package.manifest(),
|
||||
data['content']['digest'], force)
|
||||
oci_model.release.create_app_release(repo, tag_name,
|
||||
package.manifest(), data['content']['digest'], force)
|
||||
|
||||
def delete_release(self, package_name, release, media_type):
|
||||
""" Remove/Delete an app-release from an app-repository.
|
||||
|
@ -386,8 +370,7 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
""" Returns all AppChannel for a package """
|
||||
repo = self._application(package_name)
|
||||
channels = oci_model.channel.get_repo_channels(repo)
|
||||
return [ChannelView(name=chan.name,
|
||||
current=chan.linked_tag.name) for chan in channels]
|
||||
return [ChannelView(name=chan.name, current=chan.linked_tag.name) for chan in channels]
|
||||
|
||||
def fetch_channel(self, package_name, channel_name, with_releases=True):
|
||||
""" Returns an AppChannel """
|
||||
|
@ -400,12 +383,11 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
|
||||
if with_releases:
|
||||
releases = oci_model.channel.get_channel_releases(repo, channel)
|
||||
chanview = ChannelReleasesView(current=channel.linked_tag.name,
|
||||
name=channel.name,
|
||||
releases=[channel.linked_tag.name]+[c.name for c in releases])
|
||||
chanview = ChannelReleasesView(
|
||||
current=channel.linked_tag.name, name=channel.name,
|
||||
releases=[channel.linked_tag.name] + [c.name for c in releases])
|
||||
else:
|
||||
chanview = ChannelView(current=channel.linked_tag.name,
|
||||
name=channel.name)
|
||||
chanview = ChannelView(current=channel.linked_tag.name, name=channel.name)
|
||||
|
||||
return chanview
|
||||
|
||||
|
@ -424,8 +406,7 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
"""
|
||||
repo = self._application(package_name)
|
||||
channel = oci_model.channel.create_or_update_channel(repo, channel_name, release)
|
||||
return ChannelView(current=channel.linked_tag.name,
|
||||
name=channel.name)
|
||||
return ChannelView(current=channel.linked_tag.name, name=channel.name)
|
||||
|
||||
def get_user(self, username, password):
|
||||
err_msg = None
|
||||
|
@ -438,6 +419,7 @@ class OCIAppModel(AppRegistryDataInterface):
|
|||
user, err_msg = authentication.verify_and_link_user(username, password)
|
||||
return (user, err_msg)
|
||||
|
||||
|
||||
def _strip_sha256_header(digest):
|
||||
if digest.startswith('sha256:'):
|
||||
return digest.split('sha256:')[1]
|
||||
|
|
Reference in a new issue