@@ -257,6 +257,48 @@ private Model.DscConfiguration TryGetConfigurationModel(string resourceGroupName
257
257
return configuration ;
258
258
}
259
259
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
+
260
302
#endregion
261
303
262
304
#region DscMetaConfig Operations
@@ -1023,6 +1065,21 @@ public Model.CompilationJob StartCompilationJob(string resourceGroupName, string
1023
1065
#endregion
1024
1066
1025
1067
#region node configuration
1068
+ public Model . NodeConfiguration TryGetNodeConfiguration ( string resourceGroupName , string automationAccountName , string nodeConfigurationName , string rollupStatus )
1069
+ {
1070
+ using ( var request = new RequestSettings ( this . automationManagementClient ) )
1071
+ {
1072
+ try
1073
+ {
1074
+ return GetNodeConfiguration ( resourceGroupName , automationAccountName , nodeConfigurationName , rollupStatus ) ;
1075
+ }
1076
+ catch ( ResourceNotFoundException )
1077
+ {
1078
+ return null ;
1079
+ }
1080
+ }
1081
+ }
1082
+
1026
1083
public Model . NodeConfiguration GetNodeConfiguration ( string resourceGroupName , string automationAccountName , string nodeConfigurationName , string rollupStatus )
1027
1084
{
1028
1085
using ( var request = new RequestSettings ( this . automationManagementClient ) )
@@ -1118,6 +1175,90 @@ public Model.NodeConfiguration GetNodeConfiguration(string resourceGroupName, st
1118
1175
}
1119
1176
}
1120
1177
1178
+ public Model . NodeConfiguration CreateNodeConfiguration (
1179
+ string resourceGroupName ,
1180
+ string automationAccountName ,
1181
+ string sourcePath ,
1182
+ string configurationName ,
1183
+ bool overWrite )
1184
+ {
1185
+ using ( var request = new RequestSettings ( this . automationManagementClient ) )
1186
+ {
1187
+ Requires . Argument ( "ResourceGroupName" , resourceGroupName ) . NotNullOrEmpty ( ) ;
1188
+ Requires . Argument ( "AutomationAccountName" , automationAccountName ) . NotNullOrEmpty ( ) ;
1189
+ Requires . Argument ( "SourcePath" , sourcePath ) . NotNullOrEmpty ( ) ;
1190
+ Requires . Argument ( "configurationName" , configurationName ) . NotNullOrEmpty ( ) ;
1191
+
1192
+ string fileContent = null ;
1193
+ string nodeConfigurationName = null ;
1194
+ string nodeName = null ;
1195
+
1196
+ if ( File . Exists ( Path . GetFullPath ( sourcePath ) ) )
1197
+ {
1198
+ fileContent = System . IO . File . ReadAllText ( sourcePath ) ;
1199
+ nodeConfigurationName = configurationName + "." + System . IO . Path . GetFileNameWithoutExtension ( sourcePath ) ;
1200
+ }
1201
+ else
1202
+ {
1203
+ // file path not valid.
1204
+ throw new FileNotFoundException (
1205
+ string . Format (
1206
+ CultureInfo . CurrentCulture ,
1207
+ Resources . ConfigurationSourcePathInvalid ) ) ;
1208
+ }
1209
+
1210
+ // if node configuration already exists, ensure overwrite flag is specified
1211
+ var nodeConfigurationModel = this . TryGetNodeConfiguration (
1212
+ resourceGroupName ,
1213
+ automationAccountName ,
1214
+ nodeConfigurationName ,
1215
+ null ) ;
1216
+ if ( nodeConfigurationModel != null )
1217
+ {
1218
+ if ( ! overWrite )
1219
+ {
1220
+ throw new ResourceCommonException ( typeof ( Model . NodeConfiguration ) ,
1221
+ string . Format ( CultureInfo . CurrentCulture , Resources . NodeConfigurationAlreadyExists , nodeConfigurationName ) ) ;
1222
+ }
1223
+ }
1224
+
1225
+ // if configuration already exists, ensure overwrite flag is specified
1226
+ var configurationModel = this . TryGetConfigurationModel (
1227
+ resourceGroupName ,
1228
+ automationAccountName ,
1229
+ configurationName ) ;
1230
+ if ( configurationModel == null )
1231
+ {
1232
+ //create empty configuration if its empty
1233
+ this . CreateConfiguration ( resourceGroupName , automationAccountName , configurationName , nodeName ) ;
1234
+ }
1235
+
1236
+ var nodeConfigurationCreateParameters = new DscNodeConfigurationCreateOrUpdateParameters ( )
1237
+ {
1238
+ Name = nodeConfigurationName ,
1239
+ Source = new Microsoft . Azure . Management . Automation . Models . ContentSource ( )
1240
+ {
1241
+ // only embeddedContent supported for now
1242
+ ContentType = Model . ContentSourceType . embeddedContent . ToString ( ) ,
1243
+ Value = fileContent
1244
+ } ,
1245
+ Configuration = new DscConfigurationAssociationProperty ( )
1246
+ {
1247
+ Name = configurationName
1248
+ }
1249
+ } ;
1250
+
1251
+ var nodeConfiguration =
1252
+ this . automationManagementClient . NodeConfigurations . CreateOrUpdate (
1253
+ resourceGroupName ,
1254
+ automationAccountName ,
1255
+ nodeConfigurationCreateParameters ) . NodeConfiguration ;
1256
+
1257
+
1258
+ return new Model . NodeConfiguration ( resourceGroupName , automationAccountName , nodeConfiguration , null ) ;
1259
+ }
1260
+ }
1261
+
1121
1262
#endregion
1122
1263
1123
1264
#region dsc reports
@@ -1339,39 +1480,19 @@ private string FormatDateTime(DateTimeOffset dateTime)
1339
1480
private IDictionary < string , string > ProcessConfigurationParameters ( string resourceGroupName , string automationAccountName , string configurationName , IDictionary parameters )
1340
1481
{
1341
1482
parameters = parameters ?? new Dictionary < string , string > ( ) ;
1342
- IEnumerable < KeyValuePair < string , DscConfigurationParameter > > configurationParameters = this . ListConfigurationParameters ( resourceGroupName , automationAccountName , configurationName ) ;
1343
- var filteredParameters = new Dictionary < string , string > ( ) ;
1344
-
1345
- foreach ( var configParameter in configurationParameters )
1346
- {
1347
- if ( parameters . Contains ( configParameter . Key ) )
1483
+ var filteredParameters = new Dictionary < string , string > ( ) ;
1484
+ foreach ( var key in parameters . Keys )
1485
+ {
1486
+ try
1348
1487
{
1349
- object paramValue = parameters [ configParameter . Key ] ;
1350
- try
1351
- {
1352
- filteredParameters . Add ( configParameter . Key , paramValue . ToString ( ) ) ;
1353
- }
1354
- catch ( JsonSerializationException )
1355
- {
1356
- throw new ArgumentException (
1357
- string . Format (
1358
- CultureInfo . CurrentCulture , Resources . ConfigurationParameterCannotBeSerializedToJson , configParameter . Key ) ) ;
1359
- }
1488
+ filteredParameters . Add ( key . ToString ( ) , Newtonsoft . Json . JsonConvert . SerializeObject ( parameters [ key ] ) ) ;
1360
1489
}
1361
- else if ( configParameter . Value . IsMandatory )
1490
+ catch ( JsonSerializationException )
1362
1491
{
1363
- throw new ArgumentException (
1364
- string . Format (
1365
- CultureInfo . CurrentCulture , Resources . ConfigurationParameterValueRequired , configParameter . Key ) ) ;
1366
- }
1367
- }
1368
-
1369
- if ( filteredParameters . Count != parameters . Count )
1370
- {
1371
- throw new ArgumentException (
1372
- string . Format ( CultureInfo . CurrentCulture , Resources . InvalidConfigurationParameters ) ) ;
1492
+ throw new ArgumentException ( string . Format (
1493
+ CultureInfo . CurrentCulture , Resources . ConfigurationParameterCannotBeSerializedToJson , key . ToString ( ) ) ) ;
1494
+ }
1373
1495
}
1374
-
1375
1496
return filteredParameters ;
1376
1497
}
1377
1498
0 commit comments