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 """Host Info APIs implemented using Google Cloud Endpoints.""" 15 16 import datetime 17 import endpoints 18 19 from protorpc import remote 20 21 from google.appengine.api import users 22 23 from webapp.src import vtslab_status as Status 24 from webapp.src.proto import model 25 26 HOST_INFO_RESOURCE = endpoints.ResourceContainer(model.HostInfoMessage) 27 28 # Product type name for null device. 29 _NULL_DEVICE_PRODUCT_TYPE = "null" 30 31 32 def AddNullDevices(hostname, null_device_count): 33 """Adds null devices to DeviceModel data store. 34 35 Args: 36 hostname: string, the host name. 37 null_device_count: integer, the number of null devices. 38 """ 39 device_query = model.DeviceModel.query( 40 model.DeviceModel.hostname == hostname, 41 model.DeviceModel.product == _NULL_DEVICE_PRODUCT_TYPE 42 ) 43 null_devices = device_query.fetch() 44 existing_null_device_count = len(null_devices) 45 46 if existing_null_device_count < null_device_count: 47 for _ in range(null_device_count - existing_null_device_count): 48 device = model.DeviceModel() 49 device.hostname = hostname 50 device.serial = "n/a" 51 device.product = _NULL_DEVICE_PRODUCT_TYPE 52 device.status = Status.DEVICE_STATUS_DICT["ready"] 53 device.scheduling_status = Status.DEVICE_SCHEDULING_STATUS_DICT[ 54 "free"] 55 device.timestamp = datetime.datetime.now() 56 device.put() 57 58 59 @endpoints.api(name='host_info', version='v1') 60 class HostInfoApi(remote.Service): 61 """Endpoint API for host_info.""" 62 63 @endpoints.method( 64 HOST_INFO_RESOURCE, 65 model.DefaultResponse, 66 path='set', 67 http_method='POST', 68 name='set') 69 def set(self, request): 70 """Sets the host info based on the `request`.""" 71 if users.get_current_user(): 72 username = users.get_current_user().email() 73 else: 74 username = "anonymous" 75 76 for request_device in request.devices: 77 device_query = model.DeviceModel.query( 78 model.DeviceModel.serial == request_device.serial 79 ) 80 existing_device = device_query.fetch() 81 if existing_device: 82 device = existing_device[0] 83 else: 84 device = model.DeviceModel() 85 device.serial = request_device.serial 86 device.scheduling_status = Status.DEVICE_SCHEDULING_STATUS_DICT[ 87 "free"] 88 device.username = username 89 device.hostname = request.hostname 90 device.product = request_device.product 91 device.status = request_device.status 92 device.timestamp = datetime.datetime.now() 93 device.put() 94 95 return model.DefaultResponse( 96 return_code=model.ReturnCodeMessage.SUCCESS) 97