-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test.cs
147 lines (120 loc) · 5.81 KB
/
Test.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using AzureFunctionSubmitForm.Models;
using AzureFunctionSubmitForm.Repos;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Azure.Management.Automation.Models;
using AzureFunctionSubmitForm.Helpers;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace AzureFunctionSubmitForm.Controllers
{
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
private string _resourceGroup;
private string _automationAccount;
private static string _runbookName;
private static string _queryString;
private static Dictionary<string, AzureFunctionSubmitForm.Models.RunbookParameterSetting> _runbookParameterSettings;
private readonly IHubContext<Hub> _hubContext;
string hej;
private readonly ICustomAzureOperations _customAzureOperations;
public string htmlInput;
public HomeController(IConfiguration Configuration, ICustomAzureOperations customAzureOperations, IHubContext<Hub> hubContext)
{
_configuration = Configuration;
_customAzureOperations = customAzureOperations;
_resourceGroup = _configuration["AzureSettings:ResourceGroup"];
_automationAccount = _configuration["AzureSettings:AutomationAccount"];
_hubContext = hubContext;
}
/// <summary>
/// Return view with Runbook Name specified in the URL. Take Resource Group Name and Automation Account Name from static configuration
/// </summary>
/// <param name="runbookName"></param>
/// <returns></returns>
public async Task<IActionResult> Index(string runbookName)
{
if (string.IsNullOrEmpty(runbookName))
{
ViewBag.NoInput = $"No input in URL defined. Please specify Runbook Name in one of the following formats:<br><br>" +
$"http://<website>/?runbookName=<MyRunbookName><br>" +
$"http://<website>/?runbookName=<MyRunbookName>?resourceGroup=<MyResourceGroupName>?automationAccount=<automationAccountName>";
return View();
}
ViewBag.RunbookName = runbookName;
_runbookName = runbookName;
_queryString = this.Request.QueryString.ToUriComponent();
ViewBag.Response = $"ResourceGroup: {_resourceGroup} Automation Account: {_automationAccount} runbook: {runbookName}";
_runbookParameterSettings = await _customAzureOperations.GetRunbookParameterSettings(_resourceGroup, _automationAccount, runbookName);
return View(_runbookParameterSettings);
}
/// <summary>
/// Return view with Resource Group Name, Automation Account Name and Runbook Name specified in the URL
/// </summary>
/// <param name="resourceGroupName"></param>
/// <param name="automationAccountName"></param>
/// <param name="runbookName"></param>
/// <returns></returns>
//public async Task<IActionResult> Index(string resourceGroupName, string automationAccountName, string runbookName)
//{
// ViewBag.Response = $"ResourceGroup: {resourceGroupName} Automation Account: {automationAccountName} runbook: {runbookName}";
// return View(await _customAzureOperations.GetRunbookParameterSettings(resourceGroupName, automationAccountName, runbookName));
//}
//POST Jobs/Approve
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Submit(Dictionary<string, string> inputs)
{
//Invoke signal to all clients
await _signalHubContext.Clients.All.SendAsync("initSignal");
return RedirectToAction("Success", inputs);
}
public async Task<IActionResult> Success(Dictionary<string, string> inputs)
{
//Start runbook and return output
var results = await _customAzureOperations.StartRunbookAndReturnResult(_resourceGroup, _automationAccount, _runbookName, inputs);
if (results.Item2 == JobStatus.Completed)
{
TempData["JobOutput"] = results.Item1;
}
else
{
TempData["JobOutputError"] = results.Item1;
}
return View("Index", _runbookParameterSettings);
}
private WebResponse RunWebHook(string webHookUri, string input)
{
HttpWebRequest http = (HttpWebRequest)WebRequest.Create(new Uri(webHookUri));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
string parsedContent = input;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
WebResponse response = http.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
//test
return response;
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = System.Diagnostics.Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}