1+ # Licensed to the Apache Software Foundation (ASF) under one
2+ # or more contributor license agreements. See the NOTICE file
3+ # distributed with this work for additional information
4+ # regarding copyright ownership. The ASF licenses this file
5+ # to you under the Apache License, Version 2.0 (the
6+ # "License"); you may not use this file except in compliance
7+ # with the License. You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing,
12+ # software distributed under the License is distributed on an
13+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+ # KIND, either express or implied. See the License for the
15+ # specific language governing permissions and limitations
16+ # under the License.
17+ #
18+ from openserverless import app
19+ from http import HTTPStatus
20+ from flask import request
21+
22+ import openserverless .common .response_builder as res_builder
23+ from openserverless .common .utils import env_to_dict
24+ from openserverless .error .api_error import AuthorizationError
25+ from openserverless .impl .builder .build_service import BuildService
26+ from openserverless .common .openwhisk_authorize import OpenwhiskAuthorize
27+
28+ @app .route ('/system/api/v1/build' , methods = ['POST' ])
29+ def build ():
30+ """
31+ Build Endpoint
32+ ---
33+ tags:
34+ - Build
35+ summary: Build an image using the provided source, target, and kind.
36+ description: This endpoint triggers a build process based on the provided parameters.
37+ operationId: buildImage
38+ security:
39+ - openwhiskBasicAuth: []
40+ consumes:
41+ - application/json
42+ parameters:
43+ - in: body
44+ name: BuildRequest
45+ required: true
46+ schema:
47+ type: object
48+ properties:
49+ source:
50+ type: string
51+ description: Source for the build
52+ target:
53+ type: string
54+ description: Target for the build
55+ kind:
56+ type: string
57+ description: Kind of the build
58+ responses:
59+ 200:
60+ description: Build process initiated successfully.
61+ schema:
62+ $ref: '#/definitions/Message'
63+ 400:
64+ description: Bad Request. Missing or invalid parameters.
65+ schema:
66+ $ref: '#/definitions/Message'
67+ 401:
68+ description: Unauthorized. Invalid or missing authorization header.
69+ schema:
70+ $ref: '#/definitions/Message'
71+ 500:
72+ description: Internal Server Error. Build process failed.
73+ schema:
74+ $ref: '#/definitions/Message'
75+ """
76+
77+ normalized_headers = {key .lower (): value for key , value in request .headers .items ()}
78+ auth_header = normalized_headers .get ('authorization' , None )
79+
80+ if auth_header is None :
81+ return res_builder .build_error_message ("Missing authorization header" , 401 )
82+
83+ oa = OpenwhiskAuthorize ()
84+ try :
85+ user_data = oa .login (auth_header )
86+ env = env_to_dict (user_data )
87+ if env is None :
88+ return res_builder .build_error_message ("User environment not found" , status_code = HTTPStatus .UNAUTHORIZED )
89+
90+ if (request .json is None ):
91+ return res_builder .build_error_message ("No JSON payload provided for build." , status_code = HTTPStatus .BAD_REQUEST )
92+
93+ json_data = request .json
94+ if 'source' not in json_data :
95+ return res_builder .build_error_message ("No source provided for build." , status_code = HTTPStatus .BAD_REQUEST )
96+ if 'target' not in json_data :
97+ return res_builder .build_error_message ("No target provided for build." , status_code = HTTPStatus .BAD_REQUEST )
98+ if 'kind' not in json_data :
99+ return res_builder .build_error_message ("No kind provided for build." , status_code = HTTPStatus .BAD_REQUEST )
100+
101+
102+ # validate the target
103+ target = json_data .get ('target' )
104+ target_user = str (target ).split (':' )[0 ]
105+ if user_data .get ('login' ) != target_user :
106+ return res_builder .build_error_message ("Invalid target for the build." , status_code = HTTPStatus .BAD_REQUEST )
107+
108+
109+ build_service = BuildService (build_config = json_data , user_env = env )
110+ build_success = build_service .build (json_data .get ('target' )) # Replace with your desired image name
111+
112+ if not build_success :
113+ return res_builder .build_error_message ("Build process failed." , status_code = HTTPStatus .INTERNAL_SERVER_ERROR )
114+
115+ return res_builder .build_response_message ("Build process initiated successfully." , status_code = HTTPStatus .OK )
116+
117+ except AuthorizationError :
118+ return res_builder .build_error_message ("Invalid authorization" , 401 )
0 commit comments