Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

$
0
0

I like what Microsoft’s doing in the app integration space. They breathed new life into their classic integration bus ( BizTalk Server ). The family of Azure Service Bus technologies (Queues, Topics, Relay) is super solid. API Management and Event Hubs solve real needs. And Azure Logic Apps is maturing at an impressive rate. That last one is the one I wanted to dig into more. Logic Apps gets updated every few weeks , and I thought it’d be fun to put a bunch of new functionality to the test. Specifically, I’m going to check out the updated JSON support, and invoke a bunch of Azure services.

Step 1 Create Azure DocumentDB collection

In my fictitious example, I’m processing product orders. The Logic App takes in the order, and persists it in a database. In the Azure Portal, I created a database account.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

DocumentDB stores content in “collections”, so I needed one of those. To define a collection you must provide some names, throughput (read/write) capacity, and a partition key. The partition key is used to shard the data, and document IDs have to be unique within that partition.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Ok, I was all set to store my orders.

Step 2 Create Azure Function

Right now, you can’t add custom code inside a Logic App. Microsoft recommends that you call out to an Azure Function if you want to do any funny business. In this example, I wanted to generate a unique ID per order. So, I needed a snippet of code that generated a GUID.

First up, I created a new Azure Functions app.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Next up, I had to create an actual function. I could start from scratch, or use a template. I chose the “generic webhook” template for C#.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

This function is basic. All I do is generate a GUID, and return it back.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Step 3 Create Service Bus Queue

When a big order came in, I wanted to route a message to a queue for further processing. Up front, I created a new Service Bus queue to hold these messages.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

With my namespace created, I added a new queue named “largeorders.”

That was the final prerequisite for this demo. Next up, building the Logic App!

Step 4 Create the Azure Logic App

First, I defined a new Logic App in the Azure Portal.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Here’s the first new thing I saw: an updated “getting started” view. I could choose a “trigger” to start off my Logic App, or, choose from a base scenario template.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

I chose the trigger “when an HTTP request is received” and got an initial shape on my Logic App. Now, here’s where I saw the second cool update: instead of manually building a JSON schema, I could paste in a sample and generate one. Rad.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Step 5 Call out to Azure Functions from Logic App

After I received a message, I wanted to add it to DocumentDB. But first, I need my unique order ID. Recall that our Azure Function generated one. I chose to “add an action” and selected “Azure Functions” from the list. As you can see below, once I chose that action, I could browse the Function I already created. Note that a new feature of Logic Apps allows you to build (Node.js) Functions from within the Logic App designer itself. I wanted a C# Function, so that’s why I did it outside this UI.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Step 6 Insert record into DocumentDB from Logic App

Next up, I picked the “DocumentDB” activity, and chose the “create or update document” action.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Unfortunately, Logic Apps doesn’t (yet) look up connection strings for me. I opened another browser tab and navigated back to the DocumentDB “blade” to get my account name and authorization key. Once I did that, the Logic Apps Designer interrogated my account and let me pick my database and collection. After that, I built the payload to store the database. Notice that I built up a JSON message using values from the inbound HTTP message, and Azure Function. I also set the partition key to the “category” value from the inbound message.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
What I have above won’t work. Why? In the present format, the “id” value is invalid. It would contain the whole JSON result from the Azure Function. There’s no way (yet) to grab a part of the JSON in the Designer, but there is a way in code. After switching to “code view”, I added [‘orderid’] reference to the right spot …
Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

When I switched back to the Designer view, I saw “orderid” the mapped value.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

That finished the first part of the flow. In the second part, I wanted to do different things based on the “category” of the purchased product.

Step 7 Add conditional flows to Logic App

Microsoft recently added a “switch” statement condition to the palette, so I chose that. After choosing the data field to “switch” on, I added a pair of paths for different categories of product.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Inside the “electronics” switch path, I wanted to check and see if this was a big order. If so, I’d drop a message to a Service Bus queue. At the moment, Logic Apps doesn’t let me create variables (coming soon!), so I needed another way to generate the total order amount. Azure Functions to the rescue! From within the Logic Apps Designer, I once again chose the Azure Functions activity, but this time, selected “Create New Function.” Here, I passed in the full body of the initial message.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Inside the Function, I wrote some code that multiplied the quantity by the unit price.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

We’re nearly done! After this Function, I added an if/else conditional that checked the Function’s result, and if it’s over 100, I send a message to the Azure Service Bus.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Step 8 Send a response back to the Logic App caller

Whew. Last step to do? Send an HTTP response back to the caller, containing the auto-generated order ID. Ok, my entire flow was finished. It took in a message, added it to DocumentDB, and based on a set of conditions, also shipped it over the Azure Service Bus.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Step 9 Test this thing!

I grabbed the URL for the Logic App from the topmost shape, and popped it into Postman. After sending in the JSON payload, I got back a GUID representing the generated order ID.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

That’s great and all, but I needed to confirm everything worked! DocumentDB with a Function-generated ID? Check.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

Service Bus message viewable via the Service Bus Explorer ? Check.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...

The Logic Apps overview page on the Azure Portal also shows a “run history” and lets you inspect the success/failure of each step. This is new, and very useful.


Creating a JSON-Friendly Azure Logic App That Interacts with Functions, Document ...
Summary

All in all, this was pretty straightfoward. The Azure Portal still has some UI quirks, but a decent Azure dev can crank out the above flow in 20 minutes. That’s pretty powerful. Keep an eye on Logic Apps, and consider taking it for a spin!


Viewing all articles
Browse latest Browse all 6262

Trending Articles