ballerinax/azure_functions : 1.0.0

Overview

This module provides an annotation-based Azure Functions extension implementation for Ballerina.

Supported Annotations:

@azure.functions:Function

Custom 'host.json'

A custom host.json file for the functions deployment can be optionally provided by placing a 'host.json' file in the current working directory where the bal build is done. The required host.json properties are provided/overridden by the values derived from the source code by the compiler extension.

Usage Sample:

1import ballerina/uuid;
2import ballerinax/azure_functions as af;
3
4// HTTP request/response with no authentication
5@af:Function
6public function hello(@af:HTTPTrigger { authLevel: "anonymous" } string payload)
7 returns @af:HTTPOutput string|error {
8 return "Hello, " + payload + "!";
9}
10
11// HTTP request to add data to a queue
12@af:Function
13public function fromHttpToQueue(af:Context ctx,
14 @af:HTTPTrigger {} af:HTTPRequest req,
15 @af:QueueOutput { queueName: "queue1" } af:StringOutputBinding msg)
16 returns @af:HTTPOutput af:HTTPBinding {
17 msg.value = req.body;
18 return { statusCode: 200, payload: "Request: " + req.toString() };
19}
20
21// A message put to a queue is copied to another queue
22@af:Function
23public function fromQueueToQueue(af:Context ctx,
24 @af:QueueTrigger { queueName: "queue2" } string inMsg,
25 @af:QueueOutput { queueName: "queue3" } af:StringOutputBinding outMsg) {
26 ctx.log("In Message: " + inMsg);
27 ctx.log("Metadata: " + ctx.metadata.toString());
28 outMsg.value = inMsg;
29}
30
31// A blob added to a container is copied to a queue
32@af:Function
33public function fromBlobToQueue(af:Context ctx,
34 @af:BlobTrigger { path: "bpath1/{name}" } byte[] blobIn,
35 @af:BindingName { } string name,
36 @af:QueueOutput { queueName: "queue3" } af:StringOutputBinding outMsg)
37 returns error? {
38 outMsg.value = "Name: " + name + " Content: " + blobIn.toString();
39}
40
41// HTTP request to read a blob value
42@af:Function
43public function httpTriggerBlobInput(@af:HTTPTrigger { } af:HTTPRequest req,
44 @af:BlobInput { path: "bpath1/{Query.name}" } byte[]? blobIn)
45 returns @af:HTTPOutput string {
46 int length = 0;
47 if blobIn is byte[] {
48 length = blobIn.length();
49 }
50 return "Blob: " + req.query["name"].toString() + " Length: " +
51 length.toString() + " Content: " + blobIn.toString();
52}
53
54// HTTP request to add a new blob
55@af:Function
56public function httpTriggerBlobOutput(@af:HTTPTrigger { } af:HTTPRequest req,
57 @af:BlobOutput { path: "bpath1/{Query.name}" } af:StringOutputBinding bb)
58 returns @af:HTTPOutput string|error {
59 bb.value = req.body;
60 return "Blob: " + req.query["name"].toString() + " Content: " +
61 bb?.value.toString();
62}
63
64// Sending an SMS
65@af:Function
66public function sendSMS(@af:HTTPTrigger { } af:HTTPRequest req,
67 @af:TwilioSmsOutput { fromNumber: "+12069845840" }
68 af:TwilioSmsOutputBinding tb)
69 returns @af:HTTPOutput string {
70 tb.to = req.query["to"].toString();
71 tb.body = req.body.toString();
72 return "Message - to: " + tb?.to.toString() + " body: " + tb?.body.toString();
73}
74
75public type Person record {
76 string id;
77 string name;
78 string country;
79};
80
81// CosmosDB record trigger
82@af:Function
83public function cosmosDBToQueue1(@af:CosmosDBTrigger {
84 connectionStringSetting: "CosmosDBConnection", databaseName: "db1",
85 collectionName: "c1" } Person[] req,
86 @af:QueueOutput { queueName: "queue3" } af:StringOutputBinding outMsg) {
87 outMsg.value = req.toString();
88}
89
90@af:Function
91public function cosmosDBToQueue2(@af:CosmosDBTrigger {
92 connectionStringSetting: "CosmosDBConnection", databaseName: "db1",
93 collectionName: "c2" } json req,
94 @af:QueueOutput { queueName: "queue3" } af:StringOutputBinding outMsg) {
95 outMsg.value = req.toString();
96}
97
98// HTTP request to read CosmosDB records
99@af:Function
100public function httpTriggerCosmosDBInput1(
101 @af:HTTPTrigger { } af:HTTPRequest httpReq,
102 @af:CosmosDBInput { connectionStringSetting: "CosmosDBConnection",
103 databaseName: "db1", collectionName: "c1",
104 id: "{Query.id}", partitionKey: "{Query.country}" } json dbReq)
105 returns @af:HTTPOutput string|error {
106 return dbReq.toString();
107}
108
109@af:Function
110public function httpTriggerCosmosDBInput2(
111 @af:HTTPTrigger { } af:HTTPRequest httpReq,
112 @af:CosmosDBInput { connectionStringSetting: "CosmosDBConnection",
113 databaseName: "db1", collectionName: "c1",
114 id: "{Query.id}", partitionKey: "{Query.country}" } Person? dbReq)
115 returns @af:HTTPOutput string|error {
116 return dbReq.toString();
117}
118
119@af:Function
120public function httpTriggerCosmosDBInput3(
121 @af:HTTPTrigger { route: "c1/{country}" } af:HTTPRequest httpReq,
122 @af:CosmosDBInput { connectionStringSetting: "CosmosDBConnection",
123 databaseName: "db1", collectionName: "c1",
124 sqlQuery: "select * from c1 where c1.country = {country}" }
125 Person[] dbReq)
126 returns @af:HTTPOutput string|error {
127 return dbReq.toString();
128}
129
130// HTTP request to write records to CosmosDB
131@af:Function
132public function httpTriggerCosmosDBOutput1(
133 @af:HTTPTrigger { } af:HTTPRequest httpReq, @af:HTTPOutput af:HTTPBinding hb)
134 returns @af:CosmosDBOutput { connectionStringSetting: "CosmosDBConnection",
135 databaseName: "db1", collectionName: "c1" } json {
136 json entry = { id: uuid:createType1AsString(), name: "Saman", country: "Sri Lanka" };
137 hb.payload = "Adding entry: " + entry.toString();
138 return entry;
139}
140
141@af:Function
142public function httpTriggerCosmosDBOutput2(
143 @af:HTTPTrigger { } af:HTTPRequest httpReq,
144 @af:HTTPOutput af:HTTPBinding hb)
145 returns @af:CosmosDBOutput {
146 connectionStringSetting: "CosmosDBConnection",
147 databaseName: "db1", collectionName: "c1" } json {
148 json entry = [{ id: uuid:createType1AsString(), name: "John Doe A", country: "USA" },
149 { id: uuid:createType1AsString(), name: "John Doe B", country: "USA" }];
150 hb.payload = "Adding entries: " + entry.toString();
151 return entry;
152}
153
154@af:Function
155public function httpTriggerCosmosDBOutput3(
156 @af:HTTPTrigger { } af:HTTPRequest httpReq)
157 returns @af:CosmosDBOutput {
158 connectionStringSetting: "CosmosDBConnection",
159 databaseName: "db1", collectionName: "c1" } Person[] {
160 Person[] persons = [];
161 persons.push({id: uuid:createType1AsString(), name: "Jack", country: "UK"});
162 persons.push({id: uuid:createType1AsString(), name: "Will", country: "UK"});
163 return persons;
164}
165
166// A timer function which is executed every 10 seconds.
167@af:Function
168public function queuePopulationTimer(
169 @af:TimerTrigger { schedule: "*/10 * * * * *" } json triggerInfo,
170 @af:QueueOutput { queueName: "queue4" } af:StringOutputBinding msg) {
171 msg.value = triggerInfo.toString();
172}

The output of the bal build is as follows:

1$ bal build azure_functions_deployment.bal
2Compiling source
3 azure_functions_deployment.bal
4
5Generating executables
6 azure_functions_deployment.jar
7 @azure.functions:Function: hello, fromHttpToQueue, fromQueueToQueue, fromBlobToQueue, httpTriggerBlobInput, httpTriggerBlobOutput, sendSMS, cosmosDBToQueue1, cosmosDBToQueue2, httpTriggerCosmosDBInput1, httpTriggerCosmosDBInput2, httpTriggerCosmosDBInput3, httpTriggerCosmosDBOutput1, httpTriggerCosmosDBOutput2, httpTriggerCosmosDBOutput3, queuePopulationTimer

Functions

[34]

__register

INTERNAL usage - registers a handler function.

createContext

INTERNAL usage - creates function context.

getBallerinaValueFromInputData

INTERNAL usage - Returns a converted Ballerina value from input data.

getBinaryFromHTTPReq

INTERNAL usage - Returns the binary payload from the HTTP request.

getBodyFromHTTPInputData

INTERNAL usage - Returns the HTTP body value from input data.

getBytesFromInputData

INTERNAL usage - Returns the binary value from input data.

getHTTPRequestFromInputData

INTERNAL usage - Populates the HTTP request structure from an input data entry.

getHTTPRequestFromParams

INTERNAL usage - Returns the HTTP request data.

getJsonFromHTTPReq

INTERNAL usage - Returns the JSON payload from the HTTP request.

getJsonFromInputData

INTERNAL usage - Returns the JSON value from input data.

getJsonFromMetadata

INTERNAL usage - Returns a json value from metadata.

getJsonStringFromInputData

INTERNAL usage - Returns the JSON string value from input data.

getMetadata

INTERNAL usage - extracts the metadata.

getOptionalBallerinaValueFromInputData

INTERNAL usage - Returns the optional converted Ballerina value from "getParsedJsonFromJsonStringFromInputData".

getOptionalBytesFromInputData

INTERNAL usage - Returns the optional binary value from input data.

getOptionalStringConvertedBytesFromInputData

INTERNAL usage - Returns the optional string value converted from input binary data.

getOptionalStringFromInputData

INTERNAL usage - Returns the optional string value from input data.

getParsedJsonFromJsonStringFromInputData

INTERNAL usage - JSON parse the string value available from "getJsonStringFromInputData".

getStringConvertedBytesFromInputData

INTERNAL usage - Returns the string value converted from input binary data.

getStringFromHTTPReq

INTERNAL usage - Returns the string payload from the HTTP request.

getStringFromInputData

INTERNAL usage - Returns the string value from input data.

getStringFromMetadata

INTERNAL usage - Returns a string value from metadata.

setBallerinaValueAsJsonReturn

INTERNAL usage - Converts a Ballerina value to a JSON and set the return value.

setBlobOutput

INTERNAL usage - Sets the Blob output.

setCosmosDBBallerinaValueAsJsonReturn

INTERNAL usage - Converts a CosmosDS Ballerina value to a JSON and set the return value.

setCosmosDBJsonReturn

INTERNAL usage - Sets the CosmosDS JSON return value.

setHTTPOutput

INTERNAL usage - Sets the HTTP output.

setHTTPReturn

INTERNAL usage - Sets the HTTP binding return value.

setJsonReturn

INTERNAL usage - Sets the JSON return value.

setPureHTTPOutput

INTERNAL usage - Sets the pure HTTP output.

setPureStringOutput

INTERNAL usage - Sets the pure string output.

setStringOutput

INTERNAL usage - Sets the string output.

setStringReturn

INTERNAL usage - Sets the string return value.

setTwilioSmsOutput

INTERNAL usage - Sets the Twilio output.

Classes

[1]

Context

The request context holder.

Records

[15]

BindingNameConfiguration

BindingName annotation configuration.

BlobConfiguration

Blob annotation configuration.

BytesOutputBinding

Byte array output binding data.

CosmosDBInputConfiguration

CosmosDB input annotation configuration.

CosmosDBOutputConfiguration

CosmosDB output annotation configuration.

CosmosDBTriggerConfiguration

CosmosDB trigger annotation configuration.

HandlerParams

INTERNAL stucture - the request handler parameter data.

HTTPBinding

HTTP binding data.

HTTPRequest

HTTP request binding data.

HTTPTriggerConfiguration

HTTPTrigger annotation configuration.

QueueConfiguration

Queue annotation configuration.

StringOutputBinding

String output binding data.

TimerTriggerConfiguration

TimerTrigger annotation configuration.

TwilioSmsConfiguration

Twilio annotation configuration.

TwilioSmsOutputBinding

Twilion SMS output binding data.

Annotations

[14]

BindingName

@azurefunctions:BindingName annotation.

BlobInput

@azurefunctions:BlobInput annotation.

BlobOutput

@azurefunctions:BlobOutput annotation.

BlobTrigger

@azurefunctions:BlobTrigger annotation.

CosmosDBInput

@azurefunctions:CosmosDBInput annotation.

CosmosDBOutput

@azurefunctions:CosmosDBOutput annotation.

CosmosDBTrigger

@azurefunctions:CosmosDBTrigger annotation.

Function

@azurefunctions:Function annotation.

HTTPOutput

@azurefunctions:HTTPOutput annotation

HTTPTrigger

@azurefunctions:HTTPTrigger annotation.

QueueOutput

@azurefunctions:QueueOutput annotation.

QueueTrigger

@azurefunctions:QueueOutput annotation.

TimerTrigger

@azurefunctions:TimerTrigger annotation.

TwilioSmsOutput

@azurefunctions:TwilioSmsOutput annotation.

Types

[1]

AUTH_LEVEL