https://pulumi.com logo
Title
b

bulky-agent-73210

04/26/2022, 1:22 PM
Hi all, can you point me towards and example on how to create a custom resource in Golang SDK? I want to generate PGP keys. I tried some things with ComponentResource, but the output generation code runs on every
pulumi up
, and the output changes. So I assume I’m using the wrong approach here.
My example code below:
type GPGKeyResource struct {
	pulumi.ResourceState

	privateKey pulumi.String `pulumi:"privateKey"`
	publicKey  pulumi.String `pulumi:"publicKey"`
}
func NewGPGKey(ctx *pulumi.Context, keyName string) (*GPGKeyResource, error) {
	var resource GPGKeyResource
	err := ctx.RegisterComponentResource("pulumi:ex:GPGKeyResource", keyName, &resource, pulumi.AdditionalSecretOutputs([]string{"gpg-private"}))
	if err != nil {
		return nil, err
	}

	var pub, priv bytes.Buffer
	err = cryptoutils.GeneratePGPKeyPair(&pub, &priv)
	if err != nil {
		return nil, errors.Wrap(err, "Could not generate PGP key pairs")
	}

	resource.publicKey = pulumi.String(pub.String())
	resource.privateKey = pulumi.String(priv.String())
	ctx.Export("gpg-public", resource.publicKey)
	ctx.Export("gpg-private", pulumi.ToSecret(resource.privateKey))

	return &resource, nil
}
e

echoing-dinner-19531

04/26/2022, 2:38 PM
For python or nodejs you could use dynamic providers that let you write the provider inline with your program. We don't currently support that for go, so you'd have to write a full stand alone provider for the custom resources. There's docs on this at https://www.pulumi.com/docs/guides/pulumi-packages/how-to-author/
b

bulky-agent-73210

04/26/2022, 2:39 PM
Thanks. I think I’ll be able to replace my problematic components with Random and Command packages, for the time being
s

stocky-spoon-28903

04/27/2022, 3:53 PM
You might be able to base a custom “full” provider on this: https://github.com/ekristen/terraform-provider-pgp