1 # Copyright 2017 Google Inc. All rights reserved. 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 """Build Info APIs implemented using Google Cloud Endpoints.""" 16 17 import datetime 18 import endpoints 19 import logging 20 21 from protorpc import remote 22 23 from webapp.src.proto import model 24 25 26 BUILD_INFO_RESOURCE = endpoints.ResourceContainer( 27 model.BuildInfoMessage) 28 29 30 @endpoints.api(name="build_info", version="v1") 31 class BuildInfoApi(remote.Service): 32 """Endpoint API for build_info.""" 33 34 @endpoints.method( 35 BUILD_INFO_RESOURCE, 36 model.DefaultResponse, 37 path="set", 38 http_method="POST", 39 name="set") 40 def set(self, request): 41 """Sets the build info based on the `request`.""" 42 build_query = model.BuildModel.query( 43 model.BuildModel.build_id == request.build_id, 44 model.BuildModel.build_target == request.build_target, 45 model.BuildModel.build_type == request.build_type 46 ) 47 existing_builds = build_query.fetch() 48 49 if existing_builds and len(existing_builds) > 1: 50 logging.warning("Duplicated builds found for [build_id]{} " 51 "[build_target]{} [build_type]{}".format( 52 request.build_id, request.build_target, request.build_type)) 53 54 if request.signed and existing_builds: 55 # only signed builds need to overwrite the exist entities. 56 build = existing_builds[0] 57 elif not existing_builds: 58 build = model.BuildModel() 59 else: 60 # the same build existed and request is not signed build. 61 build = None 62 63 if build: 64 build.manifest_branch = request.manifest_branch 65 build.build_id = request.build_id 66 build.build_target = request.build_target 67 build.build_type = request.build_type 68 build.artifact_type = request.artifact_type 69 build.artifacts = request.artifacts 70 build.timestamp = datetime.datetime.now() 71 build.signed = request.signed 72 build.put() 73 74 return model.DefaultResponse( 75 return_code=model.ReturnCodeMessage.SUCCESS) 76