Skip to content

Commit 4fe2eff

Browse files
authored
(Azure CXP) sample fix using Set-AzApiManagement (Azure#386)
resolves MicrosoftDocs/azure-docs#92378
1 parent 1dd0ec0 commit 4fe2eff

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

api-management/setup-custom-domain/setup_custom_domain.ps1

+25-42
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,35 @@
33
# of api management service.
44
###########################################################
55

6-
$random = (New-Guid).ToString().Substring(0,8)
7-
8-
#Azure specific details
9-
$subscriptionId = "my-azure-subscription-id"
106

117
# Api Management service specific details
12-
$apimServiceName = "apim-$random"
13-
$resourceGroupName = "apim-rg-$random"
14-
$location = "Japan East"
15-
$organisation = "Contoso"
16-
$adminEmail = "admin@contoso.com"
17-
18-
# Set the context to the subscription Id where the cluster will be created
19-
Select-AzSubscription -SubscriptionId $subscriptionId
20-
21-
# Create a resource group.
22-
New-AzResourceGroup -Name $resourceGroupName -Location $location
23-
24-
# Create the Api Management service. Since the SKU is not specified, it creates a service with Developer SKU.
25-
New-AzApiManagement -ResourceGroupName $resourceGroupName -Name $apimServiceName -Location $location -Organization $organisation -AdminEmail $adminEmail
26-
27-
# Certificate related details
28-
$proxyHostname = "proxy.contoso.net"
29-
# Certificate containing Common Name CN="proxy.contoso.net" or CN=*.contoso.net
30-
$proxyCertificatePath = "C:\proxycert.pfx"
31-
$proxyCertificatePassword = "certPassword"
32-
33-
$portalHostname = "portal.contoso.net"
34-
# Certificate containing Common Name CN="portal.contoso.net" or CN=*.contoso.net
35-
$portalCertificatePath = "C:\portalcert.pfx"
36-
$portalCertificatePassword = "certPassword"
37-
38-
# Upload the custom ssl certificate to be applied to Proxy endpoint / Api Gateway endpoint
39-
$proxyCertUploadResult = Import-AzApiManagementHostnameCertificate -Name $apimServiceName -ResourceGroupName $resourceGroupName `
40-
-HostnameType "Proxy" -PfxPath $proxyCertificatePath -PfxPassword $proxyCertificatePassword
41-
42-
# Upload the custom ssl certificate to be applied to Portal endpoint
43-
$portalCertUploadResult = Import-AzApiManagementHostnameCertificate -Name $apimServiceName -ResourceGroupName $resourceGroupName `
44-
-HostnameType "Portal" -PfxPath $portalCertificatePath -PfxPassword $portalCertificatePassword
8+
$apimServiceName = "apim-service-name"
9+
$resourceGroupName = "apim-rg"
10+
$gatewayHostname = "api.contoso.com" # API gateway host
11+
$portalHostname = "portal.contoso.com" # API developer portal host
12+
13+
# Certificate specific details
14+
$gatewayCertCerPath = "C:\Users\Contoso\gateway.cer" # full path to api.contoso.net .cer file
15+
$gatewayCertPfxPath = "C:\Users\Contoso\gateway.pfx" # full path to api.contoso.net .pfx file
16+
$portalCertPfxPath = "C:\Users\Contoso\portal.pfx" # full path to portal.contoso.net .pfx file
17+
$gatewayCertPfxPassword = "certificatePassword123" # password for api.contoso.net pfx certificate
18+
$portalCertPfxPassword = "certificatePassword123" # password for portal.contoso.net pfx certificate
19+
20+
# Convert cert passwords into secure strings
21+
$gatewayCertPfxPasswordSecure = ConvertTo-SecureString -String $gatewayCertPfxPassword -AsPlainText -Force
22+
$portalCertPfxPasswordSecure = ConvertTo-SecureString -String $portalCertPfxPassword -AsPlainText -Force
4523

24+
# Create the HostnameConfiguration object for Proxy endpoint
25+
$proxyHostnameConfig = New-AzApiManagementCustomHostnameConfiguration -Hostname $gatewayHostname -HostnameType Proxy -PfxPath $gatewayCertPfxPath -PfxPassword $gatewayCertPfxPasswordSecure
4626
# Create the HostnameConfiguration object for Portal endpoint
47-
$PortalHostnameConf = New-AzApiManagementHostnameConfiguration -Hostname $proxyHostname -CertificateThumbprint $proxyCertUploadResult.Thumbprint
27+
$portalHostnameConfig = New-AzApiManagementCustomHostnameConfiguration -Hostname $portalHostname -HostnameType Portal -PfxPath $portalCertPfxPath -PfxPassword $portalCertPfxPasswordSecure
4828

49-
# Create the HostnameConfiguration object for Proxy endpoint
50-
$ProxyHostnameConf = New-AzApiManagementHostnameConfiguration -Hostname $portalHostname -CertificateThumbprint $portalCertUploadResult.Thumbprint
29+
# Get existing API Management instance object
30+
$apim = Get-AzApiManagement -ResourceGroupName $resourceGroupName -Name $apimServiceName
31+
32+
# Set Proxy and Portal config objects
33+
$apim.ProxyCustomHostnameConfiguration = $proxyHostnameConfig
34+
$apim.PortalCustomHostnameConfiguration = $portalHostnameConfig
5135

5236
# Apply the configuration to API Management
53-
Set-AzApiManagementHostnames -Name $apimServiceName -ResourceGroupName $resourceGroupName `
54-
-PortalHostnameConfiguration $PortalHostnameConf -ProxyHostnameConfiguration $ProxyHostnameConf
37+
Set-AzApiManagement -InputObject $apim

0 commit comments

Comments
 (0)