Beispiel #1
0
    def delete(self, device_id):
        """ Deletes a given device
        :param device_id: id of device to delete
        Urls:
           DELETE   /devices/{device_id}
        Returns: None
        """

        if not tenant_is_admin(request.headers):
            response.status = 401
            return dict(
                faultcode="Client",
                faultstring="Client not authorized to access this function"
            )

        with db_session() as session:
            # check for the device
            device = session.query(Device.id).\
                filter(Device.id == device_id).first()

            if device is None:
                session.rollback()
                response.status = 404
                return dict(
                    faultcode="Client",
                    faultstring="Device " + device_id + " not found"
                )

            # Is the device is attached to a LB
            lb = session.query(
                loadbalancers_devices.c.loadbalancer).\
                filter(loadbalancers_devices.c.device == device_id).\
                all()

            if lb:
                # Rebuild device
                resp = rebuild_device(device_id)
                response.status = resp[0]
                return resp[1]
            # If we get here there are no load balancers so delete device
            response.status = 204
            try:
                device = session.query(Device).\
                    filter(Device.id == device_id).first()
                device.status = 'DELETED'
                session.commit()
                return None
            except:
                session.rollback()
                LOG.exception('Error deleting device from pool')
                response.status = 500
                return dict(
                    faultcode="Server",
                    faultstring="Error deleting device from pool"
                )
            return None
Beispiel #2
0
    def delete(self, device_id):
        """ Deletes a given device
        :param device_id: id of device to delete
        Urls:
           DELETE   /devices/{device_id}
        Returns: None
        """

        if not tenant_is_admin(request.headers):
            response.status = 401
            return dict(
                faultcode="Client",
                faultstring="Client not authorized to access this function")

        with db_session() as session:
            # check for the device
            device = session.query(Device.id).\
                filter(Device.id == device_id).first()

            if device is None:
                session.rollback()
                response.status = 404
                return dict(faultcode="Client",
                            faultstring="Device " + device_id + " not found")

            # Is the device is attached to a LB
            lb = session.query(
                loadbalancers_devices.c.loadbalancer).\
                filter(loadbalancers_devices.c.device == device_id).\
                all()

            if lb:
                # Rebuild device
                resp = rebuild_device(device_id)
                response.status = resp[0]
                return resp[1]
            # If we get here there are no load balancers so delete device
            response.status = 204
            try:
                device = session.query(Device).\
                    filter(Device.id == device_id).first()
                device.status = 'DELETED'
                session.commit()
                return None
            except:
                session.rollback()
                LOG.exception('Error deleting device from pool')
                response.status = 500
                return dict(faultcode="Server",
                            faultstring="Error deleting device from pool")
            return None
Beispiel #3
0
 def put(self, tenant_id=None):
     if not tenant_is_admin(request.headers):
         response.status = 401
         return dict(
             faultcode="Client",
             faultstring="Client not authorized to access this function"
         )
     try:
         data = json.loads(request.body)
     except:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Invalid JSON received"
         )
     with db_session() as session:
         if tenant_id is None:
             for key, value in data.iteritems():
                 limit = session.query(Limits).filter(Limits.name == key).\
                     first()
                 if limit is None:
                     session.rollback()
                     response.status = 400
                     return dict(
                         faultcode="Client",
                         faultstring="Limit not found: {0}".format(key)
                     )
                 limit.value = value
         else:
             if 'maxLoadBalancers' in data:
                 limit = session.query(TenantLimits).\
                     filter(TenantLimits.tenantid == tenant_id).first()
                 if limit is not None:
                     limit.loadbalancers = data['maxLoadBalancers']
                 else:
                     new_limit = TenantLimits()
                     new_limit.tenantid = tenant_id
                     new_limit.loadbalancers = data['maxLoadBalancers']
                     session.add(new_limit)
             else:
                 session.rollback()
                 response.status = 400
                 return dict(
                     faultcode="Client",
                     faultstring="No user settable limit in json"
                 )
         session.commit()
Beispiel #4
0
 def post(self):
     """ Add a new user to the Admin API """
     if not tenant_is_admin(request.headers):
         response.status = 401
         return dict(
             faultcode="Client",
             faultstring="Client not authorized to access this function"
         )
     try:
         data = json.loads(request.body)
     except:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Invalid JSON received"
         )
     if data['tenant'] is None:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Tenant ID required"
         )
     tenant_id = data['tenant']
     if 'level' not in data:
         level = 'USER'
     elif data['level'] not in ['USER', 'ADMIN']:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Only USER or ADMIN levels allowed"
         )
     else:
         level = data['level']
     with db_session() as session:
         user_test = session.query(AdminAuth).\
             filter(AdminAuth.tenant_id == tenant_id).count()
         if user_test > 0:
             response.status = 400
             return dict(
                 faultcode="Client",
                 faultstring="Tenant already has an account"
             )
         user = AdminAuth()
         user.tenant_id = tenant_id
         user.level = level
         session.add(user)
         session.commit()
Beispiel #5
0
    def get_all(self):
        """
        Get a list of users
        """
        if not tenant_is_admin(request.headers):
            response.status = 401
            return dict(
                faultcode="Client",
                faultstring="Client not authorized to access this function"
            )

        with db_session() as session:
            user = session.query(
                AdminAuth.tenant_id.label('tenant'), AdminAuth.level
            ).all()
            session.commit()
        return user
Beispiel #6
0
 def put(self, tenant_id):
     """ Change the leve for an Admin API user """
     if not tenant_is_admin(request.headers):
         response.status = 401
         return dict(
             faultcode="Client",
             faultstring="Client not authorized to access this function"
         )
     try:
         data = json.loads(request.body)
     except:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Invalid JSON received"
         )
     if tenant_id is None:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Tenant ID required"
         )
     if not data['level']:
         level = 'USER'
     elif data['level'] not in ['USER', 'ADMIN']:
         response.status = 400
         return dict(
             faultcode="Client",
             faultstring="Only USER or ADMIN levels allowed"
         )
     else:
         level = data['level']
     with db_session() as session:
         user = session.query(AdminAuth).\
             filter(AdminAuth.tenant_id == tenant_id).first()
         if not user:
             response.status = 404
             return dict(
                 faultcode="Client",
                 faultstring="Tenant does not have an account"
             )
         user.level = level
         session.commit()
Beispiel #7
0
 def put(self, tenant_id=None):
     if not tenant_is_admin(request.headers):
         response.status = 401
         return dict(
             faultcode="Client",
             faultstring="Client not authorized to access this function")
     try:
         data = json.loads(request.body)
     except:
         response.status = 400
         return dict(faultcode="Client",
                     faultstring="Invalid JSON received")
     with db_session() as session:
         if tenant_id is None:
             for key, value in data.iteritems():
                 limit = session.query(Limits).filter(Limits.name == key).\
                     first()
                 if limit is None:
                     session.rollback()
                     response.status = 400
                     return dict(
                         faultcode="Client",
                         faultstring="Limit not found: {0}".format(key))
                 limit.value = value
         else:
             if 'maxLoadBalancers' in data:
                 limit = session.query(TenantLimits).\
                     filter(TenantLimits.tenantid == tenant_id).first()
                 if limit is not None:
                     limit.loadbalancers = data['maxLoadBalancers']
                 else:
                     new_limit = TenantLimits()
                     new_limit.tenantid = tenant_id
                     new_limit.loadbalancers = data['maxLoadBalancers']
                     session.add(new_limit)
             else:
                 session.rollback()
                 response.status = 400
                 return dict(faultcode="Client",
                             faultstring="No user settable limit in json")
         session.commit()
Beispiel #8
0
 def delete(self, tenant_id):
     """ Delete a given user from the Admin API """
     if not tenant_is_admin(request.headers):
         response.status = 401
         return dict(
             faultcode="Client",
             faultstring="Client not authorized to access this function"
         )
     with db_session() as session:
         user_test = session.query(AdminAuth).\
             filter(AdminAuth.tenant_id == tenant_id).count()
         if user_test == 0:
             response.status = 404
             return dict(
                 faultcode="Client",
                 faultstring="Tenant not found"
             )
         session.query(AdminAuth).\
             filter(AdminAuth.tenant_id == tenant_id).delete()
         session.commit()
     response.status = 204
     return None