Skip to content

Commit 2d45d32

Browse files
committed
Merge pull request #5 from kiranisaac/master
Variables
2 parents 9717ed0 + 15daba3 commit 2d45d32

12 files changed

+153
-47
lines changed

src/ServiceManagement/Automation/Commands.Automation.Test/UnitTests/NewAzureAutomationVariableTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ public void NewAzureAutomationVariableByPathSuccessfull()
6262
variable.Value = value;
6363
variable.Description = description;
6464
variable.Encrypted = true;
65+
variable.AutomationAccountName = accountName;
6566

6667
this.mockAutomationClient.Setup(
67-
f => f.CreateVariable(accountName, variable));
68+
f => f.CreateVariable(variable));
6869

6970
this.cmdlet.AutomationAccountName = accountName;
7071
this.cmdlet.Name = variableName;
@@ -74,7 +75,7 @@ public void NewAzureAutomationVariableByPathSuccessfull()
7475
this.cmdlet.ExecuteCmdlet();
7576

7677
// Assert
77-
this.mockAutomationClient.Verify(f => f.CreateVariable(accountName, variable), Times.Once());
78+
this.mockAutomationClient.Verify(f => f.CreateVariable(variable), Times.Once());
7879
}
7980
}
8081
}

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/NewAzureAutomationVariable.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ public class NewAzureAutomationVariable : AzureAutomationBaseCmdlet
2828
/// <summary>
2929
/// Gets or sets the variable name.
3030
/// </summary>
31-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
31+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
3232
[ValidateNotNullOrEmpty]
3333
public string Name { get; set; }
3434

3535
/// <summary>
36-
/// Gets or sets the variable IsEncrypted Property.
36+
/// Gets or sets the variable encrypted Property.
3737
/// </summary>
38-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The IsEncrypted property of the variable.")]
38+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The encrypted property of the variable.")]
3939
[ValidateNotNull]
40-
public SwitchParameter Encrypted { get; set; }
40+
public bool Encrypted { get; set; }
4141

4242
/// <summary>
4343
/// Gets or sets the variable description.
@@ -49,7 +49,7 @@ public class NewAzureAutomationVariable : AzureAutomationBaseCmdlet
4949
/// Gets or sets the variable value.
5050
/// </summary>
5151
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")]
52-
public string Value { get; set; }
52+
public object Value { get; set; }
5353

5454
/// <summary>
5555
/// Execute this cmdlet.
@@ -60,12 +60,13 @@ protected override void AutomationExecuteCmdlet()
6060
Variable variable = new Variable()
6161
{
6262
Name = this.Name,
63-
Encrypted = this.Encrypted.IsPresent,
63+
Encrypted = this.Encrypted,
6464
Description = this.Description,
65-
Value = this.Value
65+
Value = this.Value,
66+
AutomationAccountName = this.AutomationAccountName
6667
};
6768

68-
var ret = this.AutomationClient.CreateVariable(this.AutomationAccountName, variable);
69+
var ret = this.AutomationClient.CreateVariable(variable);
6970

7071
this.GenerateCmdletOutput(ret);
7172
}

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationVariable.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ protected override void AutomationExecuteCmdlet()
4747
{
4848
ConfirmAction(
4949
Force.IsPresent,
50-
string.Format(Resources.RemovingAzureAutomationResourceWarning, "Module"),
51-
string.Format(Resources.RemoveAzureAutomationResourceDescription, "Module"),
50+
string.Format(Resources.RemovingAzureAutomationResourceWarning, "Variable"),
51+
string.Format(Resources.RemoveAzureAutomationResourceDescription, "Variable"),
5252
Name,
5353
() =>
5454
{

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/SetAzureAutomationVariable.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Security.Permissions;
1919
using Microsoft.Azure.Commands.Automation.Common;
2020
using Microsoft.Azure.Commands.Automation.Model;
21+
using Newtonsoft.Json;
2122

2223
namespace Microsoft.Azure.Commands.Automation.Cmdlet
2324
{
@@ -31,21 +32,29 @@ public class SetAzureAutomationVariable : AzureAutomationBaseCmdlet
3132
/// <summary>
3233
/// Gets or sets the variable name.
3334
/// </summary>
34-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
35+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
36+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableDescription, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
3537
[ValidateNotNullOrEmpty]
3638
public string Name { get; set; }
3739

40+
/// <summary>
41+
/// Gets or sets the variable encrypted Property.
42+
/// </summary>
43+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The encrypted property of the variable.")]
44+
[ValidateNotNull]
45+
public bool Encrypted { get; set; }
46+
3847
/// <summary>
3948
/// Gets or sets the variable description.
4049
/// </summary>
41-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the variable.")]
50+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableDescription, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the variable.")]
4251
public string Description { get; set; }
4352

4453
/// <summary>
4554
/// Gets or sets the variable value.
4655
/// </summary>
47-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")]
48-
public string Value { get; set; }
56+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.UpdateVariableValue, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")]
57+
public object Value { get; set; }
4958

5059
/// <summary>
5160
/// Execute this cmdlet.
@@ -57,11 +66,20 @@ protected override void AutomationExecuteCmdlet()
5766
{
5867
Name = this.Name,
5968
Description = this.Description,
60-
Value = this.Value
69+
Encrypted = this.Encrypted,
70+
Value = this.Value,
71+
AutomationAccountName = this.AutomationAccountName
6172
};
6273

63-
var ret = this.AutomationClient.UpdateVariable(this.AutomationAccountName, variable);
64-
74+
Variable ret;
75+
if (ParameterSetName == AutomationCmdletParameterSets.UpdateVariableValue)
76+
{
77+
ret = this.AutomationClient.UpdateVariable(variable, VariableUpdateFields.OnlyValue);
78+
}
79+
else
80+
{
81+
ret = this.AutomationClient.UpdateVariable(variable, VariableUpdateFields.OnlyDescription);
82+
}
6583
this.GenerateCmdletOutput(ret);
6684
}
6785
}

src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<Compile Include="Common\RequiresExtensions.cs" />
169169
<Compile Include="Common\ResourceCommonException.cs" />
170170
<Compile Include="Common\ResourceNotFoundException.cs" />
171+
<Compile Include="Common\VariableUpdateFields.cs" />
171172
<Compile Include="DataContract\ErrorResponse.cs" />
172173
<Compile Include="DataContract\OdataError.cs" />
173174
<Compile Include="DataContract\OdataErrorMessage.cs" />
@@ -209,6 +210,7 @@
209210
<EmbeddedResource Include="Properties\Resources.resx">
210211
<Generator>ResXFileCodeGenerator</Generator>
211212
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
213+
<SubType>Designer</SubType>
212214
</EmbeddedResource>
213215
</ItemGroup>
214216
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,13 @@ public Job StartRunbook(string automationAccountName, string runbookName, IDicti
344344

345345
#region Variables
346346

347-
public Variable CreateVariable(string automationAccountName, Variable variable)
347+
public Variable CreateVariable(Variable variable)
348348
{
349349
bool variableExists = true;
350350

351351
try
352352
{
353-
this.GetVariable(automationAccountName, variable.Name);
353+
this.GetVariable(variable.AutomationAccountName, variable.Name);
354354
}
355355
catch (ResourceNotFoundException)
356356
{
@@ -370,16 +370,16 @@ public Variable CreateVariable(string automationAccountName, Variable variable)
370370
Name = variable.Name,
371371
Properties = new AutomationManagement.Models.EncryptedVariableCreateProperties()
372372
{
373-
Value = variable.Value,
373+
Value = JsonConvert.SerializeObject(variable.Value),
374374
Description = variable.Description
375375
}
376376
};
377377

378378
var sdkCreatedVariable =
379-
this.automationManagementClient.EncryptedVariables.Create(automationAccountName, createParams)
379+
this.automationManagementClient.EncryptedVariables.Create(variable.AutomationAccountName, createParams)
380380
.EncryptedVariable;
381381

382-
return new Variable(sdkCreatedVariable, automationAccountName);
382+
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
383383
}
384384
else
385385
{
@@ -388,15 +388,15 @@ public Variable CreateVariable(string automationAccountName, Variable variable)
388388
Name = variable.Name,
389389
Properties = new AutomationManagement.Models.VariableCreateProperties()
390390
{
391-
Value = variable.Value,
391+
Value = JsonConvert.SerializeObject(variable.Value),
392392
Description = variable.Description
393393
}
394394
};
395395

396396
var sdkCreatedVariable =
397-
this.automationManagementClient.Variables.Create(automationAccountName, createParams).Variable;
397+
this.automationManagementClient.Variables.Create(variable.AutomationAccountName, createParams).Variable;
398398

399-
return new Variable(sdkCreatedVariable, automationAccountName);
399+
return new Variable(sdkCreatedVariable, variable.AutomationAccountName);
400400
}
401401
}
402402

@@ -422,41 +422,66 @@ public void DeleteVariable(string automationAccountName, string variableName)
422422
}
423423
}
424424

425-
public Variable UpdateVariable(string automationAccountName, Variable variable)
425+
public Variable UpdateVariable(Variable variable, VariableUpdateFields updateFields)
426426
{
427-
var existingVarible = this.GetVariable(automationAccountName, variable.Name);
428-
variable.Encrypted = existingVarible.Encrypted;
427+
var existingVariable = this.GetVariable(variable.AutomationAccountName, variable.Name);
428+
429+
if (existingVariable.Encrypted != variable.Encrypted)
430+
{
431+
throw new ResourceNotFoundException(typeof(Variable),
432+
string.Format(CultureInfo.CurrentCulture, Resources.VariableEncryptionCannotBeChanged, variable.Name, existingVariable.Encrypted));
433+
}
429434

430435
if (variable.Encrypted)
431436
{
432437
var updateParams = new AutomationManagement.Models.EncryptedVariableUpdateParameters()
433438
{
434-
Name = variable.Name,
435-
Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
439+
Name = variable.Name
440+
};
441+
442+
if (updateFields == VariableUpdateFields.OnlyDescription)
443+
{
444+
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
436445
{
437-
Value = variable.Value,
438446
Description = variable.Description
439-
}
440-
};
447+
};
448+
}
449+
else
450+
{
451+
updateParams.Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
452+
{
453+
Value = JsonConvert.SerializeObject(variable.Value)
454+
};
455+
}
441456

442-
this.automationManagementClient.EncryptedVariables.Update(automationAccountName, updateParams);
457+
this.automationManagementClient.EncryptedVariables.Update(variable.AutomationAccountName, updateParams);
443458
}
444459
else
445460
{
446461
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
447462
{
448463
Name = variable.Name,
449-
Properties = new AutomationManagement.Models.VariableUpdateProperties()
464+
};
465+
466+
if (updateFields == VariableUpdateFields.OnlyDescription)
467+
{
468+
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
450469
{
451-
Value = variable.Value,
452470
Description = variable.Description
453-
}
454-
};
471+
};
472+
}
473+
else
474+
{
475+
updateParams.Properties = new AutomationManagement.Models.VariableUpdateProperties()
476+
{
477+
Value = JsonConvert.SerializeObject(variable.Value)
478+
};
479+
}
455480

456-
this.automationManagementClient.Variables.Update(automationAccountName, updateParams);
481+
this.automationManagementClient.Variables.Update(variable.AutomationAccountName, updateParams);
457482
}
458483

459-
return this.GetVariable(automationAccountName, variable.Name);
484+
return this.GetVariable(variable.AutomationAccountName, variable.Name);
460485
}
461486

462487
public Variable GetVariable(string automationAccountName, string name)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,15 @@ internal static class AutomationCmdletParameterSets
8282
/// The Job Schedule Id parameter set.
8383
/// </summary>
8484
internal const string ByJobScheduleId = "ByJobScheduleId";
85+
86+
/// <summary>
87+
/// Parameter set for updating variable value
88+
/// </summary>
89+
internal const string UpdateVariableValue = "UpdateVariableValue";
90+
91+
/// <summary>
92+
/// Parameter set for updating variable description
93+
/// </summary>
94+
internal const string UpdateVariableDescription = "UpdateVariableDescription";
8595
}
8696
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public interface IAutomationClient
3737

3838
IEnumerable<Variable> ListVariables(string automationAccountName);
3939

40-
Variable CreateVariable(string automationAccountName, Variable variable);
40+
Variable CreateVariable(Variable variable);
4141

4242
void DeleteVariable(string automationAccountName, string variableName);
4343

44-
Variable UpdateVariable(string automationAccountName, Variable variable);
44+
Variable UpdateVariable(Variable variable, VariableUpdateFields updateFields);
4545

4646
#endregion
4747

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.Automation.Common
18+
{
19+
public enum VariableUpdateFields
20+
{
21+
OnlyDescription = 0,
22+
OnlyValue = 1
23+
}
24+
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
namespace Microsoft.Azure.Commands.Automation.Model
1919
{
2020
using AutomationManagement = Management.Automation;
21+
using Newtonsoft.Json;
22+
using System.Management.Automation;
2123

2224
/// <summary>
2325
/// The Variable.
@@ -39,7 +41,16 @@ public Variable(AutomationManagement.Models.Variable variable, string automation
3941
this.Name = variable.Name;
4042
this.CreationTime = variable.Properties.CreationTime.ToLocalTime();
4143
this.LastModifiedTime = variable.Properties.LastModifiedTime.ToLocalTime();
42-
this.Value = variable.Properties.Value;
44+
45+
if (variable.Properties.Value == null)
46+
{
47+
this.Value = null;
48+
}
49+
else
50+
{
51+
this.Value = JsonConvert.DeserializeObject<object>(variable.Properties.Value);
52+
}
53+
4354
this.Description = variable.Properties.Description;
4455
this.Encrypted = false;
4556
this.AutomationAccountName = automationAccoutName;
@@ -91,7 +102,7 @@ public Variable()
91102
/// <summary>
92103
/// Gets or sets the value.
93104
/// </summary>
94-
public string Value { get; set; }
105+
public object Value { get; set; }
95106

96107
/// <summary>
97108
/// Gets or sets the description.

0 commit comments

Comments
 (0)