Azure Config Generator
 All Classes Namespaces Files Functions Variables Properties
SpecProcessor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 
7 namespace FUSE.AzureConfig.Tasks
8 {
9  public class SpecProcessor
10  {
12  {
13  var roles = new Dictionary<string, AzureRoleDetails>();
14  var envs = new Dictionary<string, AzureEnvironment>();
15  foreach (var e in spec.Environments)
16  {
17  ProcessEnv(e.Key, spec, roles, envs);
18  }
19  foreach (var r in envs.Values.SelectMany(e => e.Roles.Values).GroupBy(r => r.RoleName))
20  {
21  var currentSettings = r.SelectMany(x => x.ConfigSettings.Settings.Keys);
22  r.ToList().ForEach(x => x.Details.ConfigurationSettingKeys.UnionWith(currentSettings));
23  }
24  foreach (var r in envs.Values.SelectMany(e => e.Roles.Values))
25  {
26  r.ConfigSettings.AddMissing(r.Details.ConfigurationSettingKeys);
27  }
28  // Remove "parent" environments with no actual instances or with an empty profile name, and key off of profile name.
29  envs = envs.Where(e => e.Value.Roles.Any(x => x.Value.Instances > 0) && !string.IsNullOrEmpty(e.Value.EnvName))
30  .ToDictionary(x => x.Value.EnvName, x => x.Value);
31  return new AzureConfig(spec.Name, roles, envs);
32  }
33 
34  private AzureEnvironment ProcessEnv(string envName,
35  AzureConfigSpec spec,
36  Dictionary<string, AzureRoleDetails> roles,
37  Dictionary<string, AzureEnvironment> envs)
38  {
39  AzureEnvironment env;
40  if (envs.TryGetValue(envName, out env))
41  return env;
42 
43  var curSpec = spec.Environments.Single(s => s.Key == envName).Value;
44  if (string.IsNullOrEmpty(curSpec.Parent))
45  {
46  env = Ensure(envs, envName, n => new AzureEnvironment(curSpec.ProfileName));
47  }
48  else
49  {
50  env = Ensure(envs, envName, n =>
51  AzureEnvironment.FromParent(curSpec.ProfileName, ProcessEnv(curSpec.Parent, spec, roles, envs)));
52  }
53  if (curSpec.Roles != null)
54  {
55  foreach (var r in curSpec.Roles)
56  {
57  var role = Ensure(roles, r.Key, n => new AzureRoleDetails(n));
58  var roleEnv = Ensure(env.Roles, r.Key, n => new AzureRoleEnvironmentDetails(role));
59  roleEnv.Instances = r.Value.Instances;
60  }
61  }
62  foreach (var s in curSpec.ConfigSettings)
63  {
64  var inRoles = s.Key.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim());
65  foreach (var r in inRoles)
66  {
67  var role = Ensure(roles, r, n => new AzureRoleDetails(n));
68  var roleEnv = Ensure(env.Roles, r, n => new AzureRoleEnvironmentDetails(role));
69  roleEnv.ConfigSettings.AddSettings(s.Value);
70  }
71  }
72 
73  return env;
74  }
75 
76  private static T Ensure<T>(IDictionary<string, T> dict, string key, Func<string, T> genNew)
77  {
78  T val;
79  if (!dict.TryGetValue(key, out val))
80  {
81  val = genNew(key);
82  dict[key] = val;
83  }
84  return val;
85  }
86  }
87 }
AzureConfig Process(AzureConfigSpec spec)
Processed Azure configuration
Definition: AzureConfig.cs:12
JSON-based azure configuration specification.