<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Eder</title>
    <description>The latest articles on DEV Community by Christian Eder (@_ceder).</description>
    <link>https://dev.to/_ceder</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F444397%2Fbd222262-dfeb-4fc9-90a6-a7c10a2406bd.jpeg</url>
      <title>DEV Community: Christian Eder</title>
      <link>https://dev.to/_ceder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_ceder"/>
    <language>en</language>
    <item>
      <title>Consumer Driven Contract Testing - Hosting A Mocked API Using Azure Functions</title>
      <dc:creator>Christian Eder</dc:creator>
      <pubDate>Wed, 09 Sep 2020 10:02:42 +0000</pubDate>
      <link>https://dev.to/_ceder/consumer-driven-contract-testing-hosting-a-mocked-api-using-azure-functions-5h7h</link>
      <guid>https://dev.to/_ceder/consumer-driven-contract-testing-hosting-a-mocked-api-using-azure-functions-5h7h</guid>
      <description>&lt;p&gt;Applying the concept of Consumer Driven Contract Testing is useful when you are building a system that is composed of multiple decoupled services. Some of these services will &lt;em&gt;provide&lt;/em&gt; APIs, others will &lt;em&gt;consume&lt;/em&gt; these APIs. Consumer Driven Contract Testing is about the API consumer publishing their  expectations on the API in a way that allows&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the provider of the API to verify that that it fulfils these expectations&lt;/li&gt;
&lt;li&gt;the consumer of the API to easily stand up a mock of the provider API to run their own tests against&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are not already familiar with the concept, you can dive deeper into the topic by reading &lt;a href="https://dev.to/frosnerd/consumer-driven-contract-testing-with-pact-1a57"&gt;Frank Rosner's excellent article&lt;/a&gt; or visit &lt;a href="https://pact.io/"&gt;https://pact.io/&lt;/a&gt;. PACT is one of the major libraries available to implement Consumer Driven Contract Tests. The examples shown in this article however are based on the &lt;a href="https://github.com/ChristianEder/impact"&gt;Impact library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The basic idea is, that the consuming service defines a so called &lt;code&gt;Pact&lt;/code&gt; that codifies the consumers expectations on the API structure &amp;amp; behavior:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;Pact&lt;/code&gt; shown in the example above defines an HTTP API that responds to any &lt;code&gt;GET&lt;/code&gt; request to an URL with a path matching &lt;code&gt;weatherforecast\/([a-zA-Z]+)\/(\d+)&lt;/code&gt; and responds with weather forecast data.&lt;/p&gt;

&lt;p&gt;Based on this &lt;code&gt;Pact&lt;/code&gt;, the consumer can stand up a test server on &lt;code&gt;localhost&lt;/code&gt; and run tests of the consuming service using this mocked API:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After successfully running its own tests against this mocked API, the consumer can publish the PACT to the provider as a JSON file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The provider can now take this JSON file and run the expectations defined in that file against its own API:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  Hosting the mocked API on an Azure Function
&lt;/h1&gt;

&lt;p&gt;If the consuming service does not want to run the mocked provider API on &lt;code&gt;localhost&lt;/code&gt; - e.g. because it wants to run its own tests against a cloud hosted environment - we need a way to host the MockServer in the cloud as well. The following code snippet shows a fully working example of an Azure Function that will host an HTTP endpoint which will respond to any request as defined in the &lt;code&gt;Pact&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can find the whole example (and more) on &lt;a href="https://github.com/ChristianEder/impact/tree/master/src/Samples/Impact.Samples.ProviderMockAsAzureFunction"&gt;my GitHub repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>pact</category>
      <category>azure</category>
      <category>serverless</category>
    </item>
    <item>
      <title>Deploying Azure Functions Using Pulumi &amp; .NET</title>
      <dc:creator>Christian Eder</dc:creator>
      <pubDate>Fri, 31 Jul 2020 13:05:34 +0000</pubDate>
      <link>https://dev.to/_ceder/deploying-azure-functions-using-pulumi-net-3dcb</link>
      <guid>https://dev.to/_ceder/deploying-azure-functions-using-pulumi-net-3dcb</guid>
      <description>&lt;p&gt;In this article you'll learn how to deploy your source code to Azure Functions using just a few lines of C# powered by &lt;a href="https://www.pulumi.com/"&gt;Pulumi&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We'll start by creating a regular new Azure Function project, which you can either do from Visual Studio 2019 or using the &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#v2"&gt;func&lt;/a&gt; command line tool. The Visual Studio template will already create an HTTP-triggered function as an example, using the &lt;code&gt;func&lt;/code&gt; cli will require you to call &lt;code&gt;func new&lt;/code&gt; after creating the project using &lt;code&gt;func init&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Implementing, testing and building the Azure Function project is absolutely not affected by using Pulumi for creating Azure resources &amp;amp; deploying your application — this article will focus on the part that comes after you ran &lt;code&gt;dotnet publish&lt;/code&gt; on your function project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Friends don't let friends right-click publish&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While Visual Studio offers the option to publish an Azure Function project directly to an existing or newly created Azure Function resource, without requiring you to head to the Azure Portal to create the resources manually, you'll be left with no means to automate the creation of all the required resources and deploy your application. In this article, we'll use Pulumi to implement an automated infrastructure-as-code style approach for resource creation and application deployment.&lt;/p&gt;

&lt;p&gt;In addition to your Azure Function project, you'll need a second project for your infrastructure code. This can be a regular .NET Core command line project — you'll just have to add a reference to the &lt;a href="https://www.nuget.org/packages/Pulumi.Azure"&gt;Pulumi.Azure&lt;/a&gt; nuget package. But we're getting ahead of ourselves — lets start with some installation requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  What you need to have installed on your system
&lt;/h3&gt;

&lt;p&gt;Since we will be using Pulumi for creating Azure resources and deploying our application, you'll need the Pulumi CLI installed. The &lt;a href="https://www.pulumi.com/docs/get-started/install/"&gt;documentation&lt;/a&gt; shows a range of installation options for various operating systems. You can verify if the installation was successful by running &lt;code&gt;pulumi version&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, you'll need to install the &lt;a href="https://dotnet.microsoft.com/download"&gt;.NET Core SDK&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lastly, installing the &lt;a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest"&gt;Azure CLI&lt;/a&gt; is required - Pulumi uses it under the hood. After installation, you'll need to run &lt;code&gt;az login&lt;/code&gt; in order to create the resources in the right target subscription. There are &lt;a href="https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/"&gt;other ways&lt;/a&gt; to configure the target subscription for pulumi programs, but this is the one we'll stick with for this article.&lt;/p&gt;

&lt;p&gt;With that, you should be ready to go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the blank Azure resources using Pulumi
&lt;/h3&gt;

&lt;p&gt;Now, let's continue where we left of — with a completely empty .NET Core command line project, in which you just added a dependency to the Pulumi.Azure nuget:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qV3r4s4Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o5dwd5s2okfa52at5o27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qV3r4s4Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o5dwd5s2okfa52at5o27.png" alt="Empty .NET Core CLI project with dependency to the Pulumi.Azure nuget"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only thing you'll need to add in addition is a &lt;code&gt;Pulumi.yaml&lt;/code&gt; file next to your &lt;code&gt;Program.cs&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now let's add a stack — stacks are Pulumi's notion of a deployed environment. A stack defines all the required resources, and can be instantiated multiple times in order to create a range of environments. The &lt;a href="https://www.pulumi.com/docs/intro/concepts/stack/"&gt;documentation&lt;/a&gt; puts this as&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every Pulumi program is deployed to a stack. A stack is an isolated, independently configurable instance of a Pulumi program. Stacks are commonly used to denote different phases of development (such as development, staging and production) or feature branches (such as feature-x-dev, jane-feature-x-dev).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using the .NET SDK, a stack is nothing else than a class creating all the required resources in its constructor. Deploying the stack when running your pulumi program is a one-liner in the &lt;code&gt;Program.cs file&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This pulumi program can be run by calling &lt;code&gt;pulumi up&lt;/code&gt; within the pulumi projects directory and will create the resources defined in the stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A resource group, and within that...&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A consumption based app service plan&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A storage account&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A function app configured to use both the plan and the storage account&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Running &lt;code&gt;pulumi up&lt;/code&gt; will — at least on your first run — ask you for a name for the stack to create. Just choose something that makes sense to you — I chose "dev". The stack name will be reflected in the names of the created resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploying your application
&lt;/h3&gt;

&lt;p&gt;Now that you've got a brand new Azure Function App created (it should show up in your &lt;a href="https://portal.azure.com/"&gt;Azure Portal&lt;/a&gt;), let's run &lt;code&gt;dotnet publish&lt;/code&gt; on the function app project (not the pulumi program) and deploy the files created as a build output to the Function App. We'll do this by creating a .zip file as a new blob in our storage account, and then configure the function app to run from that .zip file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;GetSignedBlobUrl&lt;/code&gt; in the snippet above is a helper function you can find &lt;a href="https://gist.github.com/ChristianEder/42f57e27d8b6ab75d299722764704d57"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And that's it — your next call to &lt;code&gt;pulumi up&lt;/code&gt; will bundle the output of your earlier call to &lt;code&gt;dotnet publish&lt;/code&gt; into a .zip file, upload it into a new blob, and configure the Function App to use the contents of this blob.&lt;/p&gt;

&lt;p&gt;Christian Eder is a Lead Software Architect working for &lt;a href="https://www.zuehlke.com/"&gt;Zühlke Engineering&lt;/a&gt; and has a strong background in all things cloud, with a focus on IoT projects. He helps his customers in finding the right solutions, defining suitable architectures and efficient implementation.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>pulumi</category>
      <category>serverless</category>
      <category>infrastructureascode</category>
    </item>
  </channel>
</rss>
