# Path: rpaas/plan.py
# class InvalidPlanError(Exception):
# class Plan(object):
#     def __init__(self, field):
#     def __unicode__(self):
#     def __init__(self, name, description, config):
#     def validate(self):
#     def to_dict(self):
# 
# Path: rpaas/flavor.py
# class InvalidFlavorError(Exception):
# class Flavor(object):
#     def __init__(self, field):
#     def __unicode__(self):
#     def __init__(self, name, description, config):
#     def validate(self):
#     def to_dict(self):


import datetime
import pymongo.errors
from hm import storage
from rpaas import plan, flavor
    def update_flavor(self, name, description=None, config=None):
        update = {}
        if description:
            update["description"] = description
        if config:
            update["config"] = config
        if update:
            result = self.db[self.flavors_collection].update({"_id": name},
                                                             {"$set": update})
            if not result.get("updatedExisting"):
                raise FlavorNotFoundError()

    def delete_flavor(self, name):
        result = self.db[self.flavors_collection].remove({"_id": name})
        if result.get("n", 0) < 1:
            raise FlavorNotFoundError()

    def find_flavor(self, name):
        flavor_dict = self.db[self.flavors_collection].find_one({'_id': name})
        if not flavor_dict:
            raise FlavorNotFoundError()
        return self._flavor_from_dict(flavor_dict)

    def list_flavors(self):
        flavor_list = self.db[self.flavors_collection].find()
        return [self._flavor_from_dict(p) for p in flavor_list]

    def _flavor_from_dict(self, dict):
        dict["name"] = dict["_id"]
        del dict["_id"]

# Path: rpaas/plan.py
# class InvalidPlanError(Exception):
# class Plan(object):
#     def __init__(self, field):
#     def __unicode__(self):
#     def __init__(self, name, description, config):
#     def validate(self):
#     def to_dict(self):
# 
# Path: rpaas/flavor.py
# class InvalidFlavorError(Exception):
# class Flavor(object):
#     def __init__(self, field):
#     def __unicode__(self):
#     def __init__(self, name, description, config):
#     def validate(self):
#     def to_dict(self):


# Path: rpaas/storage.py
import datetime
import pymongo.errors
from hm import storage
from rpaas import plan, flavor

    def update_flavor(self, name, description=None, config=None):
        update = {}
        if description:
            update["description"] = description
        if config:
            update["config"] = config
        if update:
            result = self.db[self.flavors_collection].update({"_id": name},
                                                             {"$set": update})
            if not result.get("updatedExisting"):
                raise FlavorNotFoundError()

    def delete_flavor(self, name):
        result = self.db[self.flavors_collection].remove({"_id": name})
        if result.get("n", 0) < 1:
            raise FlavorNotFoundError()

    def find_flavor(self, name):
        flavor_dict = self.db[self.flavors_collection].find_one({'_id': name})
        if not flavor_dict:
            raise FlavorNotFoundError()
        return self._flavor_from_dict(flavor_dict)

    def list_flavors(self):
        flavor_list = self.db[self.flavors_collection].find()
        return [self._flavor_from_dict(p) for p in flavor_list]

    def _flavor_from_dict(self, dict):
        dict["name"] = dict["_id"]
        del dict["_id"]

        return flavor.Flavor(**dict)