Skip to content

Log oci events dotnet function #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)||
Expand Down
11 changes: 11 additions & 0 deletions samples/oci-event-display-dotnet/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
17 changes: 17 additions & 0 deletions samples/oci-event-display-dotnet/Function.sln
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions samples/oci-event-display-dotnet/LogEvents.cs
Original file line number Diff line number Diff line change
@@ -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<string, List<ObjectDetails>> output = new Dictionary<string, List<ObjectDetails>>();
var object_details_list = new List<ObjectDetails>();
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]); }

}
}
16 changes: 16 additions & 0 deletions samples/oci-event-display-dotnet/LogEvents.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Fnproject.Fn.Fdk" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions samples/oci-event-display-dotnet/Models/ObjectDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace LogEvents
{

class ObjectDetails
{
public string result { get; set; }


}

}
99 changes: 99 additions & 0 deletions samples/oci-event-display-dotnet/README.md
Original file line number Diff line number Diff line change
@@ -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 <app-name>
```


## 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)

8 changes: 8 additions & 0 deletions samples/oci-event-display-dotnet/func.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.