{"id":256,"date":"2020-07-15T16:09:48","date_gmt":"2020-07-15T15:09:48","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=256"},"modified":"2020-07-15T16:09:50","modified_gmt":"2020-07-15T15:09:50","slug":"azure-function-devops","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2020\/07\/15\/azure-function-devops\/","title":{"rendered":"Azure Function Devops"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Let\u2019s work with Azure Functions and Create our 1<sup>st<\/sup>\nFunction<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>To Setup your enviroment you need:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>VSCODE Installed<\/strong><ol><li><strong>Azure Functions Installed (extension)<\/strong><\/li><\/ol><\/li><li><strong>Powershell Core<\/strong><\/li><li><strong>NET SDK 2.2<\/strong><\/li><li><strong>Azure Functions Core Tools <\/strong><ol><li><a href=\"https:\/\/docs.microsoft.com\/azure\/azure-functions\/functions-run-local#v2?WT.mc_id=docs-azuredevtips-micrum\"><strong>https:\/\/docs.microsoft.com\/azure\/azure-functions\/functions-run-local#v2?WT.mc_id=docs-azuredevtips-micrum<\/strong><\/a><strong><\/strong><\/li><\/ol><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Goal\n\u2013 Build a REST API and simple web page to display the information using\npowershell and Azure Functions<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Do it in DevOps Way (not via GUI) <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, it&#8217;s easy to\ncreate a repository in Azure DevOps or GitHub and deploy azure functions at\nease. For our demo I used Azure DevOps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a GitHub repo\n(blank) on github.nike.com and give it the name iTrackv2 (my case) you can use\nthe name you want and then follow the directions below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">On your VSCODE\nenviroment I used AzureRM Module \u2013 you can choose what you feel confortable with\n(CLI , Az Module ..) <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>git clone &#8216;&lt;url&gt; of your repo\u2019<\/li><li>I had to fix a little issue here with the\nGithub Token basically if you don\u2019t have a token create one otherwise cloning\nthe repo will complain. <\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"295\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1-1024x295.png\" alt=\"\" class=\"wp-image-257\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1-1024x295.png 1024w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1-600x173.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1-300x86.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1-768x221.png 768w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions1.png 1072w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>I am using Github Enterprise should not be different from the public version<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Use your token in the following way <strong>git clone <\/strong><a href=\"https:\/\/%3ctoken%3e@github.nike.com\/owner\/repo.git\"><strong>https:\/\/&lt;token>@github.com\/owner\/repo.git<\/strong><\/a><ul><li>My case <a href=\"https:\/\/%3ctoken%3e@github.nike.com\/mgoul3\/iTrackv2.git\"><strong>https:\/\/&lt;token>@githubcom\/GithubUser\/yourrepo.git<\/strong><\/a><\/li><\/ul><\/li><li>Go to your Project folder = CD .\\YourFolder<\/li><li>You will see an empty Repo with a readme.md<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Create the Azure\nFuntion<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>In our case Worker Runtime will be Powershell <ol><li><strong>func init &#8211;worker-runtime powershell<\/strong><\/li><\/ol><\/li><li>Now let\u2019s create the function<ol><li><strong>func new &#8211;name ADPDCTASKS &#8211;template HTTPTrigger<\/strong><\/li><li><strong>ADPDCTASKS is the name I would like to give to my Function you can change that to your desired name.<\/strong><\/li><\/ol><\/li><li>This will create (2) files (function.json and run.ps1)<\/li><li>Basically we need another script here to provide us the JSON file I will provide the one I am working on as example, everything can be converted to JSON, I am pulling an AD feed and converting to JSON looking out for specific fields in AD , I think this can also give you a overview on how you could use this for other functions you might want.<\/li><li>On your Terminal \u2013 mkdir database <\/li><li>Copy the following JSON file(employees) into the folder <\/li><li>This file must be renamed to employees.json , its a sample file with fictitious employees (download below)<\/li><\/ol>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/employees.txt\">employees<\/a><a href=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/employees.txt\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>For our test Function to work we need to replace the run.ps1 code<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"250\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2-1024x250.png\" alt=\"\" class=\"wp-image-258\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2-1024x250.png 1024w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2-600x146.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2-300x73.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2-768x187.png 768w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions2.png 1046w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Code is below for copy and Paste<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">using namespace System.Net\n\nparam($Request, $TriggerMetadata)\n\n$employees = Get-Content .\\database\\employees.json | ConvertFrom-Json\n\nPush-OutputBinding -Name Response -Value ([HttpResponseContext]@{\n\n    headers = @{&#039;content-type&#039;=&#039;application\\json&#039;}\n\n    StatusCode = [httpstatuscode]::OK\n\n    Body = $employees\n\n})<\/code><\/pre>\n\n\n\n<p class=\"has-text-color has-header-gradient-color wp-block-paragraph\"><strong>Let\u2019s test the REST API endpoint <\/strong><br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Now, test your REST API endpoint which is #serverless using func host start<\/li><li>Open the Terminal (click on the Terminal TAB)<\/li><li>Type \u2013 <strong>func host start<\/strong> you should see something like below<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"293\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions3.png\" alt=\"\" class=\"wp-image-259\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions3.png 844w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions3-600x208.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions3-300x104.png 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions3-768x267.png 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>will see the API URL like below <\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"673\" height=\"74\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions4.png\" alt=\"\" class=\"wp-image-260\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions4.png 673w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions4-600x66.png 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions4-300x33.png 300w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>On your Terminal type &#8211; $employees = Invoke-RestMethod -Uri &#8220;http:\/\/localhost:7071\/api\/ ADPDCTASKS &#8221; <em>(pay attention to the name of the Function App you gav<\/em>e)<\/li><li>Type &#8211; $employees (to display the data) hosted on your JSON DB <\/li><li>Congrats this was your first Azure Function and its working.<\/li><li>CTRL+C to break the code since it\u2019s a lot of info and let\u2019s work a way to display this in a better format.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\"> <br><strong>How to present this data with a simple PSHTML page.\ufeff<\/strong> <\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a Folder \u2013 mkdir Modules<\/li><li>Save this module \u2013 Save-Module -Name PSHTML -Path .\\Modules<\/li><li>Create a new Function \u2013 func new &#8211;name ListEmployees &#8211;template HTTPTrigger (If you want to give another name to the Function you already know \ud83d\ude0a)<\/li><li>Again this will create (2) files (function.json and run.ps1)<\/li><li>Replace the run.ps1 with the following code<\/li><li><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">using namespace System.Net\n\nparam($Request, $TriggerMetadata)\n\n$body = html -Content {\n\n    head -Content {\n\n        Title -Content &quot;Name your Page - Employee Data or Something&quot;\n\n        Link -href &#039;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/css\/bootstrap.min.css&#039; -rel stylesheet\n\n        script -src &#039;https:\/\/code.jquery.com\/jquery-3.2.1.slim.min.js&#039;\n\n        script -src &#039;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.12.9\/umd\/popper.min.js&#039;\n\n        script -src &#039;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.0.0\/js\/bootstrap.min.js&#039;\n\n    }\n\n    Body -Content {\n\n        Div -Class &#039;container&#039; -Content {\n\n            Div -Class &#039;jumbotron&#039; -Content {\n\n                H1 -Class &#039;display-4&#039; -Content &quot; \nName your Page - Employee Data or Something&quot;\n\n            }\n\n        }\n\n        Div -Class &#039;container&#039; -Content {\n\n            $employees = Get-Content .\\database\\employees.json | ConvertFrom-Json\n\n            Table -class &#039;table&#039; -Content {\n\n                Thead -Content {\n\n                    th -Content &quot;EmployeeID&quot;\n\n                    th -Content &quot;GivenName&quot;\n\n                    th -Content &quot;Surname&quot;\n\n                    th -Content &quot;Building Reference&quot;\n\n                    th -Content &quot;Floor&quot;\n\n                    th -Content &quot;Area&quot;\n\n                    th -Content &quot;Desk&quot;\n\n                    th -Content &quot;Special Instructions&quot;\n\n                    th -Content &quot;Mail&quot;\n\n                    th -Content &quot;Notification Type&quot;\n\n                    th -Content &quot;OfficePhone&quot;\n\n                }\n\n                Tbody -Content {\n\n                    foreach ($employee in $employees)\n\n                    {\n\n                        tr -Content {\n\n                        td -Content $employee.EmployeeID\n\n                        td -Content $employee.GivenName\n\n                        td -Content $employee.Surname\n\n                        td -Content $employee.&#039;physicalDeliveryOfficeName&#039;\n\n                        td -Content $employee.Floor\n\n                        td -Content $employee.Area\n\n                        td -Content $employee.Desk\n\n                        td -Content $employee.&#039;Special Instructions&#039; =&quot;&quot;\n\n                        td -Content $employee.Mail\n\n                        td -Content $employee.&#039;Notification Type&#039; = &quot;Special&quot;\n\n                        td -Content $employee.OfficePhone\n\n                        }\n\n                    }\n\n                }\n\n            }\n\n        }\n\n    }\n\n}\n\n# Associate values to output bindings by calling &#039;Push-OutputBinding&#039;.\n\nPush-OutputBinding -Name Response -Value ([HttpResponseContext]@{\n\n        headers    = @{&#039;content-type&#039; = &#039;text\/html&#039; }\n\n        StatusCode = [httpstatuscode]::OK\n\n        Body       = $body\n\n    })<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Now you can see (2) functions running <\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"76\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions5.png\" alt=\"\" class=\"wp-image-262\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions5.png 448w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2020\/07\/functions5-300x51.png 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\"><li>Open your browser and head to <a href=\"http:\/\/localhost:7071\/api\/ListEmployees\">http:\/\/localhost:7071\/api\/ListEmployees<\/a> (pay attention to the name of the Function App you gave)<\/li><li>You should have a list of employees available, now this was your first function there is many ways to automate this &#8220;feed&#8221; and deliver live reports or changes its just adjust for what you need ! <\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">See you soon , enjoy =)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s work with Azure Functions and Create our 1st Function To Setup your enviroment you need: VSCODE Installed Azure Functions Installed (extension) Powershell Core NET SDK 2.2 Azure Functions Core Tools https:\/\/docs.microsoft.com\/azure\/azure-functions\/functions-run-local#v2?WT.mc_id=docs-azuredevtips-micrum Goal \u2013 Build a REST API and simple web page to display the information using powershell and Azure [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[41,53,52,51,8,12,47],"class_list":["post-256","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","tag-azure-automation","tag-azure-devops","tag-azure-functions","tag-devops","tag-michael-goulart","tag-microsoft","tag-microsoft-azure"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/11\/devops-100314993-primary.idge_.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":3,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":266,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/256\/revisions\/266"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}