deb build system
[opencloud-plugin.git] / opencloud_db_v2.py
1 from sqlalchemy import func
2 from sqlalchemy.orm import exc
3
4 import neutron.db.api as db
5 import opencloud_models_v2
6
7 def get_port_forwarding(session, port_id):
8     session = session or db.get_session()
9     try:
10         forward = (session.query(opencloud_models_v2.PortForwarding).
11                    filter_by(port_id=port_id).one())
12         return forward['forward_ports']
13     except exc.NoResultFound:
14         return
15
16 def clear_port_forwarding(session, port_id):
17     with session.begin(subtransactions=True):
18         try:
19             # Get rid of old port bindings
20             forward = (session.query(opencloud_models_v2.PortForwarding).
21                        filter_by(port_id=port_id).one())
22             if forward:
23                 session.delete(forward)
24         except exc.NoResultFound:
25             pass
26
27 def add_port_forwarding(session, port_id, forward_ports):
28     with session.begin(subtransactions=True):
29         forward = opencloud_models_v2.PortForwarding(port_id, forward_ports)
30         session.add(forward)
31