Skip to content

Commit

Permalink
Use a more illustrative default example
Browse files Browse the repository at this point in the history
Emitting a hello world result isn't very useful. Instead, show how to
compose an MR with some fields derived from the XR and some from the
input.

Signed-off-by: Nic Cope <[email protected]>
  • Loading branch information
negz committed Oct 10, 2024
1 parent 6a068dd commit d95a331
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/composition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ spec:
input:
apiVersion: template.fn.crossplane.io/v1beta1
kind: Input
example: "Hello world"
version: v1beta2
22 changes: 14 additions & 8 deletions function/fn.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""A Crossplane composition function."""

import grpc
from crossplane.function import logging, response
from crossplane.function import logging, resource, response
from crossplane.function.proto.v1 import run_function_pb2 as fnv1
from crossplane.function.proto.v1 import run_function_pb2_grpc as grpcv1

Expand All @@ -22,12 +22,18 @@ async def RunFunction(

rsp = response.to(req)

example = ""
if "example" in req.input:
example = req.input["example"]

# TODO: Add your function logic here!
response.normal(rsp, f"I was run with input {example}!")
log.info("I was run!", input=example)
version = req.input["version"]
region = req.observed.composite.resource["spec"]["region"]

resource.update(
rsp.desired.resources["bucket"],
{
"apiVersion": f"s3.aws.upbound.io/{version}",
"kind": "Bucket",
"spec": {
"forProvider": {"region": region},
},
},
)

return rsp
35 changes: 27 additions & 8 deletions tests/test_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,36 @@ class TestCase:
TestCase(
reason="The function should return the input as a result.",
req=fnv1.RunFunctionRequest(
input=resource.dict_to_struct({"example": "Hello, world"})
input=resource.dict_to_struct({"version": "v1beta2"}),
observed=fnv1.State(
composite=fnv1.Resource(
resource=resource.dict_to_struct(
{
"apiVersion": "example.crossplane.io/v1",
"kind": "XR",
"spec": {"region": "us-west-2"},
}
),
),
),
),
want=fnv1.RunFunctionResponse(
meta=fnv1.ResponseMeta(ttl=durationpb.Duration(seconds=60)),
desired=fnv1.State(),
results=[
fnv1.Result(
severity=fnv1.SEVERITY_NORMAL,
message="I was run with input Hello, world!",
)
],
desired=fnv1.State(
resources={
"bucket": fnv1.Resource(
resource=resource.dict_to_struct(
{
"apiVersion": "s3.aws.upbound.io/v1beta2",
"kind": "Bucket",
"spec": {
"forProvider": {"region": "us-west-2"},
},
}
),
),
},
),
context=structpb.Struct(),
),
),
Expand Down

0 comments on commit d95a331

Please sign in to comment.