Exemplo n.º 1
0
 def ListInternalOffers(userId):
     session = Session()
     offers = session.query(Offer).filter_by(OfferType='Internal').all()
     session.close()
     for offer in offers:
         offer.SubscribePageUrl = "{}?token={}".format(
             offer.LandingPageUrl, Offer.GetToken(offer, userId))
     return offers
Exemplo n.º 2
0
 def ListMarketplaceOffers(userId):
     session = Session()
     offers = session.query(Offer).filter_by(OfferType='Marketplace').all()
     session.close()
     for offer in offers:
         offer.SubscribePageUrl = "https://ms.portal.azure.com/#create/{}.{}/preview".format(
             offer.PublisherMicrosoftId, offer.OfferId)
     return offers
Exemplo n.º 3
0
 def Get(productName, deploymentName, versionName, publisherId):
     session = Session()
     version = session.query(APIVersion).filter_by(
         ProductName=productName,
         DeploymentName=deploymentName,
         VersionName=versionName,
         PublisherId=publisherId).first()
     session.close()
     return version
Exemplo n.º 4
0
 def GetByKey(subscriptionKey):
     session = Session()
     secret_name = key_vault_helper.find_secret_name_by_value(subscriptionKey)
     if secret_name:
         subscription = session.query(APISubscription).filter(or_(APISubscription.PrimaryKeySecretName == secret_name, APISubscription.SecondaryKeySecretName == secret_name)).first()            
     else:
         subscription = None
     session.close()
     return subscription
Exemplo n.º 5
0
 def Update(workspace):
     session = Session()
     dbWorkspace = session.query(AMLWorkspace).filter_by(WorkspaceName = workspace.WorkspaceName).first()
     dbWorkspace.AADApplicationId = workspace.AADApplicationId
     if workspace.AADApplicationSecret != "" and workspace.AADApplicationSecret != "notchanged":
         key_vault_helper.set_secret(dbWorkspace.AADApplicationSecretName, workspace.AADApplicationSecret)
     dbWorkspace.AADTenantId = workspace.AADTenantId
     session.commit()
     # update
     return
Exemplo n.º 6
0
 def Delete(publisherId):
     session = Session()
     publisher = Publisher.Get(publisherId)
     session.delete(publisher)
     session.commit()
     session.close()
     return
Exemplo n.º 7
0
 def DeleteUser(subscriptionId, userId):
     session = Session()
     user = AgentUser.GetUser(subscriptionId, userId)
     session.delete(user)
     session.commit()
     session.close()
     return
Exemplo n.º 8
0
 def DeleteAdmin(userId):
     session = Session()
     admin = AgentUser.GetAdmin(userId)
     session.delete(admin)
     session.commit()
     session.close()
     return
Exemplo n.º 9
0
 def Update(publisherId, publisher):
     session = Session()
     dbPublisher = session.query(Publisher).filter_by(PublisherId = publisherId).first()
     dbPublisher.ControlPlaneUrl = publisher.ControlPlaneUrl
     dbPublisher.Name = publisher.Name
     session.commit()
     session.close()
     return publisher.ControlPlaneUrl
Exemplo n.º 10
0
 def Get(workspaceName):
     session = Session()
     workspace = session.query(AMLWorkspace).filter_by(WorkspaceName = workspaceName).first()
     workspace.AADApplicationSecret = key_vault_helper.get_secret(workspace.AADApplicationSecretName)
     util = AzureMLUtils(workspace)
     workspace.ComputeClusters = util.getComputeClusters()
     workspace.DeploymentClusters = util.getDeploymentClusters()
     workspace.DeploymentTargetTypes = [{
             'id': 'aks',
             'displayName': 'Azure Kubernates Service'
         },
         {
             'id': 'aci',
             'displayName': 'Azure Container Instances'
         }]
     ## never return the workspace secret
     workspace.AADApplicationSecret = "notchanged"
     session.close()
     return workspace
Exemplo n.º 11
0
    def Get(subscriptionId, objectId="Admin"):
        """ the function will should only be called in local mode, otherwise, the keys might be out of date! """
        if objectId != "Admin":
            # validate the userId
            users = AgentUser.ListAllBySubscriptionId(subscriptionId)
            if not any(user.ObjectId == objectId for user in users):
                raise LunaUserException(HTTPStatus.FORBIDDEN, "The subscription {} doesn't exist or you don't have permission to access it.".format(subscriptionId))

        session = Session()
        subscription = session.query(APISubscription).filter_by(SubscriptionId = subscriptionId).first()
        session.close()
        if not subscription:
            return None
        subscription.PrimaryKey = key_vault_helper.get_secret(subscription.PrimaryKeySecretName)
        subscription.SecondaryKey = key_vault_helper.get_secret(subscription.SecondaryKeySecretName)
        if os.environ["AGENT_MODE"] == "LOCAL" and objectId == "Admin":
            subscription.Admins = AgentUser.ListAllAdmin()
            subscription.Users = AgentUser.ListAllBySubscriptionId(subscriptionId)
            subscription.AvailablePlans = ["Basic", "Premium"]
        return subscription
Exemplo n.º 12
0
 def Create(workspace):
     session = Session()
     workspace.AADApplicationSecretName = 'aml-{}'.format(uuid.uuid4())
     key_vault_helper.set_secret(workspace.AADApplicationSecretName, workspace.AADApplicationSecret)
     session.add(workspace)
     session.commit()
     return
Exemplo n.º 13
0
 def Update(subscription):
     session = Session()
     dbSubscription = session.query(APISubscription).filter_by(SubscriptionId = subscription.SubscriptionId).first()
     workspace = AMLWorkspace.Get(subscription.AMLWorkspaceName)
     if not workspace:
         raise LunaUserException(HTTPStatus.BAD_REQUEST, "The workspace {} doesn't exist or you don't have permission to access it.".format(subscription.AMLWorkspaceName))
     dbSubscription.AMLWorkspaceId = workspace.Id
     dbSubscription.AMLWorkspaceComputeClusterName = subscription.AMLWorkspaceComputeClusterName
     dbSubscription.AMLWorkspaceDeploymentTargetType = subscription.AMLWorkspaceDeploymentTargetType
     dbSubscription.AMLWorkspaceDeploymentClusterName = subscription.AMLWorkspaceDeploymentClusterName
     session.commit()
     session.close()
     # update
     return
Exemplo n.º 14
0
 def GetByIdWithSecrets(workspaceId):
     session = Session()
     workspace = session.query(AMLWorkspace).filter_by(Id = workspaceId).first()
     workspace.AADApplicationSecret = key_vault_helper.get_secret(workspace.AADApplicationSecretName)
     session.close()
     return workspace
Exemplo n.º 15
0
 def ListAll():
     session = Session()
     subscriptions = session.query(APISubscription).all()
     session.close()
     return subscriptions
Exemplo n.º 16
0
 def ListAllBySubscriptionId(subscriptionId):
     session = Session()
     users = session.query(AgentUser).filter_by(
         SubscriptionId=subscriptionId).all()
     session.close()
     return users
Exemplo n.º 17
0
 def Get(publisherId):
     session = Session()
     publisher = session.query(Publisher).filter_by(PublisherId = publisherId).first()
     session.close()
     return publisher
Exemplo n.º 18
0
 def Delete(workspaceName):
     session = Session()
     workspace = session.query(AMLWorkspace).filter_by(WorkspaceName = workspaceName).first()
     session.delete(workspace)
     session.commit()
     return
Exemplo n.º 19
0
 def GetAdmin(userId):
     session = Session()
     users = session.query(AgentUser).filter_by(AADUserId=userId,
                                                Role="Admin").first()
     session.close()
     return users
Exemplo n.º 20
0
 def ListAllAdmin():
     session = Session()
     users = session.query(AgentUser).filter_by(Role="Admin").all()
     session.close()
     return users
Exemplo n.º 21
0
 def GetUser(subscriptionId, userId):
     session = Session()
     users = session.query(AgentUser).filter_by(
         SubscriptionId=subscriptionId, AADUserId=userId).first()
     session.close()
     return users
Exemplo n.º 22
0
 def Exist(workspaceName):
     session = Session()
     return len(session.query(AMLWorkspace).filter_by(WorkspaceName = workspaceName).all()) > 0
Exemplo n.º 23
0
 def ListAll():
     session = Session()
     workspaces = session.query(AMLWorkspace).all()
     for workspace in workspaces:
         workspace.AADApplicationSecret = ""
     return workspaces
Exemplo n.º 24
0
 def ListAllByWorkspaceName(workspaceName):
     session = Session()
     workspace = AMLWorkspace.Get(workspaceName)
     subscriptions = session.query(APISubscription).filter_by(AMLWorkspaceId = workspace.Id).all()
     session.close()
     return subscriptions
Exemplo n.º 25
0
 def Create(publisher):
     session = Session()
     session.add(publisher)
     session.commit()
     session.close()
     return
Exemplo n.º 26
0
    def MergeWithDelete(subscriptions, publisherId):
        session = Session()
        try:
            dbSubscriptions = session.query(APISubscription).all()
            for dbSubscription in dbSubscriptions:
                if dbSubscription.PublisherId.lower() != publisherId.lower():
                    continue;
                # If the subscription is removed in the control plane, remove it from the agent
                try:
                    next(item for item in subscriptions if item["SubscriptionId"].lower() == dbSubscription.SubscriptionId.lower() and item["PublisherId"].lower() == dbSubscription.PublisherId.lower())
                except StopIteration:
                    session.delete(dbSubscription)

            for subscription in subscriptions:
                dbSubscription = session.query(APISubscription).filter_by(SubscriptionId = subscription["SubscriptionId"]).first()
                if dbSubscription:
                    dbSubscription.DeploymentName = subscription["DeploymentName"]
                    dbSubscription.ProductName = subscription["ProductName"]
                    dbSubscription.ProductType = subscription["ProductType"]
                    dbSubscription.Name = subscription["Name"]
                    dbSubscription.Status = subscription["Status"]
                    dbSubscription.HostType = subscription["HostType"]
                    dbSubscription.BaseUrl = subscription["BaseUrl"]
                    if key_vault_helper.get_secret(dbSubscription.PrimaryKeySecretName) != subscription["PrimaryKey"]:
                        key_vault_helper.set_secret(dbSubscription.PrimaryKeySecretName, subscription["PrimaryKey"])
                    if key_vault_helper.get_secret(dbSubscription.SecondaryKeySecretName) != subscription["SecondaryKey"]:
                        key_vault_helper.set_secret(dbSubscription.SecondaryKeySecretName, subscription["SecondaryKey"])
                else:
                    dbSubscription = APISubscription(**subscription)
                    dbSubscription.PrimaryKeySecretName = 'primarykey-{}'.format(dbSubscription.SubscriptionId)
                    dbSubscription.SecondaryKeySecretName = 'secondarykey-{}'.format(dbSubscription.SubscriptionId)
                    key_vault_helper.set_secret(dbSubscription.PrimaryKeySecretName, dbSubscription.PrimaryKey)
                    key_vault_helper.set_secret(dbSubscription.SecondaryKeySecretName, dbSubscription.SecondaryKey)
                    session.add(dbSubscription)

            session.commit()
        except:
            session.rollback()
            raise

        finally:
            session.close()
Exemplo n.º 27
0
 def ListAll():
     session = Session()
     publishers = session.query(Publisher).all()
     session.close()
     return publishers
Exemplo n.º 28
0
    def MergeWithDelete(apiVersions, publisherId):
        session = Session()
        try:
            dbAPIVersions = session.query(APIVersion).all()
            for dbAPIVersion in dbAPIVersions:
                if dbAPIVersion.PublisherId.lower() != publisherId.lower():
                    continue
                # If the subscription is removed in the control plane, remove it from the agent
                try:
                    next(item for item in apiVersions if
                         item["DeploymentName"] == dbAPIVersion.DeploymentName
                         and item["ProductName"] == dbAPIVersion.ProductName
                         and item["VersionName"] == dbAPIVersion.VersionName
                         and item["PublisherId"].lower() ==
                         dbAPIVersion.PublisherId.lower())
                except StopIteration:
                    session.delete(dbAPIVersion)

            for apiVersion in apiVersions:
                dbAPIVersion = session.query(APIVersion).filter_by(
                    ProductName=apiVersion["ProductName"],
                    DeploymentName=apiVersion["DeploymentName"],
                    VersionName=apiVersion["VersionName"],
                    PublisherId=apiVersion["PublisherId"]).first()
                if dbAPIVersion:
                    dbAPIVersion.LastUpdatedTime = apiVersion[
                        "LastUpdatedTime"]
                    dbAPIVersion.ConfigFile = apiVersion["ConfigFile"]
                else:
                    dbAPIVersion = APIVersion(**apiVersion)
                    session.add(dbAPIVersion)

            session.commit()
        except Exception as e:
            session.rollback()
            raise

        finally:
            session.close()
Exemplo n.º 29
0
    def MergeWithDelete(offers, publisherId):
        session = Session()
        try:
            dbOffers = session.query(Offer).all()
            for dbOffer in dbOffers:
                if dbOffer.PublisherId.lower() != publisherId.lower():
                    continue
                # If the offer is removed in the control plane, remove it from the agent
                try:
                    next(item for item in offers
                         if item["OfferId"] == dbOffer.OfferId
                         and item["PublisherId"].lower() ==
                         dbOffer.PublisherId.lower())
                except StopIteration:
                    session.delete(dbOffer)

            for offer in offers:
                dbOffer = session.query(Offer).filter_by(
                    OfferId=offer["OfferId"],
                    PublisherId=offer["PublisherId"]).first()
                if dbOffer:
                    dbOffer.Description = offer["Description"]

                    if "LogoImageUrl" in offer:
                        dbOffer.LogoImageUrl = offer["LogoImageUrl"]

                    dbOffer.OfferName = offer["OfferName"]
                    if "DocumentationUrl" in offer:
                        dbOffer.DocumentationUrl = offer["DocumentationUrl"]

                    dbOffer.PublisherName = offer["PublisherName"]
                    dbOffer.LandingPageUrl = offer["LandingPageUrl"]
                    dbOffer.LastUpdatedTime = offer["LastUpdatedTime"]
                    dbOffer.CreatedTime = offer["CreatedTime"]
                    if "PublisherMicrosoftId" in offer:
                        dbOffer.PublisherMicrosoftId = offer[
                            "PublisherMicrosoftId"]

                else:
                    dbOffer = Offer(**offer)
                    session.add(dbOffer)

            session.commit()
        except Exception as e:
            session.rollback()
            raise

        finally:
            session.close()
Exemplo n.º 30
0
 def Create(user):
     session = Session()
     session.add(user)
     session.commit()
     return