Permalink
Browse files

Cleaned up some transformation code.

Added settings page to configure settings for a particular application.
Never return null from settings apis.
  • Loading branch information...
1 parent a6b6dc9 commit 0e9b581acfc692c78431a895d6635d0ca0bf76d9 @davidfowl davidfowl committed Sep 17, 2011
@@ -1,5 +1,5 @@
namespace Kudu.Client.Models {
- public class Application: IApplication {
+ public class Application : IApplication {
public string Name { get; set; }
public string ServiceUrl { get; set; }
public string SiteUrl { get; set; }
@@ -56,68 +56,73 @@ public class AspNetConfigTransformer {
private void ProcessConnectionStrings(XDocument configuration) {
IEnumerable<ConnectionStringSetting> connectionStrings = _settingsManager.GetConnectionStrings();
- if (connectionStrings != null && connectionStrings.Any()) {
- // Add the connection string settings element if needed
- XElement connectionStringsElement = GetElement(configuration.Root, "connectionStrings", createIfNotExists: false);
+ if (!connectionStrings.Any()) {
+ return;
+ }
- // Do nothing if there are no connection strings to replace.
- if (connectionStringsElement == null) {
- return;
- }
+ // Add the connection string settings element if needed
+ XElement connectionStringsElement = GetElement(configuration.Root, "connectionStrings", createIfNotExists: false);
- IDictionary<string, XElement> connectionStringEntries = GetDictionary(connectionStringsElement, "name");
-
- foreach (var connectionString in connectionStrings) {
- // HACK: This is a temporary hack
- if (connectionString.Name.Equals("All", StringComparison.OrdinalIgnoreCase)) {
- foreach (var element in connectionStringEntries.Select(e => e.Value)) {
- element.SetAttributeValue("connectionString", connectionString.ConnectionString);
- }
- break;
- }
+ // Do nothing if there are no connection strings to replace.
+ if (connectionStringsElement == null) {
+ return;
+ }
- XElement connectionStringEntry;
- if (!connectionStringEntries.TryGetValue(connectionString.Name, out connectionStringEntry)) {
- connectionStringEntry = new XElement("add");
- }
+ IDictionary<string, XElement> connectionStringEntries = GetDictionary(connectionStringsElement, "name");
- connectionStringEntry.SetAttributeValue("name", connectionString.Name);
- connectionStringEntry.SetAttributeValue("connectionString", connectionString.ConnectionString);
- if (!String.IsNullOrEmpty(connectionString.ProviderName)) {
- connectionStringEntry.SetAttributeValue("providerName", connectionString.ProviderName);
+ foreach (var connectionString in connectionStrings) {
+ // HACK: This is a temporary hack
+ if (connectionString.Name.Equals("All", StringComparison.OrdinalIgnoreCase)) {
+ foreach (var element in connectionStringEntries.Select(e => e.Value)) {
+ element.SetAttributeValue("connectionString", connectionString.ConnectionString);
}
+ break;
+ }
+
+ XElement connectionStringEntry;
+ if (!connectionStringEntries.TryGetValue(connectionString.Name, out connectionStringEntry)) {
+ // Only replace connectionstrings, don't add new ones
+ continue;
+ }
+
+ connectionStringEntry.SetAttributeValue("name", connectionString.Name);
+ connectionStringEntry.SetAttributeValue("connectionString", connectionString.ConnectionString);
+ if (!String.IsNullOrEmpty(connectionString.ProviderName)) {
+ connectionStringEntry.SetAttributeValue("providerName", connectionString.ProviderName);
}
}
}
private void ProcessAppSettings(XDocument configuration) {
IEnumerable<DeploymentSetting> appSettings = _settingsManager.GetAppSettings();
- if (appSettings != null && appSettings.Any()) {
- XElement appSettingsElement = GetElement(configuration.Root, "appSettings");
- IDictionary<string, XElement> appSettingsEntries = GetDictionary(appSettingsElement, "key");
+ if (!appSettings.Any()) {
+ return;
+ }
- foreach (var setting in appSettings) {
- XElement appSettingEntry;
- if (!appSettingsEntries.TryGetValue(setting.Key, out appSettingEntry)) {
- appSettingEntry = new XElement("add");
- appSettingsElement.Add(appSettingEntry);
- }
+ XElement appSettingsElement = GetElement(configuration.Root, "appSettings");
+ IDictionary<string, XElement> appSettingsEntries = GetDictionary(appSettingsElement, "key");
- appSettingEntry.SetAttributeValue("key", setting.Key);
- appSettingEntry.SetAttributeValue("value", setting.Value);
+ foreach (var setting in appSettings) {
+ XElement appSettingEntry;
+ if (!appSettingsEntries.TryGetValue(setting.Key, out appSettingEntry)) {
+ appSettingEntry = new XElement("add");
+ appSettingsElement.Add(appSettingEntry);
}
+
+ appSettingEntry.SetAttributeValue("key", setting.Key);
+ appSettingEntry.SetAttributeValue("value", setting.Value);
}
}
private IDictionary<string, XElement> GetDictionary(XElement element, string attributeName) {
var elements = from e in element.Elements()
- let keyAttr = e.Attribute(attributeName)
- where keyAttr != null
- select new {
- Key = keyAttr.Value,
- Element = e
- };
+ let keyAttr = e.Attribute(attributeName)
+ where keyAttr != null
+ select new {
+ Key = keyAttr.Value,
+ Element = e
+ };
return elements.ToDictionary(e => e.Key, e => e.Element, StringComparer.OrdinalIgnoreCase);
}
@@ -32,7 +32,7 @@ public class DeploymentSettingsManager : IDeploymentSettingsManager {
return section.Select(selector);
}
- return null;
+ return Enumerable.Empty<T>();
}
public void SetConnectionString(string key, string value) {
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Linq;
using System.Net.Http;
using Kudu.Core.Infrastructure;
@@ -16,11 +17,15 @@ public class RemoteDeploymentSettingsManager : IDeploymentSettingsManager {
}
public IEnumerable<ConnectionStringSetting> GetConnectionStrings() {
- return _client.GetJson<IEnumerable<ConnectionStringSetting>>("connectionStrings");
+ return from pair in _client.GetJson<IEnumerable<KeyValuePair<string, string>>>("connectionStrings")
+ select new ConnectionStringSetting {
+ Name = pair.Key,
+ ConnectionString = pair.Value
+ };
}
- public void SetConnectionString(string key, string value) {
- SetValue("connectionStrings", key, value);
+ public void SetConnectionString(string name, string connectionString) {
+ SetValue("connectionStrings", name, connectionString);
}
public void RemoveConnectionString(string key) {
@@ -2,6 +2,7 @@
using System.Web.Mvc;
using Kudu.Services.Infrastructure;
using XmlSettings;
+using System.Linq;
namespace Kudu.Services.Settings {
public abstract class SettingsController : KuduController {
@@ -13,8 +14,9 @@ public abstract class SettingsController : KuduController {
_settings = settings;
}
- public IList<KeyValuePair<string, string>> Index() {
- return _settings.GetValues(_section);
+ public IEnumerable<KeyValuePair<string, string>> Index() {
+ return _settings.GetValues(_section) ??
+ Enumerable.Empty<KeyValuePair<string, string>>();
}
[HttpPost]
@@ -0,0 +1,115 @@
+using System;
+using System.Linq;
+using System.Web.Mvc;
+using Kudu.Core.Deployment;
+using Kudu.Web.Models;
+
+namespace Kudu.Web.Controllers {
+ public class SettingsController : Controller {
+ private readonly KuduContext db = new KuduContext();
+
+ public ActionResult Index(string slug) {
+ SettingsViewModel model = GetSettingsViewModel(slug);
+
+ if (model != null) {
+ return View(model);
+ }
+
+ return HttpNotFound();
+ }
+
+ [HttpPost]
+ [ActionName("new-app-setting")]
+ public ActionResult CreateAppSetting(string slug, string key, string value) {
+ try {
+ if (String.IsNullOrEmpty(key)) {
+ ModelState.AddModelError("Key", "key is required");
+ }
+ if (String.IsNullOrEmpty(value)) {
+ ModelState.AddModelError("Value", "value is required");
+ }
+
+ if (ModelState.IsValid) {
+ Application application = db.Applications.SingleOrDefault(a => a.Slug == slug);
+ var settingsManager = new RemoteDeploymentSettingsManager(application.ServiceUrl);
+
+ settingsManager.SetAppSetting(key, value);
+
+ return RedirectToAction("Index", new { slug });
+ }
+ }
+ catch {
+ }
+
+ SettingsViewModel model = GetSettingsViewModel(slug);
+ ViewBag.Key = key;
+ ViewBag.Value = value;
+
+ return View("index", model);
+ }
+
+ [HttpPost]
+ [ActionName("new-connection-string")]
+ public ActionResult CreateConnectionString(string slug, string name, string connectionString) {
+ try {
+ if (String.IsNullOrEmpty(name)) {
+ ModelState.AddModelError("Name", "name is required");
+ }
+ if (String.IsNullOrEmpty(connectionString)) {
+ ModelState.AddModelError("ConnectionString", "connection string is required");
+ }
+
+ if (ModelState.IsValid) {
+ Application application = db.Applications.SingleOrDefault(a => a.Slug == slug);
+ var settingsManager = new RemoteDeploymentSettingsManager(application.ServiceUrl);
+
+ settingsManager.SetConnectionString(name, connectionString);
+
+ return RedirectToAction("Index", new { slug });
+ }
+ }
+ catch {
+ }
+
+ SettingsViewModel model = GetSettingsViewModel(slug);
+ ViewBag.Name = name;
+ ViewBag.ConnectionString = connectionString;
+
+ return View("index", model);
+ }
+ //
+ // POST: /Settings/Delete/5
+
+ [HttpPost]
+ public ActionResult Delete(int id, FormCollection collection) {
+ try {
+ // TODO: Add delete logic here
+
+ return RedirectToAction("Index");
+ }
+ catch {
+ return View();
+ }
+ }
+
+ private SettingsViewModel GetSettingsViewModel(string slug) {
+ Application application = db.Applications.SingleOrDefault(a => a.Slug == slug);
+ if (application != null) {
+ return GetSettingsViewModel(application);
+ }
+
+ return null;
+ }
+
+ private SettingsViewModel GetSettingsViewModel(Application application) {
+ var settingsManager = new RemoteDeploymentSettingsManager(application.ServiceUrl);
+
+ ViewBag.slug = application.Slug;
+
+ return new SettingsViewModel {
+ AppSettings = settingsManager.GetAppSettings().ToList(),
+ ConnectionStrings = settingsManager.GetConnectionStrings().ToList()
+ };
+ }
+ }
+}
View
@@ -212,6 +212,7 @@
<Compile Include="App_Start\NinjectMvc3Services.cs" />
<Compile Include="App_Start\NinjectSignalR.cs" />
<Compile Include="Controllers\ApplicationController.cs" />
+ <Compile Include="Controllers\SettingsController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
@@ -221,6 +222,7 @@
<Compile Include="Models\Application.cs" />
<Compile Include="Models\ApplicationViewModel.cs" />
<Compile Include="Models\KuduContext.cs" />
+ <Compile Include="Models\SettingsViewModel.cs" />
<Compile Include="Models\Site.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -257,6 +259,9 @@
<ItemGroup>
<Folder Include="App_Data\" />
</ItemGroup>
+ <ItemGroup>
+ <Content Include="Views\Settings\Index.cshtml" />
+ </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="..\Build\Kudu.targets" />
@@ -0,0 +1,9 @@
+using System.Collections.Generic;
+using Kudu.Core.Deployment;
+
+namespace Kudu.Web.Models {
+ public class SettingsViewModel {
+ public IEnumerable<DeploymentSetting> AppSettings { get; set; }
+ public IEnumerable<ConnectionStringSetting> ConnectionStrings { get; set; }
+ }
+}
@@ -18,6 +18,9 @@
<li>
@Html.ActionLink("Manage Files", "editor", new { slug = Model.Name.GenerateSlug() })
</li>
+ <li>
+ @Html.ActionLink("Settings", "index", "settings", new { slug = Model.Name.GenerateSlug() }, null)
+ </li>
</ul>
@if (Model.RepositoryType == RepositoryType.None) {
Oops, something went wrong.

0 comments on commit 0e9b581

Please sign in to comment.