Skip to content

Commit

Permalink
Merge pull request #10 from kiranisaac/master
Browse files Browse the repository at this point in the history
Merge encrypted variable with unencrypted variable
  • Loading branch information
elvg committed Feb 4, 2015
2 parents e5da677 + e514036 commit 3a084d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,41 +405,21 @@ public Variable CreateVariable(Variable variable)
Resources.VariableAlreadyExists, variable.Name));
}

if (variable.Encrypted)
var createParams = new AutomationManagement.Models.VariableCreateParameters()
{
var createParams = new AutomationManagement.Models.EncryptedVariableCreateParameters()
Name = variable.Name,
Properties = new AutomationManagement.Models.VariableCreateProperties()
{
Name = variable.Name,
Properties = new AutomationManagement.Models.EncryptedVariableCreateProperties()
{
Value = PowerShellJsonConverter.Serialize(variable.Value),
Description = variable.Description
}
};

var sdkCreatedVariable =
this.automationManagementClient.EncryptedVariables.Create(variable.AutomationAccountName, createParams)
.EncryptedVariable;

return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
}
else
{
var createParams = new AutomationManagement.Models.VariableCreateParameters()
{
Name = variable.Name,
Properties = new AutomationManagement.Models.VariableCreateProperties()
{
Value = PowerShellJsonConverter.Serialize(variable.Value),
Description = variable.Description
}
};
Value = PowerShellJsonConverter.Serialize(variable.Value),
Description = variable.Description,
IsEncrypted = variable.Encrypted
}
};

var sdkCreatedVariable =
this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable;
var sdkCreatedVariable =
this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable;

return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
}
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
}

public void DeleteVariable(string automationAccountName, string variableName)
Expand All @@ -448,14 +428,7 @@ public void DeleteVariable(string automationAccountName, string variableName)
{
var existingVarible = this.GetVariable(automationAccountName, variableName);

if (existingVarible.Encrypted)
{
this.automationManagementClient.EncryptedVariables.Delete(automationAccountName, variableName);
}
else
{
this.automationManagementClient.Variables.Delete(automationAccountName, variableName);
}
this.automationManagementClient.Variables.Delete(automationAccountName, variableName);
}
catch (CloudException cloudException)
{
Expand All @@ -479,75 +452,33 @@ public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFie
string.Format(CultureInfo.CurrentCulture, Resources.VariableEncryptionCannotBeChanged, variable.Name, existingVariable.Encrypted));
}

if (variable.Encrypted)
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
{
var updateParams = new AutomationManagement.Models.EncryptedVariableUpdateParameters()
{
Name = variable.Name
};
Name = variable.Name,
};

if (updateFields == VariableUpdateFields.OnlyDescription)
{
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
{
Description = variable.Description
};
}
else
if (updateFields == VariableUpdateFields.OnlyDescription)
{
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
{
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
{
Value = PowerShellJsonConverter.Serialize(variable.Value)
};
}

this.automationManagementClient.EncryptedVariables.Update(variable.AutomationAccountName, updateParams);
Description = variable.Description
};
}
else
{
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
{
Name = variable.Name,
Value = PowerShellJsonConverter.Serialize(variable.Value)
};

if (updateFields == VariableUpdateFields.OnlyDescription)
{
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
{
Description = variable.Description
};
}
else
{
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
{
Value = PowerShellJsonConverter.Serialize(variable.Value)
};
}

this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams);
}

this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams);

return this.GetVariable(variable.AutomationAccountName, variable.Name);
}

public Variable GetVariable(string automationAccountName, string name)
{
try
{
var sdkEncryptedVariable = this.automationManagementClient.EncryptedVariables.Get(
automationAccountName, name).EncryptedVariable;

if (sdkEncryptedVariable != null)
{
return new Variable(sdkEncryptedVariable, automationAccountName);
}
}
catch (CloudException)
{
// do nothing
}

try
{
var sdkVarible = this.automationManagementClient.Variables.Get(automationAccountName, name).Variable;
Expand All @@ -556,14 +487,15 @@ public Variable GetVariable(string automationAccountName, string name)
{
return new Variable(sdkVarible, automationAccountName);
}

throw new ResourceNotFoundException(typeof(Variable),
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
}
catch (CloudException)
{
// do nothing
throw new ResourceNotFoundException(typeof(Variable),
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
}

throw new ResourceNotFoundException(typeof(Variable),
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
}

public IEnumerable<Variable> ListVariables(string automationAccountName)
Expand All @@ -577,20 +509,7 @@ public IEnumerable<Variable> ListVariables(string automationAccountName)
response, response.Variables);
});

var result = variables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList();

IList<AutomationManagement.Models.EncryptedVariable> encryptedVariables = AutomationManagementClient.ContinuationTokenHandler(
skipToken =>
{
var response = this.automationManagementClient.EncryptedVariables.List(
automationAccountName);
return new ResponseWithSkipToken<AutomationManagement.Models.EncryptedVariable>(
response, response.EncryptedVariables);
});

result.AddRange(encryptedVariables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList());

return result;
return variables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList();
}
#endregion

Expand Down Expand Up @@ -1581,14 +1500,6 @@ private Variable CreateVariableFromVariableModel(AutomationManagement.Models.Var
return new Variable(variable, automationAccountName);
}

private Variable CreateVariableFromVariableModel(AutomationManagement.Models.EncryptedVariable variable, string automationAccountName)
{
Requires.Argument("variable", variable).NotNull();

return new Variable(variable, automationAccountName);
}


private AutomationManagement.Models.Schedule GetScheduleModel(string automationAccountName, string scheduleName)
{
AutomationManagement.Models.Schedule scheduleModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
this.CreationTime = variable.Properties.CreationTime.ToLocalTime();
this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime();

if (variable.Properties.Value == null)
if (variable.Properties.Value == null || variable.Properties.IsEncrypted)
{
this.Value = null;
}
Expand All @@ -50,31 +50,10 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
}

this.Description = variable.Properties.Description;
this.Encrypted = false;
this.Encrypted = variable.Properties.IsEncrypted;
this.AutomationAccountName = automationAccoutName;
}

// <summary>
/// Initializes a new instance of the <see cref="Variable"/> class.
/// </summary>
/// <param name="variable">
/// The variable.
/// </param>
/// <exception cref="System.ArgumentException">
/// </exception>
public Variable(AutomationManagement.Models.EncryptedVariable variable, string automationAccountName)
{
Requires.Argument("variable", variable).NotNull();

this.Name = variable.Name;
this.CreationTime = variable.Properties.CreationTime.ToLocalTime();
this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime();
this.Value = null;
this.Description = variable.Properties.Description;
this.Encrypted = true;
this.AutomationAccountName = automationAccountName;
}

/// <summary>
/// Initializes a new instance of the <see cref="Variable"/> class.
/// </summary>
Expand Down

0 comments on commit 3a084d9

Please sign in to comment.