Skip to content

Commit c168df5

Browse files
committed
Import configuration cmdlet implemented
1 parent e18cf79 commit c168df5

File tree

5 files changed

+160
-14
lines changed

5 files changed

+160
-14
lines changed

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections;
1517
using System.Collections.Generic;
1618
using System.Management.Automation;
1719
using System.Security.Permissions;
@@ -38,46 +40,82 @@ public class ImportAzureAutomationDscConfiguration : AzureAutomationBaseCmdlet
3840
/// <summary>
3941
/// Gets or sets the configuration name.
4042
/// </summary>
41-
[Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The configuration name.")]
43+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The configuration name.")]
4244
public string ConfigurationName { get; set; }
4345

4446
/// <summary>
4547
/// Gets or sets the source path.
4648
/// </summary>
47-
[Parameter(Position = 3, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The source path for importing the configuration script.")]
49+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The source path for importing the configuration script.")]
4850
public string SourcePath { get; set; }
4951

52+
/// <summary>
53+
/// Gets or sets the configuration tags.
54+
/// </summary>
55+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The dsc configuration tags.")]
56+
[Alias("Tag")]
57+
public IDictionary Tags { get; set; }
58+
5059
/// <summary>
5160
/// Gets or sets the description.
5261
/// </summary>
53-
[Parameter(Position = 4, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the configuration being imported.")]
62+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The description of the configuration being imported.")]
5463
public string Description { get; set; }
5564

5665
/// <summary>
5766
/// Gets or sets the switch parameter to
5867
/// </summary>
59-
[Parameter(Position = 5, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Import the configuration in published state.")]
68+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Import the configuration in published state.")]
6069
public SwitchParameter Published { get; set; }
6170

6271
/// <summary>
6372
/// Gets or sets switch parameter to confirm overwriting of existing configurations.
6473
/// </summary>
65-
[Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Overwrites an existing configuration with same name.")]
74+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Overwrites an existing configuration with same name.")]
6675
public SwitchParameter Overwrite { get; set; }
6776

6877
/// <summary>
6978
/// Gets or sets a value indicating whether verbose logging should be turned on or off.
7079
/// </summary>
71-
[Parameter(Position = 7, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicate whether verbose logging should be turned on or off.")]
72-
public bool? LogVerbose { get; set; }
80+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicate whether verbose logging should be turned on or off.")]
81+
public SwitchParameter LogVerbose { get; set; }
82+
83+
/// <summary>
84+
/// Gets or sets a value indicating whether log progress should be turned on or off.
85+
/// </summary>
86+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Indicate whether progress logging should be turned on or off.")]
87+
public SwitchParameter LogProgress { get; set; }
7388

7489
/// <summary>
7590
/// Execute this cmdlet.
7691
/// </summary>
7792
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
7893
public override void ExecuteCmdlet()
7994
{
80-
var configuration = this.AutomationClient.CreateConfiguration(this.ResourceGroupName, this.AutomationAccountName, this.ConfigurationName, this.SourcePath, this.Description, this.LogVerbose);
95+
bool logVerbose = false;
96+
if (this.LogVerbose.IsPresent) logVerbose = true;
97+
98+
bool logProgress = false;
99+
if (this.LogProgress.IsPresent) logProgress = true;
100+
101+
bool published = false;
102+
if (this.Published.IsPresent) published = true;
103+
104+
bool overWrite = false;
105+
if (this.Overwrite.IsPresent) overWrite = true;
106+
107+
var configuration = this.AutomationClient.CreateConfiguration(
108+
this.ResourceGroupName,
109+
this.AutomationAccountName,
110+
this.ConfigurationName,
111+
this.SourcePath,
112+
this.Tags,
113+
this.Description,
114+
logVerbose,
115+
logProgress,
116+
published,
117+
overWrite);
118+
81119
this.WriteObject(configuration);
82120
}
83121
}

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

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
namespace Microsoft.Azure.Commands.Automation.Common
3636
{
37+
using Microsoft.Azure.Management.Resources.Models;
38+
3739
using AutomationManagement = Azure.Management.Automation;
3840
using Microsoft.Azure.Common.Authentication;
3941
using Hyak.Common;
@@ -86,33 +88,76 @@ public Model.DscConfiguration CreateConfiguration(
8688
string automationAccountName,
8789
string configurationName,
8890
string sourcePath,
91+
IDictionary tags,
8992
string description,
90-
bool? logVerbose)
93+
bool logVerbose,
94+
bool logProgress,
95+
bool published,
96+
bool overWrite)
9197
{
9298
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
9399
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
94100
Requires.Argument("ConfigurationName", configurationName).NotNull();
95101
Requires.Argument("SourcePath", sourcePath).NotNull();
96102

103+
// for the private preivew, configuration can be imported in Published mode only
104+
// Draft mode is not implemented
105+
if (!published)
106+
{
107+
throw new NotImplementedException(
108+
string.Format(
109+
CultureInfo.CurrentCulture,
110+
Resources.ConfigurationNotPublished));
111+
}
112+
113+
// if configuration already exists, ensure overwrite flag is specified
114+
if (this.TryGetConfigurationModel(resourceGroupName, automationAccountName, configurationName) != null)
115+
{
116+
if (!overWrite)
117+
{
118+
throw new ResourceCommonException(typeof(Model.DscConfiguration),
119+
string.Format(CultureInfo.CurrentCulture, Resources.ConfigurationAlreadyExists, configurationName));
120+
}
121+
}
122+
97123
string fileContent = null;
98124

99-
if (File.Exists(Path.GetFullPath(sourcePath)))
125+
try
100126
{
101-
fileContent = System.IO.File.ReadAllText(sourcePath);
127+
if (File.Exists(Path.GetFullPath(sourcePath)))
128+
{
129+
fileContent = System.IO.File.ReadAllText(sourcePath);
130+
}
102131
}
132+
catch (Exception)
133+
{
134+
// exception in accessing the file path
135+
throw new FileNotFoundException(
136+
string.Format(
137+
CultureInfo.CurrentCulture,
138+
Resources.ConfigurationSourcePathInvalid));
139+
}
140+
141+
// location of the configuration is set to same as that of automation account
103142
string location = this.GetAutomationAccount(resourceGroupName, automationAccountName).Location;
143+
144+
IDictionary<string, string> configurationTags = null;
145+
if (tags != null) configurationTags = tags.Cast<DictionaryEntry>().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());
146+
104147
var configurationCreateParameters = new DscConfigurationCreateOrUpdateParameters()
105148
{
106149
Name = configurationName,
107150
Location = location,
151+
Tags = configurationTags,
108152
Properties = new DscConfigurationCreateOrUpdateProperties()
109153
{
110154
Description = String.IsNullOrEmpty(description) ? String.Empty : description,
111-
LogVerbose = logVerbose.GetValueOrDefault(),
155+
LogVerbose = logVerbose,
156+
LogProgress = logProgress,
112157
Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
113158
{
114159
// only embeddedContent supported for now
115-
ContentType = "embeddedContent",
160+
ContentType = Model.ContentSourceType.embeddedContent.ToString(),
116161
Value = fileContent
117162
}
118163
}
@@ -127,6 +172,30 @@ public Model.DscConfiguration CreateConfiguration(
127172
return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
128173
}
129174

175+
private Model.DscConfiguration TryGetConfigurationModel(string resourceGroupName, string automationAccountName, string configurationName)
176+
{
177+
Model.DscConfiguration configuration = null;
178+
try
179+
{
180+
configuration = this.GetConfiguration(
181+
resourceGroupName,
182+
automationAccountName,
183+
configurationName);
184+
}
185+
catch (CloudException e)
186+
{
187+
if (e.Response.StatusCode == HttpStatusCode.NotFound)
188+
{
189+
configuration = null;
190+
}
191+
else
192+
{
193+
throw;
194+
}
195+
}
196+
return configuration;
197+
}
198+
130199
#endregion
131200

132201
#region AgentRegistration Operations

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

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

4646
DscConfiguration GetConfiguration(string resourceGroupName, string automationAccountName, string configurationName);
4747

48-
DscConfiguration CreateConfiguration(string resourceGroupName, string automationAccountName, string configurationName, string sourcePath, string description, bool? logVerbose);
48+
DscConfiguration CreateConfiguration(string resourceGroupName, string automationAccountName, string configurationName, string sourcePath, IDictionary tags, string description, bool logVerbose, bool logProgress, bool published, bool overWrite);
4949

5050
#endregion
5151

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

Lines changed: 27 additions & 0 deletions
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,16 @@
275275
<value>Create account arguments are invalid. Provide valid account name and location. Account Name: {0}, Location: {1} </value>
276276
<comment>Automation</comment>
277277
</data>
278+
<data name="ConfigurationAlreadyExists" xml:space="preserve">
279+
<value>Configuration already exists. Specify the parameter to force an overwrite. Configuration name: {0}</value>
280+
<comment>Automation</comment>
281+
</data>
282+
<data name="ConfigurationNotPublished" xml:space="preserve">
283+
<value>Configuration can be imported in published state only in the current preview. Use the -Published switch.</value>
284+
<comment>Automation</comment>
285+
</data>
286+
<data name="ConfigurationSourcePathInvalid" xml:space="preserve">
287+
<value>Invalid SourcePath. Verify file path is valid and file exists.</value>
288+
<comment>Automation</comment>
289+
</data>
278290
</root>

0 commit comments

Comments
 (0)