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