Source

cloudster / api / shelf / configuration / accounts.py

Full commit
Guillermo Szelig… cf1fe10 

Guillermo Szelig… 313e7f5 
Guillermo Szelig… cf1fe10 
Guillermo Szelig… 277c4b1 
Guillermo Szelig… cf1fe10 

Guillermo Szelig… 6af9684 
Guillermo Szelig… 313e7f5 

Guillermo Szelig… 6af9684 
Guillermo Szelig… 313e7f5 
Guillermo Szelig… 6af9684 




Guillermo Szelig… 277c4b1 
Guillermo Szelig… 6af9684 
Guillermo Szelig… 00abda6 

Guillermo Szelig… 6af9684 

Guillermo Szelig… 313e7f5 
Guillermo Szelig… 6af9684 

Guillermo Szelig… 8704c53 


Guillermo Szelig… cf1fe10 
Guillermo Szelig… 6af9684 
Guillermo Szelig… cf1fe10 
















Guillermo Szelig… 277c4b1 



Guillermo Szelig… 6af9684 
Guillermo Szelig… cf1fe10 


Guillermo Szelig… e2e590e 



Guillermo Szelig… 313e7f5 

Guillermo Szelig… 277c4b1 
Guillermo Szelig… e2e590e 
Guillermo Szelig… 277c4b1 
Guillermo Szelig… e2e590e 
Guillermo Szelig… 6af9684 
Guillermo Szelig… e2e590e 
Guillermo Szelig… 277c4b1 


Guillermo Szelig… de79bd5 

Guillermo Szelig… 277c4b1 
Guillermo Szelig… de79bd5 
Guillermo Szelig… 277c4b1 
Guillermo Szelig… de79bd5 





Guillermo Szelig… e2e590e 







__author__ = 'guillermo'

import os
import sys
import re
import simplejson as json

from api.shelf.registry.clients import StorageClientsRegistry
from api.shelf.configuration import config



class AccountsManager():

    def __init__(self, client_class):
        self.__client_class = client_class
        self.__account_type_class = self.__client_class.registry_metadata().account_type_class()
        self.__config_manager = config.ConfigurationManager()

    def get_configuration_descriptor(self):
        return self.__account_type_class.get_configuration_fields()

    def create(self, account_config_values):
        return self.__account_type_class(json.dumps(account_config_values))

    def authorize(self, account_data):
        client = self.__client_class(account_data)
        client.grant_client_access()

        return client,account_data

    def load(self, unique_account_id):

        filename = self.__build_account_filename(unique_account_id)
        full_account_file_path = os.path.join(config.ConfigurationManager.APP_INSTANCES_PATH, filename)

        if os.path.exists(full_account_file_path):
            account_file = open(full_account_file_path,'r')
            account_file_content = account_file.read()

            account_file.close()

            return self.__account_type_class(account_file_content)
        else:
            #TODO Use a more specific exception
            raise RuntimeError('Account file %s not found' % filename)

    def connect(self, account_data):
        client = self.__client_class(account_data)
        client.connect()

        return client

    def __build_account_filename(self, unique_account_id):
        return self.__client_class.registry_metadata().unique_id() + "_" + unique_account_id + ".json"

    def __build_account_filepath(self, unique_account_id):
        filename= self.__build_account_filename(unique_account_id)
        return os.path.join(config.ConfigurationManager.APP_INSTANCES_PATH, filename)

    def save(self, account_data):

        self.__config_manager.settleIfNecessary()
        account_data.save(self.__build_account_filepath(account_data.unique_id()))

    def delete(self, unique_account_id):

        os.remove(self.__build_account_filepath(unique_account_id))

    def list(self):

        name_expression = r"^"+ self.__client_class.registry_metadata().unique_id() + "_(\w*).json$"
        name_expression_compiled = re.compile(name_expression)

        account_file_names = self.__config_manager.list_defined_instances(name_expression_compiled)

        account_ids = []

        for account_name in account_file_names:
            result = re.match(name_expression_compiled,account_name)
            account_ids.append(result.group(1))

        return account_ids

    def exists(self,unique_account_id):

        try:
            self.list().index(unique_account_id)
            return True
        except ValueError:
            return False