Exemple #1
0
async def _get_instance_async(filter, projection) -> dict:
    """Helper to get an instance async-style"""
    result = await moto.query(collection="system",
                              filter=filter,
                              projection=projection)

    # TODO - This is not the best
    instance = result["instances"][0]
    if "_id" in instance:
        instance["id"] = str(instance["_id"])
        del instance["_id"]

    return SchemaParser.parse_instance(instance)
Exemple #2
0
    async def patch(self, system_id):
        """
        ---
        summary: Partially update a System
        description: |
          The body of the request needs to contain a set of instructions detailing the
          updates to apply.
          Currently supported operations are below:
          ```JSON
          [
            { "operation": "add", "path": "/instance", "value": "" },
            { "operation": "replace", "path": "/commands", "value": "" },
            { "operation": "replace", "path": "/description", "value": "new description"},
            { "operation": "replace", "path": "/display_name", "value": "new display name"},
            { "operation": "replace", "path": "/icon_name", "value": "new icon name"},
            { "operation": "update", "path": "/metadata", "value": {"foo": "bar"}}
          ]
          ```
          Where `value` is a list of new Commands.
        parameters:
          - name: system_id
            in: path
            required: true
            description: The ID of the System
            type: string
          - name: patch
            in: body
            required: true
            description: Instructions for how to update the System
            schema:
              $ref: '#/definitions/Patch'
        responses:
          200:
            description: System with the given ID
            schema:
              $ref: '#/definitions/System'
          400:
            $ref: '#/definitions/400Error'
          404:
            $ref: '#/definitions/404Error'
          50x:
            $ref: '#/definitions/50xError'
        tags:
          - Systems
        """
        _ = self.get_or_raise(System, SYSTEM_UPDATE, id=system_id)

        kwargs = {}
        do_reload = False

        response = ""

        for op in SchemaParser.parse_patch(self.request.decoded_body,
                                           from_string=True):
            if op.operation == "replace":
                if op.path == "/commands":
                    kwargs["new_commands"] = SchemaParser.parse_command(
                        op.value, many=True)
                elif op.path in [
                        "/description",
                        "/icon_name",
                        "/display_name",
                        "/template",
                ]:
                    kwargs[op.path.strip("/")] = op.value
                else:
                    raise ModelValidationError(
                        f"Unsupported path for replace '{op.path}'")

            elif op.operation == "add":
                if op.path == "/instance":
                    if not kwargs.get("add_instances"):
                        kwargs["add_instances"] = []

                    kwargs["add_instances"].append(
                        SchemaParser.parse_instance(op.value))
                else:
                    raise ModelValidationError(
                        f"Unsupported path for add '{op.path}'")

            elif op.operation == "update":
                if op.path == "/metadata":
                    kwargs["metadata"] = op.value
                else:
                    raise ModelValidationError(
                        f"Unsupported path for update '{op.path}'")

            elif op.operation == "reload":
                do_reload = True

            else:
                raise ModelValidationError(
                    f"Unsupported operation '{op.operation}'")

        if kwargs:
            response = await self.client(
                Operation(operation_type="SYSTEM_UPDATE",
                          args=[system_id],
                          kwargs=kwargs))

        if do_reload:
            await self.client(
                Operation(operation_type="SYSTEM_RELOAD", args=[system_id]))

        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.write(response)