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, \
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
28 from o2common.helper import o2logging
29 logger = o2logging.get_logger(__name__)
32 def configure_api_route():
33 # Set global bus for resource
35 bus = MessageBus.get_instance()
38 # ---------- API versions ---------- #
39 @api_ims_inventory_v1.route("/v1/api_versions")
40 class VersionRouter(Resource):
43 'uriPrefix': request.base_url.rsplit('/', 1)[0],
46 # 'isDeprecated': 'False',
47 # 'retirementDate': ''
52 # ---------- OClouds ---------- #
53 @api_ims_inventory_v1.route(*["/v1", "/v1/"])
54 @api_ims_inventory_v1.response(404, 'oCloud not found')
55 @api_ims_inventory_v1.param(
57 'Set any value for show all fields. This value will cover "fields" ' +
60 @api_ims_inventory_v1.param(
62 'Set fields to show, split by comma, "/" for parent and children.' +
63 ' Like "name,parent/children". This value will cover "exculde_fields".',
65 @api_ims_inventory_v1.param(
67 'Set fields to exclude showing, split by comma, "/" for parent and ' +
68 'children. Like "name,parent/children". This value will cover ' +
71 @api_ims_inventory_v1.param(
73 'Exclude showing all default fields, Set "true" to enable.',
75 class OcloudsListRouter(Resource):
76 """Ocloud get endpoint
77 O2 interface ocloud endpoint
80 ocloud_get = OcloudDTO.ocloud
82 @api_ims_inventory_v1.marshal_with(ocloud_get)
84 res = ocloud_view.oclouds(bus.uow)
87 raise NotFoundException("oCloud doesn't exist")
90 # ---------- ResourceTypes ---------- #
91 @api_ims_inventory_v1.route("/v1/resourceTypes")
92 @api_ims_inventory_v1.param(PAGE_PARAM,
93 'Page number of the results to fetch.' +
95 _in='query', default=1)
96 @api_ims_inventory_v1.param(
98 'Set any value for show all fields. This value will cover "fields" ' +
101 @api_ims_inventory_v1.param(
103 'Set fields to show, split by comma, "/" for parent and children.' +
104 ' Like "name,parent/children". This value will cover "exculde_fields".',
106 @api_ims_inventory_v1.param(
108 'Set fields to exclude showing, split by comma, "/" for parent and ' +
109 'children. Like "name,parent/children". This value will cover ' +
110 '"exclude_default".',
112 @api_ims_inventory_v1.param(
114 'Exclude showing all default fields, Set "true" to enable.',
116 @api_ims_inventory_v1.param(
118 'Filter of the query.',
120 class ResourceTypesListRouter(Resource):
122 model = ResourceTypeDTO.resource_type_get
124 @api_ims_inventory_v1.marshal_list_with(model)
126 parser = reqparse.RequestParser()
127 parser.add_argument(PAGE_PARAM, location='args')
128 parser.add_argument('filter', location='args')
129 args = parser.parse_args()
131 if args.nextpage_opaque_marker is not None:
132 kwargs['page'] = args.nextpage_opaque_marker
133 kwargs['filter'] = args.filter if args.filter is not None else ''
135 ret = ocloud_view.resource_types(bus.uow, **kwargs)
136 return link_header(request.full_path, ret)
139 @api_ims_inventory_v1.route("/v1/resourceTypes/<resourceTypeID>")
140 @api_ims_inventory_v1.param('resourceTypeID', 'ID of the resource type')
141 @api_ims_inventory_v1.response(404, 'Resource type not found')
142 @api_ims_inventory_v1.param(
144 'Set any value for show all fields. This value will cover "fields" ' +
147 @api_ims_inventory_v1.param(
149 'Set fields to show, split by comma, "/" for parent and children.' +
150 ' Like "name,parent/children". This value will cover "exculde_fields".',
152 @api_ims_inventory_v1.param(
154 'Set fields to exclude showing, split by comma, "/" for parent and ' +
155 'children. Like "name,parent/children". This value will cover ' +
156 '"exclude_default".',
158 @api_ims_inventory_v1.param(
160 'Exclude showing all default fields, Set "true" to enable.',
162 class ResourceTypeGetRouter(Resource):
164 model = ResourceTypeDTO.resource_type_get
166 @api_ims_inventory_v1.doc('Get resource type')
167 @api_ims_inventory_v1.marshal_with(model)
168 def get(self, resourceTypeID):
169 result = ocloud_view.resource_type_one(resourceTypeID, bus.uow)
170 if result is not None:
172 raise NotFoundException("Resource type {} doesn't exist".format(
176 # ---------- ResourcePools ---------- #
177 @api_ims_inventory_v1.route("/v1/resourcePools")
178 @api_ims_inventory_v1.param(PAGE_PARAM,
179 'Page number of the results to fetch.' +
181 _in='query', default=1)
182 @api_ims_inventory_v1.param(
184 'Set any value for show all fields. This value will cover "fields" ' +
187 @api_ims_inventory_v1.param(
189 'Set fields to show, split by comma, "/" for parent and children.' +
190 ' Like "name,parent/children". This value will cover "exculde_fields".',
192 @api_ims_inventory_v1.param(
194 'Set fields to exclude showing, split by comma, "/" for parent and ' +
195 'children. Like "name,parent/children". This value will cover ' +
196 '"exclude_default".',
198 @api_ims_inventory_v1.param(
200 'Exclude showing all default fields, Set "true" to enable.',
202 @api_ims_inventory_v1.param(
204 'Filter of the query.',
206 class ResourcePoolsListRouter(Resource):
208 model = ResourcePoolDTO.resource_pool_get
210 @api_ims_inventory_v1.marshal_list_with(model)
212 parser = reqparse.RequestParser()
213 parser.add_argument(PAGE_PARAM, location='args')
214 parser.add_argument('filter', location='args')
215 args = parser.parse_args()
217 if args.nextpage_opaque_marker is not None:
218 kwargs['page'] = args.nextpage_opaque_marker
219 kwargs['filter'] = args.filter if args.filter is not None else ''
221 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
222 return link_header(request.full_path, ret)
225 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>")
226 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
227 @api_ims_inventory_v1.response(404, 'Resource pool not found')
228 @api_ims_inventory_v1.param(
230 'Set any value for show all fields. This value will cover "fields" ' +
233 @api_ims_inventory_v1.param(
235 'Set fields to show, split by comma, "/" for parent and children.' +
236 ' Like "name,parent/children". This value will cover "exculde_fields".',
238 @api_ims_inventory_v1.param(
240 'Set fields to exclude showing, split by comma, "/" for parent and ' +
241 'children. Like "name,parent/children". This value will cover ' +
242 '"exclude_default".',
244 @api_ims_inventory_v1.param(
246 'Exclude showing all default fields, Set "true" to enable.',
248 class ResourcePoolGetRouter(Resource):
250 model = ResourcePoolDTO.resource_pool_get
252 @api_ims_inventory_v1.doc('Get resource pool')
253 @api_ims_inventory_v1.marshal_with(model)
254 def get(self, resourcePoolID):
255 result = ocloud_view.resource_pool_one(resourcePoolID, bus.uow)
256 if result is not None:
258 raise NotFoundException("Resource pool {} doesn't exist".format(
262 # ---------- Resources ---------- #
263 @api_ims_inventory_v1.route("/v1/resourcePools/<resourcePoolID>/resources")
264 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
265 # @api_ims_inventory_v1.param('sort', 'sort by column name',
267 # @api_ims_inventory_v1.param('per_page', 'The number of results per page ' +
268 # '(max 100). Default: 30',
269 # _in='query', default=30)
270 @api_ims_inventory_v1.param(PAGE_PARAM,
271 'Page number of the results to fetch.' +
273 _in='query', default=1)
274 @api_ims_inventory_v1.param(
276 'Set any value for show all fields. This value will cover "fields" ' +
279 @api_ims_inventory_v1.param(
281 'Set fields to show, split by comma, "/" for parent and children.' +
282 ' Like "name,parent/children". This value will cover "exculde_fields".',
284 @api_ims_inventory_v1.param(
286 'Set fields to exclude showing, split by comma, "/" for parent and ' +
287 'children. Like "name,parent/children". This value will cover ' +
288 '"exclude_default".',
290 @api_ims_inventory_v1.param(
292 'Exclude showing all default fields, Set "true" to enable.',
294 @api_ims_inventory_v1.param(
296 'Filter of the query.',
298 class ResourcesListRouter(Resource):
300 model = ResourceDTO.resource_list
302 @api_ims_inventory_v1.marshal_list_with(model)
303 def get(self, resourcePoolID):
304 parser = reqparse.RequestParser()
305 parser.add_argument(PAGE_PARAM, location='args')
306 parser.add_argument('filter', location='args')
307 args = parser.parse_args()
309 # if args.per_page is not None:
310 # kwargs['per_page'] = args.per_page
311 # base_url = base_url + 'per_page=' + args.per_page + '&'
312 if args.nextpage_opaque_marker is not None:
313 kwargs['page'] = args.nextpage_opaque_marker
314 kwargs['filter'] = args.filter if args.filter is not None else ''
316 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
317 return link_header(request.full_path, ret)
320 @api_ims_inventory_v1.route(
321 "/v1/resourcePools/<resourcePoolID>/resources/<resourceID>")
322 @api_ims_inventory_v1.param('resourcePoolID', 'ID of the resource pool')
323 @api_ims_inventory_v1.param('resourceID', 'ID of the resource')
324 @api_ims_inventory_v1.response(404, 'Resource not found')
325 @api_ims_inventory_v1.param(
327 'Set any value for show all fields. This value will cover "fields" ' +
330 @api_ims_inventory_v1.param(
332 'Set fields to show, split by comma, "/" for parent and children.' +
333 ' Like "name,parent/children". This value will cover "exculde_fields".',
335 @api_ims_inventory_v1.param(
337 'Set fields to exclude showing, split by comma, "/" for parent and ' +
338 'children. Like "name,parent/children". This value will cover ' +
339 '"exclude_default".',
341 @api_ims_inventory_v1.param(
343 'Exclude showing all default fields, Set "true" to enable.',
345 class ResourceGetRouter(Resource):
347 # dto = ResourceDTO()
348 # model = dto.get_resource_get()
349 model = ResourceDTO.recursive_resource_mapping()
351 @api_ims_inventory_v1.doc('Get resource')
352 @api_ims_inventory_v1.marshal_with(model)
353 def get(self, resourcePoolID, resourceID):
354 result = ocloud_view.resource_one(resourceID, bus.uow)
355 if result is not None:
357 raise NotFoundException("Resource {} doesn't exist".format(
361 # ---------- DeploymentManagers ---------- #
362 @api_ims_inventory_v1.route("/v1/deploymentManagers")
363 @api_ims_inventory_v1.param(PAGE_PARAM,
364 'Page number of the results to fetch.' +
366 _in='query', default=1)
367 @api_ims_inventory_v1.param(
369 'Set any value for show all fields. This value will cover "fields" ' +
372 @api_ims_inventory_v1.param(
374 'Set fields to show, split by comma, "/" for parent and children.' +
375 ' Like "name,parent/children". This value will cover "exculde_fields".',
377 @api_ims_inventory_v1.param(
379 'Set fields to exclude showing, split by comma, "/" for parent and ' +
380 'children. Like "name,parent/children". This value will cover ' +
381 '"exclude_default".',
383 @api_ims_inventory_v1.param(
385 'Exclude showing all default fields, Set "true" to enable.',
387 @api_ims_inventory_v1.param(
389 'Filter of the query.',
391 class DeploymentManagersListRouter(Resource):
393 model = DeploymentManagerDTO.deployment_manager_list
395 @api_ims_inventory_v1.marshal_list_with(model)
397 parser = reqparse.RequestParser()
398 parser.add_argument(PAGE_PARAM, location='args')
399 parser.add_argument('filter', location='args')
400 args = parser.parse_args()
402 if args.nextpage_opaque_marker is not None:
403 kwargs['page'] = args.nextpage_opaque_marker
404 kwargs['filter'] = args.filter if args.filter is not None else ''
406 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
407 return link_header(request.full_path, ret)
410 @api_ims_inventory_v1.route("/v1/deploymentManagers/<deploymentManagerID>")
411 @api_ims_inventory_v1.param('deploymentManagerID',
412 'ID of the deployment manager')
413 @api_ims_inventory_v1.param(
414 'profile', 'DMS profile: value supports "native_k8sapi"',
416 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
417 @api_ims_inventory_v1.param(
419 'Set any value for show all fields. This value will cover "fields" ' +
422 @api_ims_inventory_v1.param(
424 'Set fields to show, split by comma, "/" for parent and children.' +
425 ' Like "name,parent/children". This value will cover "exculde_fields".',
427 @api_ims_inventory_v1.param(
429 'Set fields to exclude showing, split by comma, "/" for parent and ' +
430 'children. Like "name,parent/children". This value will cover ' +
431 '"exclude_default".',
433 @api_ims_inventory_v1.param(
435 'Exclude showing all default fields, Set "true" to enable.',
437 class DeploymentManagerGetRouter(Resource):
439 model = DeploymentManagerDTO.deployment_manager_get
441 @api_ims_inventory_v1.doc('Get deployment manager')
442 @api_ims_inventory_v1.marshal_with(model)
443 def get(self, deploymentManagerID):
444 parser = reqparse.RequestParser()
445 parser.add_argument('profile', location='args')
446 args = parser.parse_args()
448 args.profile if args.profile is not None and args.profile != ''
449 else ocloud.DeploymentManagerProfileDefault)
450 result = ocloud_view.deployment_manager_one(
451 deploymentManagerID, bus.uow, profile)
452 if result is not None and result != "":
455 raise NotFoundException(
456 "Profile {} doesn't support".format(
459 raise NotFoundException("Deployment manager {} doesn't exist".format(
460 deploymentManagerID))
463 # ---------- Subscriptions ---------- #
464 @api_ims_inventory_v1.route("/v1/subscriptions")
465 class SubscriptionsListRouter(Resource):
467 model = SubscriptionDTO.subscription_get
468 expect = SubscriptionDTO.subscription_create
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(
518 mask='{subscriptionId,callback,consumerSubscriptionId,filter}')
520 data = api_ims_inventory_v1.payload
521 callback = data.get('callback', None)
523 raise BadRequestException('The callback parameter is required')
524 result = ocloud_view.subscription_create(data, bus.uow)
528 @api_ims_inventory_v1.route("/v1/subscriptions/<subscriptionID>")
529 @api_ims_inventory_v1.param('subscriptionID', 'ID of the subscription')
530 @api_ims_inventory_v1.response(404, 'Subscription not found')
531 class SubscriptionGetDelRouter(Resource):
533 model = SubscriptionDTO.subscription_get
535 @api_ims_inventory_v1.doc('Get subscription by ID')
536 @api_ims_inventory_v1.marshal_with(model)
537 @api_ims_inventory_v1.param(
539 'Set any value for show all fields. This value will cover "fields" ' +
542 @api_ims_inventory_v1.param(
544 'Set fields to show, split by comma, "/" for parent and children.' +
545 ' Like "name,parent/children". This value will cover' +
546 ' "exculde_fields".',
548 @api_ims_inventory_v1.param(
550 'Set fields to exclude showing, split by comma, "/" for parent and ' +
551 'children. Like "name,parent/children". This value will cover ' +
552 '"exclude_default".',
554 @api_ims_inventory_v1.param(
556 'Exclude showing all default fields, Set "true" to enable.',
558 def get(self, subscriptionID):
559 result = ocloud_view.subscription_one(
560 subscriptionID, bus.uow)
561 if result is not None:
563 raise NotFoundException("Subscription {} doesn't exist".format(
566 @api_ims_inventory_v1.doc('Delete subscription by ID')
567 @api_ims_inventory_v1.response(200, 'Subscription deleted')
568 def delete(self, subscriptionID):
569 result = ocloud_view.subscription_delete(subscriptionID, bus.uow)