adamant-advantage-14270
05/06/2024, 10:24 AMdef get_attribute_or_key(obj, key):
"obj.key"
if isinstance(obj, dict):
return obj[key]
return getattr(obj, key)
def get_nested_value(obj, key_list):
"obj.key1.key2..."
sub_obj = obj
for key in key_list:
sub_obj = get_attribute_or_key(sub_obj, key)
return sub_obj
# for usage
get_atrribute_or_key(configmap, "metadata")
get_nested_value(configmap, ["metadata", "name"])
..
as far as I checked pulumi objects don't have the get method so I can't just do:
configmap.get("metadata").get("name")
is there a better way to achieve this ? any tips or ideas that might help? thanks alotdry-keyboard-94795
05/06/2024, 3:37 PMdry-keyboard-94795
05/06/2024, 3:42 PMadamant-advantage-14270
05/07/2024, 8:30 AMobj.__dict__[key]
for context I am creating a lib for developers in my organization to create/ manage k8s resources, so sometimes they will use a dict and sometimes pulumi_kubernetes objects (as both work in the case of k8s), the reason I don't wanna cast everything to a dictionary is because I don't want people to lose autocompletion/type hinting in their code editor (pylance/jedi), and doing everything as pulumi objects isn't really feasible in this scenario.dry-keyboard-94795
05/07/2024, 8:59 AMobj[key]
was supported in pulumi resources, but that's only for certain input/Output objectsdry-keyboard-94795
05/07/2024, 9:05 AMadamant-advantage-14270
05/07/2024, 9:22 AMget_nested_value(configmap, ["metadata", "name"])
also one thing to note is that these functions usage will mostly be hidden behind the ci/cd tools/ wrappers that only few people will have to deal with, so I'm not sure tbhNo matter how you like to participate in developer communities, Pulumi wants to meet you there. If you want to meet other Pulumi users to share use-cases and best practices, contribute code or documentation, see us at an event, or just tell a story about something cool you did with Pulumi, you are part of our community.
Powered by