summaryrefslogtreecommitdiff
path: root/glanceclient/v3/__init__.py
blob: 937309b66e4f5a44f66967f0b3c2354b27786d58 (plain)
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
# Copyright (c) 2015 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 six

from glanceclient import exc


class ArtifactType(object):
    generic_properties = ('created_at', 'id', 'name', 'owner', 'state',
                          'type_name', 'type_version', 'updated_at',
                          'version', 'visibility', 'description', 'tags',
                          'published_at', 'deleted_at')

    supported_show_levels = ('none', 'basic', 'direct', 'transitive')

    def __init__(self, **kwargs):
        try:
            for prop in self.generic_properties:
                setattr(self, prop, kwargs.pop(prop))
        except KeyError:
            msg = "Invalid parameters were provided"
            raise exc.HTTPBadRequest(msg)
        self.type_specific_properties = {}
        for key, value in six.iteritems(kwargs):
            try:
                if _is_dependency(value):

                    self.type_specific_properties[key] = ArtifactType(**value)
                elif _is_dependencies_list(value):

                    self.type_specific_properties[key] = [ArtifactType(**elem)
                                                          for elem in value]
                else:
                    self.type_specific_properties[key] = value
            except exc.HTTPBadRequest:
                # if it's not possible to generate artifact object then
                # assign the value as a regular dict.
                self.type_specific_properties[key] = value


def _is_dependency(d):
    if type(d) is dict and d.get('type_name') and d.get('type_version'):
        return True
    return False


def _is_dependencies_list(l):
    if type(l) is list and all(_is_dependency(d) for d in l):
        return True
    return False