Skip to content

Commit d83d556

Browse files
Updated code to support 202 status
1 parent ca84434 commit d83d556

File tree

9 files changed

+664
-1162
lines changed

9 files changed

+664
-1162
lines changed

src/Websites/Websites.Test/ScenarioTests/CertificatesTests.ps1

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ Tests creating a new managed cert for app.
1919
function Test-NewAzWebAppCertificate
2020
{
2121
$rgname = "lketmtestantps10"
22-
$appname = "lketmtestantps10"
22+
$appname = "lketmtestantps10"
23+
$certName = "adorenowcert"
2324
$prodHostname = "www.adorenow.net"
2425
$thumbprint=""
2526

2627
try{
2728

28-
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -WebAppName $appname -HostName $prodHostname
29+
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -Name $certName -WebAppName $appname -HostName $prodHostname
2930
$thumbprint=$cert.ThumbPrint
3031

3132
# Assert
@@ -47,12 +48,13 @@ function Test-NewAzWebAppCertificateWithSSLBinding
4748
{
4849
$rgname = "lketmtestantps10"
4950
$appname = "lketmtestantps10"
50-
$prodHostname = "www.adorenow.net"
51+
$prodHostname = "www.adorenow.net"
52+
$certName = "adorenowcert"
5153
$thumbprint=""
5254

5355
try{
5456

55-
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -WebAppName $appname -HostName $prodHostname -AddBinding
57+
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -Name $certName -WebAppName $appname -HostName $prodHostname -AddBinding
5658
$thumbprint=$cert.ThumbPrint
5759

5860
# Assert
@@ -86,11 +88,12 @@ function Test-NewAzWebAppCertificateForSlot
8688
$appname = "lketmtestantps10"
8789
$slot = "testslot"
8890
$slotHostname = "testslot.adorenow.net"
91+
$certName = "adorenowcert"
8992
$thumbprint=""
9093

9194
try{
9295

93-
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -WebAppName $appname -HostName $slotHostname -Slot $slot
96+
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -Name $certName -WebAppName $appname -HostName $slotHostname -Slot $slot
9497
$thumbprint=$cert.ThumbPrint
9598

9699
# Assert
@@ -114,11 +117,12 @@ function Test-RemoveAzWebAppCertificate
114117
$rgname = "lketmtestantps10"
115118
$appname = "lketmtestantps10"
116119
$prodHostname = "www.adorenow.net"
120+
$certName = "adorenowcert"
117121
$thumbprint=""
118122

119123
try{
120124

121-
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -WebAppName $appname -HostName $prodHostname
125+
$cert=New-AzWebAppCertificate -ResourceGroupName $rgname -Name $certName -WebAppName $appname -HostName $prodHostname
122126
$thumbprint=$cert.ThumbPrint
123127

124128
# Assert

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.CertificatesTests/TestNewAzWebAppCertificate.json

Lines changed: 59 additions & 335 deletions
Large diffs are not rendered by default.

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.CertificatesTests/TestNewAzWebAppCertificateForSlot.json

Lines changed: 55 additions & 193 deletions
Large diffs are not rendered by default.

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.CertificatesTests/TestNewAzWebAppCertificateWithSSLBinding.json

Lines changed: 244 additions & 382 deletions
Large diffs are not rendered by default.

src/Websites/Websites.Test/SessionRecords/Microsoft.Azure.Commands.Websites.Test.ScenarioTests.CertificatesTests/TestRemoveAzWebAppCertificate.json

Lines changed: 212 additions & 206 deletions
Large diffs are not rendered by default.

src/Websites/Websites/Cmdlets/Certificates/NewAzureWebAppCertificate.cs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@
1919
using Microsoft.Azure.Commands.WebApps.Utilities;
2020
using Microsoft.Azure.Management.WebSites.Models;
2121
using Microsoft.Rest.Azure;
22+
using Microsoft.Rest.Serialization;
23+
using Newtonsoft.Json;
24+
using Newtonsoft.Json.Linq;
2225
using System;
26+
using System.Collections.Generic;
2327
using System.Linq;
2428
using System.Management.Automation;
2529
using System.Net;
30+
using System.Net.Http;
2631
using System.Text;
2732
using System.Threading;
2833
using System.Threading.Tasks;
@@ -36,6 +41,8 @@ namespace Microsoft.Azure.Commands.WebApps.Cmdlets.Certificates
3641
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "WebAppCertificate", SupportsShouldProcess = true), OutputType(typeof(PSCertificate))]
3742
public class NewAzureWebAppCertificate : WebAppBaseClientCmdLet
3843
{
44+
// Poll status for a maximum of 6 minutes (360 seconds / 2 seconds per status check)
45+
private const int NumStatusChecks = 72;
3946
const string CertNamePostFixSeparator = "_";
4047
const string ParameterSet1Name = "S1";
4148

@@ -49,12 +56,16 @@ public class NewAzureWebAppCertificate : WebAppBaseClientCmdLet
4956
[ValidateNotNullOrEmpty]
5057
public string WebAppName { get; set; }
5158

52-
[Parameter(ParameterSetName = ParameterSet1Name, Position = 2, Mandatory = false, HelpMessage = "The name of the web app slot.")]
59+
[Parameter(ParameterSetName = ParameterSet1Name, Position = 2, Mandatory = true, HelpMessage = "The name of the certificate")]
60+
[ValidateNotNullOrEmpty]
61+
public string Name { get; set; }
62+
63+
[Parameter(ParameterSetName = ParameterSet1Name, Position = 3, Mandatory = false, HelpMessage = "The name of the web app slot.")]
5364
[ResourceNameCompleter("Microsoft.Web/sites/slots", "ResourceGroupName", "WebAppName")]
5465
[ValidateNotNullOrEmpty]
5566
public string Slot { get; set; }
5667

57-
[Parameter(ParameterSetName = ParameterSet1Name, Position = 3, Mandatory = true, HelpMessage = "Custom hostnames associated with web app/slot.")]
68+
[Parameter(ParameterSetName = ParameterSet1Name, Position = 4, Mandatory = true, HelpMessage = "Custom hostnames associated with web app/slot.")]
5869
[ValidateNotNullOrEmpty]
5970
public string HostName { get; set; }
6071

@@ -70,11 +81,12 @@ public override void ExecuteCmdlet()
7081
{
7182
if (!string.IsNullOrWhiteSpace(ResourceGroupName) && !string.IsNullOrWhiteSpace(WebAppName))
7283
{
84+
string certName = null;
7385
HttpStatusCode statusCode = HttpStatusCode.OK;
7486
var webApp = new PSSite(WebsitesClient.GetWebApp(ResourceGroupName, WebAppName, Slot));
7587
var location = webApp.Location;
7688

77-
Certificate createdCertdetails = null;
89+
Certificate createdCertdetails = new Certificate();
7890

7991
var certificate = new Certificate(
8092
webApp.Location,
@@ -86,7 +98,9 @@ public override void ExecuteCmdlet()
8698
{
8799
try
88100
{
89-
createdCertdetails=WebsitesClient.CreateCertificate(ResourceGroupName, HostName, certificate);
101+
//Default certName is HostName
102+
certName = Name != null ? Name : HostName;
103+
createdCertdetails = (PSCertificate)WebsitesClient.CreateCertificate(ResourceGroupName, certName, certificate);
90104
}
91105
catch (DefaultErrorResponseException e)
92106
{
@@ -98,35 +112,35 @@ public override void ExecuteCmdlet()
98112
{
99113
throw;
100114
}
101-
}
102-
//Try to get the certificate post the create call return 'Accepted' status
103-
var watch = new System.Diagnostics.Stopwatch();
104-
if (statusCode == HttpStatusCode.Accepted)
105-
{
106-
watch.Start();
107-
for (; watch.Elapsed.Minutes < 6;)
115+
if (e.Response.StatusCode == HttpStatusCode.Accepted)
108116
{
109-
try
117+
var poll_url = e.Response.Headers["Location"].FirstOrDefault();
118+
var token=WebsitesClient.GetAccessToken(DefaultContext);
119+
HttpClient client = new HttpClient();
120+
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token.AccessToken);
121+
122+
HttpResponseMessage r;
123+
int numChecks = 0;
124+
do
110125
{
111-
createdCertdetails = WebsitesClient.GetCertificate(ResourceGroupName, HostName);
112-
break;
113-
}
114-
catch (DefaultErrorResponseException e)
126+
Thread.Sleep(TimeSpan.FromSeconds(5));
127+
r = client.GetAsync(poll_url).Result;
128+
numChecks++;
129+
} while (r.StatusCode == HttpStatusCode.Accepted && numChecks < NumStatusChecks);
130+
131+
if (r.StatusCode == HttpStatusCode.Accepted && numChecks >= NumStatusChecks)
115132
{
116-
//if 'NotFound' lets retry for every 5 seconds to get the certificate
117-
if (e.Response.StatusCode == HttpStatusCode.NotFound)
118-
Thread.Sleep(5000);
119-
else
120-
throw;
133+
var rec = new ErrorRecord(new Exception(string.Format($"The creation of the managed certificate '{this.HostName}' is taking longer than expected." +
134+
$" Please re-try the operation '{CreateInputCommand()}'")),
135+
string.Empty, ErrorCategory.OperationTimeout, null);
136+
WriteError(rec);
121137
}
122-
}
123-
watch.Stop();
124-
if (createdCertdetails == null)
125-
{
126-
throw new Exception(string.Format($"The creation of the managed certificate '{this.HostName}' is taking longer than expected." +
127-
$" Please re-try the operation '{CreateInputCommand()}'"));
138+
139+
128140
}
129141
}
142+
createdCertdetails = WebsitesClient.GetCertificate(ResourceGroupName, certName);
143+
130144
//Add only when user is opted for Binding
131145
if (AddBinding)
132146
{
@@ -144,7 +158,6 @@ public override void ExecuteCmdlet()
144158
}
145159

146160
}
147-
148161
private string CreateInputCommand()
149162
{
150163
StringBuilder command = new StringBuilder("New-AzWebAppCertificate ");

src/Websites/Websites/Cmdlets/Certificates/RemoveAzureWebAppCertificate.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ namespace Microsoft.Azure.Commands.WebApps.Cmdlets.Certificates
2929
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "WebAppCertificate", SupportsShouldProcess = true), OutputType(typeof(void))]
3030
public class RemoveAzureWebAppCertificate : WebAppBaseClientCmdLet
3131
{
32-
const string ParameterSet1Name = "S1";
33-
const string ParameterSet2Name = "S2";
32+
const string ParameterSet1Name = "S1";
3433

3534
[Parameter(ParameterSetName = ParameterSet1Name, Position = 0, Mandatory = true, HelpMessage = "The name of the resource group.")]
3635
//[Parameter(ParameterSetName = ParameterSet2Name, Position = 0, Mandatory = true, HelpMessage = "The name of the resource group.")]

src/Websites/Websites/Utilities/WebsitesClient.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,23 @@ public void SwapSlotWithPreviewResetSlotSwap(string resourceGroupName, string we
10031003
webSiteName,
10041004
sourceSlotName);
10051005
}
1006+
public IAccessToken GetAccessToken(IAzureContext context)
1007+
{
1008+
string tenant = null;
1009+
1010+
if (context.Subscription != null && context.Account != null)
1011+
{
1012+
tenant = context.Subscription.GetPropertyAsArray(AzureSubscription.Property.Tenants)
1013+
.Intersect(context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants))
1014+
.FirstOrDefault();
1015+
}
1016+
1017+
if (tenant == null && context.Tenant != null && new Guid(context.Tenant.Id) != Guid.Empty)
1018+
{
1019+
tenant = context.Tenant.Id.ToString();
1020+
}
1021+
return AzureSession.Instance.AuthenticationFactory.Authenticate(context.Account, context.Environment, tenant, null, ShowDialog.Never, null, context.Environment.GetTokenAudience(AzureEnvironment.Endpoint.ResourceManager));
1022+
}
10061023

10071024
private void WriteVerbose(string verboseFormat, params object[] args)
10081025
{

src/Websites/Websites/help/New-AzWebAppCertificate.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Creates an App service managed certificate for an Azure Web App.
1313
## SYNTAX
1414

1515
```
16-
New-AzWebAppCertificate [-ResourceGroupName] <String> [-WebAppName] <String> [[-Slot] <String>]
17-
[-HostName] <String> [-AddCertBinding] [[-SslState] <SslState>] [-DefaultProfile <IAzureContextContainer>]
16+
New-AzWebAppCertificate [-ResourceGroupName] <String> [-WebAppName] <String> [-Name] <String> [[-Slot] <String>]
17+
[-HostName] <String> [-AddBinding] [[-SslState] <SslState>] [-DefaultProfile <IAzureContextContainer>]
1818
[<CommonParameters>]
1919
```
2020

@@ -24,28 +24,28 @@ The **New-AzWebAppCertificate** cmdlet creates an Azure App Service Managed Cert
2424

2525
### Example 1
2626
```powershell
27-
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -HostName "www.ContosoSite.net"
27+
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -Name"ContosoCert" -HostName "www.ContosoSite.net"
2828
```
2929

3030
This command create an App Service Managed Certificate for the given WebApp
3131

3232
### Example 2
3333
```powershell
34-
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -HostName "www.ContosoSite.net" -Slot "test" -AddCertBinding
34+
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -Name"ContosoCert" -HostName "www.ContosoSite.net" -Slot "test" -AddCertBinding
3535
```
3636

3737
This command create an App Service Managed Certificate and binds to the given WebApp Slot.
3838

3939
### Example 3
4040
```powershell
41-
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -HostName "www.ContosoSite.net" -AddCertBinding
41+
PS C:\> New-AzWebAppCertificate -ResourceGroupName Default-Web-WestUS -WebAppName "ContosoSite" -Name"ContosoCert" -HostName "www.ContosoSite.net" -AddBinding
4242
```
4343

4444
This command create an App Service Managed Certificate and binds to the given WebApp.
4545

4646
## PARAMETERS
4747

48-
### -AddCertBinding
48+
### -AddBinding
4949
To add the created certificate to WebApp/slot.
5050

5151
```yaml
@@ -54,7 +54,7 @@ Parameter Sets: (All)
5454
Aliases:
5555

5656
Required: False
57-
Position: 4
57+
Position: 5
5858
Default value: None
5959
Accept pipeline input: False
6060
Accept wildcard characters: False
@@ -84,7 +84,22 @@ Parameter Sets: (All)
8484
Aliases:
8585

8686
Required: True
87-
Position: 3
87+
Position: 4
88+
Default value: None
89+
Accept pipeline input: False
90+
Accept wildcard characters: False
91+
```
92+
93+
### -Name
94+
The name of the certificate.
95+
96+
```yaml
97+
Type: String
98+
Parameter Sets: (All)
99+
Aliases:
100+
101+
Required: True
102+
Position: 2
88103
Default value: None
89104
Accept pipeline input: False
90105
Accept wildcard characters: False
@@ -114,7 +129,7 @@ Parameter Sets: (All)
114129
Aliases:
115130

116131
Required: False
117-
Position: 2
132+
Position: 3
118133
Default value: None
119134
Accept pipeline input: False
120135
Accept wildcard characters: False
@@ -132,7 +147,7 @@ Aliases:
132147
Accepted values: Disabled, SniEnabled, IpBasedEnabled
133148

134149
Required: False
135-
Position: 5
150+
Position: 6
136151
Default value: None
137152
Accept pipeline input: False
138153
Accept wildcard characters: False

0 commit comments

Comments
 (0)