Omit FunctionRunner class in introductory Python examples

In reality you need this, but I don't think we need to introduce it
straight away. In the Go example we already omit the equivalent.

Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit is contained in:
Nic Cope 2024-01-11 17:16:13 -08:00
parent 69f641f894
commit 42f17d9650
1 changed files with 34 additions and 46 deletions

View File

@ -154,27 +154,21 @@ method in `function/fn.py`. When you first open the file it contains a "hello
world" function. world" function.
```python {label="hello-world"} ```python {label="hello-world"}
class FunctionRunner(grpcv1beta1.FunctionRunnerService): async def RunFunction(self, req: fnv1beta1.RunFunctionRequest, _: grpc.aio.ServicerContext) -> fnv1beta1.RunFunctionResponse:
def __init__(self): log = self.log.bind(tag=req.meta.tag)
self.log = logging.get_logger() log.info("Running function")
async def RunFunction( rsp = response.to(req)
self, req: fnv1beta1.RunFunctionRequest, _: grpc.aio.ServicerContext
) -> fnv1beta1.RunFunctionResponse:
log = self.log.bind(tag=req.meta.tag)
log.info("Running function")
rsp = response.to(req) example = ""
if "example" in req.input:
example = req.input["example"]
example = "" # TODO: Add your function logic here!
if "example" in req.input: response.normal(rsp, f"I was run with input {example}!")
example = req.input["example"] log.info("I was run!", input=example)
# TODO: Add your function logic here! return rsp
response.normal(rsp, f"I was run with input {example}!")
log.info("I was run!", input=example)
return rsp
``` ```
All Python composition functions have a `RunFunction` method. Crossplane passes All Python composition functions have a `RunFunction` method. Crossplane passes
@ -193,42 +187,36 @@ for `RunFunctionRequest` and `RunFunctionResponse` in the
Edit the `RunFunction` method to replace it with this code. Edit the `RunFunction` method to replace it with this code.
```python ```python
class FunctionRunner(grpcv1beta1.FunctionRunnerService): async def RunFunction(self, req: fnv1beta1.RunFunctionRequest, _: grpc.aio.ServicerContext) -> fnv1beta1.RunFunctionResponse:
def __init__(self): log = self.log.bind(tag=req.meta.tag)
self.log = logging.get_logger() log.info("Running function")
async def RunFunction( rsp = response.to(req)
self, req: fnv1beta1.RunFunctionRequest, _: grpc.aio.ServicerContext
) -> fnv1beta1.RunFunctionResponse:
log = self.log.bind(tag=req.meta.tag)
log.info("Running function")
rsp = response.to(req) region = req.observed.composite.resource["spec"]["region"]
names = req.observed.composite.resource["spec"]["names"]
region = req.observed.composite.resource["spec"]["region"] for name in names:
names = req.observed.composite.resource["spec"]["names"] rsp.desired.resources[f"xbuckets-{name}"].resource.update(
{
for name in names: "apiVersion": "s3.aws.upbound.io/v1beta1",
rsp.desired.resources[f"xbuckets-{name}"].resource.update( "kind": "Bucket",
{ "metadata": {
"apiVersion": "s3.aws.upbound.io/v1beta1", "annotations": {
"kind": "Bucket", "crossplane.io/external-name": name,
"metadata": {
"annotations": {
"crossplane.io/external-name": name,
},
}, },
"spec": { },
"forProvider": { "spec": {
"region": region, "forProvider": {
}, "region": region,
}, },
} },
) }
)
log.info("Added desired buckets", region=region, count=len(names)) log.info("Added desired buckets", region=region, count=len(names))
return rsp return rsp
``` ```
Expand the below block to view the full `fn.py`, including imports and Expand the below block to view the full `fn.py`, including imports and