Exemple #1
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()
    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()
Exemple #3
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()