I'm deploying servers to Hetzner using Pulumi's automation API in Go, but can't figure out how to get the resulting connection information out of the deployment result.
Here's the truncated code:
import (
...
"github.com/pulumi/pulumi-command/sdk/go/command/remote"
"github.com/pulumi/pulumi/sdk/v3/go/auto"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
...
deployFunc := func(ctx *pulumi.Context) error {
// Create SSH key pair, upload them to Hetzner, get back a `*hcloud.SshKey`
...
// `server` is a *hcloud.Server object
server, _ := DeployServerToHetzner(ctx, sshKey)
// This is the info I want to retrieve from the result
connectInfo := remote.ConnectionArgs{
Host: server.Ipv4Address,
Port: pulumi.Float64(22),
User: pulumi.String("root"),
PrivateKey: sshKeyPair.Private,
}
ctx.Export("server-connect-info", connectInfo)
return nil
}
stack, _ := auto.UpsertStackInlineSource(ctx, stackName, projectName, deployFunc, opts...)
res, _ := stack.Up(ctx)
// This is a string but I need it as either map or struct
serverConnectInfo := fmt.Sprintf("%v", res.Outputs["server-connect-info"].Value)
I'm able to retrieve the result from res.Outputs
, but it's a string. I know the server deployment and response with connection details are successful because when I log serverConnectInfo
, it looks like this:
serverConnectInfo map[host:123.456.789.10 port:22 privateKey:-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNza...
-----END OPENSSH PRIVATE KEY-----
user:root]
Based on a few dubious solutions online, I tried marshalling it as we would a JSON and then unmarshalling it into a Pulumi remote.ConnectionArgs
instance. That obviously didn't work, because the struct looks like this:
// From https://pkg.go.dev/github.com/pulumi/pulumi-command/sdk@v0.7.2/go/command/remote#ConnectionArgs
type ConnectionArgs struct {
...
Host pulumi.StringInput `pulumi:"host"`
Port pulumi.Float64PtrInput `pulumi:"port"`
PrivateKey pulumi.StringPtrInput `pulumi:"privateKey"`
User pulumi.StringPtrInput `pulumi:"user"`
}
I'm thinking of constructing my own struct and then retrying the JSON unmarshalling and marshalling solution but it seems like if the ConnectionArgs
struct already has pulumi
tags, there should be some kind of pulumi.Unmarshal
method somewhere. Am I wrong? I couldn't find it anyway.
I've looked into the docs too but haven't seen anything that helps. Maybe I missed a page?
As Peter pointed out in a comment, res.Outputs["server-connect-info"].Value
is a map and correctly guessed that I ran it through fmt.Sprintf
. That was just silly of me.
This works:
serverConnectInfo := res.Outputs["server-connect-info"].Value.(map[string]interface{})
Works well in a Go test using the built-in testing package too:
assert.NotEmpty(t, serverConnectInfo["host"])
assert.Equal(t, serverConnectInfo["user"], "root")