i need to be able to throw an error during the pre...
# general
b
i need to be able to throw an error during the preview step if a particular image tag doesn't exist in a particular docker repo so that we can reject a pull request which will ultimately fail. can anybody suggest a good way of doing this? is it going to be a matter of writing a dynamic provider?
l
No, use an
if
-statement with
pulumi.isDryRun
to check if you are in preview mode. Within the if block, check your tag values and throw
ResourceError
when not OK. https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/runtime/#isDryRun https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#ResourceError
b
ok interesting thanks, how about detecting the image presence
l
What do you mean with “the image presence” ?
b
checking if the image is present on that repository
l
Well, for a Kubernetes deployment, if it can fetch the image, your deployment will be succesful. If not, your deployment will fail.
g
.getImage
will fail if it cannot find the image. You could potentially use that. https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/aws/ecr/#getImage
b
i'm about to test
Copy code
export const verifyImageExistsInRegistry = (imageVersion: pulumi.Input<string>) =>
    pulumi.output(imageVersion).apply(async image => {
        try {
            await docker.getRegistryImage({ name: image });
        }
        catch (e) {
            new pulumi.ResourceError(`${image} metadata could not be loaded`, undefined);
        };
    });
is that likely to work against an ACR?
g
Do you mean Azure? Sorry, I just assumed AWS. Looks like we don't have an equivalent for Azure unfortunately, but your code using the Docker provider looks mostly good.
I say mostly because unfortunately you won't be able to catch the error if the getRegistryImage call fails.
b
ah, how can i make pulumi fail-fast if its not there?
g
Pulumi will fail, you just can't handle the error yourself. The Pulumi engine itself will fail and exit.
https://github.com/pulumi/pulumi/issues/3364 tracks allowing you to handle the error itself.
b
ah very interesting
thanks a lot
maybe i'm best doing this as a pre check before hitting pulumi until that issue's fixed then
i
@limited-rainbow-51650 Just do your own logic and if the condition isn't satisfied do:
pulumi.log.error("error message")
💯 2
I use the
pulumi.log
stuff a lot. There is
.error
,
.warn
,
.info
,
.debug
. For example, the
debug
is really useful. They don't show unless you run
pulumi up
with the
-d
flag. Then you get all your debug outputs. Its super helpful when developing because you can get outputs that you don't normally want. I just put the
pulumi.log.error("test")
at the very first line of something I'm working on now and it errored the moment it hit it and didn't go any further and it took me right to the screenshot I posted. Should work perfect for what you need.