Skip to content

Commit 3a084d9

Browse files
committed
Merge pull request #10 from kiranisaac/master
Merge encrypted variable with unencrypted variable
2 parents e5da677 + e514036 commit 3a084d9

File tree

2 files changed

+32
-142
lines changed

2 files changed

+32
-142
lines changed

src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -405,41 +405,21 @@ public Variable CreateVariable(Variable variable)
405405
Resources.VariableAlreadyExists, variable.Name));
406406
}
407407

408-
if (variable.Encrypted)
408+
var createParams = new AutomationManagement.Models.VariableCreateParameters()
409409
{
410-
var createParams = new AutomationManagement.Models.EncryptedVariableCreateParameters()
410+
Name = variable.Name,
411+
Properties = new AutomationManagement.Models.VariableCreateProperties()
411412
{
412-
Name = variable.Name,
413-
Properties = new AutomationManagement.Models.EncryptedVariableCreateProperties()
414-
{
415-
Value = PowerShellJsonConverter.Serialize(variable.Value),
416-
Description = variable.Description
417-
}
418-
};
419-
420-
var sdkCreatedVariable =
421-
this.automationManagementClient.EncryptedVariables.Create(variable.AutomationAccountName, createParams)
422-
.EncryptedVariable;
423-
424-
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
425-
}
426-
else
427-
{
428-
var createParams = new AutomationManagement.Models.VariableCreateParameters()
429-
{
430-
Name = variable.Name,
431-
Properties = new AutomationManagement.Models.VariableCreateProperties()
432-
{
433-
Value = PowerShellJsonConverter.Serialize(variable.Value),
434-
Description = variable.Description
435-
}
436-
};
413+
Value = PowerShellJsonConverter.Serialize(variable.Value),
414+
Description = variable.Description,
415+
IsEncrypted = variable.Encrypted
416+
}
417+
};
437418

438-
var sdkCreatedVariable =
439-
this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable;
419+
var sdkCreatedVariable =
420+
this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable;
440421

441-
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
442-
}
422+
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
443423
}
444424

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

451-
if (existingVarible.Encrypted)
452-
{
453-
this.automationManagementClient.EncryptedVariables.Delete(automationAccountName, variableName);
454-
}
455-
else
456-
{
457-
this.automationManagementClient.Variables.Delete(automationAccountName, variableName);
458-
}
431+
this.automationManagementClient.Variables.Delete(automationAccountName, variableName);
459432
}
460433
catch (CloudException cloudException)
461434
{
@@ -479,75 +452,33 @@ public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFie
479452
string.Format(CultureInfo.CurrentCulture, Resources.VariableEncryptionCannotBeChanged, variable.Name, existingVariable.Encrypted));
480453
}
481454

482-
if (variable.Encrypted)
455+
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
483456
{
484-
var updateParams = new AutomationManagement.Models.EncryptedVariableUpdateParameters()
485-
{
486-
Name = variable.Name
487-
};
457+
Name = variable.Name,
458+
};
488459

489-
if (updateFields == VariableUpdateFields.OnlyDescription)
490-
{
491-
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
492-
{
493-
Description = variable.Description
494-
};
495-
}
496-
else
460+
if (updateFields == VariableUpdateFields.OnlyDescription)
461+
{
462+
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
497463
{
498-
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
499-
{
500-
Value = PowerShellJsonConverter.Serialize(variable.Value)
501-
};
502-
}
503-
504-
this.automationManagementClient.EncryptedVariables.Update(variable.AutomationAccountName, updateParams);
464+
Description = variable.Description
465+
};
505466
}
506467
else
507468
{
508-
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
469+
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
509470
{
510-
Name = variable.Name,
471+
Value = PowerShellJsonConverter.Serialize(variable.Value)
511472
};
512-
513-
if (updateFields == VariableUpdateFields.OnlyDescription)
514-
{
515-
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
516-
{
517-
Description = variable.Description
518-
};
519-
}
520-
else
521-
{
522-
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
523-
{
524-
Value = PowerShellJsonConverter.Serialize(variable.Value)
525-
};
526-
}
527-
528-
this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams);
529473
}
530474

475+
this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams);
476+
531477
return this.GetVariable(variable.AutomationAccountName, variable.Name);
532478
}
533479

534480
public Variable GetVariable(string automationAccountName, string name)
535481
{
536-
try
537-
{
538-
var sdkEncryptedVariable = this.automationManagementClient.EncryptedVariables.Get(
539-
automationAccountName, name).EncryptedVariable;
540-
541-
if (sdkEncryptedVariable != null)
542-
{
543-
return new Variable(sdkEncryptedVariable, automationAccountName);
544-
}
545-
}
546-
catch (CloudException)
547-
{
548-
// do nothing
549-
}
550-
551482
try
552483
{
553484
var sdkVarible = this.automationManagementClient.Variables.Get(automationAccountName, name).Variable;
@@ -556,14 +487,15 @@ public Variable GetVariable(string automationAccountName, string name)
556487
{
557488
return new Variable(sdkVarible, automationAccountName);
558489
}
490+
491+
throw new ResourceNotFoundException(typeof(Variable),
492+
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
559493
}
560494
catch (CloudException)
561495
{
562-
// do nothing
496+
throw new ResourceNotFoundException(typeof(Variable),
497+
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
563498
}
564-
565-
throw new ResourceNotFoundException(typeof(Variable),
566-
string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
567499
}
568500

569501
public IEnumerable<Variable> ListVariables(string automationAccountName)
@@ -577,20 +509,7 @@ public IEnumerable<Variable> ListVariables(string automationAccountName)
577509
response, response.Variables);
578510
});
579511

580-
var result = variables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList();
581-
582-
IList<AutomationManagement.Models.EncryptedVariable> encryptedVariables = AutomationManagementClient.ContinuationTokenHandler(
583-
skipToken =>
584-
{
585-
var response = this.automationManagementClient.EncryptedVariables.List(
586-
automationAccountName);
587-
return new ResponseWithSkipToken<AutomationManagement.Models.EncryptedVariable>(
588-
response, response.EncryptedVariables);
589-
});
590-
591-
result.AddRange(encryptedVariables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList());
592-
593-
return result;
512+
return variables.Select(variable => this.CreateVariableFromVariableModel(variable, automationAccountName)).ToList();
594513
}
595514
#endregion
596515

@@ -1581,14 +1500,6 @@ private Variable CreateVariableFromVariableModel(AutomationManagement.Models.Var
15811500
return new Variable(variable, automationAccountName);
15821501
}
15831502

1584-
private Variable CreateVariableFromVariableModel(AutomationManagement.Models.EncryptedVariable variable, string automationAccountName)
1585-
{
1586-
Requires.Argument("variable", variable).NotNull();
1587-
1588-
return new Variable(variable, automationAccountName);
1589-
}
1590-
1591-
15921503
private AutomationManagement.Models.Schedule GetScheduleModel(string automationAccountName, string scheduleName)
15931504
{
15941505
AutomationManagement.Models.Schedule scheduleModel;

src/ServiceManagement/Automation/Commands.Automation/Model/Variable.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
4040
this.CreationTime = variable.Properties.CreationTime.ToLocalTime();
4141
this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime();
4242

43-
if (variable.Properties.Value == null)
43+
if (variable.Properties.Value == null || variable.Properties.IsEncrypted)
4444
{
4545
this.Value = null;
4646
}
@@ -50,31 +50,10 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
5050
}
5151

5252
this.Description = variable.Properties.Description;
53-
this.Encrypted = false;
53+
this.Encrypted = variable.Properties.IsEncrypted;
5454
this.AutomationAccountName = automationAccoutName;
5555
}
5656

57-
// <summary>
58-
/// Initializes a new instance of the <see cref="Variable"/> class.
59-
/// </summary>
60-
/// <param name="variable">
61-
/// The variable.
62-
/// </param>
63-
/// <exception cref="System.ArgumentException">
64-
/// </exception>
65-
public Variable(AutomationManagement.Models.EncryptedVariable variable, string automationAccountName)
66-
{
67-
Requires.Argument("variable", variable).NotNull();
68-
69-
this.Name = variable.Name;
70-
this.CreationTime = variable.Properties.CreationTime.ToLocalTime();
71-
this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime();
72-
this.Value = null;
73-
this.Description = variable.Properties.Description;
74-
this.Encrypted = true;
75-
this.AutomationAccountName = automationAccountName;
76-
}
77-
7857
/// <summary>
7958
/// Initializes a new instance of the <see cref="Variable"/> class.
8059
/// </summary>

0 commit comments

Comments
 (0)