Hi all ! I am trying to make a POST call using the...
# python
w
Hi all ! I am trying to make a POST call using the requests library which requires the value of a Kubernetes cluster name and cluster URL but it dosent seem to be working. I am aware of the fact that Pulumi returns an Output class object and using the
apply
and
all
function the individual values are supposed to be accessed. This makes sense when the values are being passed on to another Cloud resource. Function calls and traceback are in the thread. What is supposed to be done when the values are supposed to extracted from the
Output
object and used for a non Pulumi operation like making POST calls? I found an alternative where the output is being written to file and then being read from it. https://www.leebriggs.co.uk/blog/2021/05/09/pulumi-apply.html Is this the only workaround available?
Copy code
def set_request_data_headers(args):
    data = pulumi.Output.all(args.get("cluster_name"), args.get("cluster_url"),).apply(
        lambda parameters: json.dumps(
            {
                "CLUSTER_NAME": parameters[0],
                "CLUSTER_URL": parameters[1],
            }
        )
    )

    headers = {
        "Authorization": args.get("token"),
    }

    return data, headers
register_cluster_with_argocd calls the above function
Copy code
def register_cluster_with_argocd(args):
    data, headers = set_request_data_headers(args)
    pulumi.export("Data", data)
    pulumi.export("Headers", headers)
    response = <http://requests.post|requests.post>(
        "SOME_URL",
        headers=headers,
        data=data,
    )

    return response
Copy code
File "VIRTUAL_ENV_PATH/./aws/main.py", line 23, in create
        response = argocd.register_cluster_with_argocd(argocd_registration_args)
      File "VIRTUAL_ENV_PATH/./cluster_bootstrapping/argocd.py", line 34, in register_cluster_with_argocd
        response = <http://requests.post|requests.post>(
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/api.py", line 117, in post
        return request('post', url, data=data, json=json, **kwargs)
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/api.py", line 61, in request
        return session.request(method=method, url=url, **kwargs)
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/sessions.py", line 515, in request
        prep = self.prepare_request(req)
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/sessions.py", line 443, in prepare_request
        p.prepare(
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/models.py", line 321, in prepare
        self.prepare_body(data, files, json)
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/requests/models.py", line 498, in prepare_body
        self._body_position = body.tell()
    TypeError: 'Output' object is not callable
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/sushantk/.pulumi/bin/pulumi-language-python-exec", line 107, in <module>
        loop.run_until_complete(coro)
      File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
        return future.result()
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/pulumi/runtime/stack.py", line 110, in run_in_stack
        await run_pulumi_func(lambda: Stack(func))
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/pulumi/runtime/stack.py", line 45, in run_pulumi_func
        await wait_for_rpcs()
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/pulumi/runtime/stack.py", line 94, in wait_for_rpcs
        raise exception
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/pulumi/output.py", line 182, in run
        transformed: Input[U] = func(value)
      File "VIRTUAL_ENV_PATH/venv/lib/python3.8/site-packages/pulumi/output.py", line 231, in lift
        return UNKNOWN if isinstance(v, Unknown) else getattr(v, item)
    AttributeError: 'str' object has no attribute 'tell'
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
Hi all ! I tried to use the pulumi-command in order to make the POST call after a resource has been provisioned. But there is no way the resource outputs are getting realized. This is the function I have written to use the above package
Copy code
def register_cluster_with_argocd(args, cluster):
    response = pulumi_command.local.Command(
        "command-testing",
        create=("curl "
                "-H 'Authorization: {ARGO_TOKEN}' "
                "-H 'X-Argo-E2E: true' "
                "-H 'Content-Type: application/json; charset=UTF-8' "
                "-d '{{'ARGOCD_CLUSTER_NAME': {CLUSTER_NAME}, "
                "'CLUSTER_NAME': {CLUSTER_NAME}, "
                "SOME_URL_TO_REGISTER_ARGO_CD").format(
                    CLUSTER_NAME=args.get("cluster_name"),
                    CLOUD_PROVIDER=args.get("cloud_provider"),
                    ENV=args.get("env"),
                    REGION=args.get("region"),
                    ARGO_TOKEN=args.get("argo_token")),
        opts=pulumi.ResourceOptions(depends_on=[cluster]),
    )

    return response
Attaching the recorded API in the thread. But even though I have the EKS Cluster added as a
dependsOn
parameter its not populating the real value.
Regarding the article linked in the Original Post, that does not get into the logic of reading from the file in the same session.
@billowy-army-68599 I just realized that this is your blog itself. Any thoughts on how best we can achieve this? Our team has multiple usecases after resources have been provisioned. For example making REST calls, committing files to Github etc.
I have tried writing to a file and reading from it. Also using Stack references. None of them worked out
b
you should be able to make the API call inside the
apply
@worried-xylophone-86184
from your code, it looks like you're calling the
pulumi.Output.all
inside your function, which isn't correct
you need to write your function to take a standard type, then call the function inside the
pulumi.Output.all
w
Ah I think that was a fundamental mistake in my understanding of Pulumi Outputs. Thanks Lee. That has cleared up a number of queries and helped me progress quite a bit. I ll try to create some examples for the Pulumi examples public repo. 🙂 Again, appreciate your help.