I want to create an array that collects three variable values. These variable values are obtained from my Azure Keyvault.
Here is the script:
CREDENTIAL = DefaultAzureCredential(exclude_shared_token_cache_credential=True, exclude_environment_credential=True, exclude_managed_identity_credential=True, exclude_visual_studio_code_credential=True)
KV_PARTIAL_NAME = "KeyVault_name-"
def get_secret(kv_client, secret_name):
return kv_client.get_secret(secret_name).value
# Subscription identification
def environments():
args = parse_arguments()
env = args.env_name
subscription_name = env
if env == "Prod":
subscription_name = "Production"
print(f"The script is running on {subscription_name}")
azure_key_vault_env_specific_url = f"https://{KV_PARTIAL_NAME}-{env}.vault.azure.net/"
_secret_client = SecretClient(vault_url=azure_key_vault_env_specific_url, credential=CREDENTIAL)
configuration = [ ]
for configuration in _secret_client:
ELASTIC_SEARCH_USER = _secret_client.get_secret("Elastic--User").value
ELASTIC_SEARCH_PASSWORD = _secret_client.get_secret("Elastic--Password").value
ELASTIC_SEARCH_URL = _secret_client.get_secret("Elastic--URL").value
return azure_key_vault_env_specific_url, configuration
# Provide argument -e for the script to choose the correct subscription
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('--env_name', '-e', type=str, choices=['Dev', 'QA', 'Prod'], help='The short for the environment for subscription', required=True)
return parser.parse_args()
def indice_delete(configuration):
elastic_auth_uri = f"https://{ELASTIC_SEARCH_URL}/security/_authenticate"
response = requests.get(elastic_auth_uri, auth=(ELASTIC_SEARCH_USER, ELASTIC_SEARCH_PASSWORD))
search_url_index = "_cat/indices/"
params_dict = {
"h":"index,docs.count",
"s":"docs.count:asc",
"format":"json"
}
elastic_console = f"https://{ELASTIC_SEARCH_URL}/{search_url_index}"
getRequestElasticSearch = requests.get(elastic_console, auth=(ELASTIC_SEARCH_USER, ELASTIC_SEARCH_PASSWORD), params=params_dict)
content = json.loads(getRequestElasticSearch.text)
elastic_console_delete = f"https://{ELASTIC_SEARCH_URL}/"
for index in content:
indiciesList = index
collectdoccount = index['docs.count']
search_int = int(collectdoccount)
if search_int == 0:
index_name = index['index']
delete_url = f"{elastic_console_delete}{index_name}"
response = requests.delete(delete_url, auth=(ELASTIC_SEARCH_USER, ELASTIC_SEARCH_PASSWORD))
if response.status_code == 200:
print("index deleted -" , "index name:" , index['index'] , ", doc.count:" , index['docs.count'] , ", elasticsearch url index:" , delete_url)
if response.status_code != 200:
print ("index not deleted -" , "index name:" , index['index'] , ", Reason:" , response.content)
if __name__ == '__main__':
pass_credentials = environments()
indice_delete(pass_credentials)
At the moment, the variables in the for loop (of environments function) can't get accessed to other functions e.g. indice_delete
You can use the
globalkey word to declare the variables as global from inside the function.like here
You can also return them, and use the returned values in the next function.