I have a python script that is designed to list specific VMs and push that list onto an ADO wiki page.
The script works, in the way that it does update the wiki page, but it appends the list and I need to have it update, not append.
By example, if the script is ran it lists 6 VMs, then if one of them is deleted and the script is ran again, it doesn't update the list on the wiki with the 5 VMs remaining.
Here is the script:
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import SubscriptionClient
from azure.identity import DefaultAzureCredential
from azure.common import credentials
import datetime
from azure.keyvault.secrets import SecretClient
import requests, base64
resource_group_name = "vms-rg"
kv_url = "https://kv-pass-manager-tool.vault.azure.net/"
pat_secret_name = "BuildUser--WikiUpdateDebugVMs"
wiki_url = f"https://dev.azure.com/tcg-common/Trisus/_wiki/wikis/Trisus.wiki/14228/Debug-Virtual-Machine-List"
CREDENTIAL = DefaultAzureCredential(exclude_shared_token_cache_credential=True, exclude_visual_studio_code_credential=True, exclude_environment_credential=True, exclude_managed_identity_credential=True,
exclude_powershell_credential=True, exclude_interactive_browser_credential=True)
subscription_id = credentials.get_azure_cli_credentials()[1]
subscription_name = SubscriptionClient(CREDENTIAL).subscriptions.get(subscription_id).display_name
print("Searching in Subscription:" , subscription_name)
compute_client = ComputeManagementClient(CREDENTIAL, subscription_id)
def get_or_set_secret(kv_url, secret_name, secret_value):
secret_client = SecretClient(kv_url, CREDENTIAL)
try:
secret = secret_client.get_secret(secret_name)
print(secret)
return secret.value
except Exception as ex:
secret_client.set_secret(secret_name, secret_value)
return secret_value
def list_debugvms():
debug_vms = compute_client.virtual_machines.list(resource_group_name)
vm_list = [ ]
for vm in debug_vms:
json_string = vm.tags
ownerEmail = json_string['OwnerEmail']
ticket_number = vm.name.split("-")[1]
date = datetime.datetime.strftime(vm.time_created, "%d-%m-%Y")
print("virtual machine name:", vm.name , ", ticket number:" , ticket_number , ", Owner Email:" , ownerEmail , ", creation date:" , date)
vm_list.append(vm.name)
return vm_list
# updating wiki page
def wiki_update(vm_list):
#pat = get_or_set_secret(kv_url, pat_secret_name)
pat = get_or_set_secret(kv_url, pat_secret_name, " ")
authorization = base64.b64encode(f":{pat}".encode('ascii')).decode('ascii')
basic_auth_value = f"Basic {authorization}"
get_headers = {"Authorization": basic_auth_value}
get_response = requests.get(wiki_url, verify=True, headers=get_headers)
if get_response.status_code == 200:
existing_content = get_response.json()["content"]
for token in vm_list:
if token in existing_content:
# If the token is present, overwrite it with itself
new_content = {"content": existing_content}
else:
# If the token is not present, append it to the end of the content
new_content = {"content": existing_content + "\n" + token}
response_headers = get_response.headers
etag = response_headers.get("ETag").replace('"', '')
request_headers = {"Authorization": basic_auth_value, "content-type": "application/json", "If-Match": etag}
response = requests.patch(wiki_url, verify=True, json=new_content, headers=request_headers)
status_code = response.status_code
if __name__ == '__main__':
vm_list = list_debugvms()
wiki_update(vm_list)
Any idea how to remove the initial list on wiki page and then run this script to update the wiki page?