function.response

Utilities for working with RunFunctionResponses.

 1# Copyright 2023 The Crossplane Authors.
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Utilities for working with RunFunctionResponses."""
16
17import datetime
18
19from google.protobuf import duration_pb2 as durationpb
20
21import crossplane.function.proto.v1.run_function_pb2 as fnv1
22
23"""The default TTL for which a RunFunctionResponse may be cached."""
24DEFAULT_TTL = datetime.timedelta(minutes=1)
25
26
27def to(
28    req: fnv1.RunFunctionRequest,
29    ttl: datetime.timedelta = DEFAULT_TTL,
30) -> fnv1.RunFunctionResponse:
31    """Create a response to the supplied request.
32
33    Args:
34        req: The request to respond to.
35        ttl: How long Crossplane may optionally cache the response.
36
37    Returns:
38        A response to the supplied request.
39
40    The request's tag, desired resources, and context is automatically copied to
41    the response. Using response.to is a good pattern to ensure
42    """
43    dttl = durationpb.Duration()
44    dttl.FromTimedelta(ttl)
45    return fnv1.RunFunctionResponse(
46        meta=fnv1.ResponseMeta(tag=req.meta.tag, ttl=dttl),
47        desired=req.desired,
48        context=req.context,
49    )
50
51
52def normal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
53    """Add a normal result to the response."""
54    rsp.results.append(
55        fnv1.Result(
56            severity=fnv1.SEVERITY_NORMAL,
57            message=message,
58        )
59    )
60
61
62def warning(rsp: fnv1.RunFunctionResponse, message: str) -> None:
63    """Add a warning result to the response."""
64    rsp.results.append(
65        fnv1.Result(
66            severity=fnv1.SEVERITY_WARNING,
67            message=message,
68        )
69    )
70
71
72def fatal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
73    """Add a fatal result to the response."""
74    rsp.results.append(
75        fnv1.Result(
76            severity=fnv1.SEVERITY_FATAL,
77            message=message,
78        )
79    )
DEFAULT_TTL = datetime.timedelta(seconds=60)
def to( req: crossplane.function.proto.v1.run_function_pb2.RunFunctionRequest, ttl: datetime.timedelta = datetime.timedelta(seconds=60)) -> crossplane.function.proto.v1.run_function_pb2.RunFunctionResponse:
28def to(
29    req: fnv1.RunFunctionRequest,
30    ttl: datetime.timedelta = DEFAULT_TTL,
31) -> fnv1.RunFunctionResponse:
32    """Create a response to the supplied request.
33
34    Args:
35        req: The request to respond to.
36        ttl: How long Crossplane may optionally cache the response.
37
38    Returns:
39        A response to the supplied request.
40
41    The request's tag, desired resources, and context is automatically copied to
42    the response. Using response.to is a good pattern to ensure
43    """
44    dttl = durationpb.Duration()
45    dttl.FromTimedelta(ttl)
46    return fnv1.RunFunctionResponse(
47        meta=fnv1.ResponseMeta(tag=req.meta.tag, ttl=dttl),
48        desired=req.desired,
49        context=req.context,
50    )

Create a response to the supplied request.

Arguments:
  • req: The request to respond to.
  • ttl: How long Crossplane may optionally cache the response.
Returns:

A response to the supplied request.

The request's tag, desired resources, and context is automatically copied to the response. Using response.to is a good pattern to ensure

def normal( rsp: crossplane.function.proto.v1.run_function_pb2.RunFunctionResponse, message: str) -> None:
53def normal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
54    """Add a normal result to the response."""
55    rsp.results.append(
56        fnv1.Result(
57            severity=fnv1.SEVERITY_NORMAL,
58            message=message,
59        )
60    )

Add a normal result to the response.

def warning( rsp: crossplane.function.proto.v1.run_function_pb2.RunFunctionResponse, message: str) -> None:
63def warning(rsp: fnv1.RunFunctionResponse, message: str) -> None:
64    """Add a warning result to the response."""
65    rsp.results.append(
66        fnv1.Result(
67            severity=fnv1.SEVERITY_WARNING,
68            message=message,
69        )
70    )

Add a warning result to the response.

def fatal( rsp: crossplane.function.proto.v1.run_function_pb2.RunFunctionResponse, message: str) -> None:
73def fatal(rsp: fnv1.RunFunctionResponse, message: str) -> None:
74    """Add a fatal result to the response."""
75    rsp.results.append(
76        fnv1.Result(
77            severity=fnv1.SEVERITY_FATAL,
78            message=message,
79        )
80    )

Add a fatal result to the response.