| 1 |
# Copyright (C) 2013 Rackspace Hosting Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import abc |
| 16 |
|
| 17 |
from taskflow.persistence import models |
| 18 |
|
| 19 |
|
| 20 |
class Backend(metaclass=abc.ABCMeta): |
| 21 |
"""Base class for persistence backends.""" |
| 22 |
|
| 23 |
def __init__(self, conf): |
| 24 |
if not conf: |
| 25 |
conf = {} |
| 26 |
if not isinstance(conf, dict): |
| 27 |
raise TypeError("Configuration dictionary expected not '%s' (%s)" |
| 28 |
% (conf, type(conf))) |
| 29 |
self._conf = conf |
| 30 |
|
| 31 |
@abc.abstractmethod |
| 32 |
def get_connection(self): |
| 33 |
"""Return a Connection instance based on the configuration settings.""" |
| 34 |
|
| 35 |
@abc.abstractmethod |
| 36 |
def close(self): |
| 37 |
"""Closes any resources this backend has open.""" |
| 38 |
|
| 39 |
|
| 40 |
class Connection(metaclass=abc.ABCMeta): |
| 41 |
"""Base class for backend connections.""" |
| 42 |
|
| 43 |
@property |
| 44 |
@abc.abstractmethod |
| 45 |
def backend(self): |
| 46 |
"""Returns the backend this connection is associated with.""" |
| 47 |
|
| 48 |
@abc.abstractmethod |
| 49 |
def close(self): |
| 50 |
"""Closes any resources this connection has open.""" |
| 51 |
|
| 52 |
@abc.abstractmethod |
| 53 |
def upgrade(self): |
| 54 |
"""Migrate the persistence backend to the most recent version.""" |
| 55 |
|
| 56 |
@abc.abstractmethod |
| 57 |
def clear_all(self): |
| 58 |
"""Clear all entries from this backend.""" |
| 59 |
|
| 60 |
@abc.abstractmethod |
| 61 |
def validate(self): |
| 62 |
"""Validates that a backend is still ok to be used. |
| 63 |
|
| 64 |
The semantics of this *may* vary depending on the backend. On failure a |
| 65 |
backend specific exception should be raised that will indicate why the |
| 66 |
failure occurred. |
| 67 |
""" |
| 68 |
|
| 69 |
@abc.abstractmethod |
| 70 |
def update_atom_details(self, atom_detail): |
| 71 |
"""Updates a given atom details and returns the updated version. |
| 72 |
|
| 73 |
NOTE(harlowja): the details that is to be updated must already have |
| 74 |
been created by saving a flow details with the given atom detail inside |
| 75 |
of it. |
| 76 |
""" |
| 77 |
|
| 78 |
@abc.abstractmethod |
| 79 |
def update_flow_details(self, flow_detail): |
| 80 |
"""Updates a given flow details and returns the updated version. |
| 81 |
|
| 82 |
NOTE(harlowja): the details that is to be updated must already have |
| 83 |
been created by saving a logbook with the given flow detail inside |
| 84 |
of it. |
| 85 |
""" |
| 86 |
|
| 87 |
@abc.abstractmethod |
| 88 |
def save_logbook(self, book): |
| 89 |
"""Saves a logbook, and all its contained information.""" |
| 90 |
|
| 91 |
@abc.abstractmethod |
| 92 |
def destroy_logbook(self, book_uuid): |
| 93 |
"""Deletes/destroys a logbook matching the given uuid.""" |
| 94 |
|
| 95 |
@abc.abstractmethod |
| 96 |
def get_logbook(self, book_uuid, lazy=False): |
| 97 |
"""Fetches a logbook object matching the given uuid.""" |
| 98 |
|
| 99 |
@abc.abstractmethod |
| 100 |
def get_logbooks(self, lazy=False): |
| 101 |
"""Return an iterable of logbook objects.""" |
| 102 |
|
| 103 |
@abc.abstractmethod |
| 104 |
def get_flows_for_book(self, book_uuid): |
| 105 |
"""Return an iterable of flowdetails for a given logbook uuid.""" |
| 106 |
|
| 107 |
@abc.abstractmethod |
| 108 |
def get_flow_details(self, fd_uuid, lazy=False): |
| 109 |
"""Fetches a flowdetails object matching the given uuid.""" |
| 110 |
|
| 111 |
@abc.abstractmethod |
| 112 |
def get_atom_details(self, ad_uuid): |
| 113 |
"""Fetches a atomdetails object matching the given uuid.""" |
| 114 |
|
| 115 |
@abc.abstractmethod |
| 116 |
def get_atoms_for_flow(self, fd_uuid): |
| 117 |
"""Return an iterable of atomdetails for a given flowdetails uuid.""" |
| 118 |
|
| 119 |
|
| 120 |
def _format_atom(atom_detail): |
| 121 |
return { |
| 122 |
'atom': atom_detail.to_dict(), |
| 123 |
'type': models.atom_detail_type(atom_detail), |
| 124 |
} |