def listAllAdmins(): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) admins = AgentUser.ListAllAdmin() return jsonify(admins), 200 except Exception as e: return handleExceptions(e)
def listAllAMLWorkspaces(): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) workspaces = AMLWorkspace.ListAll() return jsonify(workspaces), 200 except Exception as e: return handleExceptions(e)
def listAllPublishers(): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) publishers = Publisher.ListAll() return jsonify(publishers), 200 except Exception as e: return handleExceptions(e)
def getSubscriptionUser(subscriptionId, userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) user = AgentUser.GetUser(subscriptionId, userId) return jsonify(user), 200 except Exception as e: return handleExceptions(e)
def removeAdmin(userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) objectId = AuthenticationHelper.GetUserObjectId(getToken()) admin = AgentUser.GetAdmin(userId) if not admin: return "The admin with user id {userId} doesn't exist.".format( userId=userId), 404 if admin.ObjectId.lower() == objectId: raise LunaUserException( HTTPStatus.CONFLICT, "Admin cannot remove themselves from Admin list.") AgentUser.DeleteAdmin(userId) return jsonify({}), 204 except Exception as e: return handleExceptions(e)
def listAllSubscriptionUsers(subscriptionId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) users = AgentUser.ListAllBySubscriptionId(subscriptionId) return jsonify(users), 200 except Exception as e: return handleExceptions(e)
def getSubscription(subscriptionId): try: objectId = AuthenticationHelper.ValidateSignitureAndUser( getToken(), subscriptionId) subscription = APISubscription.Get(subscriptionId, objectId) return jsonify(subscription), 200 except Exception as e: return handleExceptions(e)
def createOrUpdateSubscription(subscriptionId): """ TODO: do we need this API? """ try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) subscription = APISubscription(**request.json) APISubscription.Update(subscription) return jsonify(request.json), 202 except Exception as e: return handleExceptions(e)
def refreshMetadata(): try: app.logger.info(getToken()) AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) controlPlane = ControlPlane(os.environ['AGENT_ID'], os.environ['AGENT_KEY']) controlPlane.UpdateMetadataDatabase() return "The metadata database is refreshed", 200 except Exception as e: return handleExceptions(e)
def removePublisher(publisherId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) if not Publisher.Get(publisherId): return "The publisher with id {publisherId} doesn't exist.".format( publisherId=publisherId), 404 Publisher.Delete(publisherId) return jsonify({}), 204 except Exception as e: return handleExceptions(e)
def getPublisher(publisherId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) publisher = Publisher.Get(publisherId) if not publisher: return "The publisher with id {publisherId} doesn't exist.".format( publisherId=publisherId), 404 return jsonify(publisher), 200 except Exception as e: return handleExceptions(e)
def listAllSubscriptions(): try: objectId = AuthenticationHelper.ValidateSignitureAndUser(getToken()) if objectId == "Admin": subscriptions = APISubscription.ListAll() else: subscriptions = APISubscription.ListAllByUserObjectId(objectId) return jsonify(subscriptions), 200 except Exception as e: return handleExceptions(e)
def getAdmin(userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) admin = AgentUser.GetAdmin(userId) if not admin: return "The admin with user id {userId} doesn't exist.".format( userId=userId), 404 return jsonify(admin), 200 except Exception as e: return handleExceptions(e)
def removeSubscriptionUser(subscriptionId, userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) if not AgentUser.GetUser(subscriptionId, userId): return "The user with user id {userId} doesn't exist in subscription {subscriptionId}".format( userId=userId, subscriptionId=subscriptionId), 404 AgentUser.DeleteUser(subscriptionId, userId) return jsonify({}), 204 except Exception as e: return handleExceptions(e)
def getAMLWorkspace(workspaceName): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) workspace = AMLWorkspace.Get(workspaceName) if workspace: return jsonify(workspace), 200 else: return "Can not find the workspace with name {}".format( workspaceName), 404 except Exception as e: return handleExceptions(e)
def createOrUpdateAMLWorkspace(workspaceName): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) workspace = AMLWorkspace(**request.json) if workspaceName != workspace.WorkspaceName: return "The workspace name in request body doesn't match the workspace name in request url.", 400 if AMLWorkspace.Exist(workspaceName): AMLWorkspace.Update(workspace) return jsonify(request.json), 200 else: AMLWorkspace.Create(workspace) return jsonify(request.json), 202 except Exception as e: return handleExceptions(e)
def deleteAMLWorkspace(workspaceName): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) if not AMLWorkspace.Exist(workspaceName): return "Workspace with name {} doesn't exist.".format( workspaceName), 404 if len(APISubscription.ListAllByWorkspaceName(workspaceName)) != 0: return "The workspace {} is still being used by API subscription. Reconfigure the subscriptions before deleting the workspace.".format( workspaceName), 409 AMLWorkspace.Delete(workspaceName) return jsonify({}), 204 except Exception as e: return handleExceptions(e)
def addPublisher(publisherId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) publisher = Publisher(**request.json) if publisherId != publisher.PublisherId: return "The id in request body doesn't match the publisher id in request url.", 400 if Publisher.Get(publisherId): Publisher.Update(publisherId, publisher) else: Publisher.Create(publisher) return jsonify(request.json), 202 except Exception as e: return handleExceptions(e)
def getAgentInfo(): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) info = { "AgentId": os.environ['AGENT_ID'], "AgentKey": os.environ['AGENT_KEY'], "AgentAPIEndpoint": os.environ['AGENT_API_ENDPOINT'], "AgentAPIConnectionString": "{}:{}@{}".format(os.environ['AGENT_ID'], os.environ['AGENT_KEY'], os.environ['AGENT_API_ENDPOINT']) } return jsonify(info), 200 except Exception as e: return handleExceptions(e)
def addSubscriptionUser(subscriptionId, userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) if AgentUser.GetUser(subscriptionId, userId): return "The user with user id {userId} already exists in subscription {subscriptionId}".format( userId=userId, subscriptionId=subscriptionId), 409 if "ObjectId" not in request.json: raise LunaUserException(HTTPStatus.BAD_REQUEST, "The object id is required") user = AgentUser(**request.json) if subscriptionId != user.SubscriptionId: return "The subscription id in request body doesn't match the subscription id in request url.", 400 if userId != user.AADUserId: return "The user id in request body doesn't match the user id in request url.", 400 AgentUser.Create(user) return jsonify(request.json), 202 except Exception as e: return handleExceptions(e)
def addAdmin(userId): try: AuthenticationHelper.ValidateSignitureAndAdmin(getToken()) if AgentUser.GetAdmin(userId): return "The admin with user id {userId} already exists.".format( userId=userId), 409 if "ObjectId" not in request.json: raise LunaUserException(HTTPStatus.BAD_REQUEST, "The object id is required") user = AgentUser(**request.json) if user.Role != "Admin": return "The role of the admin user must be Admin.", 400 if userId != user.AADUserId: return "The user id in request body doesn't match the user id in request url.", 400 AgentUser.Create(user) return jsonify(request.json), 202 except Exception as e: return handleExceptions(e)
def getMetadata(subscriptionId, isRealTimePredict=False): apiVersion = request.args.get('api-version') if not apiVersion: raise LunaUserException( HTTPStatus.BAD_REQUEST, 'The api-version query parameter is not provided.') # Verify key first if api-key is provided. Otherwise, try AAD auth subscriptionKey = request.headers.get('api-key') if subscriptionKey: sub = APISubscription.GetByKey(subscriptionKey) if not sub: raise LunaUserException(HTTPStatus.UNAUTHORIZED, 'The api key is invalid.') if subscriptionId != "default" and subscriptionId.lower( ) != sub.SubscriptionId.lower(): raise LunaUserException( HTTPStatus.UNAUTHORIZED, "The subscription {} doesn't exist or api key is invalid.". format(subscriptionId)) else: objectId = AuthenticationHelper.ValidateSignitureAndUser( getToken(), subscriptionId) sub = APISubscription.Get(subscriptionId, objectId) if not sub: raise LunaUserException( HTTPStatus.NOT_FOUND, 'The subscription {} does not exist.'.format(subscriptionId)) version = APIVersion.Get(sub.ProductName, sub.DeploymentName, apiVersion, sub.PublisherId) if not version: raise LunaUserException( HTTPStatus.NOT_FOUND, 'The api version {} does not exist.'.format(apiVersion)) if isRealTimePredict: if os.environ["AGENT_MODE"] == "LOCAL": raise LunaUserException( HTTPStatus.BAD_REQUEST, 'Cannot call SaaS service from local agent.') if version.AMLWorkspaceId and version.AMLWorkspaceId != 0: workspace = AMLWorkspace.GetByIdWithSecrets(version.AMLWorkspaceId) else: workspace = None else: if os.environ["AGENT_MODE"] == "SAAS": workspace = AMLWorkspace.GetByIdWithSecrets(version.AMLWorkspaceId) elif os.environ["AGENT_MODE"] == "LOCAL": if (not sub.AMLWorkspaceId) or sub.AMLWorkspaceId == 0: raise LunaUserException( HTTPStatus.METHOD_NOT_ALLOWED, 'There is not an Azure Machine Learning workspace configured for this subscription. Please contact your admin to finish the configuration.' .format(version.AMLWorkspaceId)) workspace = AMLWorkspace.GetByIdWithSecrets(sub.AMLWorkspaceId) if not workspace: raise LunaServerException( 'The workspace with id {} is not found.'.format( version.AMLWorkspaceId)) publisher = Publisher.Get(sub.PublisherId) if version.VersionSourceType == 'git': CodeUtils.getLocalCodeFolder( sub.SubscriptionId, sub.ProductName, sub.DeploymentName, version, pathlib.Path(__file__).parent.absolute(), publisher.ControlPlaneUrl) return sub, version, workspace, apiVersion