[API] Support per-tenant max loadbalancers
Note: Schema altering change Adds a new table that allows maximum number of load balancers to be set per tenant ID. If a value is not set, the global value is used. The per-tenant value always overrides the global value. Change-Id: Ib7911742510756e34a4ef0ecf9380abb2a759e8e
This commit is contained in:
@@ -13,20 +13,32 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from pecan import expose
|
||||
from pecan import expose, request
|
||||
from pecan.rest import RestController
|
||||
from libra.common.api.lbaas import Limits, db_session
|
||||
from libra.api.acl import get_limited_to_project
|
||||
from libra.common.api.lbaas import Limits, TenantLimits, db_session
|
||||
|
||||
|
||||
class LimitsController(RestController):
|
||||
@expose('json')
|
||||
def get(self):
|
||||
resp = {}
|
||||
tenant_id = get_limited_to_project(request.headers)
|
||||
|
||||
with db_session() as session:
|
||||
limits = session.query(Limits).all()
|
||||
|
||||
# Get per-tenant values
|
||||
tenant_lblimit = session.query(TenantLimits.loadbalancers).\
|
||||
filter(TenantLimits.tenantid == tenant_id).scalar()
|
||||
|
||||
for limit in limits:
|
||||
resp[limit.name] = limit.value
|
||||
|
||||
# Set per-tenant values
|
||||
if tenant_lblimit:
|
||||
resp['maxLoadBalancers'] = tenant_lblimit
|
||||
|
||||
resp = {"limits": {"absolute": {"values": resp}}}
|
||||
session.rollback()
|
||||
return resp
|
||||
|
||||
@@ -28,6 +28,7 @@ from logs import LogsController
|
||||
# models
|
||||
from libra.common.api.lbaas import LoadBalancer, Device, Node, db_session
|
||||
from libra.common.api.lbaas import loadbalancers_devices, Limits, Vip
|
||||
from libra.common.api.lbaas import TenantLimits
|
||||
from libra.common.api.lbaas import HealthMonitor
|
||||
from libra.common.exc import ExhaustedError
|
||||
from libra.api.model.validators import LBPut, LBPost, LBResp, LBVipResp
|
||||
@@ -267,6 +268,13 @@ class LoadBalancersController(RestController):
|
||||
filter(LoadBalancer.tenantid == tenant_id).\
|
||||
filter(LoadBalancer.status != 'DELETED').count()
|
||||
|
||||
# Allow per-tenant LB limit, defaulting to the global limit if
|
||||
# the per-tenant value is not set.
|
||||
tenant_lblimit = session.query(TenantLimits.loadbalancers).\
|
||||
filter(TenantLimits.tenantid == tenant_id).scalar()
|
||||
if tenant_lblimit:
|
||||
lblimit = tenant_lblimit
|
||||
|
||||
if len(body.name) > namelimit:
|
||||
session.rollback()
|
||||
raise ClientSideError(
|
||||
|
||||
@@ -50,6 +50,13 @@ class FormatedDateTime(types.TypeDecorator):
|
||||
return value.strftime('%Y-%m-%dT%H:%M:%S')
|
||||
|
||||
|
||||
class TenantLimits(DeclarativeBase):
|
||||
__tablename__ = 'tenant_limits'
|
||||
id = Column(u'id', Integer, primary_key=True, nullable=False)
|
||||
tenantid = Column(u'tenantid', VARCHAR(length=128), nullable=False)
|
||||
loadbalancers = Column(u'loadbalancers', INTEGER(), nullable=True)
|
||||
|
||||
|
||||
class Limits(DeclarativeBase):
|
||||
__tablename__ = 'global_limits'
|
||||
id = Column(u'id', Integer, primary_key=True, nullable=False)
|
||||
|
||||
@@ -68,7 +68,7 @@ CREATE TABLE `loadbalancers_devices` (
|
||||
CREATE TABLE monitors (
|
||||
lbid BIGINT NOT NULL, # Loadbalancer who owns this node
|
||||
type VARCHAR(128) NOT NULL, # Type of ping. CONNECT, HTTP, HTTPS
|
||||
delay INT NOT NULL, # This is the minimum time in seconds between regular calls to a monitor
|
||||
delay INT NOT NULL, # This is the minimum time in seconds between regular calls to a monitor
|
||||
timeout INT NOT NULL, # Maximum number of seconds to wait for a connection to the node before it times out.
|
||||
attemptsBeforeDeactivation INT NOT NULL, # Number of permissible failures before removing a node from rotation. 1 to 10.
|
||||
path VARCHAR(2000) NULL, # The HTTP path used in the request by the monitor. Begins with /
|
||||
@@ -89,7 +89,7 @@ CREATE TABLE `vips` (
|
||||
`device` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `device` (`device`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
|
||||
CREATE TABLE `global_limits` (
|
||||
`id` int(11) NOT NULL,
|
||||
@@ -100,6 +100,14 @@ CREATE TABLE `global_limits` (
|
||||
|
||||
INSERT INTO `global_limits` VALUES (1,'maxLoadBalancerNameLength',128),(2,'maxVIPsPerLoadBalancer',1),(3,'maxNodesPerLoadBalancer',50),(4,'maxLoadBalancers',20);
|
||||
|
||||
CREATE TABLE `tenant_limits` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`tenantid` VARCHAR(128) NOT NULL,
|
||||
`loadbalancers` INT, # Max number of load balancers
|
||||
PRIMARY KEY(id),
|
||||
UNIQUE KEY `tenantid` (`tenantid`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
|
||||
# Billing
|
||||
CREATE TABLE billing (
|
||||
id int(11) NOT NULL,
|
||||
@@ -120,4 +128,3 @@ CREATE TABLE stats (
|
||||
status VARCHAR(50) NOT NULL, # Current LB status
|
||||
PRIMARY KEY (id) # ids are unique across all LBs
|
||||
) ENGINE=InnoDB DEFAULT CHARSET latin1;
|
||||
|
||||
Reference in New Issue
Block a user