pulumi / pulumi-java

Java support for Pulumi
Apache License 2.0
64 stars 19 forks source link

`PANIC` generated into public-facing documentation #1341

Closed t0yv0 closed 2 months ago

t0yv0 commented 3 months ago

What happened?

Currently as spotted by @VenelinMartinov RandomInteger docs generate a PANIC into Java code, where other languages work fine. This is using pulumi convert to perform the translation.

Example


# The following example shows how to generate a random priority
# between 1 and 50000 for a aws_alb_listener_rule resource:

resource "random_integer" "priority" {
  min = 1
  max = 50000
  keepers = {
    # Generate a new integer each time we switch to a new listener ARN
    listener_arn = var.listener_arn
  }
}

resource "aws_alb_listener_rule" "main" {
  listener_arn = random_integer.priority.keepers.listener_arn
  priority     = random_integer.priority.result

  action {
    type             = "forward"
    target_group_arn = var.target_group_arn
  }
  # ... (other aws_alb_listener_rule arguments) ...
}

Getting this rendered:


package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.random.RandomInteger;
import com.pulumi.random.RandomIntegerArgs;
import com.pulumi.aws.albListenerRule;
import com.pulumi.aws.AlbListenerRuleArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        // The following example shows how to generate a random priority
        // between 1 and 50000 for a aws_alb_listener_rule resource:
        var priority = new RandomInteger("priority", RandomIntegerArgs.builder()        
            .min(1)
            .max(50000)
            .keepers(Map.of("listener_arn", listenerArn))
            .build());

        var main = new AlbListenerRule("main", AlbListenerRuleArgs.builder()        
            .listenerArn(priority.keepers().applyValue(keepers -> keepers.listenerArn()))
            .priority(priority.result())
            .action(%!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference))
            .build());

    }
}

Python translation:

import pulumi
import pulumi_aws as aws
import pulumi_random as random

# The following example shows how to generate a random priority
# between 1 and 50000 for a aws_alb_listener_rule resource:
priority = random.RandomInteger("priority",
    min=1,
    max=50000,
    keepers={
        "listener_arn": listener_arn,
    })
main = aws.index.AlbListenerRule("main",
    listener_arn=priority.keepers.listener_arn,
    priority=priority.result,
    action=[{
        type: forward,
        targetGroupArn: target_group_arn,
    }])

Output of pulumi about

N/A

Additional context

N/A

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

t0yv0 commented 3 months ago

Since the page https://www.pulumi.com/registry/packages/random/api-docs/randominteger/ does not have this problem yet, this must be a new issue generated by the converter rollout.

Zaid-Ajaj commented 3 months ago

@t0yv0 I've been investigating this issue. I am using the following:

Running pulumi convert --from terraform --language python --out py against the example above, gives me different results:

import pulumi
import pulumi_aws as aws
import pulumi_random as random

priority = random.RandomInteger("priority",
    min=1,
    max=50000,
    keepers={
        "listener_arn": listener_arn,
    })
main = aws.alb.ListenerRule("main",
    listener_arn=priority.keepers["listenerArn"],
    priority=priority.result,
    actions=[aws.alb.ListenerRuleActionArgs(
        type="forward",
        target_group_arn=target_group_arn,
    )])

Notice the subtle difference in actions where the only element in the array is a typed object aws.alb.ListenerRuleActionArgs(...) not just {...}. This to me suggests that the PCL binder couldn't load the schema of aws while generating examples docs for random 🤔 That would be the case if you are disabling automatic plugin acquisition during this phase (i.e. only schema of random being available)

The specific java issue is that when it tries load the schema type of an object and it can't find it, it panics. That means program-gen here needs to be more resilient with these cases.

That said, when we do have access to the schema of aws, program generation for java works fine. Running:

pulumi convert --from terraform --language java --out java

Gives:

package generated_program;

// imports ... 

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var priority = new RandomInteger("priority", RandomIntegerArgs.builder()        
            .min(1)
            .max(50000)
            .keepers(Map.of("listener_arn", listenerArn))
            .build());

        var main = new ListenerRule("main", ListenerRuleArgs.builder()        
            .listenerArn(priority.keepers().applyValue(keepers -> keepers.listenerArn()))
            .priority(priority.result())
            .actions(ListenerRuleActionArgs.builder()
                .type("forward")
                .targetGroupArn(targetGroupArn)
                .build())
            .build());

    }
}
t0yv0 commented 3 months ago

That's a brilliant suggestion, let me go ahead and do a pass at fixing this in pulumi-random.

that would be the case if you are disabling automatic plugin acquisition during this phase

We do try to run with disabled plugin acquisition indeed as much as possible.

QQ: is there a chance we can make a switch to fail hard and fast when a schema for something is not available, or perhaps not listed in a config file ? In this provider build scenario, we would very much rather have that, so that'd guide us quickly in the direction of pinning and making available all the required dependencies.

t0yv0 commented 3 months ago

Similar issue in https://github.com/pulumi/pulumi-aws/issues/3125 - still a problem, I reopened it; checking up on it as well.

t0yv0 commented 3 months ago

Random provider is unblocked, good news. Still struggling with completely closing out the issues in AWS, I opened a few more tickets with concrete examples.

t0yv0 commented 3 months ago

You can close this one as no-repro (once the AWS dependency in random is updated to 6.x).

justinvp commented 2 months ago

@lunaris, does https://github.com/pulumi/pulumi-java/pull/1357 address this issue as well?

t0yv0 commented 2 months ago

FWIW since we updated the dependencies in Random it no longer reproduces.

justinvp commented 2 months ago

I'm going to go ahead and close this as fixed by https://github.com/pulumi/pulumi-java/pull/1357 (and no longer repos anyway given updating the deps in Random).