Skip to content

Commit

Permalink
get SQL refs from session (bp sql-query-get)
Browse files Browse the repository at this point in the history
Change-Id: I2200e33868d50bb69089f3108a5a4c061afccd6e
  • Loading branch information
dolph committed May 20, 2013
1 parent 7b99bd6 commit 8d2b8e6
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 149 deletions.
10 changes: 5 additions & 5 deletions keystone/catalog/backends/sql.py
Expand Up @@ -60,10 +60,10 @@ def list_services(self):
return [s.to_dict() for s in list(services)]

def _get_service(self, session, service_id):
try:
return session.query(Service).filter_by(id=service_id).one()
except sql.NotFound:
ref = session.query(Service).get(service_id)
if not ref:
raise exception.ServiceNotFound(service_id=service_id)
return ref

def get_service(self, service_id):
session = self.get_session()
Expand Down Expand Up @@ -112,8 +112,8 @@ def create_endpoint(self, endpoint_id, endpoint_ref):
def delete_endpoint(self, endpoint_id):
session = self.get_session()
with session.begin():
if not session.query(Endpoint).filter_by(id=endpoint_id).delete():
raise exception.EndpointNotFound(endpoint_id=endpoint_id)
ref = self._get_endpoint(session, endpoint_id)
session.delete(ref)
session.flush()

def _get_endpoint(self, session, endpoint_id):
Expand Down
26 changes: 9 additions & 17 deletions keystone/credential/backends/sql.py
Expand Up @@ -14,10 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.

from keystone import clean
from keystone.common import sql
from keystone.common.sql import migration
from keystone.common import utils
from keystone import credential
from keystone import exception

Expand Down Expand Up @@ -55,22 +53,21 @@ def list_credentials(self):
refs = session.query(CredentialModel).all()
return [ref.to_dict() for ref in refs]

def get_credential(self, credential_id):
session = self.get_session()
ref = (session.query(CredentialModel)
.filter_by(id=credential_id).first())
def _get_credential(self, session, credential_id):
ref = session.query(CredentialModel).get(credential_id)
if ref is None:
raise exception.CredentialNotFound(credential_id=credential_id)
return ref.to_dict()
return ref

def get_credential(self, credential_id):
session = self.get_session()
return self._get_credential(session, credential_id).to_dict()

@sql.handle_conflicts(type='credential')
def update_credential(self, credential_id, credential):
session = self.get_session()
with session.begin():
ref = (session.query(CredentialModel)
.filter_by(id=credential_id).first())
if ref is None:
raise exception.CredentialNotFound(credential_id=credential_id)
ref = self._get_credential(session, credential_id)
old_dict = ref.to_dict()
for k in credential:
old_dict[k] = credential[k]
Expand All @@ -85,12 +82,7 @@ def update_credential(self, credential_id, credential):
def delete_credential(self, credential_id):
session = self.get_session()

try:
ref = (session.query(CredentialModel)
.filter_by(id=credential_id).one())
except sql.NotFound:
raise exception.CredentialNotFound(credential_id=credential_id)

with session.begin():
ref = self._get_credential(session, credential_id)
session.delete(ref)
session.flush()

0 comments on commit 8d2b8e6

Please sign in to comment.