Reply to Message

View discussion in a popup

Replying to:
bsaxe
VMware Employee
VMware Employee

I'm downloading Python code into the container from other action scripts using the API and then adding them to the Python path so I can import them on the fly. Here's the bootstrap code.

onboardingUtils is just a plain Python module, i.e. no module.handler and not even called directly in vRO. vacfg_constants is a set of constants in another Python module that is imported by onboardingUtils. I.e. onboardingUtils depends on vacfg_constants. As long as you have the other Python action bits inside the container, you should be able to load them into your main action.

onboardingUtils = None

_vcoUrl_ = None
_vcoToken_ = None
_headers_ = None

def handler(context, inputs):
    global onboardingUtils
    global _vcoUrl_
    global _vcoToken_
    global _headers_

    _vcoUrl_ = context['vcoUrl']
    _vcoToken_ = context['getToken']()

    _headers_ = {
        'accept': 'application/json',
        'authorization': "Bearer " + _vcoToken_
    }

    os.chdir(inputs['workdir'])
    sys.path.append(inputs['workdir'])

    downloadAction('com.client.vmware.vra.onboarding','onboardingUtils', 'onboardingUtils.py')
    downloadAction('com.client.vmware.vra.onboarding', 'vacfg_constants', 'vacfg_constants.py')

    import onboardingUtils
    onboardingUtils = sys.modules['onboardingUtils']

def downloadAction(module, action, file_path):

    url = f'{_vcoUrl_}/api/actions/{module}/{action}'
    response = requests.get(url, headers=_headers_)
    print(response.status_code)
    response.raise_for_status()

    r = json.loads(response.text)
    open(file_path, 'w').write(r['script'])

 

View solution in original post