예제 #1
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
예제 #2
0
 def show(self, request, id):
     switch = db.get_switch(id)
     if not switch:
         raise exc.NotFound(resource="switch %s" % (id))
     return dict(switch=switch.as_dict())
예제 #3
0
 def show(self, request, id):
     switchports = list(db.filter_switchports(hardware_id=id))
     if not switchports:
         raise exc.NotFound(resource="switchports %s" % (id))
     return dict(switchports=[s.as_dict() for s in switchports])