summaryrefslogtreecommitdiffstats
path: root/vio/vio/pub/utils/syscomm.py
blob: f1db022c24e623d4c3ece2ed0f284ddf5b053310 (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
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
# Copyright (c) 2017 VMware, 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.

import inspect
import json
from collections import defaultdict

keystoneV2Json = \
    {
        "auth": {
            "tenantName": "",
            "passwordCredentials": {
                "username": "",
                "password": ""
            }
        }
    }


def fun_name():
    return inspect.stack()[1][3]


def jsonResponse(data, encoding='utf-8'):

    content_type = "application/json"
    try:
        res = json.loads(data, encoding=encoding)
    except Exception:
        res = data
        content_type = "text/plain"
    return (res, content_type)


class Catalogs(object):

    def __init__(self):
        self.ct = defaultdict(dict)

    def storeEndpoint(self, vimid, endpoints):
        if vimid in self.ct:
            self.ct[vimid].update(endpoints)
        else:
            self.ct.setdefault(vimid, endpoints)

    def getEndpointBy(self, vimid, serverType, interface='public'):

        vim = self.ct.get(vimid)
        return vim.get(serverType).get(interface, "") if vim else ""


def verifyKeystoneV2(param):

    return _walk_json(param, keystoneV2Json)


# comapare two json by key
def _walk_json(data, data2):
    if isinstance(data, dict) and isinstance(data2, dict):
        if set(data.keys()) != set(data2.keys()):
            return False
        else:
            v1 = data.values()
            v2 = data2.values()
            v1.sort()
            v2.sort()
            if len(v1) != len(v2):
                return False
            for (i, j) in zip(v1, v2):
                # continue compare key
                if isinstance(i, dict) and isinstance(j, dict):
                    if not _walk_json(i, j):
                        return False
                # ignore value
                else:
                    continue

            return True

    return False


catalog = Catalogs()