Skip to content

dotnet function for sending message to ONS #16

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 1 commit into from
Oct 27, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ This repository provides examples demonstrating how to use Oracle Functions.
| 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)||[sample](./samples/oci-invoke-function-dotnet)|
| Run a SQL statement against Autonomous DB using ORDS | [sample](./samples/oci-adb-ords-runsql-python) ||[sample](./samples/oci-adb-ords-runsql-dotnet)|
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| [sample](./samples/oci-adb-client-runsql-dotnet)
| Publish a notification using ONS |[sample](./samples/oci-ons-publish-python)|
| Run a SQL statement against Autonomous DB using DB Client |[sample](./samples/oci-adb-client-runsql-python)|| [sample](./samples/oci-adb-client-runsql-dotnet)|
| Publish a notification using ONS |[sample](./samples/oci-ons-publish-python)||[sample](./samples/oci-ons-publish-dotnet)|
| Send an email using Email Delivery Service |[sample](./samples/oci-email-send-python)|
| Decrypt cipher using Vault keys |[sample](./samples/oci-vault-decrypt-python)||[sample](./samples/oci-vault-decrypt-dotnet)|
| Get a secret from Vault |[sample](./samples/oci-vault-get-secret-python)||[sample](./samples/oci-vault-get-secret-dotnet)|
Expand Down
30 changes: 30 additions & 0 deletions samples/oci-ons-publish-dotnet/Common/ONSClientHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

using System;
using System.Threading.Tasks;
using System.Text;

using Oci.Common;
using Oci.Common.Auth;
using Oci.OnsService;


namespace PublishONS
{
public class ONSClientHelper
{
public static NotificationDataPlaneClient GetONSClient()
{
try
{
return new NotificationDataPlaneClient(ResourcePrincipalAuthenticationDetailsProvider.GetProvider(), new ClientConfiguration());
}
catch (Exception ex)
{
Console.WriteLine("Unable To Create Resource Principal Provider: {0}", ex.Message);
Console.WriteLine("Defaulting to Instance Provider");
return new NotificationDataPlaneClient(new InstancePrincipalsAuthenticationDetailsProvider(), new ClientConfiguration());
}
}

}
}
56 changes: 56 additions & 0 deletions samples/oci-ons-publish-dotnet/Controller/InvokeONSHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

using System;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using System.IO;
using Oci.Common.Model;
using Oci.Common;
using Oci.Common.Auth;
using Oci.OnsService;
using Oci.OnsService.Models;
using Oci.OnsService.Requests;
using Oci.OnsService.Responses;


namespace PublishONS
{
public class InvokeONSHelper
{
public static async Task<string> SendMessage(NotificationDataPlaneClient client, string topic_id, string msg_title, string msg_body)

{

try
{

// Create a request and dependent object(s).
var messageDetails = new Oci.OnsService.Models.MessageDetails
{
Title = msg_title,
Body = msg_body
};
var publishMessageRequest = new Oci.OnsService.Requests.PublishMessageRequest
{
TopicId = topic_id,
MessageDetails = messageDetails,
MessageType = Oci.OnsService.Requests.PublishMessageRequest.MessageTypeEnum.RawText
};

var response = await client.PublishMessage(publishMessageRequest);
var messageIdValue = response.PublishResult.MessageId;
return messageIdValue;

}

catch (OciException ex)
{
Console.WriteLine("Unable To Send Message to ONS : {0}", ex.Message);
return "Failed " + ex.Message;
}

}


}
}
11 changes: 11 additions & 0 deletions samples/oci-ons-publish-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 PublishONS.csproj
RUN dotnet build PublishONS.csproj -c Release
RUN dotnet publish PublishONS.csproj -c Release -o out
FROM fnproject/dotnet:3.1-1.0.4
WORKDIR /function
COPY --from=build-stage /function/out/ /function/
ENTRYPOINT ["dotnet", "PublishONS.dll"]
CMD ["PublishONS:Function:function_handler"]
17 changes: 17 additions & 0 deletions samples/oci-ons-publish-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
15 changes: 15 additions & 0 deletions samples/oci-ons-publish-dotnet/Models/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace PublishONS
{

class InputMessage
{
public string topic_id { get; set; }
public string msg_title { get; set; }
public string msg_body { get; set; }


}

}
14 changes: 14 additions & 0 deletions samples/oci-ons-publish-dotnet/Models/ObjectDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace PublishONS
{

class ObjectDetails
{

public string result { get; set; }


}

}
39 changes: 39 additions & 0 deletions samples/oci-ons-publish-dotnet/PublishONS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 Oci.OnsService;
using Oci.OnsService.Models;

namespace PublishONS
{
class Function
{
public string function_handler(InputMessage input)
{

Dictionary<string, List<ObjectDetails>> output = new Dictionary<string, List<ObjectDetails>>();
var object_details_list = new List<ObjectDetails>();
string topic_id = input.topic_id;
string msg_title = input.msg_title;
string msg_body = input.msg_body;

NotificationDataPlaneClient client = ONSClientHelper.GetONSClient();
Task<string> messageIdValue = InvokeONSHelper.SendMessage(client, topic_id, msg_title, msg_body);

var object_detail = new ObjectDetails();
object_detail.result = messageIdValue.Result;
object_details_list.Add(object_detail);

output.Add("results", object_details_list);
return System.Text.Json.JsonSerializer.Serialize(output);

}

static void Main(string[] args) { Fdk.Handle(args[0]); }

}
}
17 changes: 17 additions & 0 deletions samples/oci-ons-publish-dotnet/PublishONS.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<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="OCI.DotNetSDK.Common" Version="42.1.0" />
<PackageReference Include="OCI.DotNetSDK.Ons" Version="42.1.0" />
</ItemGroup>

</Project>
99 changes: 99 additions & 0 deletions samples/oci-ons-publish-dotnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Function that publishes a notification
This function publishes a notification.

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.


## Create or Update IAM Policies
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.

![user input icon](./images/userinput.png)

Your policy should look something like this:
```
Allow dynamic-group <dynamic-group-name> to use ons-topics in compartment <compartment-name>
```

For more information on how to create policies, check the [documentation](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policysyntax.htm).


## Review and customize the function
Review the following files in the current folder:
* the code of the function, [PublishONS.cs](./PublishONS.cs)
* its dependencies, [PublishONS.csproj](./PublishONS.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 Notification topic
Create a Notification topic with an email subscription.

![user input icon](./images/userinput.png)

Go to the OCI console, navigate to Application Integration > Notifications. Click *Create Topic*.

![create topic](./images/ons-create-topic.png)

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.

![create subscription](./images/ons-create-email-subscription.png)

Note the OCID of your topic.


## Invoke the function
Run the following command to test your function.

![user input icon](./images/userinput.png)

```
echo '{"topic_id": "<Topic-OCID>", "msg_title": "message-title", "msg_body": "message-body"}' | fn invoke <app-name> oci-ons-publish-dotnet
```
e.g.:
```
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
```

Upon success, you should receive an email from the Notification Service.


## Monitoring Functions and Notifications Topics

Learn how to configure basic observability for your function and topic using metrics, alarms and email alerts:
* [Basic Guidance for Monitoring your Functions](../basic-observability/functions.md)
* [Basic Guidance for Monitoring your Notifications Topics](../basic-observability/notifications.md)
8 changes: 8 additions & 0 deletions samples/oci-ons-publish-dotnet/func.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
schema_version: 20180708
name: oci-ons-publish-dotnet
version: 0.0.99
runtime: dotnet3.1
build_image: fnproject/dotnet:3.1-1.0.4-dev
run_image: fnproject/dotnet:3.1-1.0.4
cmd: PublishONS:Function:function_handler
entrypoint: dotnet PublishONS.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.