Skip to content

Commit

Permalink
Feat: azapi - not all resources support tags (#210)
Browse files Browse the repository at this point in the history
* Feat: azapi - not all resources support tags

* fix test

* fix tests

* fixed some issues based on PR comments
  • Loading branch information
TomerHeber authored Dec 4, 2024
1 parent 1992269 commit 08fe5ac
Show file tree
Hide file tree
Showing 10 changed files with 893 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ hcl
tf

dist/

venv
11 changes: 11 additions & 0 deletions azapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# azapi

Utility for parsing Microsoft's azapi resource-tag tables, to locate all resources that can be tagged.
For more information check: https://github.com/env0/terratag/issues/209

```bash
python3 -m venv venv
source venv/bin/activate
python install -r ./requirements.txt
python generate.py
```
42 changes: 42 additions & 0 deletions azapi/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from io import StringIO
from bs4 import BeautifulSoup
import pandas as pd
import re
import requests

url = "https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/tag-support"
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, "html.parser")

tables = soup.find_all("table")

dfs = []

for table in tables:
parent_div = table.find_parent("div", class_="mx-tableFixed")

# Find the h2 within the parent div
title_element = parent_div.find_previous_sibling("h2")
title = title_element.text.strip().lower().replace(" ", "")

df = pd.read_html(StringIO(str(table)))[0]

df["Resource type"] = df["Resource type"].apply(
lambda x: (
f"{title}/{x.lower().replace(' ', '')}"
if title
else x.lower().replace(" ", "")
)
)

dfs.append(df)

df = pd.concat(dfs)

df = df[df["Supports tags"] == "Yes"]

df = df[["Resource type"]]

df.to_csv("azure_resource_tag_support.csv", index=False)
4 changes: 4 additions & 0 deletions azapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
beautifulsoup4==4.12.3
pandas==2.2.3
requests==2.32.3
lxml==5.3.0
Loading

0 comments on commit 08fe5ac

Please sign in to comment.