【问题标题】:Retrieve Service Bus event hub connection string检索服务总线事件中心连接字符串
【发布时间】:2016-03-30 04:21:28
【问题描述】:

我有一个现有的服务总线,其中包含一个使用 Azure 资源管理器部署的队列和事件中心。

现在我有兴趣在不使用 ServiceBus.dll 的情况下使用 Azure PowerShell 检索主键和连接字符串。可以吗??

作为一种解决方法,我创建了一个 ARM 模板,它不部署任何东西,只是查询现有资源并检索我需要的信息。以下模板检索特定服务总线命名空间的事件中心/队列的连接字符串和主键

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "serviceBusNamespace": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "The name of the service bus namespace to create."
            }
        },
        "resourceName": {
            "type": "string",
            "minLength": 1,
            "metadata": {
                "description": "The name of the resource to be retreived."
            }
        },
        "resourceType": {
            "type": "string",
            "minLength": 1,
            "allowedValues": [
                "queues",
                "eventhubs"
            ],
            "metadata": {
                "description": "The type of the resource"
            }
        },
        "policy": {
            "type": "string",
            "minLength": 1,
            "defaultValue": "ManagePolicy",
            "allowedValues": [
                "ManagePolicy",
                "SendPolicy",
                "ListenPolicy"
            ],
            "metadata": {
                "description": "The type of the resource"
            }
        }
    },
    "variables": {
    },
    "resources": [ ],
    "outputs": {
        "connectionString": {
            "type": "string",
            "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/',parameters('resourceType'),'/authorizationRules'),parameters('serviceBusNamespace'),parameters('resourceName'),parameters('policy')),'2015-08-01').primaryConnectionString]"
        },
        "primaryKey": {
            "type": "string",
            "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/',parameters('resourceType'),'/authorizationRules'),parameters('serviceBusNamespace'),parameters('resourceName'),parameters('policy')),'2015-08-01').primaryKey]"
        }
    }
}

使用 ARM 模板查询资源而不实际部署任何东西是否滥用?

编辑 要在 PowerShell 中捕获 ARM 模板的输出,请使用以下代码

$ep = New-AzureRmResourceGroupDeployment -Name "getEventHub" -ResourceGroupName myResourceGroup -Mode Incremental -TemplateFile getEventHub.json -TemplateParameterFile getEventHub.param.json
$RuleConnString = $ep.Outputs.connectionString.value
$RulePrimaryKey = $ep.Outputs.primaryKey.value

请注意,属性名称 connectionStringprimaryKey 与我的模板文件中定义的相同

编辑 2 如果我第二次重新运行 ARM 模板来部署事件中心,我会收到以下错误。

除了使用 ARM 模板查询详细信息外,我没有找到任何选择。

【问题讨论】:

    标签: azure azure-powershell azure-resource-manager arm-template


    【解决方案1】:

    我看不出你在做什么有什么问题。在我看来,资源管理器模板本质上是增量的。因此,您可以编写模板来创建具有相同资源的现有服务总线。如果属性相同,那么它将保持现有资源不变,并返回相关资源的连接字符串和主键。

    我需要自动创建服务总线和队列以及单独的发送/侦听共享访问策略。您可以使用 PowerShell 本机检索服务总线本身上的连接字符串,而无需使用 Get-AzureSBAuthorizationRule 使用 .Net ServiceBus.dll 程序集,但由于仍然存在当前错误,这在队列级别不起作用。

    我尝试使用 ServiceBus.dll 来创建共享访问策略,但有时它会随机失败,但如果您随后立即再次运行它,它随后会起作用。我也尝试过资源管理器模板,但之前您必须传入您自己生成的密钥。现在我看到微软为你生成了这些,但你仍然试图以自动化的方式获取密钥,所以我喜欢你的解决方案。

    不过,有一个问题,您可以捕获资源管理器模板输出并将它们传回 PowerShell 脚本吗?

    干杯

    罗伯

    {   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0",   "parameters": {
        "servicebusNamespace": {
          "type": "string",
          "metadata": {
            "description": "The service bus namespace"
          }
        },
        "notificationssmsqueue": {
          "type": "string",
          "metadata": {
            "description": "Notifications SMS queue"
          }
        }   },   "variables": {
        "location": "[resourceGroup().location]",   },   "resources": [
        {
          "apiVersion": "2015-08-01",
          "name": "[parameters('servicebusNamespace')]",
          "type": "Microsoft.ServiceBus/namespaces",
          "location": "[variables('location')]",
          "properties": {
            "messagingSku": 2
          },
          "resources": [
            {
              "apiVersion": "2015-08-01",
              "name": "[parameters('notificationssmsqueue')]",
              "type": "Queues",
              "dependsOn": [
                "[concat('Microsoft.ServiceBus/namespaces/', parameters('servicebusNamespace'))]"
              ],
              "properties": {
                "path": "[parameters('notificationssmsqueue')]"
              },
              "resources": [
                {
                  "apiVersion": "2015-08-01",
                  "name": "[concat(parameters('notificationssmsqueue'),'.listen')]",
                  "type": "AuthorizationRules",
                  "dependsOn": [
                    "[parameters('notificationssmsqueue')]"
                  ],
                  "properties": {
                    "keyName": "[concat(parameters('notificationssmsqueue'),'.listen')]",
                    "claimType": "SharedAccessKey",
                    "claimValue": "None",
                    "rights": [ "Listen" ],
                    "revision": -1
                  }
                },
                {
                  "apiVersion": "2015-08-01",
                  "name": "[concat(parameters('notificationssmsqueue'),'.send')]",
                  "type": "AuthorizationRules",
                  "dependsOn": [
                    "[parameters('notificationssmsqueue')]"
                  ],
                  "properties": {
                    "keyName": "[concat(parameters('notificationssmsqueue'),'.send')]",
                    "claimType": "SharedAccessKey",
                    "claimValue": "None",
                    "rights": [ "Send" ],
                    "revision": -1
                  }
                }
              ]
            }
          ]
        }   ],   "outputs": {
        "connectionString": {
          "type": "string",
          "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/AuthorizationRules'),parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]"
        },
        "smsSendPrimaryKey": {
          "type": "string",
          "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/Queues/AuthorizationRules'),parameters('serviceBusNamespace'),parameters('notificationssmsqueue'),concat(parameters('notificationssmsqueue'),'.send')),'2015-08-01').PrimaryKey]"
        },
        "smsListenPrimaryKey": {
          "type": "string",
          "value": "[listKeys(resourceId(concat('Microsoft.ServiceBus/namespaces/Queues/AuthorizationRules'),parameters('serviceBusNamespace'),parameters('notificationssmsqueue'),concat(parameters('notificationssmsqueue'),'.listen')),'2015-08-01').PrimaryKey]"
        }   } }
    

    但我这样称呼我的模板:

    New-AzureRMResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile "$scripts_folder$SB_create_script" -TemplateParameterObject ` @{ 服务总线名称空间 = $ 服务总线名称空间; 通知smsqueue = $NotificationSMSqueue }

    【讨论】:

    • 如果我重新运行 ARM 模板以重新部署服务总线和共享访问策略,powershell 将失败并出现错误。所以我别无选择,只能使用发布的模板。我猜 Get-AzureSBAuthorizationRule 不适用于 RM 部署的资源。至于您能否在 PS 中捕获 ARM 输出的问题,简短的回答是 yes
    • 为什么不修改原始模板并添加输出并重新运行它?我有一个创建服务总线和共享访问策略的模板,并添加了您的输出,我可以重新运行它,它会返回密钥而不会失败。喜欢就分享一下好吗?如何捕获 ARM 模板输出以在 PowerShell 中使用,这是我缺少的部分。
    • 用显示如何读取 ARM 模板输出的代码更新了问题。
    • 谢谢桑德什。如果你想让我为我的模板摆姿势,请告诉我。
    • 我确实尝试过两次重新运行相同的 ARM 模板,但失败并出现错误。您能否发布您的模板,以便我可以看到并理解它为什么会在您的情况下运行?
    【解决方案2】:

    这是获取所需信息的正确方法。资源管理器提供了一个与所有服务交互的通用接口。这是门户访问服务的方式,并且每个语言 SDK 只是对与您创建的请求类似的请求的包装器。

    我通常使用 Python 或 Java SDK,但有人告诉我 NodeJS 是一种非常简单的方法来包装 ARM 调用的 Web API,以构建类似于您所做的调用的类似调用,如果您正在寻找非 ARM方法来做到这一点。

    【讨论】:

    • 您有 Node.js 的示例吗?是否可以轻松地在 powershell 中使用 NodeJS 的输出?
    • NodeJS 不会与 powershell 混合,而是如果你想为你正在做的事情制作一个界面或 gui。我会看看我能找到什么。您希望专门以“*-azurerm”开头的 Powershell cmdlet。这些将与 arm 模板很好地混合
    • 目前团队只关注使用 PowerShell 的自动化。所以现在我认为 NodeJS 目前不会有帮助。在那之前,我会坚持使用 ARM。谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2017-02-18
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多