Pagination in request and response; Fix alarm client issue
[pti/o2.git] / o2ims / views / ocloud_dto.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from flask_restx import fields
16
17 from o2ims.views.api_ns import api_ims_inventory_v1
18
19
20 class OcloudDTO:
21
22     ocloud = api_ims_inventory_v1.model(
23         "OcloudDto",
24         {
25             'oCloudId': fields.String(required=True),
26             'globalCloudId': fields.String,
27             'name': fields.String,
28             'description': fields.String,
29             'infrastructureManagementServiceEndpoint': fields.String,
30         }
31     )
32
33
34 class ResourceTypeDTO:
35
36     resource_type_get = api_ims_inventory_v1.model(
37         "ResourceTypeGetDto",
38         {
39             'resourceTypeId': fields.String(required=True,
40                                             description='Resource type ID'),
41             'name': fields.String,
42             'vendor': fields.String,
43             'version': fields.String,
44             'description': fields.String,
45         }
46     )
47
48
49 class ResourcePoolDTO:
50
51     resource_pool_get = api_ims_inventory_v1.model(
52         "ResourcePoolGetDto",
53         {
54             'resourcePoolId': fields.String(required=True,
55                                             description='Resource pool ID'),
56             'name': fields.String,
57             'globalLocationId': fields.String,
58             'location': fields.String,
59             'description': fields.String,
60         }
61     )
62
63
64 class ResourceDTO:
65     resource_list = api_ims_inventory_v1.model(
66         "ResourceListDto",
67         {
68             'resourceId': fields.String(required=True,
69                                         description='Resource ID'),
70             'resourceTypeId': fields.String,
71             'resourcePoolId': fields.String,
72             'name': fields.String,
73             'parentId': fields.String,
74             'description': fields.String,
75         }
76     )
77
78     list_result = api_ims_inventory_v1.model(
79         "ResourceListPagenationDto",
80         {
81             'count': fields.Integer(),
82             'page_num': fields.Integer(),
83             'results': fields.List(fields.Nested(resource_list))
84         }
85     )
86
87     # def get_paginated_list(results, url, start, limit):
88     #     start = int(start)
89     #     limit = int(limit)
90     #     count = len(results)
91     #     if count < start or limit < 0:
92     #         api_ims_inventory_v1.abort(404)
93     #     # make response
94     #     obj = {}
95     #     obj['start'] = start
96     #     obj['limit'] = limit
97     #     obj['count'] = count
98     #     # make URLs
99     #     # make previous url
100     #     if start == 1:
101     #         obj['previous'] = ''
102     #     else:
103     #         start_copy = max(1, start - limit)
104     #         limit_copy = start - 1
105     #         obj['previous'] = url + \
106     #             '?start=%d&limit=%d' % (start_copy, limit_copy)
107     #     # make next url
108     #     if start + limit > count:
109     #         obj['next'] = ''
110     #     else:
111     #         start_copy = start + limit
112     #         obj['next'] = url + '?start=%d&limit=%d' % (start_copy, limit)
113     #     # finally extract result according to bounds
114     #     # obj['results'] = results[(start - 1):(start - 1 + limit)]
115     #     obj['result'] = fields.List(fields.Nested(ResourceDTO.resource_list))
116     #     return obj
117
118     def recursive_resource_mapping(iteration_number=2):
119         resource_json_mapping = {
120             'resourceId': fields.String(required=True,
121                                         description='Resource ID'),
122             'resourceTypeId': fields.String,
123             'resourcePoolId': fields.String,
124             'name': fields.String,
125             'parentId': fields.String,
126             'description': fields.String,
127             'elements': fields.String,
128         }
129         if iteration_number:
130             resource_json_mapping['children'] = fields.List(
131                 fields.Nested(ResourceDTO.recursive_resource_mapping(
132                     iteration_number-1)))
133         return api_ims_inventory_v1.model(
134             'ResourceGetDto' + str(iteration_number), resource_json_mapping)
135
136     # def _recursive_resource_mapping(self, iteration_number=2):
137     #     resource_json_mapping = {
138     #         'resourceId': fields.String(required=True,
139     #                                     description='Resource ID'),
140     #         'resourceTypeId': fields.String,
141     #         'resourcePoolId': fields.String,
142     #         'name': fields.String,
143     #         'parentId': fields.String,
144     #         'description': fields.String,
145     #     }
146     #     if iteration_number:
147     #         resource_json_mapping['children'] = fields.List(
148     #             fields.Nested(self._recursive_resource_mapping(
149     #                 iteration_number-1)))
150     #         # print(type(resource_json_mapping['children']))
151     #         if resource_json_mapping['children'] is None:
152     #             del resource_json_mapping['children']
153     #     return resource_json_mapping
154
155     # def get_resource_get(self):
156     #     return api_ims_inventory_v1.model(
157     #         'ResourceGetDto',
158     #         {
159     #             'resourceId': fields.String(required=True,
160     #                                         description='Resource ID'),
161     #             'resourceTypeId': fields.String,
162     #             'resourcePoolId': fields.String,
163     #             'name': fields.String,
164     #             'parentId': fields.String,
165     #             'description': fields.String,
166     #             'children': fields.List(fields.Nested(
167     #                 self._recursive_resource_mapping()))
168     #         }
169     #     )
170
171
172 class DeploymentManagerDTO:
173
174     deployment_manager_list = api_ims_inventory_v1.model(
175         "DeploymentManagerListDto",
176         {
177             'deploymentManagerId': fields.String(
178                 required=True,
179                 description='Deployment manager ID'),
180             'name': fields.String,
181             'description': fields.String,
182             'deploymentManagementServiceEndpoint': fields.String,
183             'supportedLocations': fields.String,
184             'capabilities': fields.String,
185             'capacity': fields.String,
186             'profileSupportList': fields.List(
187                 fields.String,
188                 description='Profile support list, use default for the return \
189                      endpoint'),
190         }
191     )
192
193     profile = api_ims_inventory_v1.model("DeploymentManagerGetDtoProfile", {
194         'cluster_api_endpoint': fields.String(
195             attributes='cluster_api_endpoint'),
196         'cluster_ca_cert': fields.String(attributes='cluster_ca_cert'),
197         'admin_user': fields.String(attributes='admin_user'),
198         'admin_client_cert': fields.String(attributes='admin_client_cert'),
199         'admin_client_key': fields.String(attributes='admin_client_key'),
200         # 'kube_config_file': fields.String(attributes='kube_config_file')
201         'helmcli_host_with_port': fields.String(
202             attributes='helmcli_host_with_port'),
203         'helmcli_username': fields.String(attributes='helmcli_username'),
204         'helmcli_password': fields.String(attributes='helmcli_password'),
205         'helmcli_kubeconfig': fields.String(attributes='helmcli_kubeconfig'),
206     })
207
208     deployment_manager_get = api_ims_inventory_v1.model(
209         "DeploymentManagerGetDto",
210         {
211             'deploymentManagerId': fields.String(
212                 required=True,
213                 description='Deployment manager ID'),
214             'name': fields.String,
215             'description': fields.String,
216             'deploymentManagementServiceEndpoint': fields.String,
217             'supportedLocations': fields.String,
218             'capabilities': fields.String,
219             'capacity': fields.String,
220             'profileName': fields.String,
221             'profileData': fields.Nested(profile, False, True),
222         }
223     )
224
225
226 class SubscriptionDTO:
227
228     subscription_get = api_ims_inventory_v1.model(
229         "SubscriptionGetDto",
230         {
231             'subscriptionId': fields.String(required=True,
232                                             description='Subscription ID'),
233             'callback': fields.String,
234             'consumerSubscriptionId': fields.String,
235             'filter': fields.String,
236         }
237     )
238
239     subscription = api_ims_inventory_v1.model(
240         "SubscriptionCreateDto",
241         {
242             'callback': fields.String(
243                 required=True, description='Subscription callback address'),
244             'consumerSubscriptionId': fields.String,
245             'filter': fields.String,
246         }
247     )
248
249     subscription_post_resp = api_ims_inventory_v1.model(
250         "SubscriptionCreatedRespDto",
251         {
252             'subscriptionId': fields.String(required=True,
253                                             description='Subscription ID'),
254         }
255     )