summaryrefslogtreecommitdiffstats
path: root/mock-aai/resources/cloud_region.py
blob: aa5098d229947b70ddd4cfc4707e1fd2a07b9d81 (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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""A&AI CloudRegion mock module."""
"""
   Copyright 2023 Deutsche Telekom AG, Orange

   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.
"""
from typing import Dict, List, Tuple

from flask_restful import Resource, request

CLOUD_REGIONS = {}


class CloudRegion(Resource):
    """Cloud region resource."""

    def get(self, cloud_owner: str, cloud_region_id: str) -> Dict[str, str]:
        """Get cloud region.

        Get cloud region from CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): cloud owner key value
            cloud_region_id (str): cloud region id key value

        Returns:
            Dict[str, str]: Cloud region dictionary

        """
        return CLOUD_REGIONS[cloud_owner][cloud_region_id]

    def put(self, cloud_owner: str, cloud_region_id: str) -> Tuple[str, int]:
        """Cloud region resource put method.

        Add cloud region data into CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): Cloud owner key value
            cloud_region_id (str): Cloud region id key value

        Returns:
            Tuple[str, int]: Response tuple. First element is a response body,
                the second one is HTTP response code.
        """
        CLOUD_REGIONS.update({cloud_owner: {cloud_region_id: request.get_json()}})
        return "", 201

    @staticmethod
    def reset() -> None:
        """Reset Cloud region resource.

        Clean CLOUD_REGIONS dictionary

        """
        global CLOUD_REGIONS
        CLOUD_REGIONS = {}


class CloudRegionList(Resource):
    """List of cloud regions resource."""

    def get(self):
        """Get the list of cloud regions.

        Return data from CLOUD_REGIONS dictionary.

        Returns:
            Dict[str, List]: Cloud regions dictionary

        """
        return {
            "cloud-region": [
                data
                for cloud_owner, cloud_owner_dict in CLOUD_REGIONS.items()
                for cloud_owner, data in cloud_owner_dict.items()
            ]
        }


class CloudRegionRelationship(Resource):
    """Cloud region relationship resource."""

    def put(self, cloud_owner: str, cloud_region_id: str):
        """Cloud region relationship resource put method.

        Add cloud region relationship data into CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): Cloud owner key value
            cloud_region_id (str): Cloud region id key value

        """
        try:
            CLOUD_REGIONS[cloud_owner][cloud_region_id]["relationships"].apped(request.get_json())
        except KeyError:
            CLOUD_REGIONS[cloud_owner][cloud_region_id]["relationships"] = [request.get_json()]


class CloudRegionRelationshipList(Resource):
    """List of cloud region relationships resource."""

    def get(self, cloud_owner: str, cloud_region_id: str) -> Dict[str, List]:
        """Get the list of cloud region relationships.

        Return data from CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): Cloud owner key value
            cloud_region_id (str): Cloud region id key value

        Returns:
            Dict[str, List]: Cloud region relationships dictionary

        """
        try:
            return {"relationship": CLOUD_REGIONS[cloud_owner][cloud_region_id]["relationships"]}
        except KeyError:
            return {"relationship": []}


class Tenant(Resource):
    """Cloud region tenant resource."""

    def put(self, cloud_owner: str, cloud_region_id: str, tenant_id: str) -> None:
        """Cloud region tenant resource put method.

        Add cloud region tenant data into CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): Cloud owner key value
            cloud_region_id (str): Cloud region id key value

        """
        try:
            CLOUD_REGIONS[cloud_owner][cloud_region_id]["tenants"].update(
                {tenant_id: request.get_json()}
            )
        except KeyError:
            CLOUD_REGIONS[cloud_owner][cloud_region_id]["tenants"] = {tenant_id: request.get_json()}

    def get(self, cloud_owner: str, cloud_region_id: str, tenant_id: str) -> Dict[str, str]:
        """Get cloud region tenant.

        Get cloud region tenant from CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): cloud owner key value
            cloud_region_id (str): cloud region id key value

        Returns:
            Dict[str, str]: Cloud region tenant dictionary

        """
        try:
            return CLOUD_REGIONS[cloud_owner][cloud_region_id]["tenants"][tenant_id]
        except KeyError:
            return "", 404


class TenantList(Resource):
    """List of tenants resource."""

    def get(self, cloud_owner: str, cloud_region_id: str) -> Dict[str, List]:
        """Get the list of cloud region tenants.

        Return data from CLOUD_REGIONS dictionary.

        Args:
            cloud_owner (str): Cloud owner key value
            cloud_region_id (str): Cloud region id key value

        Returns:
            Dict[str, List]: Cloud region tenants dictionary

        """
        return {
            "tenant": [
                data
                for tenant_id, data in CLOUD_REGIONS[cloud_owner][cloud_region_id]
                .get("tenants", {})
                .items()
            ]
        }