【问题标题】:Python code for resource groups code deletion in azure using Resource group tags使用资源组标签在 Azure 中用于资源组代码删除的 Python 代码
【发布时间】:2018-07-20 11:36:38
【问题描述】:

我正在尝试使用 python 代码删除其中的资源组和资源。 我已经在 powershell 中尝试过这个,它工作得很好。现在我的组织想要使用 Python。我对python真的很陌生,试图编写代码并失败了。

这是相同的 powershell 代码。任何人都可以帮助在 python 中获取代码。

提前致谢。

 $rgs = Get-AzureRmResourceGroup; 

# $rgs=Get-AzureRmResourceGroup -name "TestResourceGroupToClean1";

if(!$rgs)
{ 
    Write-Output "No resource groups in your subscription"; 
} 
else{


    Write-Output "You have $($(Get-AzureRmResourceGroup).Count) resource groups in your subscription"; 
    foreach($resourceGroup in $rgs)
    { 
        $name=  $resourceGroup.ResourceGroupName; 
     if($resourceGroup.Tags.ExpiryDate)
     {
    try{
         $ResourceGroupTagDate=[datetime]::ParseExact($resourceGroup.Tags.ExpiryDate,'MM/dd/yyyy',$null)
         $count = (Get-AzureRmResource | where { $_.ResourceGroupName -match $name }).Count;
            if($ResourceGroupTagDate.Date -lt $today.Date)
            { 
                $subject="Automated Mail from Resource Group Cleaner"
                $body="Resource Group $($resourceGroup.ResourceGroupName) including resources has been deleted"
                Write-Output "The resource group $name has $count resources. Deleting it..."; 
                Remove-AzureRmResourceGroup -Name $resourceGroup.ResourceGroupName -Force; 
                Write-Output "The resource group $name and $count resources. Deleted..";
                Send-MailMessage -To 'XXXX@XXXXXXXX.com' -Subject $subject -Body $body -UseSsl  -Port 587 -SmtpServer 'smtp.office365.com' -From $userid -Credential $creds 
            }

            }

【问题讨论】:

    标签: python azure azure-resource-group


    【解决方案1】:

    这是我收到的错误 ########################### 失败的回溯(最后一次调用):文件“ C:\Temp\kukzwo13.tgf\9bd12580-5780-4109-abab-66e88fb4df87",第 59 行,如果不是 item.tags['ExpiryDate']:KeyError: 'ExpiryDate'

    在python代码中,我们需要检查key是否对字典有效。我已经做了一个演示,用 python 代码通过标签删除资源组。

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.resource import ResourceManagementClient
    import datetime
    from datetime import time
    
    # Tenant ID for your Azure Subscription
    TENANT_ID = 'tenant id '
    # Your Service Principal App ID
    CLIENT = 'client id'
    # Your Service Principal Password
    KEY = 'scret key'
    subscription_id = 'subscrptionId'
    credentials = ServicePrincipalCredentials(client_id=CLIENT, secret=KEY, tenant=TENANT_ID)
    client = ResourceManagementClient(credentials, subscription_id)
    groups = client.resource_groups.list()
    null_variable = None
    for item in groups:
        if item.tags != null_variable:
                if 'ExpiryDate' in item.tags:
                    expiryDate = datetime.datetime.strptime(item.tags["ExpiryDate"],"%m/%d/%Y")
                    timediff = expiryDate < datetime.datetime.today()
                    if timediff:
                        print(item.tags["ExpiryDate"])
                        print(item.name)
                        client.resource_groups.delete(item.name)
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 我一直在说这个。我能够获得资源组列表,但我无法获得特定的标记资源组。这是我的python...........我一直在参考这个并且能够获取资源组的列表,但我无法获取那个的标签.......跨度>
      • 这是我的代码######################### resources.print('List Resource Groups') 项目在 client.resource_groups.list(): print(item) GROUP_NAME=item.name print(GROUP_NAME) print(item.tags) if (item.tags) is not None: print("tags are not none") if not item .tags['ExpiryDate']: print("没有名为 ExpiryDate 的标签") else: ExpiryDate=item.tags['ExpiryDate'] print("\tExpiryDate: {}".format(ExpiryDate)) else: print("没有标签") ############################################跨度>
      • 这是我收到的错误 ########################### 回溯失败(最近的通话最后):文件“C:\Temp\kukzwo13.tgf\9bd12580-5780-4109-abab-66e88fb4df87”,第 59 行,在 如果不是 item.tags['ExpiryDate']:KeyError: 'ExpiryDate'跨度>
      • 您的初始消息不是 Python,而是 Powershell :)。如果您需要更好的帮助,您应该使用您的 Python 代码编辑您的消息。
      猜你喜欢
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多