diff --git a/README.md b/README.md index 830150c..505275f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This repository provides examples demonstrating how to use Oracle Functions. | Create an object in OCI Object Storage |[sample](./samples/oci-objectstorage-put-object-python)|[sample](./samples/oci-objectstorage-put-object-java)|[sample](./samples/oci-objectstorage-put-object-dotnet)| | Create a PAR in OCI Object Storage |[sample](./samples/oci-objectstorage-create-par-python)||[sample](./samples/oci-objectstorage-create-par-dotnet)| | Copy object from one OCI Object Storage bucket to another |[sample](./samples/oci-objectstorage-copy-objects-python)||[sample](./samples/oci-objectstorage-copy-objects-dotnet)| -| Display an OCI Cloud Event |[sample](./samples/oci-event-display-python)| +| Display an OCI Cloud Event |[sample](./samples/oci-event-display-python)||[sample](./samples/oci-event-display-dotnet)| | Invoke another Function using the OCI SDK |[sample](./samples/oci-invoke-function-python)||| | Run a SQL statement against Autonomous DB using ORDS | [sample](./samples/oci-adb-ords-runsql-python) | | Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| diff --git a/samples/oci-event-display-dotnet/Dockerfile b/samples/oci-event-display-dotnet/Dockerfile new file mode 100644 index 0000000..9ebfdd5 --- /dev/null +++ b/samples/oci-event-display-dotnet/Dockerfile @@ -0,0 +1,11 @@ +FROM fnproject/dotnet:3.1-1.0.4-dev as build-stage +WORKDIR /function +COPY . . +RUN dotnet sln add LogEvents.csproj +RUN dotnet build LogEvents.csproj -c Release +RUN dotnet publish LogEvents.csproj -c Release -o out +FROM fnproject/dotnet:3.1-1.0.4 +WORKDIR /function +COPY --from=build-stage /function/out/ /function/ +ENTRYPOINT ["dotnet", "LogEvents.dll"] +CMD ["LogEvents:Function:function_handler"] \ No newline at end of file diff --git a/samples/oci-event-display-dotnet/Function.sln b/samples/oci-event-display-dotnet/Function.sln new file mode 100644 index 0000000..f87916a --- /dev/null +++ b/samples/oci-event-display-dotnet/Function.sln @@ -0,0 +1,17 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/samples/oci-event-display-dotnet/LogEvents.cs b/samples/oci-event-display-dotnet/LogEvents.cs new file mode 100644 index 0000000..360ff5e --- /dev/null +++ b/samples/oci-event-display-dotnet/LogEvents.cs @@ -0,0 +1,38 @@ +using Fnproject.Fn.Fdk; +using System.Runtime.CompilerServices; +using System.Collections.Generic; +using System; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using Newtonsoft.Json; + + +namespace LogEvents +{ + class Function + { + public string function_handler(String input) + { + + Dictionary> output = new Dictionary>(); + var object_details_list = new List(); + dynamic event_json = JsonConvert.DeserializeObject(input); + var event_str = Newtonsoft.Json.JsonConvert.SerializeObject(event_json, Newtonsoft.Json.Formatting.Indented); + Console.WriteLine("event type: {0} ", event_json.eventType); + Console.WriteLine("compartment name: {0} ", event_json.data.compartmentName); + Console.WriteLine("Full Cloud event json data:"); + Console.WriteLine(event_str); + var object_detail = new ObjectDetails(); + + object_detail.result = event_str; + + output.Add("results", object_details_list); + return System.Text.Json.JsonSerializer.Serialize(output); + + } + + static void Main(string[] args) { Fdk.Handle(args[0]); } + + } +} diff --git a/samples/oci-event-display-dotnet/LogEvents.csproj b/samples/oci-event-display-dotnet/LogEvents.csproj new file mode 100644 index 0000000..7d80a83 --- /dev/null +++ b/samples/oci-event-display-dotnet/LogEvents.csproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + diff --git a/samples/oci-event-display-dotnet/Models/ObjectDetails.cs b/samples/oci-event-display-dotnet/Models/ObjectDetails.cs new file mode 100644 index 0000000..adc608f --- /dev/null +++ b/samples/oci-event-display-dotnet/Models/ObjectDetails.cs @@ -0,0 +1,13 @@ +using System; + +namespace LogEvents +{ + + class ObjectDetails + { + public string result { get; set; } + + + } + +} diff --git a/samples/oci-event-display-dotnet/README.md b/samples/oci-event-display-dotnet/README.md new file mode 100644 index 0000000..29f7069 --- /dev/null +++ b/samples/oci-event-display-dotnet/README.md @@ -0,0 +1,99 @@ +# Function that logs the details of a Cloud event + +This function logs the details of a Cloud event. + +As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png). +Whenever you see it, it's time for you to perform an action. + + +## Prerequisites +Before you deploy this sample function, make sure you have run step A, B and C of the [Oracle Functions Quick Start Guide for Cloud Shell](https://www.oracle.com/webfolder/technetwork/tutorials/infographics/oci_functions_cloudshell_quickview/functions_quickview_top/functions_quickview/index.html) +* A - Set up your tenancy +* B - Create application +* C - Set up your Cloud Shell dev environment + + +## List Applications +Assuming your have successfully completed the prerequisites, you should see your +application in the list of applications. +``` +fn ls apps +``` + + +## Create or Update your Dynamic Group +In order to use other OCI Services, your function must be part of a dynamic group. For information on how to create a dynamic group, refer to the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Tasks/managingdynamicgroups.htm#To). + +When specifying the *Matching Rules*, we suggest matching all functions in a compartment with: +``` +ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'} +``` +Please check the [Accessing Other Oracle Cloud Infrastructure Resources from Running Functions](https://docs.cloud.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsaccessingociresources.htm) for other *Matching Rules* options. + + +## Review and customize your function +Review the following files in the current folder: +* the code of the function, [LogEvents.cs](./LogEvents.cs) +* its dependencies, [LogEvents.csproj](./LogEvents.csproj) +* the function metadata, [func.yaml](./func.yaml) + + +## Deploy the function +In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image, +push the image to OCIR, and deploy the function to Oracle Functions in your application. + +![user input icon](./images/userinput.png) +``` +fn -v deploy --app +``` + + +## Create the Cloud Event rule +Create a Cloud Event rule on the console navigating to Application Integration > Event Service. Click *Create Rule*. + +![user input icon](./images/1-create_event_rule.png) + +Assign a display name and a description, customize the Rule Conditions or leave them empty to match all events. In the *Actions* section, set the *Action type* as "Functions", select your *Function Compartment*, your *Function Application*, and your *Function ID*. + +![user input icon](./images/2-create_event_rule.png) + +## Test +Go to the logs, you should see events from your compartment. You can create some resource such as an Object Storage bucket to generate an event. +For example: +```json +event type: com.oraclecloud.objectstorage.createbucket +compartment name: greg-verstraeten +Full Cloud event json data: +{ + "eventType": "com.oraclecloud.objectstorage.createbucket", + "cloudEventsVersion": "0.1", + "eventTypeVersion": "2.0", + "source": "ObjectStorage", + "eventTime": "2019-12-12T22:25:08.502Z", + "contentType": "application/json", + "data": { + "compartmentId": "ocid1.compartment.oc1..aaaaaaaal66tw5k262fsjsrgdqan5cbbfxvoydbhxx5hijth2h3qlbwmtdlq", + "compartmentName": "greg-verstraeten", + "resourceName": "bucket-20191212-1425", + "resourceId": "/n/devrel/b/", + "availabilityDomain": "PHX-AD-2", + "additionalDetails": { + "bucketName": "bucket-20191212-1425", + "publicAccessType": "NoPublicAccess", + "namespace": "devrel", + "eTag": "47b12898-1925-449a-a761-7d1db57f0695" + } + }, + "eventID": "fca0653f-85c5-9466-8812-001c51d338a4", + "extensions": { + "compartmentId": "ocid1.compartment.oc1..aaaaaaaal66tw5k262fsjsrgdqan5cbbfxvoydbhxx5hijth2h3qlbwmtdlq" + } +} +``` + + +## Monitoring Functions + +Learn how to configure basic observability for your function using metrics, alarms and email alerts: +* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md) + diff --git a/samples/oci-event-display-dotnet/func.yaml b/samples/oci-event-display-dotnet/func.yaml new file mode 100644 index 0000000..137c932 --- /dev/null +++ b/samples/oci-event-display-dotnet/func.yaml @@ -0,0 +1,8 @@ +schema_version: 20180708 +name: oci-event-display-dotnet +version: 0.0.71 +runtime: dotnet3.1 +build_image: fnproject/dotnet:3.1-1.0.4-dev +run_image: fnproject/dotnet:3.1-1.0.4 +cmd: LogEvents:Function:function_handler +entrypoint: dotnet LogEvents.dll diff --git a/samples/oci-event-display-dotnet/images/1-create_event_rule.png b/samples/oci-event-display-dotnet/images/1-create_event_rule.png new file mode 100644 index 0000000..33bf415 Binary files /dev/null and b/samples/oci-event-display-dotnet/images/1-create_event_rule.png differ diff --git a/samples/oci-event-display-dotnet/images/2-create_event_rule.png b/samples/oci-event-display-dotnet/images/2-create_event_rule.png new file mode 100644 index 0000000..d1bace0 Binary files /dev/null and b/samples/oci-event-display-dotnet/images/2-create_event_rule.png differ diff --git a/samples/oci-event-display-dotnet/images/userinput.png b/samples/oci-event-display-dotnet/images/userinput.png new file mode 100644 index 0000000..ce6a202 Binary files /dev/null and b/samples/oci-event-display-dotnet/images/userinput.png differ diff --git a/samples/oci-objectstorage-copy-objects-dotnet/Models/InputMessage.cs b/samples/oci-objectstorage-copy-objects-dotnet/Models/InputMessage.cs deleted file mode 100644 index e464532..0000000 --- a/samples/oci-objectstorage-copy-objects-dotnet/Models/InputMessage.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace CopyObjects -{ - - class InputMessage - { - public string objectName { get; set; } - public string bucketName { get; set; } - public string namespaceName { get; set; } - - public string content { get; set; } - - } - -}