Skip to content

Commit 7a4b4f5

Browse files
authored
Merge pull request #16 from ddevadat/dotnet-samples
dotnet function for sending message to ONS
2 parents adaee25 + 0f5a53a commit 7a4b4f5

File tree

15 files changed

+308
-2
lines changed

15 files changed

+308
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ This repository provides examples demonstrating how to use Oracle Functions.
2727
| 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)||[sample](./samples/oci-invoke-function-dotnet)|
2929
| Run a SQL statement against Autonomous DB using ORDS | [sample](./samples/oci-adb-ords-runsql-python) ||[sample](./samples/oci-adb-ords-runsql-dotnet)|
30-
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| [sample](./samples/oci-adb-client-runsql-dotnet)
31-
| Publish a notification using ONS |[sample](./samples/oci-ons-publish-python)|
30+
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| [sample](./samples/oci-adb-client-runsql-dotnet)|
31+
| Publish a notification using ONS |[sample](./samples/oci-ons-publish-python)||[sample](./samples/oci-ons-publish-dotnet)|
3232
| Send an email using Email Delivery Service |[sample](./samples/oci-email-send-python)|
3333
| Decrypt cipher using Vault keys |[sample](./samples/oci-vault-decrypt-python)||[sample](./samples/oci-vault-decrypt-dotnet)|
3434
| Get a secret from Vault |[sample](./samples/oci-vault-get-secret-python)||[sample](./samples/oci-vault-get-secret-dotnet)|
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
using System;
3+
using System.Threading.Tasks;
4+
using System.Text;
5+
6+
using Oci.Common;
7+
using Oci.Common.Auth;
8+
using Oci.OnsService;
9+
10+
11+
namespace PublishONS
12+
{
13+
public class ONSClientHelper
14+
{
15+
public static NotificationDataPlaneClient GetONSClient()
16+
{
17+
try
18+
{
19+
return new NotificationDataPlaneClient(ResourcePrincipalAuthenticationDetailsProvider.GetProvider(), new ClientConfiguration());
20+
}
21+
catch (Exception ex)
22+
{
23+
Console.WriteLine("Unable To Create Resource Principal Provider: {0}", ex.Message);
24+
Console.WriteLine("Defaulting to Instance Provider");
25+
return new NotificationDataPlaneClient(new InstancePrincipalsAuthenticationDetailsProvider(), new ClientConfiguration());
26+
}
27+
}
28+
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
using System;
3+
using System.Threading.Tasks;
4+
using System.Text;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using Oci.Common.Model;
8+
using Oci.Common;
9+
using Oci.Common.Auth;
10+
using Oci.OnsService;
11+
using Oci.OnsService.Models;
12+
using Oci.OnsService.Requests;
13+
using Oci.OnsService.Responses;
14+
15+
16+
namespace PublishONS
17+
{
18+
public class InvokeONSHelper
19+
{
20+
public static async Task<string> SendMessage(NotificationDataPlaneClient client, string topic_id, string msg_title, string msg_body)
21+
22+
{
23+
24+
try
25+
{
26+
27+
// Create a request and dependent object(s).
28+
var messageDetails = new Oci.OnsService.Models.MessageDetails
29+
{
30+
Title = msg_title,
31+
Body = msg_body
32+
};
33+
var publishMessageRequest = new Oci.OnsService.Requests.PublishMessageRequest
34+
{
35+
TopicId = topic_id,
36+
MessageDetails = messageDetails,
37+
MessageType = Oci.OnsService.Requests.PublishMessageRequest.MessageTypeEnum.RawText
38+
};
39+
40+
var response = await client.PublishMessage(publishMessageRequest);
41+
var messageIdValue = response.PublishResult.MessageId;
42+
return messageIdValue;
43+
44+
}
45+
46+
catch (OciException ex)
47+
{
48+
Console.WriteLine("Unable To Send Message to ONS : {0}", ex.Message);
49+
return "Failed " + ex.Message;
50+
}
51+
52+
}
53+
54+
55+
}
56+
}
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 PublishONS.csproj
5+
RUN dotnet build PublishONS.csproj -c Release
6+
RUN dotnet publish PublishONS.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", "PublishONS.dll"]
11+
CMD ["PublishONS: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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace PublishONS
4+
{
5+
6+
class InputMessage
7+
{
8+
public string topic_id { get; set; }
9+
public string msg_title { get; set; }
10+
public string msg_body { get; set; }
11+
12+
13+
}
14+
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace PublishONS
4+
{
5+
6+
class ObjectDetails
7+
{
8+
9+
public string result { get; set; }
10+
11+
12+
}
13+
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 Oci.OnsService;
9+
using Oci.OnsService.Models;
10+
11+
namespace PublishONS
12+
{
13+
class Function
14+
{
15+
public string function_handler(InputMessage input)
16+
{
17+
18+
Dictionary<string, List<ObjectDetails>> output = new Dictionary<string, List<ObjectDetails>>();
19+
var object_details_list = new List<ObjectDetails>();
20+
string topic_id = input.topic_id;
21+
string msg_title = input.msg_title;
22+
string msg_body = input.msg_body;
23+
24+
NotificationDataPlaneClient client = ONSClientHelper.GetONSClient();
25+
Task<string> messageIdValue = InvokeONSHelper.SendMessage(client, topic_id, msg_title, msg_body);
26+
27+
var object_detail = new ObjectDetails();
28+
object_detail.result = messageIdValue.Result;
29+
object_details_list.Add(object_detail);
30+
31+
output.Add("results", object_details_list);
32+
return System.Text.Json.JsonSerializer.Serialize(output);
33+
34+
}
35+
36+
static void Main(string[] args) { Fdk.Handle(args[0]); }
37+
38+
}
39+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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="OCI.DotNetSDK.Common" Version="42.1.0" />
14+
<PackageReference Include="OCI.DotNetSDK.Ons" Version="42.1.0" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Function that publishes a notification
2+
This function publishes a notification.
3+
4+
As you make your way through this tutorial, look out for this icon ![user input icon](./images/userinput.png).
5+
Whenever you see it, it's time for you to perform an action.
6+
7+
8+
## Prerequisites
9+
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)
10+
* A - Set up your tenancy
11+
* B - Create application
12+
* C - Set up your Cloud Shell dev environment
13+
14+
15+
## List Applications
16+
Assuming your have successfully completed the prerequisites, you should see your
17+
application in the list of applications.
18+
```
19+
fn ls apps
20+
```
21+
22+
23+
## Create or Update your Dynamic Group
24+
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).
25+
26+
When specifying the *Matching Rules*, we suggest matching all functions in a compartment with:
27+
```
28+
ALL {resource.type = 'fnfunc', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}
29+
```
30+
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.
31+
32+
33+
## Create or Update IAM Policies
34+
Create a new policy that allows the dynamic group to use the Notificaton Service. We will grant `use` access to `ons-topics` in the compartment.
35+
36+
![user input icon](./images/userinput.png)
37+
38+
Your policy should look something like this:
39+
```
40+
Allow dynamic-group <dynamic-group-name> to use ons-topics in compartment <compartment-name>
41+
```
42+
43+
For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).
44+
45+
46+
## Review and customize the function
47+
Review the following files in the current folder:
48+
* the code of the function, [PublishONS.cs](./PublishONS.cs)
49+
* its dependencies, [PublishONS.csproj](./PublishONS.csproj)
50+
* the function metadata, [func.yaml](./func.yaml)
51+
52+
53+
## Deploy the function
54+
In Cloud Shell, run the *fn deploy* command to build the function and its dependencies as a Docker image,
55+
push the image to OCIR, and deploy the function to Oracle Functions in your application.
56+
57+
![user input icon](./images/userinput.png)
58+
```
59+
fn -v deploy --app <app-name>
60+
```
61+
62+
63+
## Create the Notification topic
64+
Create a Notification topic with an email subscription.
65+
66+
![user input icon](./images/userinput.png)
67+
68+
Go to the OCI console, navigate to Application Integration > Notifications. Click *Create Topic*.
69+
70+
![create topic](./images/ons-create-topic.png)
71+
72+
Click on your topic and create a email subscription to this topic by clicking *Create Subscription* and entering your email address. You will receive an email to confirm your subscription.
73+
74+
![create subscription](./images/ons-create-email-subscription.png)
75+
76+
Note the OCID of your topic.
77+
78+
79+
## Invoke the function
80+
Run the following command to test your function.
81+
82+
![user input icon](./images/userinput.png)
83+
84+
```
85+
echo '{"topic_id": "<Topic-OCID>", "msg_title": "message-title", "msg_body": "message-body"}' | fn invoke <app-name> oci-ons-publish-dotnet
86+
```
87+
e.g.:
88+
```
89+
echo '{"topic_id": "ocid1.onstopic.xxxx", "msg_title": "a message from Functions", "msg_body": "This email was sent by Oracle Functions!"}' | fn invoke myapp oci-ons-publish-dotnet
90+
```
91+
92+
Upon success, you should receive an email from the Notification Service.
93+
94+
95+
## Monitoring Functions and Notifications Topics
96+
97+
Learn how to configure basic observability for your function and topic using metrics, alarms and email alerts:
98+
* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md)
99+
* [Basic Guidance for Monitoring your Notifications Topics](../basic-observability/notifications.md)
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-ons-publish-dotnet
3+
version: 0.0.99
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: PublishONS:Function:function_handler
8+
entrypoint: dotnet PublishONS.dll
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)