https://pulumi.com logo
#golang
Title
# golang
c

cold-toothbrush-60276

10/23/2021, 7:57 PM
Hi all, is there a way to get a raw string value from a StringOutput that I can use to feed into another resource? I'm currently using the golang digital ocean provider and the second argument of most objects is just a "plain" string and not a pulumi.String... and as pulumi seemingly only returns internal types I am having a hard time converting them...
b

bored-table-20691

10/24/2021, 3:26 AM
You can use Apply to get it, but it sounds like you are trying to use an Output as the resource name?
If you have some sample code of what you'd like to do it would help
c

cold-toothbrush-60276

10/24/2021, 9:57 AM
Copy code
func linkResourcesToProject(ctx *plm.Context, prj *do.Project, resources *plm.StringArray) error {
reference := fmt.Sprintf("%s-resource", prj.Name)
	_, err := do.NewProjectResources(ctx, reference, &do.ProjectResourcesArgs{
		Project:   prj.ID().ToStringOutput(),
		Resources: resources,
	})

	return err
}
something like that, of course this doesn't work due to type mismatches
b

billowy-army-68599

10/24/2021, 3:53 PM
@cold-toothbrush-60276 as Itay said, resource names must be known at resource creation time, so you can't use an output for that. This is intentional. You're creating a
do.Project
with a string, so you should already know the value, so use a
string
c

cold-toothbrush-60276

10/25/2021, 8:20 AM
by the time the project is created this function is called, with the resource urns available, so theoretically I should be able to just pass in the project and fetch the name from there... so the resource, as you put it, should also already be available.
I must me missing something specific to the lifecycle pulumi manages internally that won't allow for this behavior. I can do exactly this with terraform, and from what I understand, under the hood terraform providers are being used.
b

billowy-army-68599

10/25/2021, 11:54 AM
@cold-toothbrush-60276 can you show what this would look like in terraform? I don't believe you can do it with tf when defining a resource in tf:
Copy code
resource "digital_ocean_project" "my_project" {

}
you could not build
my_project
dynamically in this case, as you're building the resource and it must have a known name
also, it's a common misconception that "use terraform under the hood": we only use terraform providers to populate the CRUD lifecycle of resources (or a better way to think of it, we use terraform to map the API calls needed to create the resource. The execution engine and everything else is completely different
c

cold-toothbrush-60276

10/25/2021, 11:55 AM
then how do they do it using for_each?
b

billowy-army-68599

10/25/2021, 11:58 AM
if you use
for_each
you end up needing to specify a specific name, and then also referencing the value with dependencies using the array element In addition, terraform essentially compiles down to json, which is a different working mechanism to Pulumi. We don't transpile to a configuration language
c

cold-toothbrush-60276

10/25/2021, 11:59 AM
okay let me rephrase my question: is there a way to have dynamically created names that could potentially be sourced from resources already being created?
if not, fine with that.
case closed.
b

billowy-army-68599

10/25/2021, 12:00 PM
no, there isn't a way to do that, resource names must be known at runtime, as I originally stated
c

cold-toothbrush-60276

10/25/2021, 12:02 PM
cool, I know what to look out for now, thanks.
5 Views