【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码
问题描述
对于Azure资源进行配置操作,门户上可以正常操作。但是想通过Python代码实现,这样可以批量处理。那么在没有SDK的情况下,是否有快速办法呢?
问题解答
当然可以,Azure Portal上操作的所有资源都是通过REST API来实现的,所以只要找到正确的API,就可以通过浏览器中抓取到的请求Body/Header来实现转换为Python代码。
第一步:打开浏览器开发者模式(F12), 查看操作所发送的API请求
比如在操作对Resource group 进行Tags修改的时候,抓取到发送的请求为:https://management.chinacloudapi.cn/batch?api-version=2020-06-01, 所以把它的URL, Authorization,Payload内容都复制到文本编辑器中。
第二步:复制请求的Body/Header,特别是Authorization
从第一步发出的请求中复制的内容示例:
Host URL: https://management.chinacloudapi.cn/batch?api-version=2020-06-01
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6InpfMk...........
Payload:
{"requests":[{"content":{"operation":"Replace","properties":{"tags":{"test":"test","test1":"test2"}}},
"httpMethod":"PATCH","name":"xxxx-xx8","requestHeaderDetails":{"commandName":"HubsExtension.ArmTags.patchResourceTags"},
"url":"/subscriptions/xxxxxxxxxxxxx/resourceGroups/adls-rg/providers/Microsoft.Resources/tags/default?api-version=2019-10-01"}]}
复制好请求的Body,Header等信息后,组合成可以正确使用的URL, Authorization,Request Body。
- URL 为 host + payload中的url ,拼接后的正确值是 :https://management.chinacloudapi.cn/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxx/resourceGroups/<resource group name>/providers/Microsoft.Resources/tags/default?api-version=2019-10-01
- Body 内容为Payload中的content信息,所以是:{"operation":"Replace","properties":{"tags":{"test":"test","test1":"test2"}}}
第三步:在Postman等发送API的工具中测试请求是否成功,本处使用 VS Code 插件 Thunder Client
把第二步中的内容,填入到发送REST API的工具中验证,结果显示 200,修改成功。
第四步:转换为Python代码,并测试运行是否成功
在Thunder Client的Response窗口点击“{ }” 按钮,并选择Python 语言,复制示例代码。
Python示例代码(替换为正确的Access Token 和 SubscriptionID , Resource Group名称后,代码正常运行):
import http.client import json conn = http.client.HTTPSConnection("management.chinacloudapi.cn") headersList = { "Accept": "*/*", "User-Agent": "Thunder Client (https://www.thunderclient.com)", "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJ.......", "Content-Type": "application/json" } payload = json.dumps({ "operation": "Replace", "properties": { "tags": { "test": "test", "test1": "test2" } } }) conn.request("PATCH", "/subscriptions/xxxxxxxxx/resourceGroups/xxxx/providers/Microsoft.Resources/tags/default?api-version=2019-10-01", payload, headersList) response = conn.getresponse() result = response.read() print(result.decode("utf-8"))
第五步:用Python Code替换 hardcode Authorization
使用azure.identity来完成认证和显示获取AccessToken
from azure.identity import DefaultAzureCredential ##get access token credential = DefaultAzureCredential() accessToken = credential.get_token("https://management.chinacloudapi.cn/.default") print(accessToken.token)
在结合第四步的Python代码后,就可以实现实时获取Access Token,并Python代码发送REST API.
完整示例代码:
import http.client import json from azure.identity import DefaultAzureCredential ##get access token credential = DefaultAzureCredential() accessToken = credential.get_token("https://management.chinacloudapi.cn/.default") #print(accessToken.token) ## Send API conn = http.client.HTTPSConnection("management.chinacloudapi.cn") headersList = { "Accept": "*/*", "User-Agent": "Thunder Client (https://www.thunderclient.com)", "Authorization": "Bearer " +accessToken.token, "Content-Type": "application/json" } payload = json.dumps({ "operation": "Replace", "properties": { "tags": { "test": "test", "test1": "test2" } } }) conn.request("PATCH", "/subscriptions/xxxxxxxxxxxxxx/resourceGroups/xxxxxxxx/providers/Microsoft.Resources/tags/default?api-version=2019-10-01", payload, headersList) response = conn.getresponse() result = response.read() print(result.decode("utf-8"))
参考资料
Thunder Client for VS Code : https://www.thunderclient.com/
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!