https://pulumi.com logo
Title
r

rapid-ram-37207

10/29/2019, 7:06 PM
Hi everyone, I've been pulling my hair out trying to make the outputs of my stack work. I want to get each network security rule by name, and then output it. Here is what I have right now : https://gist.github.com/MathieuBuisson/0aeb53488077beb4da484cc79caad079#file-stack-ts This deploys resources perfectly, however the outputs
consulRule
and
sshRule
are incorrect. All the rule outputs have the same property values : https://gist.github.com/MathieuBuisson/0aeb53488077beb4da484cc79caad079#file-outputs-txt As you can see in the Gist, all rules outputs are identical. It looks like these 3 rules outputs are all referencing the same object : the first rule. Could anyone give me any pointers on what I'm doing wrong here ? Thank you.
w

white-balloon-205

10/29/2019, 9:56 PM
What is the type of
nsg.rules
here?
Does this work?
function getRuleByName(name: string)     {
    return pulumi.all(nsg.rules.map(r => r.rule.name)).apply(ruleNames => {
        return ruleNames.find(ruleName => name === ruleName);
    });
}
r

rapid-ram-37207

10/30/2019, 9:48 AM
nsg.rules
is of type
NetworkSecurityRule[]
,
NetworkSecurityRule
being a custom ComponentResource and has a
rule
property of type
azure.network.NetworkSecurityRule
.
Thank you for your help @white-balloon-205. This only outputs the name of the rules, instead of the rules object.
I'm still stuck on this : https://pulumi-community.slack.com/archives/CJ909TL6P/p1572375960100600 Anybody has any suggestion, please ?
t

tall-librarian-49374

11/01/2019, 2:34 PM
Why do you try to convert to promise?
You should export Outputs directly, potentially changed with
apply
and
all
So something like Luke suggested but without mapping to name first
r

rapid-ram-37207

11/02/2019, 11:24 AM
Thanks for your help @tall-librarian-49374 The
Promise()
is just to unwrap the underlying object from the output, as explained in this article : https://www.pulumi.com/blog/unit-testing-infrastructure-in-nodejs-and-mocha/ As per your suggestion, i modified
getRuleByName
like so :
function getRuleByName(name: string) {
    return pulumi.all(nsg.rules.apply(r => {
        return r.find(r => r.rule.name === name)
    })
}
But then I get the error :
Property 'apply' does not exist on type 'NetworkSecurityRule[]'.
NetworkSecurityRule
is a custom ComponentResource which contains an
azure.network.NetworkSecurityRule.
t

tall-librarian-49374

11/02/2019, 11:35 AM
Should be
return pulumi.all(nsg.rules).apply(r  => ...
r

rapid-ram-37207

11/02/2019, 12:43 PM
Thanks @tall-librarian-49374 So this :
function getRuleByName(name: string) {
    return pulumi.all(nsg.rules).apply(r => {
        return r.find(r => r.rule.name === name)
    })
}
Results in the following error for the test in the find method:
This condition will always return 'false' since the types 'Output<string>' and 'string' have no overlap
And this might work :
function getRuleByName(name: string) {
    return pulumi.all(nsg.rules).apply(r => {
        return r.find(r => r.rule.name.get() === name)
    })
}
But it result in the following error :
Error: Cannot call '.get' during update or preview.
    To manipulate the value of this Output, use '.apply' instead.
t

tall-librarian-49374

11/02/2019, 9:10 PM
Sorry for many false hints. Now I finally opened an editor and this seems to fit your use case:
function getRuleByName(name: string) {
  return pulumi.all(nsg.rules.map(r => r.name)).apply(names => {
      const index = names.indexOf(name);
      return index >= 0 ? nsg.rules[index] : undefined;
  });
}
r

rapid-ram-37207

11/03/2019, 2:13 PM
@tall-librarian-49374 Wow, that is some ugly and convoluted code, but it works ! I remember seeing a Github issue related to making outputs easier to deal with, but I cannot find it. I think that outputs should be simpler to use. A common use case is testing : importing the outputs into the tests suite and running assertions against them should be much simpler than it is now. Anyway, thanks a lot for your help.
t

tall-librarian-49374

11/03/2019, 2:24 PM
That's a valid but a challenging goal. I'm glad you are unblocked for now.