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 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
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
29 def configure_api_route():
30 # Set global bus for resource
32 bus = MessageBus.get_instance()
35 # ---------- OClouds ---------- #
36 @api_ims_inventory_v1.route("/")
37 @api_ims_inventory_v1.response(404, 'oCloud not found')
38 @api_ims_inventory_v1.param(
40 'Set any value for show all fields. This value will cover "fields" ' +
43 @api_ims_inventory_v1.param(
45 'Set fields to show, split by comman, "/" for parent and children.' +
46 ' Like "name,parent/children". This value will cover "exculde_fields".',
48 @api_ims_inventory_v1.param(
50 'Set fields to exclude showing, split by comman, "/" for parent and ' +
51 'children. Like "name,parent/children". This value will cover ' +
54 @api_ims_inventory_v1.param(
56 'Exclude showing all default fields, Set "true" to enable.',
58 class OcloudsListRouter(Resource):
59 """Ocloud get endpoint
60 O2 interface ocloud endpoint
63 ocloud_get = OcloudDTO.ocloud
65 @api_ims_inventory_v1.marshal_with(ocloud_get)
67 res = ocloud_view.oclouds(bus.uow)
70 api_ims_inventory_v1.abort(
71 404, "oCloud doesn't exist")
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.' +
79 _in='query', default=1)
80 @api_ims_inventory_v1.param(
82 'Set any value for show all fields. This value will cover "fields" ' +
85 @api_ims_inventory_v1.param(
87 'Set fields to show, split by comman, "/" for parent and children.' +
88 ' Like "name,parent/children". This value will cover "exculde_fields".',
90 @api_ims_inventory_v1.param(
92 'Set fields to exclude showing, split by comman, "/" for parent and ' +
93 'children. Like "name,parent/children". This value will cover ' +
96 @api_ims_inventory_v1.param(
98 'Exclude showing all default fields, Set "true" to enable.',
100 class ResourceTypesListRouter(Resource):
102 model = ResourceTypeDTO.resource_type_get
104 @api_ims_inventory_v1.marshal_list_with(model)
106 parser = reqparse.RequestParser()
107 parser.add_argument(PAGE_PARAM, location='args')
108 args = parser.parse_args()
110 if args.nextpage_opaque_marker is not None:
111 kwargs['page'] = args.nextpage_opaque_marker
113 ret = ocloud_view.resource_types(bus.uow, **kwargs)
114 return link_header(request.full_path, ret)
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(
122 'Set any value for show all fields. This value will cover "fields" ' +
125 @api_ims_inventory_v1.param(
127 'Set fields to show, split by comman, "/" for parent and children.' +
128 ' Like "name,parent/children". This value will cover "exculde_fields".',
130 @api_ims_inventory_v1.param(
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".',
136 @api_ims_inventory_v1.param(
138 'Exclude showing all default fields, Set "true" to enable.',
140 class ResourceTypeGetRouter(Resource):
142 model = ResourceTypeDTO.resource_type_get
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:
150 api_ims_inventory_v1.abort(
151 404, "Resource type {} doesn't exist".format(resourceTypeID))
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.' +
159 _in='query', default=1)
160 @api_ims_inventory_v1.param(
162 'Set any value for show all fields. This value will cover "fields" ' +
165 @api_ims_inventory_v1.param(
167 'Set fields to show, split by comman, "/" for parent and children.' +
168 ' Like "name,parent/children". This value will cover "exculde_fields".',
170 @api_ims_inventory_v1.param(
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".',
176 @api_ims_inventory_v1.param(
178 'Exclude showing all default fields, Set "true" to enable.',
180 class ResourcePoolsListRouter(Resource):
182 model = ResourcePoolDTO.resource_pool_get
184 @api_ims_inventory_v1.marshal_list_with(model)
186 parser = reqparse.RequestParser()
187 parser.add_argument(PAGE_PARAM, location='args')
188 args = parser.parse_args()
190 if args.nextpage_opaque_marker is not None:
191 kwargs['page'] = args.nextpage_opaque_marker
193 ret = ocloud_view.resource_pools(bus.uow, **kwargs)
194 return link_header(request.full_path, ret)
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(
202 'Set any value for show all fields. This value will cover "fields" ' +
205 @api_ims_inventory_v1.param(
207 'Set fields to show, split by comman, "/" for parent and children.' +
208 ' Like "name,parent/children". This value will cover "exculde_fields".',
210 @api_ims_inventory_v1.param(
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".',
216 @api_ims_inventory_v1.param(
218 'Exclude showing all default fields, Set "true" to enable.',
220 class ResourcePoolGetRouter(Resource):
222 model = ResourcePoolDTO.resource_pool_get
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:
230 api_ims_inventory_v1.abort(
231 404, "Resource pool {} doesn't exist".format(resourcePoolID))
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',
239 @api_ims_inventory_v1.param('parentId', 'filter parentId',
241 # @api_ims_inventory_v1.param('sort', 'sort by column name',
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.' +
249 _in='query', default=1)
250 @api_ims_inventory_v1.param(
252 'Set any value for show all fields. This value will cover "fields" ' +
255 @api_ims_inventory_v1.param(
257 'Set fields to show, split by comman, "/" for parent and children.' +
258 ' Like "name,parent/children". This value will cover "exculde_fields".',
260 @api_ims_inventory_v1.param(
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".',
266 @api_ims_inventory_v1.param(
268 'Exclude showing all default fields, Set "true" to enable.',
270 class ResourcesListRouter(Resource):
272 model = ResourceDTO.resource_list
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()
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
294 ret = ocloud_view.resources(resourcePoolID, bus.uow, **kwargs)
295 return link_header(request.full_path, ret)
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(
305 'Set any value for show all fields. This value will cover "fields" ' +
308 @api_ims_inventory_v1.param(
310 'Set fields to show, split by comman, "/" for parent and children.' +
311 ' Like "name,parent/children". This value will cover "exculde_fields".',
313 @api_ims_inventory_v1.param(
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".',
319 @api_ims_inventory_v1.param(
321 'Exclude showing all default fields, Set "true" to enable.',
323 class ResourceGetRouter(Resource):
325 # dto = ResourceDTO()
326 # model = dto.get_resource_get()
327 model = ResourceDTO.recursive_resource_mapping()
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:
335 api_ims_inventory_v1.abort(
336 404, "Resource {} doesn't exist".format(resourceID))
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.' +
344 _in='query', default=1)
345 @api_ims_inventory_v1.param(
347 'Set any value for show all fields. This value will cover "fields" ' +
350 @api_ims_inventory_v1.param(
352 'Set fields to show, split by comman, "/" for parent and children.' +
353 ' Like "name,parent/children". This value will cover "exculde_fields".',
355 @api_ims_inventory_v1.param(
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".',
361 @api_ims_inventory_v1.param(
363 'Exclude showing all default fields, Set "true" to enable.',
365 class DeploymentManagersListRouter(Resource):
367 model = DeploymentManagerDTO.deployment_manager_list
369 @api_ims_inventory_v1.marshal_list_with(model)
371 parser = reqparse.RequestParser()
372 parser.add_argument(PAGE_PARAM, location='args')
373 args = parser.parse_args()
375 if args.nextpage_opaque_marker is not None:
376 kwargs['page'] = args.nextpage_opaque_marker
378 ret = ocloud_view.deployment_managers(bus.uow, **kwargs)
379 return link_header(request.full_path, ret)
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"',
387 @api_ims_inventory_v1.response(404, 'Deployment manager not found')
388 @api_ims_inventory_v1.param(
390 'Set any value for show all fields. This value will cover "fields" ' +
393 @api_ims_inventory_v1.param(
395 'Set fields to show, split by comman, "/" for parent and children.' +
396 ' Like "name,parent/children". This value will cover "exculde_fields".',
398 @api_ims_inventory_v1.param(
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".',
404 @api_ims_inventory_v1.param(
406 'Exclude showing all default fields, Set "true" to enable.',
408 class DeploymentManagerGetRouter(Resource):
410 model = DeploymentManagerDTO.deployment_manager_get
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()
419 args.profile if args.profile is not None and args.profile != ''
421 result = ocloud_view.deployment_manager_one(
422 deploymentManagerID, bus.uow, profile)
423 if result is not None:
425 api_ims_inventory_v1.abort(
427 "Deployment manager {} doesn't exist".format(deploymentManagerID))
430 # ---------- Subscriptions ---------- #
431 @api_ims_inventory_v1.route("/subscriptions")
432 class SubscriptionsListRouter(Resource):
434 model = SubscriptionDTO.subscription_get
435 expect = SubscriptionDTO.subscription
436 post_resp = SubscriptionDTO.subscription_post_resp
438 @api_ims_inventory_v1.doc('List subscriptions')
439 @api_ims_inventory_v1.marshal_list_with(model)
440 @api_ims_inventory_v1.param(
442 'Page number of the results to fetch. Default: 1',
443 _in='query', default=1)
444 @api_ims_inventory_v1.param(
446 'Set any value for show all fields. This value will cover "fields" ' +
449 @api_ims_inventory_v1.param(
451 'Set fields to show, split by comman, "/" for parent and children.' +
452 ' Like "name,parent/children". This value will cover' +
453 ' "exculde_fields".',
455 @api_ims_inventory_v1.param(
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".',
461 @api_ims_inventory_v1.param(
463 'Exclude showing all default fields, Set "true" to enable.',
466 parser = reqparse.RequestParser()
467 parser.add_argument(PAGE_PARAM, location='args')
468 args = parser.parse_args()
470 if args.nextpage_opaque_marker is not None:
471 kwargs['page'] = args.nextpage_opaque_marker
473 ret = ocloud_view.subscriptions(bus.uow, **kwargs)
474 return link_header(request.full_path, ret)
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)
480 data = api_ims_inventory_v1.payload
481 result = ocloud_view.subscription_create(data, bus.uow)
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):
490 model = SubscriptionDTO.subscription_get
492 @api_ims_inventory_v1.doc('Get subscription by ID')
493 @api_ims_inventory_v1.marshal_with(model)
494 @api_ims_inventory_v1.param(
496 'Set any value for show all fields. This value will cover "fields" ' +
499 @api_ims_inventory_v1.param(
501 'Set fields to show, split by comman, "/" for parent and children.' +
502 ' Like "name,parent/children". This value will cover' +
503 ' "exculde_fields".',
505 @api_ims_inventory_v1.param(
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".',
511 @api_ims_inventory_v1.param(
513 'Exclude showing all default fields, Set "true" to enable.',
515 def get(self, subscriptionID):
516 result = ocloud_view.subscription_one(
517 subscriptionID, bus.uow)
518 if result is not None:
520 api_ims_inventory_v1.abort(404, "Subscription {} doesn't exist".format(
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)