在Azure Workbook中如何使用条件参数值?

huangapple go评论64阅读模式
英文:

How can I have conditional parameter value in Azure Workbook?

问题

如何在Azure Workbook中实现有条件的参数值?

我想要一个名为"environment"的参数。它包含一些文本值。例如,它有两个值:testproduction

此外,我想要另外两个参数:subscription和app_insights。

现在,我希望根据"environment"参数的值自动选择subscription和app_insights的值。

因此,当"environment"参数为test时,我尝试如下选择subscription参数的test订阅:

summarize by subscriptionId
| project subscriptionId=strcat("/subscriptions/", subscriptionId),
selected = case("{environment}" == "test" and subscriptionId == "test subscription id", true, false)

但似乎"environment"参数没有被替换为"{environment}"占位符。如果我移除"{environment}" == "test"的检查,那么测试订阅会如预期地被选择。

我该如何使用一个参数来决定另一个参数的值?是否有更好的实现我想要的方法?最好还希望subscription和app_insights参数不会显示给工作簿的用户,并防止用户手动编辑它们。

英文:

How can I have conditional parameter value in Azure Workbook?

I want to have one parameter called environment. It has some text values. E.g. it has two values test and production.

Also, I want to have two other parameters: subscription and app_insights.

Now, I want to select the subscription and app_insights values automatically based on the environment parameter value.

So, here is how I am trying to select the test subscription for the subscription parameter when the environment parameter is test:

summarize by subscriptionId
| project subscriptionId=strcat("/subscriptions/", subscriptionId),
selected = case("{environment}" == "test" and subscriptionId == "test subscription id", true, false)

在Azure Workbook中如何使用条件参数值?

But it seems that the environment parameter does not get substituted for the "{environment}" placeholder. If I remove the "{environment}" == "test" check, then the test subscription gets selected as expected.

How can I use one parameter to decide the value of another parameter? Is there a better way to achieve what I want? Preferably I also want the subscription and app_insights parameters not to be shown to the user of the workbook and prevent them from being edited manually.

答案1

得分: 1

是的,这是100%支持的。

参数必须在引用它们之前声明,然后参数可以从左到右引用,就像代码一样。在这两个步骤中,顺序都很重要,参数也很重要。

如果您在Azure Monitor的Workbooks中打开“Resource Picker”示例:
在Azure Workbook中如何使用条件参数值?

  • 这里有一个隐藏的参数,用于显示要显示哪种资源类型,订阅选择器和资源选择器依赖于它
  • 订阅后的所有内容都依赖于所选的订阅,依此类推
  • 资源选择器依赖于所选的资源组,等等
  • 下面的网格依赖于资源类型、订阅和所选的资源等

如果您查看这里的资源参数的确切查询,其查询文本如下:

Resources
| where type in~({ResourceTypes})
| extend resourceGroupId = strcat('/subscriptions/', subscriptionId, '/resourceGroups/', resourceGroup)
| where resourceGroupId in~({ResourceGroups}) or '*' in~({ResourceGroups})
| order by name asc
| extend Rank = row_number()
| project value = id, label = name, selected = Rank <= 10, group = resourceGroup

您可以看到查询中引用了ResourceGroups参数和ResourceTypes参数,并在查询时替换它们。Subscriptions参数在ARG查询的订阅字段中被选中。

如果在查询中没有替换参数,那么在该查询之前该参数不存在(或者参数未标记为必需且没有值,因此它被替换为空字符串)。

在编辑查询步骤时,该步骤右上角的工具栏有一个项目,可以打开查询,其中所有参数在适当的视图中替换(Resource Graph Explorer窗口、日志查询窗口、ADX查询站点等),这样您可以看到参数被替换的方式。

英文:

Yes, this is 100% supported.

Parameters have to be declared before they are referenced, and then parameters can be referenced left to right, just like code. Order is important in both steps and parameters.

If you open the "Resource Picker" sample in Workbooks in Azure Monitor:
在Azure Workbook中如何使用条件参数值?

  • there's a hidden parameter of which resource type to show that the subscription picker and resource picker depends on
  • everything after subscriptions depends on the selected subscriptions, etc.
  • the resource picker depends on which resource groups are selected, etc
  • the grid below this depends on resource type, subs, and resources selected, etc.

if you look at the exact query for the resources parameter here, its query text is:

Resources
| where type in~({ResourceTypes})
| extend resourceGroupId = strcat('/subscriptions/', subscriptionId, '/resourceGroups/', resourceGroup)
| where resourceGroupId in~({ResourceGroups}) or '*' in~({ResourceGroups})
| order by name asc
| extend Rank = row_number()
| project value = id, label = name, selected = Rank <= 10, group = resourceGroup

Where you can see that the ResourceGroups parameter and the ResourceTypes parameter are both referenced in the query, and replaced at query time. The Subscriptions parameter is selected in the ARG query's subscription field.

If a parameter isn't being replaced in the query, then that parameter doesn't exist before that query (or the parameter is not marked required and has no value, so it gets replaced as an empty string).

while editing a query step, the toolbar in the upper right corner of that step has an item to open up the query with all the parameters replaced in the appropriate view (the Resource Graph Explorer blade, or Logs query blade, or ADX query site, etc) so you can see exactly how the parameters were replaced.

答案2

得分: 1

以下是翻译好的内容:


这是一个示例,说明一个参数如何依赖于另一个参数。

假设我们有一个名为 Environment 的参数,类型为 Options group,从以下JSON中获取数据:

[
    { "value":"'dev'", "label":"dev", "selected":true },
    { "value":"'prod'", "label":"prod" }
]

然后,我们可以创建一个次要参数,如果选择了 dev,它将返回一个特定的列表,如果选择了 prod,它将返回另一个列表。这第二个参数的类型为 Dropdown,配置为在所有订阅上运行Azure资源图查询。

resources
| extend dev_subs = dynamic([
  "dev-sub1",
  "dev-sub2"
  ])
| extend prod_subs = dynamic([
  "prod-sub1",
  "prod-sub2"
  ])
| extend selection = {Environment}
| extend output = iff(selection == "dev", dev_subs, prod_subs)
| project output
| mv-expand output
| distinct tostring(output)

当在第一个参数中选择了 dev 时,第二个参数将显示一个下拉菜单,其中包含选项 dev-sub1dev-sub2;当选择了 prod 时,第二个参数将显示一个下拉菜单,其中包含选项 prod-sub1prod-sub2

英文:

Here's an example which illustrates how a parameter can be conditional upon another.


Let's say we have a parameter named Environment, of type Options group which gets data from the following JSON:

[
    { "value":"'dev'", "label":"dev", "selected":true },
    { "value":"'prod'", "label":"prod" }
]

then, we can create a secondary parameter which will return a certain list if dev is selected, and a different one if prod is selected. This second parameter is of type Dropdown and is configured to run an Azure resource graph query across all subscriptions.

resources
| extend dev_subs = dynamic([
  "dev-sub1",
  "dev-sub2"
  ])
| extend prod_subs = dynamic([
  "prod-sub1",
  "prod-sub2"
  ])
| extend selection = {Environment}
| extend output = iff(selection == "dev", dev_subs, prod_subs)
| project output
| mv-expand output
| distinct tostring(output)

when dev is selected in the first parameter, the second parameter will show a dropdown with options dev-sub1 and dev-sub2; when prod is selected, the second parameter will show a dropdown with options prod-sub1 and prod-sub2.

huangapple
  • 本文由 发表于 2023年7月27日 19:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定