-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5aee4e
commit 8831636
Showing
5 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
name: azure-tag-updation-using-python-github-workflow | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
tag_key: | ||
required: true | ||
type: string | ||
description: "Corresponding tag key" | ||
tag_value: | ||
required: true | ||
type: string | ||
description: "Corresponding tag value for the key" | ||
subscription_name: | ||
required: true | ||
type: string | ||
description: "The azure subscription name." | ||
resource_group_names: | ||
required: true | ||
type: string | ||
description: "names of resource groups. more than one can be provided as comma seperated values" | ||
run-name: ${{ inputs.actor }} changing tags of ${{ inputs.resource_group_names }} on ${{inputs.subscription_name }} | ||
jobs: | ||
azure-tag-updation-using-python-github-workflow: | ||
runs-on: ubuntu-latest | ||
env: | ||
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }} | ||
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }} | ||
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
- name: set up python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.11' | ||
- name: package installations | ||
run: | | ||
pip install pipenv | ||
pipenv install | ||
- name: run python program | ||
run: | | ||
pipenv run python3 update_azure_tag.py --tag_key ${{ inputs.tag_key }} \ | ||
--tag_value ${{ inputs.tag_value }} \ | ||
--subscription_name ${{ inputs.subscription_name }} \ | ||
--resource_group_names ${{ inputs.resource_group_names }} | ||
- name: program execution completed | ||
run: echo "program execution completed" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from azure.identity import DefaultAzureCredential | ||
import azure.mgmt.resourcegraph as arg | ||
import logging | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
def run_azure_rg_query(subscription_name: str): | ||
""" | ||
Run a resource graph query to get the subscription id of a subscription back | ||
:return: subscription_id str | ||
""" | ||
credential = DefaultAzureCredential() | ||
# Create Azure Resource Graph client and set options | ||
arg_client = arg.ResourceGraphClient(credential) | ||
|
||
query = f""" | ||
resourcecontainers | ||
| where type == 'microsoft.resources/subscriptions' and name == '{subscription_name}' | ||
| project subscriptionId | ||
""" | ||
|
||
print(f"query is {query}") | ||
|
||
# Create query | ||
arg_query = arg.models.QueryRequest( query=query) | ||
|
||
# Run query | ||
arg_result = arg_client.resources(arg_query) | ||
|
||
# Show Python object | ||
# print(arg_result) | ||
subscription_id = arg_result.data[0]['subscriptionId'] | ||
print(f"Subscription ID is : {subscription_id}") | ||
return subscription_id | ||
|
||
def main(): | ||
""" | ||
To test the script | ||
:return: | ||
""" | ||
load_dotenv() | ||
logging.info("ARG query being prepared......") | ||
run_azure_rg_query(subscription_name="TECH-ARCHITECTS-NONPROD") | ||
logging.info("ARG query Completed......") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters