Skip to content

Commit 363e815

Browse files
Merge pull request #15 from balukambala/dev
Added DSC delete cmdlets
2 parents 6aba7e4 + 56bdd81 commit 363e815

File tree

7 files changed

+215
-2
lines changed

7 files changed

+215
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 Microsoft.Azure.Commands.Automation.Properties;
16+
using System.Management.Automation;
17+
using System.Security.Permissions;
18+
19+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
20+
{
21+
/// <summary>
22+
/// Remove a DSC configuration
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Remove, "AzureRmAutomationDscConfiguration")]
25+
public class RemoveAzureAutomationDscConfiguration : AzureAutomationBaseCmdlet
26+
{
27+
/// <summary>
28+
/// Gets or sets the Configuration name.
29+
/// </summary>
30+
[Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true,
31+
HelpMessage = "The configuration name.")]
32+
[Alias("ConfigurationName")]
33+
[ValidateNotNullOrEmpty]
34+
public string Name { get; set; }
35+
36+
[Parameter(Position = 3, HelpMessage = "Force confirmation of the removal of the configuration")]
37+
public SwitchParameter Force { get; set; }
38+
39+
/// <summary>
40+
/// Execute this cmdlet.
41+
/// </summary>
42+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
43+
protected override void AutomationProcessRecord()
44+
{
45+
ConfirmAction(
46+
Force.IsPresent,
47+
string.Format(Resources.RemovingAzureAutomationDscConfigurationWarning, "DSC Configuration"),
48+
string.Format(Resources.RemoveAzureAutomationResourceDescription, "DSC Configuration"),
49+
Name,
50+
() => this.AutomationClient.DeleteConfiguration(
51+
this.ResourceGroupName,
52+
this.AutomationAccountName,
53+
this.Name));
54+
}
55+
}
56+
}
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 Microsoft.Azure.Commands.Automation.Properties;
16+
using System.Management.Automation;
17+
using System.Security.Permissions;
18+
19+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
20+
{
21+
/// <summary>
22+
/// Remove a DSC configuration
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Remove, "AzureRmAutomationDscNodeConfiguration")]
25+
public class RemoveAzureAutomationDscNodeConfiguration : AzureAutomationBaseCmdlet
26+
{
27+
/// <summary>
28+
/// Gets or sets the Configuration name.
29+
/// </summary>
30+
[Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true,
31+
HelpMessage = "The node configuration name.")]
32+
[Alias("NodeConfigurationName")]
33+
[ValidateNotNullOrEmpty]
34+
public string Name { get; set; }
35+
36+
[Parameter(Position = 3, HelpMessage = "Force confirmation of the removal of the node configuration")]
37+
public SwitchParameter Force { get; set; }
38+
39+
[Parameter(Position = 4, HelpMessage = "Remove the node configuration even if the node configuration is mapped to one or more nodes")]
40+
public SwitchParameter IgnoreNodeMappings { get; set; }
41+
42+
/// <summary>
43+
/// Execute this cmdlet.
44+
/// </summary>
45+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
46+
protected override void AutomationProcessRecord()
47+
{
48+
ConfirmAction(
49+
Force.IsPresent,
50+
string.Format(Resources.RemovingAzureAutomationResourceWarning, "DSC node configuration"),
51+
string.Format(Resources.RemoveAzureAutomationResourceDescription, "DSC node configuration"),
52+
Name,
53+
() => this.AutomationClient.DeleteNodeConfiguration(
54+
this.ResourceGroupName,
55+
this.AutomationAccountName,
56+
this.Name,
57+
IgnoreNodeMappings.IsPresent));
58+
}
59+
}
60+
}

src/ResourceManager/Automation/Commands.Automation/Commands.Automation.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@
164164
<Compile Include="Cmdlet\RemoveAzureAutomationConnectionType.cs" />
165165
<Compile Include="Cmdlet\RemoveAzureAutomationConnection.cs" />
166166
<Compile Include="Cmdlet\RemoveAzureAutomationCredential.cs" />
167+
<Compile Include="Cmdlet\RemoveAzureAutomationDscNodeConfiguration.cs" />
167168
<Compile Include="Cmdlet\RemoveAzureAutomationModule.cs" />
168169
<Compile Include="Cmdlet\RemoveAzureAutomationRunbook.cs" />
169170
<Compile Include="Cmdlet\RemoveAzureAutomationSchedule.cs" />
170171
<Compile Include="Cmdlet\RemoveAzureAutomationVariable.cs" />
172+
<Compile Include="Cmdlet\RemoveAzureAutomationDscConfiguration.cs" />
171173
<Compile Include="Cmdlet\RemoveAzureAutomationWebhook.cs" />
172174
<Compile Include="Cmdlet\ResumeAzureAutomationJob.cs" />
173175
<Compile Include="Cmdlet\SetAzureAutomationCertificate.cs" />

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,29 @@ public Model.DscConfiguration CreateConfiguration(
300300
}
301301
}
302302

303+
public void DeleteConfiguration(string resourceGroupName, string automationAccountName, string name)
304+
{
305+
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
306+
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
307+
using (var request = new RequestSettings(this.automationManagementClient))
308+
{
309+
try
310+
{
311+
this.automationManagementClient.Configurations.Delete(resourceGroupName, automationAccountName, name);
312+
}
313+
catch (CloudException cloudException)
314+
{
315+
if (cloudException.Response.StatusCode == HttpStatusCode.NoContent)
316+
{
317+
throw new ResourceNotFoundException(
318+
typeof(Model.DscConfiguration),
319+
string.Format(CultureInfo.CurrentCulture, Resources.ConfigurationNotFound, name));
320+
}
321+
throw;
322+
}
323+
}
324+
}
325+
303326
#endregion
304327

305328
#region DscMetaConfig Operations
@@ -568,7 +591,7 @@ public Model.DscNode GetDscNodeById(
568591

569592
IEnumerable<AutomationManagement.Models.DscNode> dscNodes;
570593

571-
if (!String.IsNullOrEmpty(status))
594+
if (!string.IsNullOrEmpty(status))
572595
{
573596
dscNodes = AutomationManagementClient.ContinuationTokenHandler(
574597
skipToken =>
@@ -1262,6 +1285,48 @@ public Model.NodeConfiguration CreateNodeConfiguration(
12621285
}
12631286
}
12641287

1288+
public void DeleteNodeConfiguration(string resourceGroupName, string automationAccountName, string name, bool ignoreNodeMappings)
1289+
{
1290+
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
1291+
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
1292+
Requires.Argument("NodeConfigurationName", name).NotNull();
1293+
1294+
using (var request = new RequestSettings(this.automationManagementClient))
1295+
{
1296+
try
1297+
{
1298+
if (ignoreNodeMappings)
1299+
{
1300+
this.automationManagementClient.NodeConfigurations.Delete(resourceGroupName, automationAccountName, name);
1301+
}
1302+
else
1303+
{
1304+
var nodeList = this.ListDscNodesByNodeConfiguration(resourceGroupName, automationAccountName, name, null);
1305+
if (nodeList.Any())
1306+
{
1307+
throw new ResourceCommonException(
1308+
typeof (Model.NodeConfiguration),
1309+
string.Format(CultureInfo.CurrentCulture, Resources.CannotDeleteNodeConfiguration, name));
1310+
}
1311+
else
1312+
{
1313+
this.automationManagementClient.NodeConfigurations.Delete(resourceGroupName, automationAccountName, name);
1314+
}
1315+
}
1316+
}
1317+
catch (CloudException cloudException)
1318+
{
1319+
if (cloudException.Response.StatusCode == HttpStatusCode.NotFound)
1320+
{
1321+
throw new ResourceNotFoundException(
1322+
typeof(Model.NodeConfiguration),
1323+
string.Format(CultureInfo.CurrentCulture, Resources.NodeConfigurationNotFound, name));
1324+
}
1325+
throw;
1326+
}
1327+
}
1328+
}
1329+
12651330
#endregion
12661331

12671332
#region dsc reports

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public interface IAutomationClient
6262
IEnumerable<NodeConfiguration> ListNodeConfigurations(string resourceGroupName, string automationAccountName, string rollupStatus);
6363

6464
NodeConfiguration CreateNodeConfiguration(string resourceGroupName, string automationAccountName, string sourcePath, string nodeConfiguraionName, bool overWrite);
65+
66+
void DeleteNodeConfiguration(string resourceGroupName, string automationAccountName, string name, bool ignoreNodeMappings);
6567
#endregion
6668

6769
#region Configurations
@@ -74,6 +76,8 @@ public interface IAutomationClient
7476

7577
DirectoryInfo GetConfigurationContent(string resourceGroupName, string automationAccountName, string configurationName, bool? isDraft, string outputFolder, bool overwriteExistingFile);
7678

79+
void DeleteConfiguration(string resourceGroupName, string automationAccountName, string name);
80+
7781
#endregion
7882

7983
#region AgentRegistrationInforamtion

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

Lines changed: 19 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@
173173
<value>Are you sure you want to remove the Azure Automation {0} ?</value>
174174
<comment>Automation</comment>
175175
</data>
176+
<data name="RemovingAzureAutomationDscConfigurationWarning" xml:space="preserve">
177+
<value>Are you sure you want to remove the Azure Automation {0}? Note: Any DSC node configurations under this DSC configuration will not be removed.</value>
178+
<comment>Automation</comment>
179+
</data>
176180
<data name="ResourceExists" xml:space="preserve">
177181
<value>Resource exist.</value>
178182
<comment>Automation</comment>
@@ -384,6 +388,10 @@
384388
<value>The Dsc Configuration was not found. Configuration name {0}.</value>
385389
<comment>Automation</comment>
386390
</data>
391+
<data name="CannotDeleteNodeConfiguration" xml:space="preserve">
392+
<value>The node configuration '{0}' is currently assigned to one or more nodes. Either specify the IgnoreNodeMappings parameter, or reassign these nodes to a different node configuration, to delete this node configuration.</value>
393+
<comment>Automation</comment>
394+
</data>
387395
<data name="WebhookNotFound" xml:space="preserve">
388396
<value>The Webhook with Name: {0} was not found.</value>
389397
<comment>Automation</comment>

0 commit comments

Comments
 (0)