Skip to content

Commit

Permalink
Merge pull request #111 from OctopusDeploy/enhancement-keyvault
Browse files Browse the repository at this point in the history
Azure Key Vault specific xml does not break calmari
  • Loading branch information
pawelpabich authored Jul 18, 2016
2 parents 19911b3 + 1a7221d commit c6dedd6
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 120 deletions.
6 changes: 3 additions & 3 deletions source/Calamari.Azure.Tests/Calamari.Azure.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
<HintPath>..\packages\Microsoft.WindowsAzure.Management.Compute.6.1.2\lib\net40\Microsoft.WindowsAzure.Management.Compute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NSubstitute, Version=1.8.1.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
Expand Down Expand Up @@ -122,7 +122,7 @@
<Compile Include="PowerShell\AzurePowerShellContextFixture.cs" />
<Compile Include="PowerShell\AzurePowershellFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceGroups\ResourceGroupTemplateParserFixture.cs" />
<Compile Include="ResourceGroups\ResourceGroupTemplateNormalizerFixture.cs" />
<Compile Include="SetUpFixture.cs" />
<Compile Include="AzureTestEnvironment.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.IO;
using Calamari.Azure.Deployment.Integration.ResourceGroups;
using NUnit.Framework;

namespace Calamari.Azure.Tests.ResourceGroups
{
[TestFixture]
public class ResourceGroupTemplateNormalizerFixture
{
ResourceGroupTemplateNormalizer subject;

[SetUp]
public void SetUp()
{
subject = new ResourceGroupTemplateNormalizer();
}

[Test]
public void ShouldNormallizeParametersWithEnvelope()
{
var result = subject.Normalize(ReadParametersFile("params_with_envelope.json"));

Assert.AreEqual(StripWhiteSpace(GetParameter()), StripWhiteSpace(result));
}

[Test]
public void ShouldNormallizeParametersSansEnvelope()
{
var result = subject.Normalize(ReadParametersFile("params_sans_envelope.json"));

Assert.AreEqual(StripWhiteSpace(GetParameter()), StripWhiteSpace(result));
}

private string ReadParametersFile(string fileName)
{
var path = GetType().Namespace.Replace("Calamari.Azure.Tests.", String.Empty);
path = path.Replace('.', Path.DirectorySeparatorChar);
return File.ReadAllText(Path.Combine(AzureTestEnvironment.TestWorkingDirectory, path, fileName));
}

private string StripWhiteSpace(string input)
{
return input.Replace(" ", string.Empty)
.Replace(Environment.NewLine, string.Empty);
}

private string GetParameter()
{
return @"{
'lifeTheUniverseAndEverything': {
'value': '42'
},
'password': {
'reference': {
'keyVault': {
'id': 'id/othervalue/test'
},
'secretName': 'secretName'
}
}
}".Replace("'", "\"");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"lifeTheUniverseAndEverything": {
"value": "42"
}
"lifeTheUniverseAndEverything": {
"value": "42"
},
"password": {
"reference": {
"keyVault": {
"id": "id/othervalue/test"
},
"secretName": "secretName"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"parameters": {
"lifeTheUniverseAndEverything": {
"value": "42"
},
"password": {
"reference": {
"keyVault": {
"id": "id/othervalue/test"
},
"secretName": "secretName"
}
}
}
}
2 changes: 1 addition & 1 deletion source/Calamari.Azure.Tests/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
2 changes: 1 addition & 1 deletion source/Calamari.Azure.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<package id="Microsoft.WindowsAzure.Common" version="1.4.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Compute" version="6.1.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="NSubstitute" version="1.8.1.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="Octostache" version="1.0.2.32" targetFramework="net45" />
Expand Down
3 changes: 1 addition & 2 deletions source/Calamari.Azure/Calamari.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@
<Compile Include="Deployment\Conventions\SwapAzureDeploymentConvention.cs" />
<Compile Include="Deployment\Conventions\RePackageCloudServiceConvention.cs" />
<Compile Include="Deployment\Conventions\UploadAzureCloudServicePackageConvention.cs" />
<Compile Include="Deployment\Integration\ResourceGroups\ResourceGroupTemplateParameter.cs" />
<Compile Include="Deployment\Integration\ResourceGroups\ResourceGroupTemplateParameterParser.cs" />
<Compile Include="Deployment\Integration\ResourceGroups\ResourceGroupTemplateNormalizer.cs" />
<Compile Include="Integration\AzureCloudServiceConfigurationRetriever.cs" />
<Compile Include="Integration\AzurePackageUploader.cs" />
<Compile Include="Integration\AzurePowershellContext.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override int Execute(string[] commandLineArguments)
new ContributeEnvironmentVariablesConvention(),
new LogVariablesConvention(),
new ExtractPackageToStagingDirectoryConvention(new GenericPackageExtractor(), fileSystem),
new DeployAzureResourceGroupConvention(templateFile, templateParameterFile, filesInPackage, fileSystem, new ResourceGroupTemplateParameterParser())
new DeployAzureResourceGroupConvention(templateFile, templateParameterFile, filesInPackage, fileSystem, new ResourceGroupTemplateNormalizer())
};

var deployment = new RunningDeployment(packageFile, variables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public class DeployAzureResourceGroupConvention : IInstallConvention
readonly string templateParametersFile;
private readonly bool filesInPackage;
readonly ICalamariFileSystem fileSystem;
readonly IResourceGroupTemplateParameterParser parameterParser;
readonly IResourceGroupTemplateNormalizer parameterNormalizer;

public DeployAzureResourceGroupConvention(string templateFile, string templateParametersFile, bool filesInPackage,
ICalamariFileSystem fileSystem, IResourceGroupTemplateParameterParser parameterParser)
ICalamariFileSystem fileSystem, IResourceGroupTemplateNormalizer parameterNormalizer)
{
this.templateFile = templateFile;
this.templateParametersFile = templateParametersFile;
this.filesInPackage = filesInPackage;
this.fileSystem = fileSystem;
this.parameterParser = parameterParser;
this.parameterNormalizer = parameterNormalizer;
}

public void Install(RunningDeployment deployment)
Expand All @@ -55,7 +55,7 @@ public void Install(RunningDeployment deployment)
variables[SpecialVariables.Action.Azure.ResourceGroupDeploymentMode]);
var template = ResolveAndSubstituteFile(templateFile, filesInPackage, variables);
var parameters = !string.IsNullOrWhiteSpace(templateParametersFile)
? parameterParser.ParseParameters(ResolveAndSubstituteFile(templateParametersFile, filesInPackage, variables))
? parameterNormalizer.Normalize(ResolveAndSubstituteFile(templateParametersFile, filesInPackage, variables))
: null;

Log.Info(
Expand All @@ -82,14 +82,12 @@ static string GenerateDeploymentNameFromStepName(string stepName)
}

static void CreateDeployment(Func<IResourceManagementClient> createArmClient, string resourceGroupName, string deploymentName,
DeploymentMode deploymentMode, string template, IDictionary<string, ResourceGroupTemplateParameter> parameters)
DeploymentMode deploymentMode, string template, string parameters)
{
var parameterJson = parameters != null ? JsonConvert.SerializeObject(parameters, Formatting.Indented) : null;

Log.Verbose($"Template:\n{template}\n");
if (parameterJson != null)
if (parameters != null)
{
Log.Verbose($"Parameters:\n{parameterJson}\n");
Log.Verbose($"Parameters:\n{parameters}\n");
}

using (var armClient = createArmClient())
Expand All @@ -101,7 +99,7 @@ static void CreateDeployment(Func<IResourceManagementClient> createArmClient, st
{
Mode = deploymentMode,
Template = template,
Parameters = parameterJson
Parameters = parameters
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Calamari.Azure.Deployment.Integration.ResourceGroups
{
public interface IResourceGroupTemplateNormalizer
{
string Normalize(string json);
}

/// <summary>
/// There are 2 ways the parameters can be passed to Clamari but the Resource Group Client supports only one format so we need to
/// normalize the input. Have a look at ResourceGroupTemplateNormalizerFixture for more details.
/// </summary>
public class ResourceGroupTemplateNormalizer : IResourceGroupTemplateNormalizer
{
public string Normalize(string json)
{
try
{
var envelope = JsonConvert.DeserializeObject<ParameterEnvelope>(json);
return JsonConvert.SerializeObject(envelope.Parameters);
}
catch (JsonSerializationException)
{
return json;
}
}

class ParameterEnvelope
{
[JsonProperty("parameters", Required = Required.Always)]
public JObject Parameters { get; set; }
}
}
}

This file was deleted.

This file was deleted.

0 comments on commit c6dedd6

Please sign in to comment.