Skip to content

Commit acc3cda

Browse files
committed
2 cmdlets for variables
1 parent 35edea9 commit acc3cda

File tree

9 files changed

+435
-0
lines changed

9 files changed

+435
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,22 @@ public override void ExecuteCmdlet()
9393
}
9494
}
9595

96+
protected bool GenerateCmdletOutput(object result)
97+
{
98+
var ret = true;
99+
100+
try
101+
{
102+
WriteObject(result);
103+
}
104+
catch (PipelineStoppedException)
105+
{
106+
ret = false;
107+
}
108+
109+
return ret;
110+
}
111+
96112
protected bool GenerateCmdletOutput(IEnumerable<object> results)
97113
{
98114
var ret = true;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Model;
21+
22+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
23+
{
24+
/// <summary>
25+
/// Gets azure automation variables for a given account.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Get, "AzureAutomationVariable", DefaultParameterSetName = AutomationCmdletParameterSets.ByAll)]
28+
[OutputType(typeof(Variable))]
29+
public class GetAzureAutomationVariable : AzureAutomationBaseCmdlet
30+
{
31+
/// <summary>
32+
/// Gets or sets the variable name.
33+
/// </summary>
34+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
35+
[ValidateNotNullOrEmpty]
36+
public string Name { get; set; }
37+
38+
/// <summary>
39+
/// Execute this cmdlet.
40+
/// </summary>
41+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
42+
protected override void AutomationExecuteCmdlet()
43+
{
44+
IEnumerable<Variable> ret = null;
45+
if (this.ParameterSetName == AutomationCmdletParameterSets.ByName)
46+
{
47+
ret = new List<Variable>
48+
{
49+
this.AutomationClient.GetVariable(this.AutomationAccountName, this.Name)
50+
};
51+
}
52+
else if (this.ParameterSetName == AutomationCmdletParameterSets.ByAll)
53+
{
54+
ret = this.AutomationClient.ListVariables(this.AutomationAccountName);
55+
}
56+
57+
this.GenerateCmdletOutput(ret);
58+
}
59+
}
60+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Model;
21+
22+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
23+
{
24+
/// <summary>
25+
/// Gets azure automation variables for a given account.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Get, "AzureAutomationVariable")]
28+
[OutputType(typeof(Variable))]
29+
public class SetAzureAutomationVariable : AzureAutomationBaseCmdlet
30+
{
31+
/// <summary>
32+
/// Gets or sets the variable name.
33+
/// </summary>
34+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The variable name.")]
35+
[ValidateNotNullOrEmpty]
36+
public string Name { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets the variable IsEncrypted Property.
40+
/// </summary>
41+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The IsEncrypted property of the variable.")]
42+
[ValidateNotNull]
43+
public bool IsEncrypted { get; set; }
44+
45+
/// <summary>
46+
/// Gets or sets the variable description.
47+
/// </summary>
48+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the variable.")]
49+
public string Description { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets the variable value.
53+
/// </summary>
54+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The value of the variable.")]
55+
public string Value { get; set; }
56+
57+
/// <summary>
58+
/// Execute this cmdlet.
59+
/// </summary>
60+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
61+
protected override void AutomationExecuteCmdlet()
62+
{
63+
Variable variable = new Variable()
64+
{
65+
Name = this.Name,
66+
IsEncrypted = this.IsEncrypted,
67+
Description = this.Description,
68+
Value = this.Value
69+
};
70+
71+
var ret = this.AutomationClient.SetVariable(this.AutomationAccountName, variable);
72+
73+
this.GenerateCmdletOutput(ret);
74+
}
75+
}
76+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@
100100
</ItemGroup>
101101
<ItemGroup>
102102
<Compile Include="Cmdlet\AzureAutomationBaseCmdlet.cs" />
103+
<Compile Include="Cmdlet\GetAzureAutomationVariable.cs" />
103104
<Compile Include="Cmdlet\GetAzureAutomationRunbook.cs" />
104105
<Compile Include="Cmdlet\GetAzureAutomationSchedule.cs" />
106+
<Compile Include="Cmdlet\SetAutomationVariable.cs" />
105107
<Compile Include="Common\AutomationClient.cs" />
106108
<Compile Include="Common\AutomationCmdletParameterSet.cs" />
107109
<Compile Include="Common\IAutomationClient.cs" />
@@ -114,6 +116,7 @@
114116
<Compile Include="Model\Runbook.cs" />
115117
<Compile Include="Model\Schedule.cs" />
116118
<Compile Include="Model\ScheduleFrequency.cs" />
119+
<Compile Include="Model\Variable.cs" />
117120
<Compile Include="Properties\AssemblyInfo.cs" />
118121
<Compile Include="Properties\Resources.Designer.cs">
119122
<AutoGen>True</AutoGen>

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

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,149 @@ public Runbook GetRunbook(string automationAccountName, string name)
8989
return new Runbook(sdkRunbook);
9090
}
9191

92+
public Variable SetVariable(string automationAccountName, Variable variable)
93+
{
94+
bool variableExists = true;
95+
96+
try
97+
{
98+
this.GetVariable(automationAccountName, variable.Name);
99+
}
100+
catch (ResourceNotFoundException)
101+
{
102+
variableExists = false;
103+
}
104+
105+
if (variableExists)
106+
{
107+
if (variable.IsEncrypted)
108+
{
109+
var updateParams = new AutomationManagement.Models.EncryptedVariableUpdateParameters()
110+
{
111+
Name = variable.Name,
112+
Properties = new AutomationManagement.Models.EncryptedVariableUpdateProperties()
113+
{
114+
Value = variable.Value,
115+
Description = variable.Description
116+
}
117+
};
118+
119+
this.automationManagementClient.EncryptedVariables.Update(automationAccountName, updateParams);
120+
}
121+
else
122+
{
123+
var updateParams = new AutomationManagement.Models.VariableUpdateParameters()
124+
{
125+
Name = variable.Name,
126+
Properties = new AutomationManagement.Models.VariableUpdateProperties()
127+
{
128+
Value = variable.Value,
129+
Description = variable.Description
130+
}
131+
};
132+
133+
this.automationManagementClient.Variables.Update(automationAccountName, updateParams);
134+
}
135+
136+
return this.GetVariable(automationAccountName, variable.Name);
137+
}
138+
else
139+
{
140+
if (variable.IsEncrypted)
141+
{
142+
var createParams = new AutomationManagement.Models.EncryptedVariableCreateParameters()
143+
{
144+
Name = variable.Name,
145+
Properties = new AutomationManagement.Models.EncryptedVariableCreateProperties()
146+
{
147+
Value = variable.Value,
148+
Description = variable.Description
149+
}
150+
};
151+
152+
var sdkCreatedVariable = this.automationManagementClient.EncryptedVariables.Create(automationAccountName, createParams).EncryptedVariable;
153+
154+
if (sdkCreatedVariable == null)
155+
{
156+
// TODO: throw the right error here
157+
throw new ArgumentNullException();
158+
}
159+
160+
return new Variable(sdkCreatedVariable);
161+
}
162+
else
163+
{
164+
var createParams = new AutomationManagement.Models.VariableCreateParameters()
165+
{
166+
Name = variable.Name,
167+
Properties = new AutomationManagement.Models.VariableCreateProperties()
168+
{
169+
Value = variable.Value,
170+
Description = variable.Description
171+
}
172+
};
173+
174+
var sdkCreatedVariable = this.automationManagementClient.Variables.Create(automationAccountName, createParams).Variable;
175+
176+
if (sdkCreatedVariable == null)
177+
{
178+
// TODO: throw the right error here
179+
throw new ArgumentNullException();
180+
}
181+
182+
return new Variable(sdkCreatedVariable);
183+
}
184+
}
185+
186+
}
187+
188+
public Variable GetVariable(string automationAccountName, string name)
189+
{
190+
var sdkEncryptedVariable = this.automationManagementClient.EncryptedVariables.Get(
191+
automationAccountName, name).EncryptedVariable;
192+
193+
if (sdkEncryptedVariable != null)
194+
{
195+
return new Variable(sdkEncryptedVariable);
196+
}
197+
198+
var sdkVarible = this.automationManagementClient.Variables.Get(automationAccountName, name).Variable;
199+
200+
if (sdkVarible != null)
201+
{
202+
return new Variable(sdkVarible);
203+
}
204+
205+
throw new ResourceNotFoundException(typeof(Variable), string.Format(CultureInfo.CurrentCulture, Resources.VariableNotFound, name));
206+
}
207+
208+
public IEnumerable<Variable> ListVariables(string automationAccountName)
209+
{
210+
IList<AutomationManagement.Models.Variable> variables = AutomationManagementClient.ContinuationTokenHandler(
211+
skipToken =>
212+
{
213+
var response = this.automationManagementClient.Variables.List(
214+
automationAccountName);
215+
return new ResponseWithSkipToken<AutomationManagement.Models.Variable>(
216+
response, response.Variables);
217+
});
218+
219+
var result = variables.Select(this.CreateVariableFromVariableModel).ToList();
220+
221+
IList<AutomationManagement.Models.EncryptedVariable> encryptedVariables = AutomationManagementClient.ContinuationTokenHandler(
222+
skipToken =>
223+
{
224+
var response = this.automationManagementClient.EncryptedVariables.List(
225+
automationAccountName);
226+
return new ResponseWithSkipToken<AutomationManagement.Models.EncryptedVariable>(
227+
response, response.EncryptedVariables);
228+
});
229+
230+
result.AddRange(encryptedVariables.Select(this.CreateVariableFromVariableModel).ToList());
231+
232+
return result;
233+
}
234+
92235
public IEnumerable<Runbook> ListRunbooks(string automationAccountName)
93236
{
94237
return AutomationManagementClient
@@ -105,6 +248,20 @@ public IEnumerable<Runbook> ListRunbooks(string automationAccountName)
105248
#endregion
106249

107250
#region Private Methods
251+
private Variable CreateVariableFromVariableModel(AutomationManagement.Models.Variable variable)
252+
{
253+
Requires.Argument("variable", variable).NotNull();
254+
255+
return new Variable(variable);
256+
}
257+
258+
private Variable CreateVariableFromVariableModel(AutomationManagement.Models.EncryptedVariable variable)
259+
{
260+
Requires.Argument("variable", variable).NotNull();
261+
262+
return new Variable(variable);
263+
}
264+
108265

109266
private Schedule CreateScheduleFromScheduleModel(AutomationManagement.Models.Schedule schedule)
110267
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public interface IAutomationClient
2424
{
2525
AzureSubscription Subscription { get; }
2626

27+
Variable GetVariable(string automationAccountName, string variableName);
28+
29+
IEnumerable<Variable> ListVariables(string automationAccountName);
30+
31+
Variable SetVariable(string automationAccountName, Variable variable);
32+
2733
Schedule GetSchedule(string automationAccountName, string scheduleName);
2834

2935
IEnumerable<Schedule> ListSchedules(string automationAccountName);

0 commit comments

Comments
 (0)