Revision 9393d4ee

b/snf-cyclades-app/synnefo/plankton/backend.py
54 54
import json
55 55
import warnings
56 56
import logging
57
import os
58

  
57 59
from time import gmtime, strftime
58 60
from functools import wraps
59 61
from operator import itemgetter
60 62

  
61 63
from django.conf import settings
64
from django.utils import importlib
62 65
from pithos.backends.base import NotAllowedError, VersionNotExists
63 66

  
64 67
logger = logging.getLogger(__name__)
......
468 471
                image[key] = val
469 472

  
470 473
    return image
474

  
475

  
476
class JSONFileBackend(object):
477
    """
478
    A dummy image backend that loads available images from a file with json
479
    formatted content.
480

  
481
    usage:
482
        PLANKTON_BACKEND_MODULE = 'synnefo.plankton.backend.JSONFileBackend'
483
        PLANKTON_IMAGES_JSON_BACKEND_FILE = '/tmp/images.json'
484

  
485
        # loading images from an existing plankton service
486
        $ curl -H "X-Auth-Token: <MYTOKEN>" \
487
                https://cyclades.synnefo.org/plankton/images/detail | \
488
                python -m json.tool > /tmp/images.json
489
    """
490
    def __init__(self, userid):
491
        self.images_file = getattr(settings,
492
                                   'PLANKTON_IMAGES_JSON_BACKEND_FILE', '')
493
        if not os.path.exists(self.images_file):
494
            raise Exception("Invalid plankgon images json backend file: %s",
495
                            self.images_file)
496
        fp = file(self.images_file)
497
        self.images = json.load(fp)
498
        fp.close()
499

  
500
    def iter(self, *args, **kwargs):
501
        return self.images.__iter__()
502

  
503
    def list_images(self, *args, **kwargs):
504
        return self.images
505

  
506
    def get_image(self, image_uuid):
507
        try:
508
            return filter(lambda i: i['id'] == image_uuid, self.images)[0]
509
        except IndexError:
510
            raise Exception("Unknown image uuid: %s" % image_uuid)
511

  
512
    def close(self):
513
        pass
514

  
515

  
516
def get_backend():
517
    backend_module = getattr(settings, 'PLANKTON_BACKEND_MODULE', None)
518
    if not backend_module:
519
        # no setting set
520
        return ImageBackend
521

  
522
    parts = backend_module.split(".")
523
    module = ".".join(parts[:-1])
524
    cls = parts[-1]
525
    try:
526
        return getattr(importlib.import_module(module), cls)
527
    except (ImportError, AttributeError), e:
528
        raise ImportError("Cannot import plankton module: %s (%s)" %
529
                          (backend_module, e.message))
b/snf-cyclades-app/synnefo/plankton/utils.py
45 45
    erros to cloud faults.
46 46

  
47 47
    """
48
    image_backend = backend.ImageBackend(user_id)
48
    image_backend = backend.get_backend()(user_id)
49 49
    try:
50 50
        yield image_backend
51 51
    except backend.Forbidden:

Also available in: Unified diff