Leveraging OCI Functions to invoke OIC Integrations
In today’s blog we’ll discuss on how to Leverage OCI Functions to invoke OIC Integrations.
Pre-Requisites
- Access to OCI Console.
- Access to OIC Instance and availability of Integrations to cover below scenarios.
- Access to create Function Applications & Functions within a compartment.
- Availability of a VCN & Subnet with a Function Application created.
- Availability of AuthToken for your user.
Assuming the above pre-requisites are met. We will discuss on how to call OIC Integrations from OCI Functions covering below scenarios.
Scenarios
a. Calling OIC Integration which doesn’t need any input Parameters → Calling an OIC EchoWorld Integration from OCI Functions.
b. Calling OIC Integration with a single Input & Output Parameter → Calling an OIC Hello World Integration from OCI Functions.
c. Calling OIC Integration with a single Input & Multiple Output parameters → Calling an OIC FetchEmpData Integration from OCI Functions.
d. Calling OIC Integration with multiple Input & Multiple Output parameters → Calling an OIC FetchEmpDeptData Integration from OCI Functions.
NOTE: To explain all these scenarios I am choosing Python as my language to build Functions. I will be building and invoking functions from OCI Cloud Shell in this blog. You can set it up on your local as well which ain’t scope of this blog.
How to create to the Fn environment and create a boiler plate function is given in detail in OCI Console.
There are 3 parts to any function: (I) Creating (II) Deploying & (III) Invoking
Scenario (a) → Calling EchoWorld Integration (without Parameters)
Generating Boiler plate code
In this we’ll first generate a boiler plate code for function using the command
fn init — runtime python OICEchoWorld
These creates a function code with a directory OICEchoWorld in a chosen repo with 3 files (func.yaml, func.py & requirements.txt).
In the above snippet you are seeing an extra file InvokeEchoWorld.py, which we have created to invoke OIC Integration.
You might ask as to why can’t we write the entire code in a single .py file rather than separate files? We can very well do that, but I want to keep func.py as standard file which can be replicated across different other functions and keep another function specific to calling OIC Integration.
func.py
- Boiler plate creates a handler function which will where the incoming hit to your function land.
- Declaring logging i.e., level of logging and format of log i.e., with time followed by logging level (INFO) and the message to be printed in your log.
- declaring response variable to be returned
- storing data from input configuration variables in temp variables.
- Imported the integration related .py file, calling function/method inside that file along with parameters and storing output of that call in “resp” variable.
InvokeEchoWorld.py
- storing username & password in a temp variable, to be passed to OIC call.
- sending GET method request to the Integration endpoint via oicbaseurl along with credentials.
- setting values to be returned in case of success. Sending “Message” field value from Integration output via ‘output’ field.
requirements.txt
Need to add few more records here w.r.t OCI version and requests package which needs to be imported for calling OIC Integration.
func.yaml
This file basically gives your information as to name, version, runtime, memory etc., of your function.
Deploying Function
fn -v deploy — app <FunctionApplication>
Configuring Function Variables
Post deploying the function, in my case I am setting OIC Integration endpoint and credentials to make call.
Invoke Function
To invoke function you need to know the name of function, which can be obtained via the below command:
fn list functions <FunctionApplication>
fn -v invoke <FunctionApplication> <FunctionName>
Trigger in OIC
Scenario (b) → Calling HelloWorld Integration (with Single Input & Output Parameters)
Here I will be covering only the Integration specific .py file and how to invoke it. As rest of the part remains the same as in scenario (a).
InvokeHelloWorld.py
Invoke Function
Here, I need to pass “Name” as Input to the Integration
Trigger in OIC
Scenario (c) → Calling FetchEmpData Integration (with Single Input Parameter and multiple output parameters)
Here I will be covering only the Integration specific .py file and how to invoke it. As rest of the part remains the same as in scenario (a).
InvokeFetchEmpData.py
The only difference in InvokeFetchEmpData.py when compared to the previous files is the output variable.
Invoke Function
Here, I need to pass “EmpId” as Input to the Integration
Trigger in OIC
Scenario (d) → Calling FetchEmpDeptData Integration (with Multiple Input parameters and multiple output parameters)
Here I will be covering only the Integration specific .py file and how to invoke it. As rest of the part remains the same as in scenario (a).
InvokeFetchEmpDeptData.py
Invoke Function
Here, I need to pass “EmpId” & “DeptNo” as Input to the Integration
Trigger in OIC
!!! HAPPY READING!!!
Code for these is available in here → https://github.com/santhoshbvsrk/OCIFunctions