Before 23.04 one had to create a REST connection for OCI functions, now, thanks to a capability RPST - Resource Principal authentication.
The resource principal provider uses a resource provider session token (RPST) that enables the function to authenticate itself with other Oracle Cloud Infrastructure services. The token is only valid for the resources to which the dynamic group has been granted access. This requires one to use Identity Domains.
Ergo, instead of having to create a connection, define security etc., we get access, based on policies granted to the OIC instance.
Essentially, you create dynamic group with a matching rule that includes your OIC ocid(s). e.g.
myOICDynGroup
Matching Rule: resource.id = myOIC clientId
You now create a policy leveraging the above -
allow dynamic-group myOICDynGroup to manage functions-family in compartment myFnCompartment
So where to find the OIC instance client id?
Finally, I create the policy -
So now, we know how to do the pre-requisites, let's kick the tyres -
My simple integration above invokes a simple python based function. Here's the actual code -
import json
import logging
from fdk import response
discount = 5
try:
body = json.loads(data.getvalue())
product = body.get("product")
unitsOrdered = body.get("unitsOrdered")
except (Exception, ValueError) as ex:
logging.getLogger().info('error parsing json payload: ' + str(ex))
try:
body = json.loads(data.getvalue())
product = body.get("product")
unitsOrdered = body.get("unitsOrdered")
except (Exception, ValueError) as ex:
logging.getLogger().info('error parsing json payload: ' + str(ex))
logging.getLogger().info("Inside Python order discount function")
if product == "iBike" and int(unitsOrdered) > 30:
discount = 10
else:
discount = 5
return response.Response(
ctx, response_data=json.dumps(
{"product": product,
"discount": discount}),
headers={"Content-Type": "application/json"}
)
"discount": 10}
The actual functions are contained within an application - here is the orderDiscount function I will invoke -
No comments:
Post a Comment