Skip to content

Commit ad8d8b1

Browse files
authored
Merge pull request #9 from ddevadat/dotnet-samples
Log oci events dotnet function
2 parents a238aab + 003a810 commit ad8d8b1

File tree

12 files changed

+203
-17
lines changed

12 files changed

+203
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This repository provides examples demonstrating how to use Oracle Functions.
2424
| 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)|
2525
| Create a PAR in OCI Object Storage |[sample](./samples/oci-objectstorage-create-par-python)||[sample](./samples/oci-objectstorage-create-par-dotnet)|
2626
| Copy object from one OCI Object Storage bucket to another |[sample](./samples/oci-objectstorage-copy-objects-python)||[sample](./samples/oci-objectstorage-copy-objects-dotnet)|
27-
| Display an OCI Cloud Event |[sample](./samples/oci-event-display-python)|
27+
| Display an OCI Cloud Event |[sample](./samples/oci-event-display-python)||[sample](./samples/oci-event-display-dotnet)|
2828
| Invoke another Function using the OCI SDK |[sample](./samples/oci-invoke-function-python)|||
2929
| Run a SQL statement against Autonomous DB using ORDS | [sample](./samples/oci-adb-ords-runsql-python) |
3030
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)||
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM fnproject/dotnet:3.1-1.0.4-dev as build-stage
2+
WORKDIR /function
3+
COPY . .
4+
RUN dotnet sln add LogEvents.csproj
5+
RUN dotnet build LogEvents.csproj -c Release
6+
RUN dotnet publish LogEvents.csproj -c Release -o out
7+
FROM fnproject/dotnet:3.1-1.0.4
8+
WORKDIR /function
9+
COPY --from=build-stage /function/out/ /function/
10+
ENTRYPOINT ["dotnet", "LogEvents.dll"]
11+
CMD ["LogEvents:Function:function_handler"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio 15
3+
VisualStudioVersion = 15.0.26124.0
4+
MinimumVisualStudioVersion = 15.0.26124.0
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Debug|x64 = Debug|x64
9+
Debug|x86 = Debug|x86
10+
Release|Any CPU = Release|Any CPU
11+
Release|x64 = Release|x64
12+
Release|x86 = Release|x86
13+
EndGlobalSection
14+
GlobalSection(SolutionProperties) = preSolution
15+
HideSolutionNode = FALSE
16+
EndGlobalSection
17+
EndGlobal
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Fnproject.Fn.Fdk;
2+
using System.Runtime.CompilerServices;
3+
using System.Collections.Generic;
4+
using System;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
8+
using Newtonsoft.Json;
9+
10+
11+
namespace LogEvents
12+
{
13+
class Function
14+
{
15+
public string function_handler(String input)
16+
{
17+
18+
Dictionary<string, List<ObjectDetails>> output = new Dictionary<string, List<ObjectDetails>>();
19+
var object_details_list = new List<ObjectDetails>();
20+
dynamic event_json = JsonConvert.DeserializeObject(input);
21+
var event_str = Newtonsoft.Json.JsonConvert.SerializeObject(event_json, Newtonsoft.Json.Formatting.Indented);
22+
Console.WriteLine("event type: {0} ", event_json.eventType);
23+
Console.WriteLine("compartment name: {0} ", event_json.data.compartmentName);
24+
Console.WriteLine("Full Cloud event json data:");
25+
Console.WriteLine(event_str);
26+
var object_detail = new ObjectDetails();
27+
28+
object_detail.result = event_str;
29+
30+
output.Add("results", object_details_list);
31+
return System.Text.Json.JsonSerializer.Serialize(output);
32+
33+
}
34+
35+
static void Main(string[] args) { Fdk.Handle(args[0]); }
36+
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Fnproject.Fn.Fdk" Version="1.0.4" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace LogEvents
4+
{
5+
6+
class ObjectDetails
7+
{
8+
public string result { get; set; }
9+
10+
11+
}
12+
13+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Function that logs the details of a Cloud event
2+
3+
This function logs the details of a Cloud event.
4+
5+
As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png).
6+
Whenever you see it, it's time for you to perform an action.
7+
8+
9+
## Prerequisites
10+
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)
11+
* A - Set up your tenancy
12+
* B - Create application
13+
* C - Set up your Cloud Shell dev environment
14+
15+
16+
## List Applications
17+
Assuming your have successfully completed the prerequisites, you should see your
18+
application in the list of applications.
19+
```
20+
fn ls apps
21+
```
22+
23+
24+
## Create or Update your Dynamic Group
25+
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).
26+
27+
When specifying the *Matching Rules*, we suggest matching all functions in a compartment with:
28+
```
29+
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
30+
```
31+
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.
32+
33+
34+
## Review and customize your function
35+
Review the following files in the current folder:
36+
* the code of the function, [LogEvents.cs](./LogEvents.cs)
37+
* its dependencies, [LogEvents.csproj](./LogEvents.csproj)
38+
* the function metadata, [func.yaml](./func.yaml)
39+
40+
41+
## Deploy the function
42+
In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image,
43+
push the image to OCIR, and deploy the function to Oracle Functions in your application.
44+
45+
![user input icon](./images/userinput.png)
46+
```
47+
fn -v deploy --app <app-name>
48+
```
49+
50+
51+
## Create the Cloud Event rule
52+
Create a Cloud Event rule on the console navigating to Application Integration > Event Service. Click *Create Rule*.
53+
54+
![user input icon](./images/1-create_event_rule.png)
55+
56+
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*.
57+
58+
![user input icon](./images/2-create_event_rule.png)
59+
60+
## Test
61+
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.
62+
For example:
63+
```json
64+
event type: com.oraclecloud.objectstorage.createbucket
65+
compartment name: greg-verstraeten
66+
Full Cloud event json data:
67+
{
68+
"eventType": "com.oraclecloud.objectstorage.createbucket",
69+
"cloudEventsVersion": "0.1",
70+
"eventTypeVersion": "2.0",
71+
"source": "ObjectStorage",
72+
"eventTime": "2019-12-12T22:25:08.502Z",
73+
"contentType": "application/json",
74+
"data": {
75+
"compartmentId": "ocid1.compartment.oc1..aaaaaaaal66tw5k262fsjsrgdqan5cbbfxvoydbhxx5hijth2h3qlbwmtdlq",
76+
"compartmentName": "greg-verstraeten",
77+
"resourceName": "bucket-20191212-1425",
78+
"resourceId": "/n/devrel/b/",
79+
"availabilityDomain": "PHX-AD-2",
80+
"additionalDetails": {
81+
"bucketName": "bucket-20191212-1425",
82+
"publicAccessType": "NoPublicAccess",
83+
"namespace": "devrel",
84+
"eTag": "47b12898-1925-449a-a761-7d1db57f0695"
85+
}
86+
},
87+
"eventID": "fca0653f-85c5-9466-8812-001c51d338a4",
88+
"extensions": {
89+
"compartmentId": "ocid1.compartment.oc1..aaaaaaaal66tw5k262fsjsrgdqan5cbbfxvoydbhxx5hijth2h3qlbwmtdlq"
90+
}
91+
}
92+
```
93+
94+
95+
## Monitoring Functions
96+
97+
Learn how to configure basic observability for your function using metrics, alarms and email alerts:
98+
* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md)
99+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
schema_version: 20180708
2+
name: oci-event-display-dotnet
3+
version: 0.0.71
4+
runtime: dotnet3.1
5+
build_image: fnproject/dotnet:3.1-1.0.4-dev
6+
run_image: fnproject/dotnet:3.1-1.0.4
7+
cmd: LogEvents:Function:function_handler
8+
entrypoint: dotnet LogEvents.dll
Loading
Loading
Loading

samples/oci-objectstorage-copy-objects-dotnet/Models/InputMessage.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)