【问题标题】:Pulumi InputMap<string> - how can I use resource outputs as the keys?Pulumi InputMap<string> - 如何使用资源输出作为键?
【发布时间】:2021-10-31 02:34:05
【问题描述】:

在创建一些 Azure 资源时,例如 MetricAlert (https://www.pulumi.com/docs/reference/pkg/azure-native/insights/metricalert/),我遇到了 InputMap 类型。

从 MetricAlert 文档中可以看出,在某些情况下,您需要在 Tags 属性中传入对其他资源的引用。 Intellisense 向我展示了 Tags 是一个 InputMap,它接受 Output 作为值...但不幸的是,它的工作方式是引用需要作为键传入 - 值只是字符串“Resource”。

有什么方法可以将我的输出传递到这些标签中,它正在成为将这些警报放入我们的堆栈的障碍。

    Tags = 
    {
        { "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/components/webtest-name-example", "Resource" },
        { "hidden-link:/subscriptions/12345678-1234-1234-1234-123456789101/resourcegroups/rg-example/providers/microsoft.insights/webtests/component-example", "Resource" },
    },

【问题讨论】:

    标签: .net .net-core pulumi


    【解决方案1】:

    您需要使用Apply 来构建这样的字典。对于单个键:

    Tags = otherResource.Id.Apply(id => new Dictionary<string, string>
    {
        { $"hidden-link:{id}", "Resource" },
    }),
    

    两个键:

    Tags = Output.Tuple(resource1.Id, resource2.Id).Apply(items => new Dictionary<string, string>
    {
        { $"hidden-link:{items.Item1}", "Resource" },
        { $"hidden-link:{items.Item2}", "Resource" },
    }),
    

    【讨论】:

    • 我找不到任何这样做的例子,我没有意识到我只需要创建一个字典并且有一个隐式转换。谢谢!!
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2021-02-14
    • 2017-09-24
    • 2020-05-09
    • 2021-05-04
    • 2021-05-23
    • 2023-03-02
    • 2022-11-11
    相关资源
    最近更新 更多