def DeleteUser(subscriptionId, userId): session = Session() user = AgentUser.GetUser(subscriptionId, userId) session.delete(user) session.commit() session.close() return
def DeleteAdmin(userId): session = Session() admin = AgentUser.GetAdmin(userId) session.delete(admin) session.commit() session.close() return
def Delete(publisherId): session = Session() publisher = Publisher.Get(publisherId) session.delete(publisher) session.commit() session.close() return
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
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
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
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
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
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
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()
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
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()
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
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()
def Get(publisherId): session = Session() publisher = session.query(Publisher).filter_by(PublisherId = publisherId).first() session.close() return publisher
def ListAll(): session = Session() publishers = session.query(Publisher).all() session.close() return publishers
def ListAllBySubscriptionId(subscriptionId): session = Session() users = session.query(AgentUser).filter_by( SubscriptionId=subscriptionId).all() session.close() return users
def Create(publisher): session = Session() session.add(publisher) session.commit() session.close() return
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
def GetUser(subscriptionId, userId): session = Session() users = session.query(AgentUser).filter_by( SubscriptionId=subscriptionId, AADUserId=userId).first() session.close() return users
def ListAllAdmin(): session = Session() users = session.query(AgentUser).filter_by(Role="Admin").all() session.close() return users
def ListAllByWorkspaceName(workspaceName): session = Session() workspace = AMLWorkspace.Get(workspaceName) subscriptions = session.query(APISubscription).filter_by(AMLWorkspaceId = workspace.Id).all() session.close() return subscriptions
def ListAll(): session = Session() subscriptions = session.query(APISubscription).all() session.close() return subscriptions
def GetAdmin(userId): session = Session() users = session.query(AgentUser).filter_by(AADUserId=userId, Role="Admin").first() session.close() return users