Azure 服务没有内置方法来实现您的要求,但我认为您可以通过自己的 python 代码来实现。主要逻辑是从您的订阅中轮询 VM 名称,然后将 VM 名称存储在某处,如果它们发生更改,则将请求发布到类似“HttpTrigger”端点的东西(或者只是将逻辑放入轮询算法中。)。
而对于轮询算法,你可以自己设计或者直接使用'TimeTrigger'来实现。
我注意到您添加了“Python”标签,因此只需使用如下代码并将它们放入轮询算法中:
import requests
from azure.identity import ClientSecretCredential
import json
client_id = 'xxx'
tenant_id = 'xxx'
client_secret = 'xxx'
subscription_id = 'xxx'
credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
accesstoken = str(credential.get_token('https://management.azure.com/.default'))[19:1287]
bearertoken = "Bearer "+accesstoken
r = requests.get("https://management.azure.com/subscriptions/"+subscription_id+"/resources?$filter=resourceType eq 'Microsoft.Compute/virtualMachines'&api-version=2020-06-01",headers={'Authorization': bearertoken})
items = json.loads(r.text)
print(r.text)
for item in items['value']:
print(item['name'])#This line is print, you need to store this in some place such as database, azure blob storage, azure table storage etc.
#check the VM names here. If some VM been added, post a request to the HttpTrigger function.
如果你使用 azure function 'Time Trigger' 而不是自己设计的算法,那么你可以将 client id、tenent id、client_secret 和 subscription id 存储到 keyvault 中,然后让你的函数应用配置设置参考 keyvault,这将使它安全。
以上代码基于 AAD 不记名令牌,您需要创建一个 AAD 应用程序并让它具有订阅的“所有者”RBAC 角色。你需要这样的东西:
这就像由您在“订阅”中创建的虚拟机触发的“自定义触发器”。而且我认为你的VM不会很多,所以不会消耗太多的计算资源。