ns = params_kw.copy() for k,v in list(ns.items()): if v == 'NaN' or v == 'null': ns[k] = None if k.endswith('_text'): ns.pop(k, None) id = params_kw.get('id','') if not id or len(str(id)) > 32: id = uuid() ns['id'] = id userorgid = await get_userorgid() if not userorgid: return {'widgettype':'Error','options':{'title':'Authorization Error','timeout':3,'cwidth':16,'cheight':9,'message':'Please login'}} cluster_id = ns.get('cluster_id','') node_id = ns.get('node_id','') role = ns.get('role','') if not cluster_id: return {'widgettype':'Error','options':{'title':'Error','message':'缺少集群ID','cwidth':16,'cheight':9,'timeout':3}} if not node_id: return {'widgettype':'Error','options':{'title':'Error','message':'缺少节点ID','cwidth':16,'cheight':9,'timeout':3}} if role not in ('control','compute','storage'): return {'widgettype':'Error','options':{'title':'Error','message':'角色必须是 control/compute/storage','cwidth':16,'cheight':9,'timeout':3}} async with DBPools().sqlorContext(get_module_dbname('pcc')) as sor: # 校验节点是否已被其他集群占用 existing = await sor.R('cluster_node', {'node_id': node_id}) if existing: return {'widgettype':'Error','options':{'title':'Error','message':'该节点已被集群占用,不可重复添加','cwidth':16,'cheight':9,'timeout':3}} # 校验节点是否属于该集群的算力池 cluster = await sor.R('cluster', {'id': cluster_id}) if cluster: pool_id = getattr(cluster[0], 'pool_id', '') node = await sor.R('compute_node', {'id': node_id}) if node and pool_id and getattr(node[0], 'pool_id', '') != pool_id: return {'widgettype':'Error','options':{'title':'Error','message':'节点不属于该集群的算力池','cwidth':16,'cheight':9,'timeout':3}} ns['assigned_at'] = datetime.datetime.now().isoformat() ns.pop('allocated_at', None) await sor.C('cluster_node', ns) return {'widgettype':'Message','options':{'cwidth':16,'cheight':9,'title':'Success','timeout':3,'message':'ok'}}