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

getNumberAttribute() returns number instead of reference #679

Closed
Tracked by #525
ansgarm opened this issue May 3, 2021 · 10 comments · Fixed by #899
Closed
Tracked by #525

getNumberAttribute() returns number instead of reference #679

ansgarm opened this issue May 3, 2021 · 10 comments · Fixed by #899
Labels
bug Something isn't working cdktf

Comments

@ansgarm
Copy link
Member

ansgarm commented May 3, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

cdktf & Language Versions

0.3.0

Affected Resource(s)

import { DatabaseCluster } from "./.gen/providers/digitalocean";
(generated from "digitalocean/digitalocean@~> 2.8.0")

Debug Output

Expected Behavior

db.port should synthesize to Terraform reference instead of number:

"output": {
    "database_host_port": {
      "value": "${digitalocean_database_cluster.db.host}:${digitalocean_database_cluster.db.port}"
    }
}

This workaround can achieve the above:

new TerraformOutput(this, 'database_host_port', { value: `${db.host}:${db.getStringAttribute('port')}` });

Actual Behavior

new TerraformOutput(this, 'database_host_port', { value: `${db.host}:${db.port}` });

synthesizes to

"output": {
    "database_host_port": {
      "value": "${digitalocean_database_cluster.db.host}:-1.8881545897087498e+289"
    },
}

Steps to Reproduce

The above code originates from my CDK Day talk example.

Important Factoids

References

@ansgarm ansgarm added bug Something isn't working cdktf v0.3 labels May 3, 2021
@jsteinich
Copy link
Collaborator

getNumberAttribute does correctly return a token; that token just isn't resolved correctly. I hadn't observed this exact case before, but this falls under #525.

@danieldreier danieldreier added new Un-triaged issue and removed new Un-triaged issue labels May 3, 2021
@STollenaar
Copy link

I can confirm this is also happening in 0.2.2,
I'm using my own custom provider to do some math and I noticed that only the getNumberAttribute suffers from this not resolving correctly, and always synthesizing to this negative float.
Happens with both Data and Resource source types, and not sure if there is a work around for me if I wanted to pass those references around and do some more with it

@ansgarm
Copy link
Member Author

ansgarm commented May 6, 2021

Hey @STollenaar
When I encountered the issue I used this workaround: db.getStringAttribute('port') which resolved to the correct reference (it seemed to not have mattered that the type was a number).
Does that workaround work for you?

@STollenaar
Copy link

@ansgarm not really, I unfortunately need a number as the output, but whenever you try to cast it to a number it will just go to that negative float.
I was able to get a work around by just copy the bare minimum of my custom provider code into javascript/typescript. This should work until this issue gets resolved

@ansgarm
Copy link
Member Author

ansgarm commented May 7, 2021

Ah, I see. How did you try to cast it? In TS/JS or with the tonumber Terraform function? I suspect using the latter could work, but I haven't tried.

@STollenaar
Copy link

I tried it with the TS/JS casting. Wasn't sure how to use the Terraform function for this, which in theory could work if we get that reference from the getStringAttribute

@jsteinich
Copy link
Collaborator

@STollenaar what are you trying to do with the number? Assign to a different property of type number? Assign to a string property? Do operations based on the number value?

@danieldreier danieldreier added this to the v0.5 milestone May 12, 2021
@danieldreier danieldreier removed this from the v0.4.x milestone Jun 25, 2021
@STollenaar
Copy link

@jsteinich somehow I missed the notification of your comment. But I was passing it to another Data resource which is doing some operations on it (Basically convert to GiB) and I was doing some direct operations on it (Just calling Math.min).
Currently my work around is just doing those direct operations and not taking in that extra number I would've gotten from a Data source

@jsteinich
Copy link
Collaborator

The actual value from a data source isn't available during synth. If the values are know at synth time, then using them directly is your best bet; otherwise, need to use Terraform functions via escape hatches.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2022

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working cdktf
Projects
None yet
4 participants