Exemplo n.º 1
0
    def do_mapping(self,
                   scope,
                   local_ids=None,
                   remote_system=None,
                   remote_ids=None):
        """
        Perform mapping
        :param scope: scope name
        :param local_ids: List of Local id
        :param remote_system: Remote system id
        :param remote_ids: List of Id from remote system
        :param kwargs: Ignored args
        :return:
        """
        def format_obj(o):
            r = {"scope": scope, "id": str(o.id), "mappings": []}
            if o.remote_system:
                r["mappings"] += [{
                    "remote_system": str(o.remote_system.id),
                    "remote_id": str(o.remote_id)
                }]
            return r

        # Get model to query
        model = get_model(self.SCOPES[scope])
        if not model:
            return 400, self.error_msg("Invalid scope")
        # Query remote objects
        result = []
        if remote_system and remote_ids:
            rs = RemoteSystem.get_by_id(remote_system)
            if not rs:
                return 404, self.error_msg("Remote system not found")
            if len(remote_ids) == 1:
                qs = model.objects.filter(remote_system=rs.id,
                                          remote_id=remote_ids[0])
            else:
                qs = model.objects.filter(remote_system=rs.id,
                                          remote_id__in=remote_ids)
            result += [format_obj(o) for o in qs]
        # Query local objects
        seen = set(o["id"] for o in result)
        # Skip already collected objects
        local_ids = local_ids or []
        local_ids = [o for o in local_ids if o not in seen]
        if local_ids:
            if len(local_ids) == 1:
                qs = model.objects.filter(id=local_ids[0])
            else:
                qs = model.objects.filter(id__in=local_ids)
            result += [format_obj(o) for o in qs]
        # 404 if no objects found
        if not result:
            return 404, self.error_msg("Not found")
        return 200, list(sorted(result, key=operator.itemgetter("id")))
Exemplo n.º 2
0
 def extract(self):
     containers = ReportContainerData(
         sorted(
             set(ManagedObject.objects.all().order_by(
                 "container").values_list("container", flat=True))))
     containers = containers.get_dictionary()
     for (
             mo_id,
             bi_id,
             name,
             address,
             profile,
             platform,
             version,
             remote_id,
             remote_system,
             adm_id,
             adm_name,
             container,
     ) in (ManagedObject.objects.all().order_by("id").values_list(
             "id",
             "bi_id",
             "name",
             "address",
             "profile",
             "platform",
             "version",
             "remote_id",
             "remote_system",
             "administrative_domain",
             "administrative_domain__name",
             "container",
     )):
         yield (
             bi_id,
             mo_id,
             name,
             address,
             Profile.get_by_id(profile).name if profile else "",
             Platform.get_by_id(platform).name if platform else "",
             Firmware.get_by_id(version).version if version else "",
             remote_id if remote_id else "",
             RemoteSystem.get_by_id(remote_system).name
             if remote_system else "",
             adm_id,
             adm_name,
             containers.get(container, ("", ))[0] if container else "",
         )
Exemplo n.º 3
0
    def get_object_and_interface(self,
                                 object=None,
                                 interface=None,
                                 service=None):
        # type: (Optional[Dict[str, Any]], Optional[Dict[str, Any]], Optional[Dict[str, Any]], Optional[Dict[str, Any]]) -> Tuple[ManagedObject, Optional[Interface]]
        """
        Process from and to section of request and get object and interface

        :param object: request.object
        :param interface: request.interface
        :param service: request.service

        :return: ManagedObject Instance, Optional[Interface Instance]
        :raises ValueError:
        """
        if object:
            if "id" in object:
                # object.id
                mo = ManagedObject.get_by_id(object["id"])
            elif "remote_system" in object:
                # object.remote_system/remote_id
                rs = RemoteSystem.get_by_id(object["remote_system"])
                if not rs:
                    raise ValueError("Remote System not found")
                mo = ManagedObject.objects.filter(
                    remote_system=rs.id,
                    remote_id=object["remote_id"]).first()
            else:
                raise ValueError("Neither id or remote system specified")
            if not mo:
                raise ValueError("Object not found")
            if interface:
                # Additional interface restriction
                iface = mo.get_interface(interface["name"])
                if iface is None:
                    raise ValueError("Interface not found")
                return mo, iface
            else:
                # No interface restriction
                return mo, None
        if interface:
            iface = Interface.objects.filter(id=interface["id"]).first()
            if not iface:
                raise ValueError("Interface not found")
            return iface.managed_object, iface
        if service:
            if "id" in service:
                svc = Service.objects.filter("id").first()
            elif "remote_system" in service:
                rs = RemoteSystem.get_by_id(object["remote_system"])
                if not rs:
                    raise ValueError("Remote System not found")
                svc = Service.objects.filter(
                    remote_system=rs.id,
                    remote_id=service["remote_id"]).first()
            else:
                raise ValueError("Neither id or remote system specified")
            if svc is None:
                raise ValueError("Service not found")
            iface = Interface.objects.filter(service=svc.id).first()
            if not iface:
                raise ValueError("Interface not found")
            return iface.managed_object, iface
        raise ValueError("Invalid search condition")