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

completion not working when using a variable as a replace argument #1477

Closed
toby-griffiths opened this issue Jan 18, 2024 · 0 comments · Fixed by #1526
Closed

completion not working when using a variable as a replace argument #1477

toby-griffiths opened this issue Jan 18, 2024 · 0 comments · Fixed by #1526
Labels
area: variables Changes related to variables. type: bug Something not working as intended.

Comments

@toby-griffiths
Copy link

toby-griffiths commented Jan 18, 2024

This relates to #1415, but I didn't want to add a load of noise to that ticket.

I'm attempting to prepate a JSON string, to include variable names. I was unable to work out how to use them directly in the YAML object notation…

tasks:
  workspace:create:
    desc: 'Creates a workspace'
    vars:
      JSON:
        map:
          data:
            type: 'workspaces'
            attributes:
              name: '{{.NAME}}'
            relationships:
              project:
                data:
                  id: '{{.PROJECT_ID}}'

… so I'm using replace…

tasks:
  workspace:create:
    desc: 'Creates a workspace'
    required:
      vars:
        - 'PROJECT_ID'
        - 'NAME'
      JSON:
        map:
          data:
            type: 'workspaces'
            attributes:
              name: '__NAME__'
            relationships:
              project:
                data:
                  id: '__PROJECT_ID__'
    cmds:
      - 'echo {{.PROJECT_ID}}'
      - task: 'api:post'
        vars:
          ENDPOINT: 'organizations/{{.TERRAFORM_CLOUD_ORG_NAME}}/workspaces'
          JSON: '{{replace "__PROJECT_ID__" .PROJECT_ID (replace "__NAME__" .NAME (toJson .JSON))}}'

  api:post:
    internal: true
    cmds:
      - 'echo ''{{.JSON}}'''
      - exit 1

This seems to work fine, however when I attempt to autocomplete on the command line I get the error…

template: :1:21: executing "" at <.NAME>: invalid value; expected string

I tried using…

          JSON: '{{replace "__PROJECT_ID__" "{{.PROJECT_ID}}" (replace "__NAME__" "{{.NAME}}" (toJson .JSON))}}'

… however this resulted in a JSON string, when echoed in the api:post task contains empty values…

{"data":{"attributes":{"name":""},"relationships":{"project":{"data":{"id":""}}},"type":"workspaces"}}

If I echo the {{replace "__PROJECT_ID__" "{{.PROJECT_ID}}" (replace "__NAME__" "{{.NAME}}" (toJson .JSON))}} in the calling task then it contains the literal placehodlers…

tasks:
  workspace:create:
    desc: 'Creates a workspace'
    required:
      vars:
        - 'PROJECT_ID'
        - 'NAME'
      JSON:
        map:
          data:
            type: 'workspaces'
            attributes:
              name: '__NAME__'
            relationships:
              project:
                data:
                  id: '__PROJECT_ID__'
    cmds:
      #
      - 'echo ''{{replace "__PROJECT_ID__" "{{.PROJECT_ID}}" (replace "__NAME__" "{{.NAME}}" (toJson .JSON))}}'''

… results in…

{"data":{"attributes":{"name":"{{.NAME}}"},"relationships":{"project":{"data":{"id":"{{.PROJECT_ID}}"}}},"type":"workspaces"}}

So then I tried passing the NAME & PROJECT_ID though to api:post as well in the hope it would interpolate the values where, but alas, they are still empty with this example…

tasks:
  workspace:create:
    desc: 'Creates a workspace'
    required:
      vars:
        - 'PROJECT_ID
        - 'NAME'
    vars:
      JSON:
        map:
          data:
            type: 'workspaces'
            attributes:
              name: '__NAME__'
            relationships:
              project:
                data:
                  id: '__PROJECT_ID__'
    cmds:
      - task: 'api:post'
        vars:
          ENDPOINT: 'organizations/{{.TERRAFORM_CLOUD_ORG_NAME}}/workspaces'
          JSON: '{{replace "__PROJECT_ID__" "{{.PROJECT_ID}}" (replace "__NAME__" "{{.NAME}}" (toJson .JSON))}}'
          NAME: '{{.NAME}}'
          PROJECT_ID: '{{.PROJECT_ID}}'

… or even without the placement…

tasks:
  workspace:create:
    desc: 'Creates a workspace'
    required:
      vars:
        - 'PROJECT_ID
        - 'NAME'
    vars:
      JSON:
        map:
          data:
            type: 'workspaces'
            attributes:
              name: '{{.NAME}}'
            relationships:
              project:
                data:
                  id: '{{.PROJECT_ID}}'
    cmds:
      - task: 'api:post'
        vars:
          ENDPOINT: 'organizations/{{.TERRAFORM_CLOUD_ORG_NAME}}/workspaces'
          JSON: '{{toJson .JSON)}}'
          NAME: '{{.NAME}}'
          PROJECT_ID: '{{.PROJECT_ID}}'

… but still the values of name and projectId are empty.

Any ideas if this is expected behaviour, I'm doing something wrong, or in the wrong way, or is this just not possible right now?

  • Task version: v3.33.2-0.20240117105351-f1e2fee0887b (h1:YOkJH07u9obw1zZ68tIAuMUTlCZXHZN1emoQ/Vk3SJI=)
  • Operating system: MacOS 14.1.2 (23B92)
  • Experiments enabled:
    • GENTLE_FORCE: off
    • REMOTE_TASKFILES: off
    • ANY_VARIABLES: on (2)
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Jan 18, 2024
@pd93 pd93 added type: bug Something not working as intended. area: variables Changes related to variables. and removed state: needs triage Waiting to be triaged by a maintainer. labels Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Changes related to variables. type: bug Something not working as intended.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants