Enhance: Enable O2 DMS by exposing k8s API endpoint
[pti/o2.git] / o2dms / api / dms_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 jsonify
16 from os.path import exists
17 from flask import send_file
18 from flask_restx import Resource, reqparse
19
20 from o2dms.api.dms_dto import DmsDTO
21 from o2dms.api import dms_lcm_view
22 from o2dms.api.dms_api_ns import api_dms_lcm_v1
23
24 from o2common.service.messagebus import MessageBus
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28
29 def configure_api_route():
30     pass
31
32
33 # ----------  DeploymentManagers ---------- #
34 @api_dms_lcm_v1.route("/<deploymentManagerID>")
35 @api_dms_lcm_v1.param('deploymentManagerID', 'ID of the deployment manager')
36 @api_dms_lcm_v1.param('profile', 'DMS profile',
37                       location='args')
38 @api_dms_lcm_v1.response(404, 'Deployment manager not found')
39 class DmsGetRouter(Resource):
40
41     model = DmsDTO.dms_get
42
43     @api_dms_lcm_v1.doc('Get deployment manager')
44     @api_dms_lcm_v1.marshal_with(model)
45     def get(self, deploymentManagerID):
46         logger.debug("get o2dms info:{}".format(
47             deploymentManagerID
48         ))
49         bus = MessageBus.get_instance()
50
51         parser = reqparse.RequestParser()
52         parser.add_argument('profile', location='args')
53         args = parser.parse_args()
54
55         result = dms_lcm_view.deployment_manager_one(
56             deploymentManagerID, bus.uow, args.profile)
57         if result is not None:
58             return result
59         api_dms_lcm_v1.abort(404, "Deployment manager {} doesn't exist".format(
60             deploymentManagerID))
61
62
63 @api_dms_lcm_v1.route("/<deploymentManagerID>/download/<filename>")
64 @api_dms_lcm_v1.param('deploymentManagerID',
65                       'ID of the deployment manager')
66 @api_dms_lcm_v1.param('filename',
67                       'profile filename')
68 @api_dms_lcm_v1.response(404, 'profile not found')
69 class DeploymentManagerGetFileRouter(Resource):
70     def get(self, deploymentManagerID, filename):
71         path = "/tmp/kubeconfig_" + filename
72
73         if exists(path):
74             return send_file(path, as_attachment=True)
75         api_dms_lcm_v1.abort(
76             404,
77             "Deployment manager {}'s Kube config file doesn't exist".
78             format(deploymentManagerID))