コード例 #1
0
    def _validate_hardware_id(cls, switchports):
        # Ensure all given hardware_ids are !None
        hardware_ids = set([s.get("hardware_id") for s in switchports])
        if None in hardware_ids:
            raise exc.BadRequest(resource="switchports",
                                 reason="hardware_id cannot be empty")

        # Ensure all given hardware_ids match
        if (len(hardware_ids) != 1):
            raise exc.BadRequest(
                resource="switchports",
                reason="all switchport hardware_ids must match")

        return list(hardware_ids)[0]
コード例 #2
0
    def create(self, request):
        try:
            body = request.json_body
        except json_scanner.JSONDecodeError:
            raise exc.BadRequest(resource="switchports",
                                 reason="invalid JSON body")

        try:
            body = body.pop("switchports")
        except KeyError:
            raise exc.BadRequest(
                resource="switchports",
                reason="'switchports' not found in request body")

        switchports = self.create_switchports(body)
        return dict(switchports=[s.as_dict() for s in switchports])
コード例 #3
0
    def create(self, request):
        try:
            body = request.json_body
        except json_scanner.JSONDecodeError:
            raise exc.BadRequest(resource="switch", reason="invalid JSON body")

        try:
            body = body.pop("switch")
        except KeyError:
            raise exc.BadRequest(resource="switch",
                                 reason="'switch' not found in request body")

        try:
            id = body.pop('id')

            host = body.pop('host')
            username = body.pop('username')
            password = body.pop('password')

            switch_type = body.pop('type')
        except KeyError as e:
            raise exc.BadRequest(resource="switch",
                                 reason="missing required key: %s" %
                                 (e.message))

        # optional
        description = body.get('description')

        switch = db.create_switch(id,
                                  host,
                                  username,
                                  password,
                                  switch_type,
                                  description=description)

        return dict(switch=switch.as_dict())
コード例 #4
0
    def delete_switchports(cls, hardware_id, switchports=None, session=None):
        if not session:
            session = db_api.get_session()

        # find portmaps and check if they are in-use before deleting.
        with session.begin(subtransactions=True):

            if not switchports:
                switchports = list(
                    db.filter_switchports(hardware_id=hardware_id,
                                          session=session))

            switchport_ids = [sp.id for sp in switchports]
            if switchport_ids:
                bindings = db.filter_switchport_bindings_by_switch_port_ids(
                    switchport_ids, session=session)
                bindings = list(bindings)
                if bindings:
                    raise exc.BadRequest(
                        resource="switchport",
                        reason=("Cannot delete, switchport(s) "
                                "'%s' in use" % (','.join(switchport_ids))))
            return db.delete_switchports(switchport_ids, session=session)
コード例 #5
0
    def validate_switchports(cls, switchports, session=None):
        """TODO(morgabra) Split this up, think about it more. It's
        inefficient and large.
        """
        if not session:
            session = db_api.get_session()

        if not switchports:
            raise exc.BadRequest(resource="switchports",
                                 reason="must specify at least 1 switchport")

        hardware_id = cls._validate_hardware_id(switchports)

        # Ensure no switchports exist for the given hardware_id
        existing = list(
            db.filter_switchports(hardware_id=hardware_id, session=session))
        if existing:
            raise exc.BadRequest(resource="switchports",
                                 reason=("switchports already exist for "
                                         "hardware_id='%s'" % hardware_id))

        # Ensure all given names are !None
        names = set([s.get("name") for s in switchports])
        if None in names:
            raise exc.BadRequest(resource="switchports",
                                 reason="name cannot be empty")

        # Ensure all given names are unique
        if (len(names) != len(switchports)):
            raise exc.BadRequest(resource="switchports",
                                 reason="all switchport names must be unique")

        # Ensure all given switch_id/port maps are unique
        ports = set([(s.get("switch_id"), s.get("port")) for s in switchports])
        if (len(ports) != len(switchports)):
            raise exc.BadRequest(
                resource="switchports",
                reason=("cannot add switchport with identical "
                        "switch_id/port values"))

        for switch_id, port in ports:

            # Ensure switch_id is !None
            if not switch_id:
                raise exc.BadRequest(resource="switchports",
                                     reason="switch_id cannot be empty")

            # Ensure switch_id is !None
            if not port:
                raise exc.BadRequest(resource="switchports",
                                     reason="port cannot be empty")

            # Ensure referenced switch actually exists
            switch = db.get_switch(switch_id, session=session)
            if not switch:
                raise exc.NotFound(resource="switch %s" % (switch_id))

            # Ensure switchport not taken by another hardware_id
            existing = list(
                db.filter_switchports(switch_id=switch_id,
                                      port=port,
                                      session=session))
            if len(existing) >= 1:
                raise exc.BadRequest(
                    resource="switchport",
                    reason=("port already mapped to hardware_id "
                            "'%s'" % (existing[0].hardware_id)))

        return switchports