How do i convert a `pulumi.StringPtrOutpu`` to a s...
# general
a
How do i convert a `pulumi.StringPtrOutpu`` to a string or a normal pointer in go, feels like i am missing something. I want to get the name of the created service with `service, err := corev1.NewService(ctx, svcName, &corev1.ServiceArgs...``
l
You can access it only from within an
Apply
function. Often, you don't need to, as you can pass the StringPtrOutput into Pulumi resource constructors without resolving the StringPtr inside it.
What do want to do with the name of the service?
a
use as ingress backend
im creating a service, and then i want to create a ingress that references the service just created
so i need the servicename
l
So the name of the service is being passed to the constructor of another Pulumi resource?
a
correct
Copy code
ing, err := networkingv1.NewIngress(ctx, svcName, &networkingv1.IngressArgs{
		Spec: &networkingv1.IngressSpecArgs{
			Rules: &networkingv1.IngressRuleArray{
				&networkingv1.IngressRuleArgs{
					Host: pulumi.String("<http://test.roffe.nu|test.roffe.nu>"),
					Http: &networkingv1.HTTPIngressRuleValueArgs{
						Paths: &networkingv1.HTTPIngressPathArray{
							&networkingv1.HTTPIngressPathArgs{
								Backend: &networkingv1.IngressBackendArgs{
									Service: &networkingv1.IngressServiceBackendArgs{
										Name: svcName,
										Port: &networkingv1.ServiceBackendPortArgs{
											Number: <http://pulumi.Int|pulumi.Int>(port),
										},
									},
								},
							},
						},
					},
				},
			},
		},
	})
l
Then don't get the StringPtr. Use the StringPtrOutput as-is.
a
but it’s complaining
cannot use service.Metadata.Name() (value of type pulumi.StringPtrOutput) as pulumi.StringInput value in argument to createIngress: missing method ToStringOutput
Copy code
if _, err := createIngress(ctx, svc.Name, service.Metadata.Name(), t.Port); err != nil {
				return err
			}
service
is a *corev1.Service
and the backend does not take a pointer it seems
cannot use backendName (variable of type pulumi.StringPtrInput) as pulumi.StringInput value in struct literal: missing method ToStringOutput
must be a `pulumi.StringInput`so i need to get the value of it xD
l
Usually Pulumi looks after that. You can't get the value, since it's not known yet. What resource type is NewService creating? Something in Azure or GCP?
a
neither
Kubernetes
here is the relevant code
line 36 is where it does not accept the stringptroutput i get from the service creation
which needs to be just a string
and it must be the name of the service i just created
l
I see that line 78 is getting the name. Is
service.Metadata.Elem().Name()
different to what you need?
a
does not work
same shit
it returns a StringPtrOutput that i need to get as a string
the ctx.Export resolves it and prints it yes, but i need it as a string 🙂
it seems
backendName.ToStringPtrOutput().Elem()
might work
atleast it’s not complaining about types now
l
Ah good. Generally, you don't need a string. You can't get at a string, most of the time. You need to provide the Output (StringOutput or StringPtrOutput) to something that knows how to handle it.
There are a few exceptions, such as when you create a new output string from an old one (often used when building AWS ARNs, for example). But most of the time, you just hang on to the Output object and pass that into the next Pulumi resource you're creating.
w
it seems
backendName.ToStringPtrOutput().Elem()
might work Yes - if you want to convert a
StringPtrOutput
to a
StringOutput
you can use
.Elem()
which is the equivalent of de-referencing with
*
on a plain value. Elem is just a shorthand for an Apply that de references the pointer value of the output (if/when it becomes available). A
StringOutput
can then be passed anywhere a
StringInput
is expected.