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

quaint-electrician-41503

02/24/2021, 11:13 PM
I’d like to write something like this for a stack output where an API changed the exported name between versions
Copy code
try 
  output_value: str = stack.require_output("new_api_name")
except Error:
  output_value: str = stack.require_output("old_api_name")
I had an issue above when I tried this for a reference using “old_api_name”. How can I  write a python pulumi program that will behave as expected?
r

red-match-15116

02/25/2021, 1:08 AM
I had an issue above when I tried this for a reference using “old_api_name”.
Can you say more about what exactly the issue was? Unless
Error
is a python
Exception
defined in your code, I’m not sure if that would work since
require_output
returns a
KeyError
. You could try:
Copy code
try:
  output_value: str = stack.require_output("new_api_name")
except KeyError:
  output_value: str = stack.require_output("old_api_name")
Alternatively, you could use
get_output
instead of
require_output
so it doesn’t error and fetch the “old_api_name” if
None
is returned:
Copy code
output_value = stack.get_output("new_api_name")
if output_value is None:
  output_value = stack.get_output("old_api_name")
q

quaint-electrician-41503

02/26/2021, 3:12 AM
I don’t believe I returned a key error. I wrote something similar to get_output code above. None was not returned and it didn’t function.