I am trying to write a python script that can check for an existing token on the host server and, if not found, it creates one. There could be many other tokens, but I just wanted to ignore them and process only the token specified in the variable section.
The script below is able to find an existing token, but if there is nothing matching, the token is not created. What mistake have I made?
Note: If I execute the create_token
section without a while
, the condition gets applied to all other tokens as well. But I wanted to restrict the loop with only the variable value I provided.
token_name = "example-1"
if __name__ == '__main__':
existing_tokens = get_access_token(hostname, 'authorizations', username, userpass)
#print(existing_tokens)
if existing_tokens: # Checking whether any token exists or not
for token in existing_tokens:
token_value = (token['app']['name'])
if token_value == token_name:
print("Token already exist!")
else:
while token_value is token_name:
create_token = post_access_token(hostname, 'authorizations', token_params, username, userpass)
print("Token Value: ", create_token['token'])
else:
create_token = post_access_token(hostname, 'authorizations', token_params, username, userpass)
print("Token Value: ", create_token['token'])