Ejemplo n.º 1
0
    def UpdateRecord(self, request, context):
        """ Update the directory record of an object

        Args:
            request (UpdateRecordRequest): update record request
        """
        if len(request.id) == 0:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details("ID argument cannot be empty in "
                                "UpdateRecordRequest")
            return

        # Lock Redis for requested key until update is complete
        with self._redis_dict.lock(request.id):
            hwid = get_gateway_hwid()
            record = self._redis_dict.get(request.id) or \
                     DirectoryRecord(location_history=[hwid], identifiers={})

            if record.location_history[0] != hwid:
                record.location_history = [hwid] + record.location_history

            for field_key in request.fields:
                record.identifiers[field_key] = request.fields[field_key]

            # Truncate location history to the five most recent hwid's
            record.location_history = \
                record.location_history[:LOCATION_MAX_LEN]
            self._redis_dict[request.id] = record
Ejemplo n.º 2
0
def make_scoped_device_id(idval: str, scope: str) -> str:
    """
    Create a deviceID of the format <id> for scope 'network'
    Otherwise create a key of the format <hwid>:<id> for 'gateway' or
    unrecognized scope.
    """
    if scope == "network":
        return idval
    else:
        return get_gateway_hwid() + ":" + idval
Ejemplo n.º 3
0
    def UpdateRecord(self, request, context):
        """ Update the directory record of an object

        Args:
            request (UpdateRecordRequest): update record request
        """
        logging.debug("UpdateRecord request received")
        self._print_grpc(request)
        if len(request.id) == 0:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(
                "ID argument cannot be empty in "
                "UpdateRecordRequest",
            )
            return

        try:
            # Lock Redis for requested key until update is complete
            with self._redis_dict.lock(request.id):
                hwid = get_gateway_hwid()
                record = self._redis_dict.get(request.id) or \
                    DirectoryRecord(
                        location_history=[hwid],
                        identifiers={},
                    )

                if record.location_history[0] != hwid:
                    record.location_history = [hwid] + record.location_history

                for field_key in request.fields:
                    record.identifiers[field_key] = request.fields[field_key]

                # Truncate location history to the five most recent hwid's
                record.location_history = \
                    record.location_history[:LOCATION_MAX_LEN]
                self._redis_dict[request.id] = record
        except (RedisError, LockError) as e:
            logging.error(e)
            context.set_code(grpc.StatusCode.UNAVAILABLE)
            context.set_details("Could not connect to redis: %s" % e)