https://pulumi.com logo
#python
Title
# python
f

fast-spoon-69536

03/14/2022, 5:45 PM
create_app_role is a function i created within the ___main___.py._
Copy code
esa.create_app_role(example.endpoint.apply(lambda endpoint: f"{endpoint}"),admin_username,admin_password,app_role_payload,role_name)
_The create_app_role function takes the following inputs._ endpoint - the endpoint is only available after the previous resource is deployed. _admin_username - username to log into the endpoint_ _admin_password - admin password_ _app_role_payload - a dictionary / REST payload_ _role_name - name of the role to create_ The basics of what I'm trying to do is. 1. deploy aws opensearch 2. call the opensearch internal API to create roles and users after it is deployed. endpoint needs to be a str, but it is a Output<str>. How can I get it to be a str? Or is there a better approach ?
Copy code
TypeError: can only concatenate str (not "Output") to str
p

prehistoric-activity-61023

03/14/2022, 5:48 PM
can you paste the
create_app_role
body?
AFAIK, you cannot really convert
Output[str]
to
str
due to its lazy evaluation features. I guess, you’re trying to modify somehow the
endpoint
argument within
create_app_role
. I don’t know the whole picture but would it be a problem for you to accept
Input[str]
instead of
str
in this function and perform
apply
on it?
f

fast-spoon-69536

03/14/2022, 7:18 PM
@prehistoric-activity-61023 Here is the body:
Copy code
def create_app_role(host,admin_username,admin_password,payload,role_name):
    #logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
    #logging.debug('This message should go to the log file')
    url = 'https://' + host + '/_plugins/_security/api/roles/' + role_name
    basic_creds = admin_username + ':' + admin_password
    base64_bytes = base64.standard_b64encode(basic_creds.encode('utf-8'))
    base64_message = base64_bytes.decode('utf-8')

    headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic ' + base64_message
    }
    http = urllib3.PoolManager()
    resp = http.request(
        "GET",
        url,
        headers=headers
    )
    #print(resp)
    if resp.status == 404:
        print('404 Role not found')
        #logging.debug('404 Role not found')
        print('Adding custom role')
        app_role_payload_bytes = json.dumps(payload).encode('ascii')
        resp = http.request(
            "PUT",
            url,
            body=app_role_payload_bytes,
            headers=headers
        )
    else:
        print('Role exists ')
p

prehistoric-activity-61023

03/14/2022, 7:51 PM
well, if you accept
Input[str]
instead of
str
, you could do:
Copy code
url = pulumi.Output.concat('https://', host, '/_plugins/_security/api/roles/', role_name)
within that function
What actually bothers me more is that this code seems to be pretty imperative rather than declarative and IMO it’s not gonna be a good fit for pulumi (or any other declarative tool).
If I were you, I’d take a closer look a dynamic providers and try to translate the given function into a proper resource.
f

fast-spoon-69536

03/15/2022, 6:18 PM
As I mentioned I am very new to pulumi, and the code was never meant to be declarative. It was simply code I'm using to understand Input / Output in pulumi. Test/POC code. The Dynamic Provider looks to be what I need. It even mentions calling REST apis. I will investigate.
p

prehistoric-activity-61023

03/15/2022, 6:35 PM
Oh, I see. That changes a lot 🙂.
f

fast-spoon-69536

03/16/2022, 12:05 PM
Thank you for pointing me in the right direction.
4 Views