Skip to content

Commit 41be7f3

Browse files
author
Leandro Wajswajn Pereyra
committed
Added configuration name validation. Improved error handling for Import-AzAutomationDscConfiguration
1 parent 5bcd2fb commit 41be7f3

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/Automation/Automation/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Added configuration name validation to Import-AzAutomationDscConfiguration cmdlet
22+
* Improved error handling for Import-AzAutomationDscConfiguration cmdlet
2123

2224
## Version 1.1.0
2325
* Added support for Python 2 runbooks

src/Automation/Automation/Common/AutomationPSClientDSC.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,11 @@ public Model.DscConfiguration CreateConfiguration(
173173
string fileContent = null;
174174
string configurationName = String.Empty;
175175

176-
try
176+
if (File.Exists(Path.GetFullPath(sourcePath)))
177177
{
178-
if (File.Exists(Path.GetFullPath(sourcePath)))
179-
{
180-
fileContent = System.IO.File.ReadAllText(sourcePath);
181-
}
178+
fileContent = System.IO.File.ReadAllText(sourcePath);
182179
}
183-
catch (Exception)
184-
{
180+
else {
185181
// exception in accessing the file path
186182
throw new FileNotFoundException(
187183
string.Format(
@@ -192,6 +188,10 @@ public Model.DscConfiguration CreateConfiguration(
192188
// configuration name is same as filename
193189
configurationName = Path.GetFileNameWithoutExtension(sourcePath);
194190

191+
if (!System.Text.RegularExpressions.Regex.IsMatch(configurationName, "^([a-zA-Z]{1}([a-zA-Z0-9]|_){0,63})$")) {
192+
throw new PSInvalidOperationException("Invalid configuration name. Valid configuration names can contain only letters, numbers, and underscores. The name must start with a letter. The length of the name must be between 1 and 64 characters. ");
193+
}
194+
195195
// for the private preview, configuration can be imported in Published mode only
196196
// Draft mode is not implemented
197197
if (!published)
@@ -238,14 +238,30 @@ public Model.DscConfiguration CreateConfiguration(
238238
}
239239
};
240240

241-
var configuration =
242-
this.automationManagementClient.DscConfiguration.CreateOrUpdate(
241+
try
242+
{
243+
var configuration =
244+
this.automationManagementClient.DscConfiguration.CreateOrUpdate(
243245
resourceGroupName,
244246
automationAccountName,
245247
configurationName,
246248
configurationCreateParameters);
247249

248-
return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
250+
return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
251+
}
252+
catch (Microsoft.Azure.Management.Automation.Models.ErrorResponseException ex)
253+
{
254+
if (ex.Response.Content != null)
255+
{
256+
throw new Microsoft.Azure.Management.Automation.Models.ErrorResponseException(ex.Response.Content, ex);
257+
}
258+
else {
259+
throw ex;
260+
}
261+
}
262+
catch (Exception ex) {
263+
throw ex;
264+
}
249265
}
250266
}
251267

0 commit comments

Comments
 (0)