How do I reference a Global variable outside of the function im running?

Viewed 20

Hello all I have a python script entailed updateVcenter.py, this script has a global string identified as api_user and api_pass.

The way I have this script functioning is init.py is the start script it asks for a username password then theirs a menu this menu references the new function im trying to create.

            elif option == 6:
            print('V to T vlan backed migration assistant')
            startMigratevt(username,password)

this script does a few things in the beginning - parsing a csv getting the vm name ect... but the problem I'm running into is when I try and pipe the appName into a function defined in updateVcenter.py

    for _, row in df.iterrows():
    appName = row["Name"]
    state = row["State"]
find_vm(appName,vc,api_user,api_pass)

Im referencing the below function however its a series of functions that gets the api token ect...

def find_vm(appName,vc,api_user,api_pass):
#vms = s.get('https://'+ vc +'/rest/vcenter/vm?filter.names='+appName)
resp=get_api_data('{}/rest/vcenter/vm?filter.names={}'.format(vc,appName))
if resp.status_code != 200:
    print('Error! API responded with: {}'.format(resp.status_code))
    return
j = resp.json()
return (j)

The error im encountering is when the find_VM function tries to get the api token if tells me im lacking api_user and api_pass. My question is why do I need to provide this? I have both of those strings as a global object inside updateVcenter script for example

#import local modules
from vcrest import *
from updatevcenter import *
from whoisyou import *

global api_user
global api_pass

If I were to pause the IDE and hover over this string my username and password are present so not sure what I'm doing wrong here.

the Error

I get the below error when i remove the imports of api_user,api_pass

name 'api_user' is not defined

found in function get_api_data found in updateVcenter

when I add those two to the function start I get

get_api_data() missing 2 required positional arguments: 'api_user' and 'api_pass'

I presume that's because I not seeing that to the get_api_data function (see find_VM for what I'm sending). Sorry I feel like I'm ranting this is a hard one to describe but I feel like since its defined globally in that script should it be able to refence that without me telling the function to include those strings?

Thanks All

0 Answers
Related