Skip to content

Commit aee2f5a

Browse files
Remove-AAConnectionType cmdlet and Optional RunbookName
1 parent 395ab26 commit aee2f5a

File tree

13 files changed

+293
-4
lines changed

13 files changed

+293
-4
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public class ImportAzureAutomationRunbook : AzureAutomationBaseCmdlet
4343
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook description.")]
4444
public string Description { get; set; }
4545

46+
/// <summary>
47+
/// Gets or sets the runbook name
48+
/// </summary>
49+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")]
50+
[Alias("RunbookName")]
51+
public string Name { get; set; }
52+
4653
/// <summary>
4754
/// Gets or sets the runbook tags.
4855
/// </summary>
@@ -98,7 +105,8 @@ protected override void AutomationExecuteCmdlet()
98105
this.LogProgress,
99106
this.LogVerbose,
100107
this.Published.IsPresent,
101-
this.Force.IsPresent);
108+
this.Force.IsPresent,
109+
this.Name);
102110

103111
this.WriteObject(runbook);
104112
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Properties;
21+
22+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
23+
{
24+
/// <summary>
25+
/// Removes a ConnectionType for automation.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Remove, "AzureAutomationConnectionType", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)]
28+
public class RemoveAzureAutomationConnectionType : AzureAutomationBaseCmdlet
29+
{
30+
/// <summary>
31+
/// Gets or sets the connection name.
32+
/// </summary>
33+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The connectiontype name.")]
34+
[ValidateNotNullOrEmpty]
35+
public string Name { get; set; }
36+
37+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 3, HelpMessage = "Confirm the removal of the connectiontype")]
38+
public SwitchParameter Force { get; set; }
39+
40+
/// <summary>
41+
/// Execute this cmdlet.
42+
/// </summary>
43+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
44+
protected override void AutomationExecuteCmdlet()
45+
{
46+
var nextLink = string.Empty;
47+
var removeMessageWarning = Resources.RemovingAzureAutomationResourceWarning;
48+
49+
// check if any connections exists that use this connection type
50+
do
51+
{
52+
var ret = this.AutomationClient.ListConnections(this.ResourceGroupName, this.AutomationAccountName, ref nextLink);
53+
54+
if (ret.ToList().Any(connection => 0 ==
55+
string.Compare(connection.ConnectionTypeName, this.Name,
56+
StringComparison.CurrentCultureIgnoreCase)))
57+
{
58+
removeMessageWarning = Resources.RemoveConnectionTypeThatHasConnectionWarning;
59+
break;
60+
}
61+
62+
} while (!string.IsNullOrEmpty(nextLink));
63+
64+
65+
ConfirmAction(
66+
Force.IsPresent,
67+
string.Format(removeMessageWarning, "ConnectionType"),
68+
string.Format(Resources.RemoveAzureAutomationResourceDescription, "ConnectionType"),
69+
Name,
70+
() => this.AutomationClient.DeleteConnectionType(this.ResourceGroupName, this.AutomationAccountName, Name));
71+
}
72+
}
73+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<Compile Include="Cmdlet\RegisterAzureAutomationDscNode.cs" />
150150
<Compile Include="Cmdlet\RegisterAzureAutomationScheduledRunbook.cs" />
151151
<Compile Include="Cmdlet\RemoveAzureAutomationCertificate.cs" />
152+
<Compile Include="Cmdlet\RemoveAzureAutomationConnectionType.cs" />
152153
<Compile Include="Cmdlet\RemoveAzureAutomationConnection.cs" />
153154
<Compile Include="Cmdlet\RemoveAzureAutomationCredential.cs" />
154155
<Compile Include="Cmdlet\RemoveAzureAutomationModule.cs" />

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public Runbook CreateRunbookByName(string resourceGroupName, string automationAc
475475
}
476476
}
477477

478-
public Runbook ImportRunbook(string resourceGroupName, string automationAccountName, string runbookPath, string description, IDictionary tags, string type, bool? logProgress, bool? logVerbose, bool published, bool overwrite)
478+
public Runbook ImportRunbook(string resourceGroupName, string automationAccountName, string runbookPath, string description, IDictionary tags, string type, bool? logProgress, bool? logVerbose, bool published, bool overwrite, string name)
479479
{
480480

481481
var fileExtension = Path.GetExtension(runbookPath);
@@ -503,6 +503,20 @@ public Runbook ImportRunbook(string resourceGroupName, string automationAccountN
503503

504504
var runbookName = Path.GetFileNameWithoutExtension(runbookPath);
505505

506+
if (String.IsNullOrWhiteSpace(name) == false)
507+
{
508+
if (0 == string.Compare(type, Constants.RunbookType.PowerShellWorkflow, StringComparison.OrdinalIgnoreCase))
509+
{
510+
if (0 != string.Compare(runbookName, name, StringComparison.CurrentCultureIgnoreCase))
511+
{
512+
throw new ResourceCommonException(typeof(Runbook),
513+
string.Format(CultureInfo.CurrentCulture, Resources.FileNameRunbookNameMismatch));
514+
}
515+
}
516+
517+
runbookName = name;
518+
}
519+
506520
using (var request = new RequestSettings(this.automationManagementClient))
507521
{
508522
var runbook = this.CreateRunbookByName(resourceGroupName, automationAccountName, runbookName, description, tags, type, logProgress, logVerbose, overwrite);
@@ -1478,6 +1492,27 @@ public void UnregisterScheduledRunbook(string resourceGroupName, string automati
14781492

14791493
#endregion
14801494

1495+
#region ConnectionType
1496+
1497+
public void DeleteConnectionType(string resourceGroupName, string automationAccountName, string name)
1498+
{
1499+
try
1500+
{
1501+
this.automationManagementClient.ConnectionTypes.Delete(resourceGroupName, automationAccountName, name);
1502+
}
1503+
catch (CloudException cloudException)
1504+
{
1505+
if (cloudException.Response.StatusCode == HttpStatusCode.NoContent)
1506+
{
1507+
throw new ResourceNotFoundException(typeof(ConnectionType),
1508+
string.Format(CultureInfo.CurrentCulture, Resources.ConnectionTypeNotFound, name));
1509+
}
1510+
1511+
throw;
1512+
}
1513+
}
1514+
1515+
#endregion
14811516

14821517
#region Private Methods
14831518

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Model.Webhook CreateWebhook(
187187

188188
Runbook CreateRunbookByName(string resourceGroupName, string automationAccountName, string runbookName, string description, IDictionary tags, string type, bool? logProgress, bool? logVerbose, bool overwrite);
189189

190-
Runbook ImportRunbook(string resourceGroupName, string automationAccountName, string runbookPath, string description, IDictionary tags, string type, bool? logProgress, bool? logVerbose, bool published, bool overwrite);
190+
Runbook ImportRunbook(string resourceGroupName, string automationAccountName, string runbookPath, string description, IDictionary tags, string type, bool? logProgress, bool? logVerbose, bool published, bool overwrite, string runbookName);
191191

192192
void DeleteRunbook(string resourceGroupName, string automationAccountName, string runbookName);
193193

@@ -283,6 +283,12 @@ IEnumerable<JobStream> GetJobStream(string resourceGroupName, string automationA
283283
void UnregisterScheduledRunbook(string resourceGroupName, string automationAccountName, string runbookName, string scheduleName);
284284

285285

286+
#endregion
287+
288+
#region ConnectionType
289+
290+
void DeleteConnectionType(string resourceGroupName, string automationAccountName, string name);
291+
286292
#endregion
287293
}
288294
}

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,15 @@
403403
<value>Runbook file already exists. Specify the force switch switch overwrite. Runbook file name: {0}</value>
404404
<comment>Automation</comment>
405405
</data>
406+
<data name="FileNameRunbookNameMismatch" xml:space="preserve">
407+
<value>FileName and RunbookName should be same for PowerShellWorkflow runbook type.</value>
408+
<comment>Automation</comment>
409+
</data>
410+
<data name="ConnectionTypeNotFound" xml:space="preserve">
411+
<value>The connectiontype was not found. ConnectionType name: {0}.</value>
412+
</data>
413+
<data name="RemoveConnectionTypeThatHasConnectionWarning" xml:space="preserve">
414+
<value>This connectiontype has connections associated with it. If you delete this connectiontype, all connections associated with it will be unusable and should be removed, unless you create a new connectiontype with the same name that has the same field definitions as the deleted connectiontype. However, it can have additional fields as well. Are you sure you want to remove the Azure Automation {0} ?</value>
415+
<comment>Automation</comment>
416+
</data>
406417
</root>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Properties;
21+
22+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
23+
{
24+
/// <summary>
25+
/// Removes a ConnectionType for automation.
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Remove, "AzureAutomationConnectionType", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)]
28+
public class RemoveAzureAutomationConnectionType : AzureAutomationBaseCmdlet
29+
{
30+
/// <summary>
31+
/// Gets or sets the connection name.
32+
/// </summary>
33+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The connectiontype name.")]
34+
[ValidateNotNullOrEmpty]
35+
public string Name { get; set; }
36+
37+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByName, Position = 2, HelpMessage = "Confirm the removal of the connectiontype")]
38+
public SwitchParameter Force { get; set; }
39+
40+
/// <summary>
41+
/// Execute this cmdlet.
42+
/// </summary>
43+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
44+
protected override void AutomationExecuteCmdlet()
45+
{
46+
var nextLink = string.Empty;
47+
var removeMessageWarning = Resources.RemovingAzureAutomationResourceWarning;
48+
49+
// check if any connections exists that use this connection type
50+
do
51+
{
52+
var ret = this.AutomationClient.ListConnections(this.AutomationAccountName, ref nextLink);
53+
54+
if (ret.ToList().Any(connection => 0 ==
55+
string.Compare(connection.ConnectionTypeName, this.Name,
56+
StringComparison.CurrentCultureIgnoreCase)))
57+
{
58+
removeMessageWarning = Resources.RemoveConnectionTypeThatHasConnectionWarning;
59+
break;
60+
}
61+
62+
} while (!string.IsNullOrEmpty(nextLink));
63+
64+
65+
ConfirmAction(
66+
Force.IsPresent,
67+
string.Format(removeMessageWarning, "ConnectionType"),
68+
string.Format(Resources.RemoveAzureAutomationResourceDescription, "ConnectionType"),
69+
Name,
70+
() => this.AutomationClient.DeleteConnectionType(this.AutomationAccountName, Name));
71+
}
72+
}
73+
}

src/ServiceManagement/Automation/Commands.Automation/Commands.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
<Compile Include="Cmdlet\GetAzureAutomationConnection.cs" />
126126
<Compile Include="Cmdlet\NewAzureAutomationConnection.cs" />
127127
<Compile Include="Cmdlet\RemoveAzureAutomationConnection.cs" />
128+
<Compile Include="Cmdlet\RemoveAzureAutomationConnectionType.cs" />
128129
<Compile Include="Cmdlet\SetAzureAutomationConnection.cs" />
129130
<Compile Include="Cmdlet\SetAzureAutomationCertificate.cs" />
130131
<Compile Include="Cmdlet\NewAzureAutomationCertificate.cs" />

src/ServiceManagement/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,28 @@ public void UnregisterScheduledRunbook(string automationAccountName, string runb
14211421

14221422
#endregion
14231423

1424+
#region ConnectionType
1425+
1426+
public void DeleteConnectionType(string automationAccountName, string name)
1427+
{
1428+
try
1429+
{
1430+
this.automationManagementClient.ConnectionTypes.Delete(automationAccountName, name);
1431+
}
1432+
catch (CloudException cloudException)
1433+
{
1434+
if (cloudException.Response.StatusCode == HttpStatusCode.NoContent)
1435+
{
1436+
throw new ResourceNotFoundException(typeof(ConnectionType),
1437+
string.Format(CultureInfo.CurrentCulture, Resources.ConnectionTypeNotFound, name));
1438+
}
1439+
1440+
throw;
1441+
}
1442+
}
1443+
1444+
#endregion
1445+
14241446
#region Private Methods
14251447

14261448
private Schedule CreateScheduleFromScheduleModel(string automationAccountName, AutomationManagement.Models.Schedule schedule)

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ public interface IAutomationClient
186186
void UnregisterScheduledRunbook(string automationAccountName, string runbookName, string scheduleName);
187187

188188

189+
#endregion
190+
191+
#region ConnectionType
192+
193+
void DeleteConnectionType(string automationAccountName, string name);
194+
189195
#endregion
190196
}
191197
}

0 commit comments

Comments
 (0)