Skip to content

Commit 802904b

Browse files
committed
report model update2
1 parent 694947e commit 802904b

File tree

5 files changed

+146
-50
lines changed

5 files changed

+146
-50
lines changed

src/ResourceManager/Automation/Commands.Automation/Cmdlet/ImportAzureAutomationDscNodeConfiguration.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,40 @@ namespace Microsoft.Azure.Commands.Automation.Cmdlet
2626
/// <summary>
2727
/// Imports dsc node configuration script
2828
/// </summary>
29-
[Cmdlet(VerbsData.Import, "ImportAzureAutomationDscNodeConfiguration")]
30-
[OutputType(typeof(DscConfiguration))]
29+
[Cmdlet(VerbsData.Import, "AzureAutomationDscNodeConfiguration")]
30+
[OutputType(typeof(NodeConfiguration))]
3131
public class ImportAzureAutomationDscNodeConfiguration : AzureAutomationBaseCmdlet
32-
{
32+
{
33+
/// <summary>
34+
/// True to overwrite the existing configuration; false otherwise.
35+
/// </summary>
36+
private bool overwriteExistingConfiguration;
37+
3338
/// <summary>
3439
/// Gets or sets the source path.
3540
/// </summary>
36-
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Path to the node configuration script .mof to import.")]
41+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Path to the node configuration .mof to import.")]
3742
[Alias("Path")]
3843
[ValidateNotNullOrEmpty]
3944
public string SourcePath { get; set; }
4045

4146
/// <summary>
42-
/// Gets or sets the node configuration name.
47+
/// Gets or sets the configuration name for the node configuration.
4348
/// </summary>
44-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The dsc node configuration name for which .mof is imported.")]
45-
[Alias("Name")]
46-
public string Name { get; set; }
49+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the DSC Configuration to import the node configuration under. All Node Configurations in Azure Automation must exist under a Configuration. The name of the Configuration will become the namespace of the imported Node Configuration, in the form of 'ConfigurationName.MofFileName'")]
50+
[Alias("NodeConfigurationName")]
51+
public string ConfigurationName { get; set; }
52+
4753

4854
/// <summary>
49-
/// Gets or sets the configuration name for the node configuration.
55+
/// Gets or sets switch parameter to confirm overwriting of existing configurations.
5056
/// </summary>
51-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The dsc configuration name for .mof imported")]
52-
[Alias("ConfigurationName")]
53-
public string ConfigurationName { get; set; }
57+
[Parameter(Mandatory = false, HelpMessage = "Forces the command to overwrite an existing configuration.")]
58+
public SwitchParameter Force
59+
{
60+
get { return this.overwriteExistingConfiguration; }
61+
set { this.overwriteExistingConfiguration = value; }
62+
}
5463

5564
/// <summary>
5665
/// Execute this cmdlet.
@@ -62,8 +71,8 @@ public override void ExecuteCmdlet()
6271
this.ResourceGroupName,
6372
this.AutomationAccountName,
6473
this.SourcePath,
65-
this.Name,
66-
this.ConfigurationName);
74+
this.ConfigurationName,
75+
this.Force);
6776

6877
this.WriteObject(nodeConfiguration);
6978
}

src/ResourceManager/Automation/Commands.Automation/Common/AutomationClientDSC.cs

Lines changed: 108 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,48 @@ private Model.DscConfiguration TryGetConfigurationModel(string resourceGroupName
257257
return configuration;
258258
}
259259

260+
public Model.DscConfiguration CreateConfiguration(
261+
string resourceGroupName,
262+
string automationAccountName,
263+
string configrationName,
264+
string nodeName)
265+
{
266+
string configurationContent = "Configuration {0} { Node {1} { } } ";
267+
configurationContent = string.Format(configurationContent,configrationName,nodeName);
268+
269+
using (var request = new RequestSettings(this.automationManagementClient))
270+
{
271+
272+
// location of the configuration is set to same as that of automation account
273+
string location = this.GetAutomationAccount(resourceGroupName, automationAccountName).Location;
274+
275+
var configurationCreateParameters = new DscConfigurationCreateOrUpdateParameters()
276+
{
277+
Name = configrationName,
278+
Location = location,
279+
Properties = new DscConfigurationCreateOrUpdateProperties()
280+
{
281+
Description = String.Empty,
282+
LogVerbose = false,
283+
Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
284+
{
285+
// only embeddedContent supported for now
286+
ContentType = Model.ContentSourceType.embeddedContent.ToString(),
287+
Value = configurationContent
288+
}
289+
}
290+
};
291+
292+
var configuration =
293+
this.automationManagementClient.Configurations.CreateOrUpdate(
294+
resourceGroupName,
295+
automationAccountName,
296+
configurationCreateParameters).Configuration;
297+
298+
return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
299+
}
300+
}
301+
260302
#endregion
261303

262304
#region DscMetaConfig Operations
@@ -1122,17 +1164,19 @@ public Model.NodeConfiguration CreateNodeConfiguration(
11221164
string resourceGroupName,
11231165
string automationAccountName,
11241166
string sourcePath,
1125-
string nodeConfiguraionName,
1126-
string configurationName)
1167+
string nodeConfigurationName,
1168+
bool overWrite)
11271169
{
11281170
using (var request = new RequestSettings(this.automationManagementClient))
11291171
{
11301172
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
11311173
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
11321174
Requires.Argument("SourcePath", sourcePath).NotNull();
1175+
Requires.Argument("nodeConfigurationName", nodeConfigurationName).NotNull();
11331176

11341177
string fileContent = null;
1135-
string nodeConfigurationName = String.Empty;
1178+
string configurationName = null;
1179+
string nodeName = null;
11361180

11371181
try
11381182
{
@@ -1150,9 +1194,57 @@ public Model.NodeConfiguration CreateNodeConfiguration(
11501194
Resources.ConfigurationSourcePathInvalid));
11511195
}
11521196

1197+
try
1198+
{
1199+
var configuration = nodeConfigurationName.Split('.');
1200+
if(configuration.Length == 2)
1201+
{
1202+
configurationName = configuration[0];
1203+
nodeName = configuration[1];
1204+
}
1205+
else
1206+
{
1207+
throw new Exception();
1208+
}
1209+
}
1210+
catch (Exception)
1211+
{
1212+
// exception in getting the config name from NodeConfigName
1213+
throw new FileNotFoundException(
1214+
string.Format(
1215+
CultureInfo.CurrentCulture,
1216+
Resources.NodeConfigurationNameInvalid));
1217+
}
1218+
1219+
// if node configuration already exists, ensure overwrite flag is specified
1220+
var nodeConfigurationModel = this.GetNodeConfiguration(
1221+
resourceGroupName,
1222+
automationAccountName,
1223+
nodeConfigurationName,
1224+
null);
1225+
if (nodeConfigurationModel != null)
1226+
{
1227+
if (!overWrite)
1228+
{
1229+
throw new ResourceCommonException(typeof(Model.NodeConfiguration),
1230+
string.Format(CultureInfo.CurrentCulture, Resources.NodeConfigurationAlreadyExists, nodeConfigurationName));
1231+
}
1232+
}
1233+
1234+
// if configuration already exists, ensure overwrite flag is specified
1235+
var configurationModel = this.TryGetConfigurationModel(
1236+
resourceGroupName,
1237+
automationAccountName,
1238+
configurationName);
1239+
if (configurationModel == null)
1240+
{
1241+
//create empty configuration if its empty
1242+
this.CreateConfiguration(resourceGroupName, automationAccountName, configurationName, nodeName);
1243+
}
1244+
11531245
var nodeConfigurationCreateParameters = new DscNodeConfigurationCreateOrUpdateParameters()
11541246
{
1155-
Name = nodeConfiguraionName,
1247+
Name = nodeConfigurationName,
11561248
Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
11571249
{
11581250
// only embeddedContent supported for now
@@ -1165,13 +1257,14 @@ public Model.NodeConfiguration CreateNodeConfiguration(
11651257
}
11661258
};
11671259

1168-
var configuration =
1260+
var nodeConfiguration =
11691261
this.automationManagementClient.NodeConfigurations.CreateOrUpdate(
11701262
resourceGroupName,
11711263
automationAccountName,
11721264
nodeConfigurationCreateParameters).NodeConfiguration;
11731265

1174-
return new Model.NodeConfiguration(resourceGroupName, automationAccountName, configuration);
1266+
1267+
return new Model.NodeConfiguration(resourceGroupName, automationAccountName, nodeConfiguration, null);
11751268
}
11761269
}
11771270

@@ -1396,39 +1489,20 @@ private string FormatDateTime(DateTimeOffset dateTime)
13961489
private IDictionary<string, string> ProcessConfigurationParameters(string resourceGroupName, string automationAccountName, string configurationName, IDictionary parameters)
13971490
{
13981491
parameters = parameters ?? new Dictionary<string, string>();
1399-
IEnumerable<KeyValuePair<string, DscConfigurationParameter>> configurationParameters = this.ListConfigurationParameters(resourceGroupName, automationAccountName, configurationName);
1400-
var filteredParameters = new Dictionary<string, string>();
1401-
1402-
foreach (var configParameter in configurationParameters)
1403-
{
1404-
if (parameters.Contains(configParameter.Key))
1492+
var filteredParameters = new Dictionary<string,string>();
1493+
foreach (var configParameter in parameters.Keys)
1494+
{
1495+
try
14051496
{
1406-
object paramValue = parameters[configParameter.Key];
1407-
try
1408-
{
1409-
filteredParameters.Add(configParameter.Key, paramValue.ToString());
1410-
}
1411-
catch (JsonSerializationException)
1412-
{
1413-
throw new ArgumentException(
1414-
string.Format(
1415-
CultureInfo.CurrentCulture, Resources.ConfigurationParameterCannotBeSerializedToJson, configParameter.Key));
1416-
}
1497+
filteredParameters.Add(configParameter.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(parameters[configParameter]));
14171498
}
1418-
else if (configParameter.Value.IsMandatory)
1499+
catch (JsonSerializationException)
14191500
{
14201501
throw new ArgumentException(
1421-
string.Format(
1422-
CultureInfo.CurrentCulture, Resources.ConfigurationParameterValueRequired, configParameter.Key));
1423-
}
1502+
string.Format(
1503+
CultureInfo.CurrentCulture, Resources.ConfigurationParameterCannotBeSerializedToJson, configParameter.ToString()));
1504+
}
14241505
}
1425-
1426-
if (filteredParameters.Count != parameters.Count)
1427-
{
1428-
throw new ArgumentException(
1429-
string.Format(CultureInfo.CurrentCulture, Resources.InvalidConfigurationParameters));
1430-
}
1431-
14321506
return filteredParameters;
14331507
}
14341508

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public interface IAutomationClient
6060

6161
IEnumerable<NodeConfiguration> ListNodeConfigurations(string resourceGroupName, string automationAccountName, string rollupStatus);
6262

63-
NodeConfiguration CreateNodeConfiguration(string resourceGroupName, string automationAccountName, string sourcePath, IDictionary tags);
63+
NodeConfiguration CreateNodeConfiguration(string resourceGroupName, string automationAccountName, string sourcePath, string nodeConfiguraionName, bool overWrite);
6464
#endregion
6565

6666
#region Configurations

src/ResourceManager/Automation/Commands.Automation/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Automation/Commands.Automation/Properties/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,8 @@
414414
<value>This connection type has connections associated with it. If you delete this connection type, all connections associated with it will be unusable and should be removed, unless you create a new connection type with the same name that has the same field definitions as the deleted connection type. However, it can have additional fields as well. Are you sure you want to remove the Azure Automation {0} ?</value>
415415
<comment>Automation</comment>
416416
</data>
417+
<data name="NodeConfigurationNameInvalid" xml:space="preserve">
418+
<value>Invalid node configuration name. Please specify in the format &lt;node name&gt;.&lt;config name&gt;</value>
419+
<comment>Automation</comment>
420+
</data>
417421
</root>

0 commit comments

Comments
 (0)