如何使用Azure CLI为使用ARM模板创建/更新的新应用程序服务获取publishcredentials

时间:2016-05-10 16:08:12

标签: java azure ftp azure-cli

我正在尝试使用ARM模板自动创建Java应用服务的过程,然后利用FTP发布方法为Tomcat上传Java WAR文件。

以下在Azure CLI中不起作用 -

# Load data.
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
folds <- createFolds(mydata$admit, k=5)

# Create variable "mydata$fold" and assign values of 1:5 to mydata$fold.
? 

但这适用于Azure PowerShell -

$ azure resource show <resource-group> <appservice-name>/publishingcredentials Microsoft.Web/sites/config 2015-08-01

error:   The resource type could not be found in the namespace 'Microsoft.Web' for api version '2015-08-01'

我一直在引用资源管理器https://resources.azure.com中的命名空间,但无法找到正确的语法来获取FTP用户名和密码,以后我可以在脚本中使用它来上传WAR文件。

1 个答案:

答案 0 :(得分:0)

Azure CLI azure resource show类似于Azure PowerShell Get-AzureRmResource,它始终使用“GET”方法。如果添加“-vv”选项,您将看到它正在使用的REST API。另一方面,PowerShell Invoke-AzureRmResourceAction正在使用“POST”方法。如果您向Invoke-AzureRmResourceAction添加'-debug'选项,您将能够看到REST API。

https://management.azure.com/subscriptions/<your subscription id>/resourceGroups/<resource group>/providers/Microsoft.Web/sites/<app service>/config/publishingcredentials/list?api-version=2015-08-01

以下是调用此REST API的PowerShell脚本:

Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'

# The tenant ID of you Subscription. You can use tenant name instead.
$tenantID = "<the tenant ID of your Subscription>"

# You can leave the variables as what they are, if you are under Azure Cloud Environment.
$loginEndpoint = "https://login.windows.net/"
$managementResourceURI = "https://management.core.windows.net/"
$redirectURI = New-Object System.Uri ("urn:ietf:wg:oauth:2.0:oob")
$clientID = "1950a258-227b-4e31-a9cf-717495945fc2"

# Fill in the below variables.
$subscriptionID = "<your subscription id>"
$resouceGroup = "<your resource group>"
$appService = "<your app service>"
$username = "<your Azure account>"


# Constructing the authentication string.
$authString = $loginEndpoint + $tenantID

# Use the above authentication string to create an authentication context.
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ($authString, $false)

$promptBehaviour = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto

$userIdentifierType = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType]::RequiredDisplayableId

$userIdentifier = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier ($username, $userIdentifierType)


# Prompt for signing in.
$authenticationResult = $authenticationContext.AcquireToken($managementResourceURI, $clientID, $redirectURI, $promptBehaviour, $userIdentifier); 

# construct authorization header for the REST API.
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"}

# Invoke the REST API.
Invoke-RestMethod -Method POST -Uri "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01" `
                  -Headers $headers

如果您使用的是Linux系统,则可以在OAuth 2中使用curl。以下bash脚本将为您提供Web应用程序的发布凭据。但是,为了使用我的脚本,您需要create a service principleinstall curl in you Linux system

#!/bin/bash

tenantID="<the tenant id of your subscription>"

client_id="<the client id of your AD application>"

client_secret="<a key you added to your AD application>"

body="grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&resource=https%3A%2F%2Fmanagement.core.windows.net%2F"

authorization=$(curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data-ascii "$body" "https://login.microsoftonline.com/$tenantID/oauth2/token" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["token_type"]+" "+obj["access_token"])')


subscriptionID="<your subscription id>"
resourceGroup="<the resource group of you web app>"
appService="<your web app>"

curl -X POST -H "Authorization: $authorization" --data-ascii "" "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01"