Add fields selector for API with query parameters INF-300
[pti/o2.git] / o2ims / views / ocloud_route.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 import request
16 from flask_restx import Resource, reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2common.views.pagination_route import link_header, PAGE_PARAM
20 from o2ims.views import ocloud_view
21 from o2ims.views.api_ns import api_ims_inventory_v1
22 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
23     ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
24
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28
29 def configure_api_route():
30     # Set global bus for resource
31     global bus
32     bus = MessageBus.get_instance()
33
34
35 # ----------  OClouds ---------- #
36 @api_ims_inventory_v1.route("/")
37 @api_ims_inventory_v1.response(404, 'oCloud not found')
38 @api_ims_inventory_v1.param(
39     'all_fields',
40     'Set any value for show all fields. This value will cover "fields" ' +
41     'and "all_fields".',
42     _in='query')
43 @api_ims_inventory_v1.param(
44     'fields',
45     'Set fields to show, split by comman, "/" for parent and children.' +
46     ' Like "name,parent/children". This value will cover "exculde_fields".',
47     _in='query')
48 @api_ims_inventory_v1.param(
49     'exclude_fields',
50     'Set fields to exclude showing, split by comman, "/" for parent and ' +
51     'children. Like "name,parent/children". This value will cover ' +
52     '"exclude_default".',
53     _in='query')
54 @api_ims_inventory_v1.param(
55     'exclude_default',
56     'Exclude showing all default fields, Set "true" to enable.',
57     _in='query')
58 class OcloudsListRouter(Resource):
59     """Ocloud get endpoint
60     O2 interface ocloud endpoint
61     """
62
63     ocloud_get = OcloudDTO.ocloud
64
65     @api_ims_inventory_v1.marshal_with(ocloud_get)
66     def get(self):
67         res = ocloud_view.oclouds(bus.uow)
68         if len(res) > 0:
69             return res[0]
70         api_ims_inventory_v1.abort(
71             404, "oCloud doesn't exist")
72
73
74 # ----------  ResourceTypes ---------- #
75 @api_ims_inventory_v1.route("/resourceTypes")
76 @api_ims_inventory_v1.param(PAGE_PARAM,
77                             'Page number of the results to fetch.' +
78                             ' Default: 1',
79                             _in='query', default=1)
80 @api_ims_inventory_v1.param(
81     'all_fields',
82     'Set any value for show all fields. This value will cover "fields" ' +
83     'and "all_fields".',
84     _in='query')
85 @api_ims_inventory_v1.param(
86     'fields',
87     'Set fields to show, split by comman, "/" for parent and children.' +
88     ' Like "name,parent/children". This value will cover "exculde_fields".',
89     _in='query')
90 @api_ims_inventory_v1.param(
91     'exclude_fields',
92     'Set fields to exclude showing, split by comman, "/" for parent and ' +
93     'children. Like "name,parent/children". This value will cover ' +
94     '"exclude_default".',
95     _in='query')
96 @api_ims_inventory_v1.param(
97     'exclude_default',
98     'Exclude showing all default fields, Set "true" to enable.',
99     _in='query')
100 class ResourceTypesListRouter(Resource):
101
102     model = ResourceTypeDTO.resource_type_get
103
104     @api_ims_inventory_v1.marshal_list_with(model)
105     def get(self):
106         parser = reqparse.RequestParser()
107         parser.add_argument(PAGE_PARAM, location='args')
108         args = parser.parse_args()
109         kwargs = {}
110         if args.nextpage_opaque_marker is not None:
111             kwargs['page'] = args.nextpage_opaque_marker
112
113         ret = ocloud_view.resource_types(bus.uow, **kwargs)
114         return link_header(request.full_path, ret)
115
116
117 @api_ims_inventory_v1.route("/resourceTypes/<resourceTypeID>")
118 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
119 @api_ims_inventory_v1.response(404, 'Resource type not found')
120 @api_ims_inventory_v1.param(
121     'all_fields',
122     'Set any value for show all fields. This value will cover "fields" ' +
123     'and "all_fields".',
124     _in='query')
125 @api_ims_inventory_v1.param(
126     'fields',
127     'Set fields to show, split by comman, "/" for parent and children.' +
128     ' Like "name,parent/children". This value will cover "exculde_fields".',
129     _in='query')
130 @api_ims_inventory_v1.param(
131     'exclude_fields',
132     'Set fields to exclude showing, split by comman, "/" for parent and ' +
133     'children. Like "name,parent/children". This value will cover ' +
134     '"exclude_default".',
135     _in='query')
136 @api_ims_inventory_v1.param(
137     'exclude_default',
138     'Exclude showing all default fields, Set "true" to enable.',
139     _in='query')
140 class ResourceTypeGetRouter(Resource):
141
142     model = ResourceTypeDTO.resource_type_get
143
144     @api_ims_inventory_v1.doc('Get resource type')
145     @api_ims_inventory_v1.marshal_with(model)
146     def get(self, resourceTypeID):
147         result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
148         if result is not None:
149             return result
150         api_ims_inventory_v1.abort(
151             404, "Resource type {} doesn't exist".format(resourceTypeID))
152
153
154 # ----------  ResourcePools ---------- #
155 @api_ims_inventory_v1.route("/resourcePools")
156 @api_ims_inventory_v1.param(PAGE_PARAM,
157                             'Page number of the results to fetch.' +
158                             ' Default: 1',
159                             _in='query', default=1)
160 @api_ims_inventory_v1.param(
161     'all_fields',
162     'Set any value for show all fields. This value will cover "fields" ' +
163     'and "all_fields".',
164     _in='query')
165 @api_ims_inventory_v1.param(
166     'fields',
167     'Set fields to show, split by comman, "/" for parent and children.' +
168     ' Like "name,parent/children". This value will cover "exculde_fields".',
169     _in='query')
170 @api_ims_inventory_v1.param(
171     'exclude_fields',
172     'Set fields to exclude showing, split by comman, "/" for parent and ' +
173     'children. Like "name,parent/children". This value will cover ' +
174     '"exclude_default".',
175     _in='query')
176 @api_ims_inventory_v1.param(
177     'exclude_default',
178     'Exclude showing all default fields, Set "true" to enable.',
179     _in='query')
180 class ResourcePoolsListRouter(Resource):
181
182     model = ResourcePoolDTO.resource_pool_get
183
184     @api_ims_inventory_v1.marshal_list_with(model)
185     def get(self):
186         parser = reqparse.RequestParser()
187         parser.add_argument(PAGE_PARAM, location='args')
188         args = parser.parse_args()
189         kwargs = {}
190         if args.nextpage_opaque_marker is not None:
191             kwargs['page'] = args.nextpage_opaque_marker
192
193         ret = ocloud_view.resource_pools(bus.uow, **kwargs)
194         return link_header(request.full_path, ret)
195
196
197 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>")
198 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
199 @api_ims_inventory_v1.response(404, 'Resource pool not found')
200 @api_ims_inventory_v1.param(
201     'all_fields',
202     'Set any value for show all fields. This value will cover "fields" ' +
203     'and "all_fields".',
204     _in='query')
205 @api_ims_inventory_v1.param(
206     'fields',
207     'Set fields to show, split by comman, "/" for parent and children.' +
208     ' Like "name,parent/children". This value will cover "exculde_fields".',
209     _in='query')
210 @api_ims_inventory_v1.param(
211     'exclude_fields',
212     'Set fields to exclude showing, split by comman, "/" for parent and ' +
213     'children. Like "name,parent/children". This value will cover ' +
214     '"exclude_default".',
215     _in='query')
216 @api_ims_inventory_v1.param(
217     'exclude_default',
218     'Exclude showing all default fields, Set "true" to enable.',
219     _in='query')
220 class ResourcePoolGetRouter(Resource):
221
222     model = ResourcePoolDTO.resource_pool_get
223
224     @api_ims_inventory_v1.doc('Get resource pool')
225     @api_ims_inventory_v1.marshal_with(model)
226     def get(self, resourcePoolID):
227         result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
228         if result is not None:
229             return result
230         api_ims_inventory_v1.abort(
231             404, "Resource pool {} doesn't exist".format(resourcePoolID))
232
233
234 # ----------  Resources ---------- #
235 @api_ims_inventory_v1.route("/resourcePools/<resourcePoolID>/resources")
236 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
237 @api_ims_inventory_v1.param('resourceTypeName', 'filter resource type',
238                             _in='query')
239 @api_ims_inventory_v1.param('parentId', 'filter parentId',
240                             _in='query')
241 # @api_ims_inventory_v1.param('sort', 'sort by column name',
242 #                             _in='query')
243 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
244 #                             '(max 100). Default: 30',
245 #                             _in='query', default=30)
246 @api_ims_inventory_v1.param(PAGE_PARAM,
247                             'Page number of the results to fetch.' +
248                             ' Default: 1',
249                             _in='query', default=1)
250 @api_ims_inventory_v1.param(
251     'all_fields',
252     'Set any value for show all fields. This value will cover "fields" ' +
253     'and "all_fields".',
254     _in='query')
255 @api_ims_inventory_v1.param(
256     'fields',
257     'Set fields to show, split by comman, "/" for parent and children.' +
258     ' Like "name,parent/children". This value will cover "exculde_fields".',
259     _in='query')
260 @api_ims_inventory_v1.param(
261     'exclude_fields',
262     'Set fields to exclude showing, split by comman, "/" for parent and ' +
263     'children. Like "name,parent/children". This value will cover ' +
264     '"exclude_default".',
265     _in='query')
266 @api_ims_inventory_v1.param(
267     'exclude_default',
268     'Exclude showing all default fields, Set "true" to enable.',
269     _in='query')
270 class ResourcesListRouter(Resource):
271
272     model = ResourceDTO.resource_list
273
274     @api_ims_inventory_v1.marshal_list_with(model)
275     def get(self, resourcePoolID):
276         parser = reqparse.RequestParser()
277         parser.add_argument('resourceTypeName', location='args')
278         parser.add_argument('parentId', location='args')
279         parser.add_argument(PAGE_PARAM, location='args')
280         args = parser.parse_args()
281         kwargs = {}
282         if args.resourceTypeName is not None:
283             kwargs['resourceTypeName'] = args.resourceTypeName
284         if args.parentId is not None:
285             kwargs['parentId'] = args.parentId
286             if args.parentId.lower() == 'null':
287                 kwargs['parentId'] = None
288         # if args.per_page is not None:
289         #     kwargs['per_page'] = args.per_page
290         #     base_url = base_url + 'per_page=' + args.per_page + '&'
291         if args.nextpage_opaque_marker is not None:
292             kwargs['page'] = args.nextpage_opaque_marker
293
294         ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
295         return link_header(request.full_path, ret)
296
297
298 @api_ims_inventory_v1.route(
299     "/resourcePools/<resourcePoolID>/resources/<resourceID>")
300 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
301 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
302 @api_ims_inventory_v1.response(404, 'Resource not found')
303 @api_ims_inventory_v1.param(
304     'all_fields',
305     'Set any value for show all fields. This value will cover "fields" ' +
306     'and "all_fields".',
307     _in='query')
308 @api_ims_inventory_v1.param(
309     'fields',
310     'Set fields to show, split by comman, "/" for parent and children.' +
311     ' Like "name,parent/children". This value will cover "exculde_fields".',
312     _in='query')
313 @api_ims_inventory_v1.param(
314     'exclude_fields',
315     'Set fields to exclude showing, split by comman, "/" for parent and ' +
316     'children. Like "name,parent/children". This value will cover ' +
317     '"exclude_default".',
318     _in='query')
319 @api_ims_inventory_v1.param(
320     'exclude_default',
321     'Exclude showing all default fields, Set "true" to enable.',
322     _in='query')
323 class ResourceGetRouter(Resource):
324
325     # dto = ResourceDTO()
326     # model = dto.get_resource_get()
327     model = ResourceDTO.recursive_resource_mapping()
328
329     @api_ims_inventory_v1.doc('Get resource')
330     @api_ims_inventory_v1.marshal_with(model)
331     def get(self, resourcePoolID, resourceID):
332         result = ocloud_view.resource_one(resourceID, bus.uow)
333         if result is not None:
334             return result
335         api_ims_inventory_v1.abort(
336             404, "Resource {} doesn't exist".format(resourceID))
337
338
339 # ----------  DeploymentManagers ---------- #
340 @api_ims_inventory_v1.route("/deploymentManagers")
341 @api_ims_inventory_v1.param(PAGE_PARAM,
342                             'Page number of the results to fetch.' +
343                             ' Default: 1',
344                             _in='query', default=1)
345 @api_ims_inventory_v1.param(
346     'all_fields',
347     'Set any value for show all fields. This value will cover "fields" ' +
348     'and "all_fields".',
349     _in='query')
350 @api_ims_inventory_v1.param(
351     'fields',
352     'Set fields to show, split by comman, "/" for parent and children.' +
353     ' Like "name,parent/children". This value will cover "exculde_fields".',
354     _in='query')
355 @api_ims_inventory_v1.param(
356     'exclude_fields',
357     'Set fields to exclude showing, split by comman, "/" for parent and ' +
358     'children. Like "name,parent/children". This value will cover ' +
359     '"exclude_default".',
360     _in='query')
361 @api_ims_inventory_v1.param(
362     'exclude_default',
363     'Exclude showing all default fields, Set "true" to enable.',
364     _in='query')
365 class DeploymentManagersListRouter(Resource):
366
367     model = DeploymentManagerDTO.deployment_manager_list
368
369     @api_ims_inventory_v1.marshal_list_with(model)
370     def get(self):
371         parser = reqparse.RequestParser()
372         parser.add_argument(PAGE_PARAM, location='args')
373         args = parser.parse_args()
374         kwargs = {}
375         if args.nextpage_opaque_marker is not None:
376             kwargs['page'] = args.nextpage_opaque_marker
377
378         ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
379         return link_header(request.full_path, ret)
380
381
382 @api_ims_inventory_v1.route("/deploymentManagers/<deploymentManagerID>")
383 @api_ims_inventory_v1.param('deploymentManagerID',
384                             'ID of the deployment manager')
385 @api_ims_inventory_v1.param('profile', 'DMS profile: value supports "sol018"',
386                             _in='query')
387 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
388 @api_ims_inventory_v1.param(
389     'all_fields',
390     'Set any value for show all fields. This value will cover "fields" ' +
391     'and "all_fields".',
392     _in='query')
393 @api_ims_inventory_v1.param(
394     'fields',
395     'Set fields to show, split by comman, "/" for parent and children.' +
396     ' Like "name,parent/children". This value will cover "exculde_fields".',
397     _in='query')
398 @api_ims_inventory_v1.param(
399     'exclude_fields',
400     'Set fields to exclude showing, split by comman, "/" for parent and ' +
401     'children. Like "name,parent/children". This value will cover ' +
402     '"exclude_default".',
403     _in='query')
404 @api_ims_inventory_v1.param(
405     'exclude_default',
406     'Exclude showing all default fields, Set "true" to enable.',
407     _in='query')
408 class DeploymentManagerGetRouter(Resource):
409
410     model = DeploymentManagerDTO.deployment_manager_get
411
412     @api_ims_inventory_v1.doc('Get deployment manager')
413     @api_ims_inventory_v1.marshal_with(model)
414     def get(self, deploymentManagerID):
415         parser = reqparse.RequestParser()
416         parser.add_argument('profile', location='args')
417         args = parser.parse_args()
418         profile = (
419             args.profile if args.profile is not None and args.profile != ''
420             else 'default')
421         result = ocloud_view.deployment_manager_one(
422             deploymentManagerID, bus.uow, profile)
423         if result is not None:
424             return result
425         api_ims_inventory_v1.abort(
426             404,
427             "Deployment manager {} doesn't exist".format(deploymentManagerID))
428
429
430 # ----------  Subscriptions ---------- #
431 @api_ims_inventory_v1.route("/subscriptions")
432 class SubscriptionsListRouter(Resource):
433
434     model = SubscriptionDTO.subscription_get
435     expect = SubscriptionDTO.subscription
436     post_resp = SubscriptionDTO.subscription_post_resp
437
438     @api_ims_inventory_v1.doc('List subscriptions')
439     @api_ims_inventory_v1.marshal_list_with(model)
440     @api_ims_inventory_v1.param(
441         PAGE_PARAM,
442         'Page number of the results to fetch. Default: 1',
443         _in='query', default=1)
444     @api_ims_inventory_v1.param(
445         'all_fields',
446         'Set any value for show all fields. This value will cover "fields" ' +
447         'and "all_fields".',
448         _in='query')
449     @api_ims_inventory_v1.param(
450         'fields',
451         'Set fields to show, split by comman, "/" for parent and children.' +
452         ' Like "name,parent/children". This value will cover' +
453         ' "exculde_fields".',
454         _in='query')
455     @api_ims_inventory_v1.param(
456         'exclude_fields',
457         'Set fields to exclude showing, split by comman, "/" for parent and ' +
458         'children. Like "name,parent/children". This value will cover ' +
459         '"exclude_default".',
460         _in='query')
461     @api_ims_inventory_v1.param(
462         'exclude_default',
463         'Exclude showing all default fields, Set "true" to enable.',
464         _in='query')
465     def get(self):
466         parser = reqparse.RequestParser()
467         parser.add_argument(PAGE_PARAM, location='args')
468         args = parser.parse_args()
469         kwargs = {}
470         if args.nextpage_opaque_marker is not None:
471             kwargs['page'] = args.nextpage_opaque_marker
472
473         ret = ocloud_view.subscriptions(bus.uow, **kwargs)
474         return link_header(request.full_path, ret)
475
476     @api_ims_inventory_v1.doc('Create a subscription')
477     @api_ims_inventory_v1.expect(expect)
478     @api_ims_inventory_v1.marshal_with(post_resp, code=201)
479     def post(self):
480         data = api_ims_inventory_v1.payload
481         result = ocloud_view.subscription_create(data, bus.uow)
482         return result, 201
483
484
485 @api_ims_inventory_v1.route("/subscriptions/<subscriptionID>")
486 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
487 @api_ims_inventory_v1.response(404, 'Subscription not found')
488 class SubscriptionGetDelRouter(Resource):
489
490     model = SubscriptionDTO.subscription_get
491
492     @api_ims_inventory_v1.doc('Get subscription by ID')
493     @api_ims_inventory_v1.marshal_with(model)
494     @api_ims_inventory_v1.param(
495         'all_fields',
496         'Set any value for show all fields. This value will cover "fields" ' +
497         'and "all_fields".',
498         _in='query')
499     @api_ims_inventory_v1.param(
500         'fields',
501         'Set fields to show, split by comman, "/" for parent and children.' +
502         ' Like "name,parent/children". This value will cover' +
503         ' "exculde_fields".',
504         _in='query')
505     @api_ims_inventory_v1.param(
506         'exclude_fields',
507         'Set fields to exclude showing, split by comman, "/" for parent and ' +
508         'children. Like "name,parent/children". This value will cover ' +
509         '"exclude_default".',
510         _in='query')
511     @api_ims_inventory_v1.param(
512         'exclude_default',
513         'Exclude showing all default fields, Set "true" to enable.',
514         _in='query')
515     def get(self, subscriptionID):
516         result = ocloud_view.subscription_one(
517             subscriptionID, bus.uow)
518         if result is not None:
519             return result
520         api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
521             subscriptionID))
522
523     @api_ims_inventory_v1.doc('Delete subscription by ID')
524     @api_ims_inventory_v1.response(204, 'Subscription deleted')
525     def delete(self, subscriptionID):
526         result = ocloud_view.subscription_delete(subscriptionID, bus.uow)
527         return result, 204