pulumi / pulumi-yaml

YAML language provider for Pulumi
Apache License 2.0
38 stars 11 forks source link

Support (logical) name for resources and config variables #546

Closed Zaid-Ajaj closed 5 months ago

Zaid-Ajaj commented 5 months ago

Description

Fixes #539, #477

This PR adds support for resources and config variables to have a logical name other than their lexical name which is used to reference the resources or variables in the YAML program.

Added as a top-level field:

name: simple-yaml
runtime: yaml
resources:
  bucket:
    type: aws:s3:Bucket
    name: my-bucket
    properties:
      website:
        indexDocument: index.html

Where the bucket is the lexical name that is used for the program evaluator and my-bucket is the name that is used when registering the resource. In TypeScript it would look like this:

const bucket = new Bucket("my-bucket", ...)

Similarly, configuration variables can now have a logical name as well where at runtime, it will be the key used to query the config data provided by Pulumi CLI whereas their lexical name is just how they are defined and referenced in the program.

Program-gen for YAML has been updated. Take this PCL:

config configLexicalName string {
  __logicalName = "cC-Charlie_charlie.😃⁉️"
}

resource resourceLexicalName "random:index/randomPet:RandomPet" {
  // not necessarily a valid logical name, just testing that it passes through to codegen unmodified
  __logicalName = "aA-Alpha_alpha.🤯⁉️"

  prefix = configLexicalName
}

output outputLexicalName {
  __logicalName = "bB-Beta_beta.💜⁉"
  value = resourceLexicalName.id
}

Becomes:

configuration:
  configLexicalName:
    type: string
    name: "cC-Charlie_charlie.\U0001F603⁉️"
resources:
  resourceLexicalName:
    type: random:RandomPet
    name: "aA-Alpha_alpha.\U0001F92F⁉️"
    properties:
      prefix: ${configLexicalName}
outputs:
  "bB-Beta_beta.\U0001F49C⁉": ${resourceLexicalName.id}

Outputs just keep using their logical name because their lexical name cannot be referenced anywhere

Frassle commented 5 months ago

Outputs just keep using their logical name because their lexical name cannot be referenced anywhere

Why do we even support __logicalName on outputs in PCL? I don't think any language supports referencing of outputs, this looks like something we should clean up.

Frassle commented 5 months ago

Why do we even support __logicalName on outputs in PCL?

My first thought was maybe block labels couldn't be arbitary strings, and that's true for the first label but that's always going to be "output". The second block label is fine to be a string literal: https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#structural-elements

We should sanitize this.

Zaid-Ajaj commented 5 months ago

Why do we even support __logicalName on outputs in PCL? I don't think any language supports referencing of outputs, this looks like something we should clean up.

Indeed 💯

AaronFriel commented 4 months ago

@Frassle outputs have logical names because they can have names that are not legal identifiers using the export facility.

https://github.com/pulumi/pulumi/pull/9464#issuecomment-1109026906

Frassle commented 4 months ago

Yeh we worked that out, but outputs only have one name. Unlike resources which have a source name and a logical name outputs just have a logical name. See https://github.com/pulumi/pulumi/pull/15180 for the follow up on this.