Last active
March 18, 2021 11:07
-
-
Save starkfell/0dfc708d5e073397074e85b634c58f5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # | |
| # Name: query-rg.py | |
| # | |
| # Author: Ryan Irujo | |
| # | |
| # Description: Script demonstrating how to query for an Azure Resource Group using the Azure CLI and Python. | |
| # | |
| # Notes: You must already be logged into an Azure Subscription for this script to work. | |
| # | |
| # Syntax: ./query-rg.py -a {RG_NAME} | |
| # Example: ./query-rg.py -a test-rg | |
| import argparse | |
| import subprocess | |
| import sys | |
| # Parsing the argument variable passed to the script. | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('-a', help='Please provide a Resource Group Name.') | |
| args = parser.parse_args() | |
| # Static Variables. | |
| rgName=(args.a) | |
| # JMESPath Query used to only return the Resource Group Name. | |
| rgQuery = "[?name=='%s'].name" % rgName | |
| # Checking for the existence of a Resource Group in an Azure Subscription. | |
| try: | |
| process = subprocess.Popen(['az','group','list','--query',rgQuery,'--output','tsv'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| rgQueryResult = process.communicate()[0].strip() | |
| exitCode = process.returncode | |
| except: | |
| print (rgQueryResult) | |
| if rgQueryResult == rgName : | |
| print("Found Resource Group {}.".format(rgName)) | |
| else: | |
| print("Could not find Resource Group {}.".format(rgName)) | |
| print("Process Complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment