Exemplo n.º 1
0
def rollback(provider, profile, json_data, msg):

    # Check the created exceptions in order to determine if is
    # necessary to remove something
    if msg == 'Missing required fields' or msg == 'Invalid version format' \
    or msg == 'The offering already exists':
        return

    # Check files
    dir_name = profile.current_organization.name + '__' + json_data['name'] + '__' + json_data['version']
    path = os.path.join(settings.MEDIA_ROOT, dir_name)

    if os.path.exists(path):
        # Remove all files and directory
        for file_ in os.listdir(path):
            file_path = os.path.join(path, file_)
            os.remove(file_path)

        os.rmdir(path)

    # Check if the offering has been created
    offering = Offering.objects.filter(owner_organization=profile.current_organization, name=json_data['name'], version=json_data['version'])

    remove = False
    if len(offering) > 0:
        offering = offering[0]

        # If the offerings has been created means that the USDL is
        # uploaded in the repository
        if 'offering_description' in json_data:
            remove = True
            url = offering.description_url

        offering.delete()
    else:
        # Check if the usdl was uploaded before the exception
        if 'offerings_description' in json_data:
            repository = Repository.objects.get(name=json_data['repository'])
            repository_adaptor = RepositoryAdaptor(repository.host, 'storeOfferingCollection')
            offering_id = profile.current_organization.name + '__' + json_data['name'] + '__' + json_data['version']

            uploaded = True
            try:
                repository_adaptor.download(name=offering_id, content_type=json_data['offering_description']['content_type'])
            except HTTPError:
                uploaded = False

            if uploaded:
                remove = True
                url = urljoin(repository.host, 'storeOfferingCollection')
                url = urljoin(url, offering_id)

    if remove:
        repository_adaptor = RepositoryAdaptor(url)
        repository_adaptor.delete()
Exemplo n.º 2
0
def delete_offering(offering):
    # If the offering has been purchased it is not deleted
    # it is marked as deleted in order to allow customers that
    # have purchased the offering to install it if needed

    #delete the usdl description from the repository
    if offering.state == 'deleted':
        raise PermissionDenied('The offering is already deleted')

    parsed_url = urlparse(offering.description_url)
    path = parsed_url.path
    host = parsed_url.scheme + '://' + parsed_url.netloc
    path = path.split('/')
    host += '/' + path[1] + '/' + path[2]
    collection = path[3]

    repository_adaptor = RepositoryAdaptor(host, collection)
    repository_adaptor.delete(path[4])

    index_path = os.path.join(settings.BASEDIR, 'wstore')
    index_path = os.path.join(index_path, 'search')
    index_path = os.path.join(index_path, 'indexes')

    se = SearchEngine(index_path)

    if offering.state == 'uploaded':
        _remove_offering(offering, se)
    else:
        offering.state = 'deleted'
        offering.save()

        # Delete the offering from marketplaces
        for market in offering.marketplaces:
            m = Marketplace.objects.get(pk=market)
            market_adaptor = MarketAdaptor(m.host)
            market_adaptor.delete_service(settings.STORE_NAME, offering.name)

        # Update offering indexes
        if not offering.open:
            se.update_index(offering)

        context = Context.objects.all()[0]
        # Check if the offering is in the newest list
        if offering.pk in context.newest:
            # Remove the offering from the newest list
            newest = context.newest

            if len(newest) < 8:
                newest.remove(offering.pk)
            else:
                # Get the 8 newest offerings using the publication date for sorting
                connection = MongoClient()
                db = connection[settings.DATABASES['default']['NAME']]
                offerings = db.wstore_offering
                newest_off = offerings.find({'state': 'published'}).sort('publication_date', -1).limit(8)

                newest = []
                for n in newest_off:
                    newest.append(str(n['_id']))

            context.newest = newest
            context.save()

        # Check if the offering is in the top rated list
        if offering.pk in context.top_rated:
            # Remove the offering from the top rated list
            top_rated = context.top_rated
            if len(top_rated) < 8:
                top_rated.remove(offering.pk)
            else:
                # Get the 4 top rated offerings
                connection = MongoClient()
                db = connection[settings.DATABASES['default']['NAME']]
                offerings = db.wstore_offering
                top_off = offerings.find({'state': 'published', 'rating': {'$gt': 0}}).sort('rating', -1).limit(8)

                top_rated = []
                for t in top_off:
                    top_rated.append(str(t['_id']))

            context.top_rated = top_rated
            context.save()

        if offering.open:
            _remove_offering(offering, se)
Exemplo n.º 3
0
def rollback(provider, profile, json_data, msg):

    # Check the created exceptions in order to determine if is
    # necessary to remove something
    if msg == "Missing required fields" or msg == "Invalid version format" or msg == "The offering already exists":
        return

    # Check files
    dir_name = profile.current_organization.name + "__" + json_data["name"] + "__" + json_data["version"]
    path = os.path.join(settings.MEDIA_ROOT, dir_name)

    if os.path.exists(path):
        # Remove all files and directory
        for file_ in os.listdir(path):
            file_path = os.path.join(path, file_)
            os.remove(file_path)

        os.rmdir(path)

    # Check if the offering has been created
    offering = Offering.objects.filter(
        owner_organization=profile.current_organization, name=json_data["name"], version=json_data["version"]
    )

    remove = False
    if len(offering) > 0:
        offering = offering[0]

        # If the offerings has been created means that the USDL is
        # uploaded in the repository
        if "offering_description" in json_data:
            remove = True
            url = offering.description_url

        # Check if the offering has been bound
        if len(offering.resources):
            for res in offering.resources:
                re = Resource.objects.get(pk=unicode(res))
                re.offerings.remove(offering.pk)
                re.save()

        offering.delete()
    else:
        # Check if the usdl was uploaded before the exception
        if "offerings_description" in json_data:
            repository = Repository.objects.get(name=json_data["repository"])
            repository_adaptor = RepositoryAdaptor(repository.host, "storeOfferingCollection")
            offering_id = profile.current_organization.name + "__" + json_data["name"] + "__" + json_data["version"]

            uploaded = True
            try:
                repository_adaptor.download(
                    name=offering_id, content_type=json_data["offering_description"]["content_type"]
                )
            except HTTPError:
                uploaded = False

            if uploaded:
                remove = True
                url = urljoin(repository.host, "storeOfferingCollection")
                url = urljoin(url, offering_id)

    if remove:
        repository_adaptor = RepositoryAdaptor(url)
        repository_adaptor.delete()