Deleting all resources in an Azure Resource Group with age more than x days

拥有回忆 提交于 2020-11-29 09:47:07

问题


I attempted to "expand" creationTime on a resource in a resource group (python api) so I could find it's age and if > max_age_days I would have deleted the resource within the resource group.

But creationTime doesn't seem to be available on resources.

Is there another way to delete resources within a resource group based on their age ?

Creation Date and Created By are probably the most basic requirements of resource management.


回答1:


Azure stores a createdTime and changedTime property on all resources that you can query by adding the $expand parameter to the URL.

The expand parameter isn't documented in the Resource Groups API docs, but it is documented in the List Resources API docs (https://docs.microsoft.com/en-us/rest/api/resources/resources/list) and it works the same way.

I don't know offhand how to add this parameter using the Python API, but here is an example of the REST API itself (using the Try It button from the docs at https://docs.microsoft.com/en-us/rest/api/resources/resourcegroups/list#code-try-0).

GET https://management.azure.com/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourcegroups?api-version=2019-08-01&%24expand=createdTime

{
  "value": [
    {
      "id": "/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourceGroups/test2rg-3-backup",
      "name": "test2rg-3-backup",
      "type": "Microsoft.Resources/resourceGroups",
      "location": "eastus",
      "createdTime": "2018-11-12T18:08:38.667582Z",
      "properties": {
        "provisioningState": "Succeeded"
      }
    },
    {
      "id": "/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourceGroups/proddeploy",
      "name": "proddeploy",
      "type": "Microsoft.Resources/resourceGroups",
      "location": "eastus",
      "createdTime": "2019-01-10T21:28:48.8057057Z",
      "tags": {
        "a": "b"
      },
      "properties": {
        "provisioningState": "Succeeded"
      }
    },
...



回答2:


No, azure resource group api doesn't offer createdate or something similar, you need to store that information in tags (when you create the resource) or pull it from azure api (when new resource appears) and store it somewhere externally.



来源:https://stackoverflow.com/questions/58707068/deleting-all-resources-in-an-azure-resource-group-with-age-more-than-x-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!