Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
def getSubscriptionAPIVersionAndWorkspace(subscriptionId, apiVersion):
    sub = APISubscription.Get(subscriptionId)
    version = APIVersion.Get(sub.ProductName, sub.DeploymentName, apiVersion,
                             sub.PublisherId)
    if os.environ["AGENT_MODE"] == "SAAS":
        workspace = AMLWorkspace.GetByIdWithSecrets(version.AMLWorkspaceId)
    elif os.environ["AGENT_MODE"] == "LOCAL":
        workspace = AMLWorkspace.GetByIdWithSecrets(sub.AMLWorkspaceId)

    return sub, version, workspace
Esempio n. 5
0
 def UpdateMetadataDatabase(self):
     publishers = Publisher.ListAll()
     for publisher in publishers:
         subscriptions = self.GetAgentSubscriptionsFromControlPlane(publisher.ControlPlaneUrl)
         APISubscription.MergeWithDelete(subscriptions, publisher.PublisherId)
         apiVersions = self.GetAgentAPIVersionsFromControlPlane(publisher.ControlPlaneUrl)
         APIVersion.MergeWithDelete(apiVersions, publisher.PublisherId)
         offers = self.GetAgentOffersFromControlPlane(publisher.ControlPlaneUrl)
         Offer.MergeWithDelete(offers, publisher.PublisherId)
     return
Esempio n. 6
0
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)
Esempio n. 7
0
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