gentle-account-13294
02/26/2022, 6:45 PMpulumi.Outputs
question tied to resource_name
:
I would like to have a resource_name
by dynamic and tied to a function
that is taking a Output
as parameter. e.g.
def some_func(database_name: Output[str], schema: Output[str]) :
snowflake.SchemaGrant(f"database_{database_name}_schema_{schema}_privilege_{one_privilege}_future_false_{role_hash}",
database_name=database_name,
privilege=one_privilege,
roles=roles,
schema_name=schema,
with_grant_option=False)
In this specific case I’m trying to write a generic function that will creates schema’s using the Snowflake provider but in principle this question should be applicable to any resource.
It seems like the resource_name
should be a str
and using the Output
just causes the resource name to have something like:
<pulumi.output.Output object at 0x111f482d0>_schema_<pulumi.output.Output object at 0x111f51290>_privilege_CREATE
instead of the actual name of the Output. Note that the Output is already known in this case. I also tried using apply
For e.g.
all_outputs = pulumi.Output.all(database_name, schema)
all_outputs.apply(inner). # where inner function just encapsulates the real work
the only way i could think of is using two apply
one inside the other for the two outputs to resolve this.
I’m hoping there is a better way or I’m missing something in my understanding of using Outputs
??Output.all
seems to have worked, though would be good to discuss with experts here whether this is the correct approach.. e.g.
def snowflake_db_table_grant_future_access(database_name: Output[str], schema: Output[str], privileges: list, roles: list):
"""Grant access to a list of privileges for a schema to a list of roles ....
"""
def inner(all_outputs: Output[List[Output]]):
inner_database_name = all_outputs[0]
inner_schema = all_outputs[1]
role_hash = abs(hash(str(roles)))
for one_privilege in privileges:
snowflake.TableGrant(f"{inner_database_name}_schema_{inner_schema}_table_privilege_{one_privilege}_future_true_{role_hash}",
database_name=database_name,
privilege=one_privilege,
roles=roles,
schema_name=schema,
on_future=True
)
all_outputs = pulumi.Output.all(database_name, schema)
all_outputs.apply(inner)
when i call the above function , resources are now created with correct values for the database_name
and schema_name
.. though I guess in really they can even be thought of as CONSTANTS 🤷echoing-dinner-19531
02/26/2022, 8:13 PMi guess another way to ask my question would be, can resource names be dynamic and dependent on Output of previous resources ?Not really. You can put the whole resource inside an apply but that has problems with previews and update plans.
gentle-account-13294
02/26/2022, 10:42 PM