waldur / waldur-custom-offerings

MIT License
0 stars 2 forks source link

User input variables in Python Script #1

Closed mattronix closed 9 months ago

mattronix commented 9 months ago

Hello Team,

How do i get User input variables in a python or bash script, did a scan through the docs and code in here and was not sure if they actually use that functionality.

livenson commented 9 months ago

you need to define them as inputs in offering. they are passed as capitalized environment variables to the scripts

Screenshot 2024-01-18 at 20 33 16

Code is here: https://github.com/waldur/waldur-mastermind/blob/6ab1e71c2a0f2ab5e6bf974cab742578e1963086/src/waldur_mastermind/marketplace_script/utils.py#L238C15-L238C15

mattronix commented 9 months ago

Amazing Thank you :)

mattronix commented 9 months ago

Do i use os.environ to access the keys?

As so far i get none if i use the internal name in Upper case

mattronix commented 9 months ago

Dont quite understand how to access this variable ("environment"):

environment = { key.upper(): input_parameters[key] for key in input_parameters.keys() }

mattronix commented 9 months ago

ah its passed through as

image

caught me off guard

mattronix commented 9 months ago

When i try to load and display the attributes still get issues :(

import os
import json
import re

def run():
    print("Hello World!")

if __name__ == "__main__":
    run()
    attributes = json.loads(os.environ.get("ATTRIBUTES"))
    print(attributes)
Error message
[ErrorDetail(string='Unexpected structure of output', code=["{'name':", "'test',", "'secret':", "'tEAT'}"])]
Error traceback
Traceback (most recent call last): File "/usr/src/waldur/src/waldur_mastermind/marketplace/utils.py", line 104, in process_order processor(order).process_order(user) File "/usr/src/waldur/src/waldur_mastermind/marketplace/processors.py", line 70, in process_order scope = self.send_request(user) ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/waldur/src/waldur_mastermind/marketplace_script/processors.py", line 73, in send_request raise serializers.ValidationError( rest_framework.exceptions.ValidationError: [ErrorDetail(string='Unexpected structure of output', code=["{'name':", "'test',", "'secret':", "'tEAT'}"])]
mattronix commented 9 months ago

Took that reference from the custom examples in here, i can see its probably the " vs ' but how does this work for you if its not working in my case, probably better that i use ast literal instead.

mattronix commented 9 months ago

Got it working in the end:

import os
import json
import re
import ast

def run():
    print("Hello World!")
    attributes = json.loads(os.environ.get("ATTRIBUTES"))
    print(attributes)
    print(type(attributes))
    print(attributes["secret"])

if __name__ == "__main__":
    run()