1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from flask import request
16 from flask_restx import Resource, reqparse
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 from o2ims.domain import ocloud
22 from o2ims.views import ocloud_view
23 from o2ims.views.api_ns import api_ims_inventory as api_ims_inventory_v1
24 from o2ims.views.ocloud_dto import OcloudDTO, ResourceTypeDTO,\
25 ResourcePoolDTO, ResourceDTO, DeploymentManagerDTO, SubscriptionDTO
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
31 def configure_api_route():
32 # Set global bus for resource
34 bus = MessageBus.get_instance()
37 # ---------- API versions ---------- #
38 @api_ims_inventory_v1.route("/v1/api_versions")
39 class VersionRouter(Resource):
42 'uriPrefix': request.base_url.rsplit('/', 1)[0],
45 # 'isDeprecated': 'False',
46 # 'retirementDate': ''
51 # ---------- OClouds ---------- #
52 @api_ims_inventory_v1.route("/v1/")
53 @api_ims_inventory_v1.response(404, 'oCloud not found')
54 @api_ims_inventory_v1.param(
56 'Set any value for show all fields. This value will cover "fields" ' +
59 @api_ims_inventory_v1.param(
61 'Set fields to show, split by comma, "/" for parent and children.' +
62 ' Like "name,parent/children". This value will cover "exculde_fields".',
64 @api_ims_inventory_v1.param(
66 'Set fields to exclude showing, split by comma, "/" for parent and ' +
67 'children. Like "name,parent/children". This value will cover ' +
70 @api_ims_inventory_v1.param(
72 'Exclude showing all default fields, Set "true" to enable.',
74 class OcloudsListRouter(Resource):
75 """Ocloud get endpoint
76 O2 interface ocloud endpoint
79 ocloud_get = OcloudDTO.ocloud
81 @api_ims_inventory_v1.marshal_with(ocloud_get)
83 res = ocloud_view.oclouds(bus.uow)
86 raise NotFoundException("oCloud doesn't exist")
89 # ---------- ResourceTypes ---------- #
90 @api_ims_inventory_v1.route("/v1/resourceTypes")
91 @api_ims_inventory_v1.param(PAGE_PARAM,
92 'Page number of the results to fetch.' +
94 _in='query', default=1)
95 @api_ims_inventory_v1.param(
97 'Set any value for show all fields. This value will cover "fields" ' +
100 @api_ims_inventory_v1.param(
102 'Set fields to show, split by comma, "/" for parent and children.' +
103 ' Like "name,parent/children". This value will cover "exculde_fields".',
105 @api_ims_inventory_v1.param(
107 'Set fields to exclude showing, split by comma, "/" for parent and ' +
108 'children. Like "name,parent/children". This value will cover ' +
109 '"exclude_default".',
111 @api_ims_inventory_v1.param(
113 'Exclude showing all default fields, Set "true" to enable.',
115 @api_ims_inventory_v1.param(
117 'Filter of the query.',
119 class ResourceTypesListRouter(Resource):
121 model = ResourceTypeDTO.resource_type_get
123 @api_ims_inventory_v1.marshal_list_with(model)
125 parser = reqparse.RequestParser()
126 parser.add_argument(PAGE_PARAM, location='args')
127 parser.add_argument('filter', location='args')
128 args = parser.parse_args()
130 if args.nextpage_opaque_marker is not None:
131 kwargs['page'] = args.nextpage_opaque_marker
132 kwargs['filter'] = args.filter if args.filter is not None else ''
134 ret = ocloud_view.resource_types(bus.uow, **kwargs)
135 return link_header(request.full_path, ret)
138 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
139 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
140 @api_ims_inventory_v1.response(404, 'Resource type not found')
141 @api_ims_inventory_v1.param(
143 'Set any value for show all fields. This value will cover "fields" ' +
146 @api_ims_inventory_v1.param(
148 'Set fields to show, split by comma, "/" for parent and children.' +
149 ' Like "name,parent/children". This value will cover "exculde_fields".',
151 @api_ims_inventory_v1.param(
153 'Set fields to exclude showing, split by comma, "/" for parent and ' +
154 'children. Like "name,parent/children". This value will cover ' +
155 '"exclude_default".',
157 @api_ims_inventory_v1.param(
159 'Exclude showing all default fields, Set "true" to enable.',
161 class ResourceTypeGetRouter(Resource):
163 model = ResourceTypeDTO.resource_type_get
165 @api_ims_inventory_v1.doc('Get resource type')
166 @api_ims_inventory_v1.marshal_with(model)
167 def get(self, resourceTypeID):
168 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
169 if result is not None:
171 raise NotFoundException("Resource type {} doesn't exist".format(
175 # ---------- ResourcePools ---------- #
176 @api_ims_inventory_v1.route("/v1/resourcePools")
177 @api_ims_inventory_v1.param(PAGE_PARAM,
178 'Page number of the results to fetch.' +
180 _in='query', default=1)
181 @api_ims_inventory_v1.param(
183 'Set any value for show all fields. This value will cover "fields" ' +
186 @api_ims_inventory_v1.param(
188 'Set fields to show, split by comma, "/" for parent and children.' +
189 ' Like "name,parent/children". This value will cover "exculde_fields".',
191 @api_ims_inventory_v1.param(
193 'Set fields to exclude showing, split by comma, "/" for parent and ' +
194 'children. Like "name,parent/children". This value will cover ' +
195 '"exclude_default".',
197 @api_ims_inventory_v1.param(
199 'Exclude showing all default fields, Set "true" to enable.',
201 @api_ims_inventory_v1.param(
203 'Filter of the query.',
205 class ResourcePoolsListRouter(Resource):
207 model = ResourcePoolDTO.resource_pool_get
209 @api_ims_inventory_v1.marshal_list_with(model)
211 parser = reqparse.RequestParser()
212 parser.add_argument(PAGE_PARAM, location='args')
213 parser.add_argument('filter', location='args')
214 args = parser.parse_args()
216 if args.nextpage_opaque_marker is not None:
217 kwargs['page'] = args.nextpage_opaque_marker
218 kwargs['filter'] = args.filter if args.filter is not None else ''
220 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
221 return link_header(request.full_path, ret)
224 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
225 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
226 @api_ims_inventory_v1.response(404, 'Resource pool not found')
227 @api_ims_inventory_v1.param(
229 'Set any value for show all fields. This value will cover "fields" ' +
232 @api_ims_inventory_v1.param(
234 'Set fields to show, split by comma, "/" for parent and children.' +
235 ' Like "name,parent/children". This value will cover "exculde_fields".',
237 @api_ims_inventory_v1.param(
239 'Set fields to exclude showing, split by comma, "/" for parent and ' +
240 'children. Like "name,parent/children". This value will cover ' +
241 '"exclude_default".',
243 @api_ims_inventory_v1.param(
245 'Exclude showing all default fields, Set "true" to enable.',
247 class ResourcePoolGetRouter(Resource):
249 model = ResourcePoolDTO.resource_pool_get
251 @api_ims_inventory_v1.doc('Get resource pool')
252 @api_ims_inventory_v1.marshal_with(model)
253 def get(self, resourcePoolID):
254 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
255 if result is not None:
257 raise NotFoundException("Resource pool {} doesn't exist".format(
261 # ---------- Resources ---------- #
262 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
263 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
264 # @api_ims_inventory_v1.param('sort', 'sort by column name',
266 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
267 # '(max 100). Default: 30',
268 # _in='query', default=30)
269 @api_ims_inventory_v1.param(PAGE_PARAM,
270 'Page number of the results to fetch.' +
272 _in='query', default=1)
273 @api_ims_inventory_v1.param(
275 'Set any value for show all fields. This value will cover "fields" ' +
278 @api_ims_inventory_v1.param(
280 'Set fields to show, split by comma, "/" for parent and children.' +
281 ' Like "name,parent/children". This value will cover "exculde_fields".',
283 @api_ims_inventory_v1.param(
285 'Set fields to exclude showing, split by comma, "/" for parent and ' +
286 'children. Like "name,parent/children". This value will cover ' +
287 '"exclude_default".',
289 @api_ims_inventory_v1.param(
291 'Exclude showing all default fields, Set "true" to enable.',
293 @api_ims_inventory_v1.param(
295 'Filter of the query.',
297 class ResourcesListRouter(Resource):
299 model = ResourceDTO.resource_list
301 @api_ims_inventory_v1.marshal_list_with(model)
302 def get(self, resourcePoolID):
303 parser = reqparse.RequestParser()
304 parser.add_argument(PAGE_PARAM, location='args')
305 parser.add_argument('filter', location='args')
306 args = parser.parse_args()
308 # if args.per_page is not None:
309 # kwargs['per_page'] = args.per_page
310 # base_url = base_url + 'per_page=' + args.per_page + '&'
311 if args.nextpage_opaque_marker is not None:
312 kwargs['page'] = args.nextpage_opaque_marker
313 kwargs['filter'] = args.filter if args.filter is not None else ''
315 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
316 return link_header(request.full_path, ret)
319 @api_ims_inventory_v1.route(
320 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
321 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
322 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
323 @api_ims_inventory_v1.response(404, 'Resource not found')
324 @api_ims_inventory_v1.param(
326 'Set any value for show all fields. This value will cover "fields" ' +
329 @api_ims_inventory_v1.param(
331 'Set fields to show, split by comma, "/" for parent and children.' +
332 ' Like "name,parent/children". This value will cover "exculde_fields".',
334 @api_ims_inventory_v1.param(
336 'Set fields to exclude showing, split by comma, "/" for parent and ' +
337 'children. Like "name,parent/children". This value will cover ' +
338 '"exclude_default".',
340 @api_ims_inventory_v1.param(
342 'Exclude showing all default fields, Set "true" to enable.',
344 class ResourceGetRouter(Resource):
346 # dto = ResourceDTO()
347 # model = dto.get_resource_get()
348 model = ResourceDTO.recursive_resource_mapping()
350 @api_ims_inventory_v1.doc('Get resource')
351 @api_ims_inventory_v1.marshal_with(model)
352 def get(self, resourcePoolID, resourceID):
353 result = ocloud_view.resource_one(resourceID, bus.uow)
354 if result is not None:
356 raise NotFoundException("Resource {} doesn't exist".format(
360 # ---------- DeploymentManagers ---------- #
361 @api_ims_inventory_v1.route("/v1/deploymentManagers")
362 @api_ims_inventory_v1.param(PAGE_PARAM,
363 'Page number of the results to fetch.' +
365 _in='query', default=1)
366 @api_ims_inventory_v1.param(
368 'Set any value for show all fields. This value will cover "fields" ' +
371 @api_ims_inventory_v1.param(
373 'Set fields to show, split by comma, "/" for parent and children.' +
374 ' Like "name,parent/children". This value will cover "exculde_fields".',
376 @api_ims_inventory_v1.param(
378 'Set fields to exclude showing, split by comma, "/" for parent and ' +
379 'children. Like "name,parent/children". This value will cover ' +
380 '"exclude_default".',
382 @api_ims_inventory_v1.param(
384 'Exclude showing all default fields, Set "true" to enable.',
386 @api_ims_inventory_v1.param(
388 'Filter of the query.',
390 class DeploymentManagersListRouter(Resource):
392 model = DeploymentManagerDTO.deployment_manager_list
394 @api_ims_inventory_v1.marshal_list_with(model)
396 parser = reqparse.RequestParser()
397 parser.add_argument(PAGE_PARAM, location='args')
398 parser.add_argument('filter', location='args')
399 args = parser.parse_args()
401 if args.nextpage_opaque_marker is not None:
402 kwargs['page'] = args.nextpage_opaque_marker
403 kwargs['filter'] = args.filter if args.filter is not None else ''
405 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
406 return link_header(request.full_path, ret)
409 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
410 @api_ims_inventory_v1.param('deploymentManagerID',
411 'ID of the deployment manager')
412 @api_ims_inventory_v1.param(
413 'profile', 'DMS profile: value supports "native_k8sapi"',
415 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
416 @api_ims_inventory_v1.param(
418 'Set any value for show all fields. This value will cover "fields" ' +
421 @api_ims_inventory_v1.param(
423 'Set fields to show, split by comma, "/" for parent and children.' +
424 ' Like "name,parent/children". This value will cover "exculde_fields".',
426 @api_ims_inventory_v1.param(
428 'Set fields to exclude showing, split by comma, "/" for parent and ' +
429 'children. Like "name,parent/children". This value will cover ' +
430 '"exclude_default".',
432 @api_ims_inventory_v1.param(
434 'Exclude showing all default fields, Set "true" to enable.',
436 class DeploymentManagerGetRouter(Resource):
438 model = DeploymentManagerDTO.deployment_manager_get
440 @api_ims_inventory_v1.doc('Get deployment manager')
441 @api_ims_inventory_v1.marshal_with(model)
442 def get(self, deploymentManagerID):
443 parser = reqparse.RequestParser()
444 parser.add_argument('profile', location='args')
445 args = parser.parse_args()
447 args.profile if args.profile is not None and args.profile != ''
448 else ocloud.DeploymentManagerProfileDefault)
449 result = ocloud_view.deployment_manager_one(
450 deploymentManagerID, bus.uow, profile)
451 if result is not None and result != "":
454 raise NotFoundException(
455 "Profile {} doesn't support".format(
458 raise NotFoundException("Deployment manager {} doesn't exist".format(
459 deploymentManagerID))
462 # ---------- Subscriptions ---------- #
463 @api_ims_inventory_v1.route("/v1/subscriptions")
464 class SubscriptionsListRouter(Resource):
466 model = SubscriptionDTO.subscription_get
467 expect = SubscriptionDTO.subscription
468 post_resp = SubscriptionDTO.subscription_post_resp
470 @api_ims_inventory_v1.doc('List subscriptions')
471 @api_ims_inventory_v1.marshal_list_with(model)
472 @api_ims_inventory_v1.param(
474 'Page number of the results to fetch. Default: 1',
475 _in='query', default=1)
476 @api_ims_inventory_v1.param(
478 'Set any value for show all fields. This value will cover "fields" ' +
481 @api_ims_inventory_v1.param(
483 'Set fields to show, split by comma, "/" for parent and children.' +
484 ' Like "name,parent/children". This value will cover' +
485 ' "exculde_fields".',
487 @api_ims_inventory_v1.param(
489 'Set fields to exclude showing, split by comma, "/" for parent and ' +
490 'children. Like "name,parent/children". This value will cover ' +
491 '"exclude_default".',
493 @api_ims_inventory_v1.param(
495 'Exclude showing all default fields, Set "true" to enable.',
497 @api_ims_inventory_v1.param(
499 'Filter of the query.',
502 parser = reqparse.RequestParser()
503 parser.add_argument(PAGE_PARAM, location='args')
504 parser.add_argument('filter', location='args')
505 args = parser.parse_args()
507 if args.nextpage_opaque_marker is not None:
508 kwargs['page'] = args.nextpage_opaque_marker
509 kwargs['filter'] = args.filter if args.filter is not None else ''
511 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
512 return link_header(request.full_path, ret)
514 @api_ims_inventory_v1.doc('Create a subscription')
515 @api_ims_inventory_v1.expect(expect)
516 @api_ims_inventory_v1.marshal_with(post_resp, code=201)
518 data = api_ims_inventory_v1.payload
519 result = ocloud_view.subscription_create(data, bus.uow)
523 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
524 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
525 @api_ims_inventory_v1.response(404, 'Subscription not found')
526 class SubscriptionGetDelRouter(Resource):
528 model = SubscriptionDTO.subscription_get
530 @api_ims_inventory_v1.doc('Get subscription by ID')
531 @api_ims_inventory_v1.marshal_with(model)
532 @api_ims_inventory_v1.param(
534 'Set any value for show all fields. This value will cover "fields" ' +
537 @api_ims_inventory_v1.param(
539 'Set fields to show, split by comma, "/" for parent and children.' +
540 ' Like "name,parent/children". This value will cover' +
541 ' "exculde_fields".',
543 @api_ims_inventory_v1.param(
545 'Set fields to exclude showing, split by comma, "/" for parent and ' +
546 'children. Like "name,parent/children". This value will cover ' +
547 '"exclude_default".',
549 @api_ims_inventory_v1.param(
551 'Exclude showing all default fields, Set "true" to enable.',
553 def get(self, subscriptionID):
554 result = ocloud_view.subscription_one(
555 subscriptionID, bus.uow)
556 if result is not None:
558 raise NotFoundException("Subscription {} doesn't exist".format(
561 @api_ims_inventory_v1.doc('Delete subscription by ID')
562 @api_ims_inventory_v1.response(200, 'Subscription deleted')
563 def delete(self, subscriptionID):
564 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)