Skip to content

Commit 67523d6

Browse files
Saving change before pull from Elvan dev
1 parent 06b8c89 commit 67523d6

File tree

5 files changed

+92
-80
lines changed

5 files changed

+92
-80
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="UnitTests\PublishAzureAutomationRunbookTest.cs" />
104104
<Compile Include="UnitTests\RemoveAzureAutomationAccountTest.cs" />
105105
<Compile Include="UnitTests\RegisterAzureAutomationScheduledRunbook.cs" />
106+
<Compile Include="UnitTests\RemoveAzureAutomationCertificateTest.cs" />
106107
<Compile Include="UnitTests\RemoveAzureAutomationRunbookTest.cs" />
107108
<Compile Include="UnitTests\SetAzureAutomationRunbookDefinitionTest.cs" />
108109
<Compile Include="UnitTests\SetAzureAutomationRunbookTest.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 Microsoft.Azure.Commands.Automation.Cmdlet;
17+
using Microsoft.Azure.Commands.Automation.Common;
18+
using Microsoft.VisualStudio.TestTools.UnitTesting;
19+
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
20+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
21+
using Moq;
22+
23+
namespace Microsoft.Azure.Commands.Automation.Test.UnitTests
24+
{
25+
[TestClass]
26+
public class RemoveAzureAutomationCertificateTest : TestBase
27+
{
28+
private Mock<IAutomationClient> mockAutomationClient;
29+
30+
private MockCommandRuntime mockCommandRuntime;
31+
32+
private RemoveAzureAutomationCertificate cmdlet;
33+
34+
[TestInitialize]
35+
public void SetupTest()
36+
{
37+
this.mockAutomationClient = new Mock<IAutomationClient>();
38+
this.mockCommandRuntime = new MockCommandRuntime();
39+
this.cmdlet = new RemoveAzureAutomationCertificate
40+
{
41+
AutomationClient = this.mockAutomationClient.Object,
42+
CommandRuntime = this.mockCommandRuntime
43+
};
44+
}
45+
46+
[TestMethod]
47+
public void RemoveAzureAutomationCertificateByNameSuccessfull()
48+
{
49+
// Setup
50+
string accountName = "automation";
51+
string certifiateName = "cert";
52+
53+
this.mockAutomationClient.Setup(f => f.DeleteCertificate(accountName, certifiateName));
54+
55+
// Test
56+
this.cmdlet.AutomationAccountName = accountName;
57+
this.cmdlet.Name = certifiateName;
58+
this.cmdlet.Force = true;
59+
this.cmdlet.ExecuteCmdlet();
60+
61+
// Assert
62+
this.mockAutomationClient.Verify(f => f.DeleteCertificate(accountName, certifiateName), Times.Once());
63+
}
64+
}
65+
}

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationAccount.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ public override void ExecuteCmdlet()
7373
{
7474
this.ConfirmAction(
7575
this.Force.IsPresent,
76-
string.Format(CultureInfo.CurrentCulture, Resources.RemovingAzureAutomationResourceWarning),
77-
string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationResourceDescription),
76+
string.Format(CultureInfo.CurrentCulture, Resources.RemovingAzureAutomationResourceWarning, this.Name),
77+
string.Format(CultureInfo.CurrentCulture, Resources.RemoveAzureAutomationResourceDescription, this.Name),
7878
this.Name,
79-
() => AutomationClient.DeleteAutomationAccount(this.Name));
79+
() =>
80+
{
81+
AutomationClient.DeleteAutomationAccount(this.Name);
82+
});
8083
}
8184
}
8285
}

src/ServiceManagement/Automation/Commands.Automation/Cmdlet/RemoveAzureAutomationCredential.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected override void AutomationExecuteCmdlet()
4646
{
4747
ConfirmAction(
4848
Force.IsPresent,
49-
string.Format(Resources.RemovingAzureAutomationResourceWarning, "Credential"),
49+
string.Format(Resources.RemovingAzureAutomationResourceWarning, "Credential"),
5050
string.Format(Resources.RemoveAzureAutomationResourceDescription, "Credential"),
5151
Name,
5252
() =>

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

Lines changed: 19 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
using System.IO;
2121
using System.Net;
2222
using System.Security;
23-
using System.Security.Cryptography;
2423
using System.Security.Cryptography.X509Certificates;
25-
using System.Text;
2624
using Microsoft.Azure.Commands.Automation.Model;
2725
using Microsoft.Azure.Commands.Automation.Properties;
2826
using Microsoft.Azure.Management.Automation;
@@ -131,7 +129,7 @@ public IEnumerable<Schedule> ListSchedules(string automationAccountName)
131129
skipToken =>
132130
{
133131
var response = this.automationManagementClient.Schedules.List(
134-
automationAccountName, skipToken);
132+
automationAccountName);
135133

136134
return new ResponseWithSkipToken<AutomationManagement.Models.Schedule>(
137135
response, response.Schedules);
@@ -170,7 +168,7 @@ public IEnumerable<Runbook> ListRunbooks(string automationAccountName)
170168
skipToken =>
171169
{
172170
var response = this.automationManagementClient.Runbooks.List(
173-
automationAccountName, skipToken);
171+
automationAccountName);
174172
return new ResponseWithSkipToken<AutomationManagement.Models.Runbook>(
175173
response, response.Runbooks);
176174
}).Select(c => new Runbook(automationAccountName, c));
@@ -195,7 +193,7 @@ public Runbook CreateRunbookByName(string automationAccountName, string runbookN
195193

196194
var rdcparam = new RunbookCreateDraftParameters() { Name = runbookName, Properties = rdcprop, Tags = tags };
197195

198-
this.automationManagementClient.Runbooks.CreateWithDraftParameters(automationAccountName, rdcparam);
196+
this.automationManagementClient.Runbooks.CreateWithDraft(automationAccountName, rdcparam);
199197

200198
return this.GetRunbook(automationAccountName, runbookName);
201199
}
@@ -1007,37 +1005,21 @@ public void DeleteAutomationAccount(string automationAccountName)
10071005
{
10081006
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
10091007

1010-
string location = string.Empty;
1008+
var csName = string.Empty;
10111009

1012-
var cloudServices = new List<CloudService>(this.automationManagementClient.CloudServices.List().CloudServices);
1010+
var cloudServices = this.automationManagementClient.CloudServices.List().CloudServices;
10131011

10141012
foreach (var cloudService in cloudServices)
10151013
{
10161014
if (cloudService.Resources.Any(resource => 0 == String.Compare(resource.Name, automationAccountName, CultureInfo.InvariantCulture,
10171015
CompareOptions.IgnoreCase)))
10181016
{
1019-
location = cloudService.GeoRegion;
1017+
csName = cloudService.Name;
10201018
break;
10211019
}
10221020
}
10231021

1024-
try
1025-
{
1026-
this.automationManagementClient.DeleteAutomationAccount(automationAccountName,location);
1027-
}
1028-
catch (CloudException e)
1029-
{
1030-
if (e.Response.StatusCode == HttpStatusCode.NotFound)
1031-
{
1032-
// Try with SHA encoded cloud Service name
1033-
var generatedCsName = GetCloudServiceName(automationAccountName, location);
1034-
this.automationManagementClient.AutomationAccounts.Delete(generatedCsName, automationAccountName);
1035-
}
1036-
else
1037-
{
1038-
throw;
1039-
}
1040-
}
1022+
this.automationManagementClient.AutomationAccounts.Delete(csName, automationAccountName);
10411023
}
10421024

10431025
#endregion
@@ -1077,17 +1059,18 @@ public Certificate CreateCertificate(string automationAccountName, string name,
10771059
public Certificate UpdateCertificate(string automationAccountName, string name, string path, SecureString password,
10781060
string description, bool exportable)
10791061
{
1080-
var cert = (password == null)
1081-
? new X509Certificate2(path)
1082-
: new X509Certificate2(path, password);
1083-
1084-
var cuprop = new CertificateUpdateProperties()
1062+
var cuprop = new CertificateUpdateProperties();
1063+
1064+
if (description != null) cuprop.Description = description;
1065+
1066+
if (path != null)
10851067
{
1086-
Description = description,
1087-
Base64Value = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12)),
1088-
Thumbprint = cert.Thumbprint,
1089-
IsExportable = exportable
1090-
};
1068+
var cert = (password == null) ? new X509Certificate2(path) : new X509Certificate2(path, password);
1069+
cuprop.Base64Value = Convert.ToBase64String(cert.Export(X509ContentType.Pkcs12));
1070+
cuprop.Thumbprint = cert.Thumbprint;
1071+
}
1072+
1073+
if (exportable) cuprop.IsExportable = true;
10911074

10921075
var cuparam = new CertificateUpdateParameters() { Name = name, Properties = cuprop };
10931076

@@ -1187,7 +1170,7 @@ public IEnumerable<JobSchedule> ListJobSchedules(string automationAccountName)
11871170
skipToken =>
11881171
{
11891172
var response = this.automationManagementClient.JobSchedules.List(
1190-
automationAccountName, skipToken);
1173+
automationAccountName);
11911174

11921175
return new ResponseWithSkipToken<AutomationManagement.Models.JobSchedule>(
11931176
response, response.JobSchedules);
@@ -1394,46 +1377,6 @@ private IDictionary<string, string> ProcessRunbookParameters(string automationAc
13941377
return filteredParameters;
13951378
}
13961379

1397-
private string GetCloudServiceName(string subscriptionId, string region)
1398-
{
1399-
string hashedSubId = string.Empty;
1400-
using (SHA256 sha256 = SHA256Managed.Create())
1401-
{
1402-
hashedSubId = Base32NoPaddingEncode(sha256.ComputeHash(UTF8Encoding.UTF8.GetBytes(subscriptionId)));
1403-
}
1404-
1405-
return string.Format(CultureInfo.InvariantCulture, "{0}{1}-{2}", Constants.AutomationServicePrefix, hashedSubId, region.Replace(' ', '-'));
1406-
}
1407-
1408-
private string Base32NoPaddingEncode(byte[] data)
1409-
{
1410-
const string base32StandardAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
1411-
1412-
StringBuilder result = new StringBuilder(Math.Max((int)Math.Ceiling(data.Length * 8 / 5.0), 1));
1413-
1414-
byte[] emptyBuffer = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
1415-
byte[] workingBuffer = new byte[8];
1416-
1417-
// Process input 5 bytes at a time
1418-
for (int i = 0; i < data.Length; i += 5)
1419-
{
1420-
int bytes = Math.Min(data.Length - i, 5);
1421-
Array.Copy(emptyBuffer, workingBuffer, emptyBuffer.Length);
1422-
Array.Copy(data, i, workingBuffer, workingBuffer.Length - (bytes + 1), bytes);
1423-
Array.Reverse(workingBuffer);
1424-
ulong val = BitConverter.ToUInt64(workingBuffer, 0);
1425-
1426-
for (int bitOffset = ((bytes + 1) * 8) - 5; bitOffset > 3; bitOffset -= 5)
1427-
{
1428-
result.Append(base32StandardAlphabet[(int)((val >> bitOffset) & 0x1f)]);
1429-
}
1430-
}
1431-
1432-
return result.ToString();
1433-
}
1434-
1435-
1436-
14371380
private JobStream CreateJobStreamFromJobStreamModel(AutomationManagement.Models.JobStream jobStream, string automationAccountName, Guid jobId)
14381381
{
14391382
Requires.Argument("jobStream", jobStream).NotNull();

0 commit comments

Comments
 (0)