Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex pulumi.Ouputs; using apply when an output string is constructed from multiple places in an iac code base #14135

Open
gmhta opened this issue Feb 21, 2025 · 1 comment
Labels
area/docs Improvements or additions to documentation

Comments

@gmhta
Copy link

gmhta commented Feb 21, 2025

File: content/docs/iac/concepts/inputs-outputs/apply.md

Issue

The is not clear documentation on how to construct an complex Output<string> when there contents comes from multiple places in the code base.

Suggestion

Add a section to the apply.md doc, specifically dealing with this pattern.

Example text

Solutions in typescript only.

Solution 1.
Context
A complex output is being constructed in multiple places in the code base.
And the place that is collecting the output can't be changes (e.g. BootScript class is provided by a thirdparty)

import * as random from "@pulumi/random";

const r1 = new random.RandomString("random1", {
    length: 16,
});
const r2 = new random.RandomString("random2", {
  length: 16,
});

class BootScript {
  lines: string[] = [];

  sh(line: string) {
    this.lines.push(line);
  }

  compile(): string {
    return this.lines.join("\n")
  }
}

const bs0 = new BootScript()

const bs1 = r1.result.apply(v => {
  bs0.sh(`R1=${v}`)
  return bs0
})

/// Note, the following doesn't work, as bs is of type pulumi.UnwrappedObject<BootScript>, and not BootScript
// pulumi.all([bs1, r2.result]).apply(([bs, v]) => { bs.sh(`R2=${v}`); return bs } )
const bs2 = bs1.apply(bs => {
  return r2.result.apply(v => {
    bs.sh(`R2=${v}`)
    return bs
  })
})

export const data = bs2.apply(v => v.compile())

Solution 2.
Context
A complex output is being constructed in multiple places in the code base.
And the place that is collecting the output CAN be changes

import * as random from "@pulumi/random";
import * as pulumi from "@pulumi/pulumi";

const r1 = new random.RandomString("random1", {
  length: 16,
});
const r2 = new random.RandomString("random2", {
  length: 16,
});

class BootScript {
  lines: pulumi.Output<string>[] = [];

  sh(line: string): void;
  sh(line: pulumi.Output<string>): void;
  sh(line: pulumi.Output<string> | string): void {
    switch (typeof line) {
      case "string":
        this.lines.push(pulumi.output(line));
        break
      case "object":
        this.lines.push(line);
        break
    }
  }

  compile(): pulumi.Output<string> {
    // could used all instead of output
    return pulumi.output(this.lines).apply(v => v.join("\n"))
  }
}

const bs = new BootScript()

bs.sh("#!/bin/sh")
// apply or interpolate
bs.sh(r1.result.apply(v => `R1=${v}`))
bs.sh(pulumi.interpolate`R2=${r2.result}`)

export const data = bs.compile()
@pulumi-bot pulumi-bot added the needs-triage Needs attention from the triage team label Feb 21, 2025
@github-project-automation github-project-automation bot moved this to 🤔 Triage in Docs 📚 Feb 21, 2025
@shughes26
Copy link
Contributor

Thanks for your feedback! We'll look into this while we seek to continuously improve our docs.

@shughes26 shughes26 added area/docs Improvements or additions to documentation and removed needs-triage Needs attention from the triage team labels Feb 24, 2025
@shughes26 shughes26 moved this from 🤔 Triage to 🧳 Backlog in Docs 📚 Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs Improvements or additions to documentation
Projects
Status: 🧳 Backlog
Development

No branches or pull requests

3 participants