File: packages.py

package info (click to toggle)
murano-dashboard 1%3A10.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,620 kB
  • sloc: python: 15,632; javascript: 1,627; sh: 106; makefile: 38
file content (128 lines) | stat: -rw-r--r-- 4,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#    Copyright (c) 2014 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import itertools

from django.conf import settings
import yaml

from muranodashboard import api
from muranodashboard.common import cache
from muranodashboard.dynamic_ui import yaql_expression


def package_list(request, marker=None, filters=None, paginate=False,
                 page_size=20, sort_dir=None, limit=None):
    limit = limit or getattr(settings, 'PACKAGES_LIMIT', 100)
    filters = filters or {}

    if paginate:
        request_size = page_size + 1
    else:
        request_size = limit

    if marker:
        filters['marker'] = marker
    if sort_dir:
        filters['sort_dir'] = sort_dir

    client = api.muranoclient(request)

    packages_iter = client.packages.filter(limit=request_size,
                                           **filters)

    has_more_data = False
    if paginate:
        packages = list(itertools.islice(packages_iter, request_size))
        if len(packages) > page_size:
            packages.pop()
            has_more_data = True
    else:
        packages = list(packages_iter)

    return packages, has_more_data


def apps_that_inherit(request, fqn):
    glare = getattr(settings, 'MURANO_USE_GLARE', False)
    if not glare:
        return []
    apps = api.muranoclient(request).packages.filter(inherits=fqn)
    return apps


def app_by_fqn(request, fqn, catalog=True, version=None):
    kwargs = {'fqn': fqn, 'catalog': catalog}
    glare = getattr(settings, 'MURANO_USE_GLARE', False)
    if glare and version:
        kwargs['version'] = version
    apps = api.muranoclient(request).packages.filter(**kwargs)
    try:
        return next(apps)
    except StopIteration:
        return None


def make_loader_cls():
    class Loader(yaml.SafeLoader):
        pass

    def yaql_constructor(loader, node):
        value = loader.construct_scalar(node)
        return yaql_expression.YaqlExpression(value)

    # workaround for PyYAML bug: http://pyyaml.org/ticket/221
    resolvers = {}
    for k, v in yaml.SafeLoader.yaml_implicit_resolvers.items():
        resolvers[k] = v[:]
    Loader.yaml_implicit_resolvers = resolvers

    Loader.add_constructor(u'!yaql', yaql_constructor)
    Loader.add_implicit_resolver(
        u'!yaql', yaql_expression.YaqlExpression, None)

    return Loader


# Here are cached some data calls to api; note that not every package attribute
# getter should be cached - only immutable ones could be safely cached. E.g.,
# it would be a mistake to cache Application Name because it is mutable and can
# be changed in Manage -> Packages while cache is immutable (i.e. it
# its contents are obtained from the api only the first time).
@cache.with_cache('ui', 'ui.yaml')
def get_app_ui(request, app_id):
    return api.muranoclient(request).packages.get_ui(app_id, make_loader_cls())


@cache.with_cache('logo', 'logo.png')
def get_app_logo(request, app_id):
    return api.muranoclient(request).packages.get_logo(app_id)


@cache.with_cache('supplier_logo', 'supplier_logo.png')
def get_app_supplier_logo(request, app_id):
    return api.muranoclient(request).packages.get_supplier_logo(app_id)


def get_app_fqn(request, app_id):
    return get_package_details(request, app_id).fully_qualified_name


def get_service_name(request, app_id):
    return get_package_details(request, app_id).name


@cache.with_cache('package_details')
def get_package_details(request, app_id):
    return api.muranoclient(request).packages.get(app_id)