使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法 [英] Way to get Azure Function default key with ARM output or powershell

查看:10
本文介绍了使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Azure Function 应用设置集成测试.部署进展顺利,但我需要一种以编程方式获取默认密钥的方法来运行我的集成测试.

I'm attempting to set up integration testing for an Azure Function app. Deployment is going fine, but I need a way to programatically get the default key to run my integration tests.

我已经尝试过这里链接的内容 - 获取函数&Powershell 中 Azure 函数的主机密钥 - 但无法让 listsecrets 在我的 ARM 部署模板中工作.无法识别 Listsecrets.

I've tried what is linked here - Get Function & Host Keys of Azure Function In Powershell - but cannot get the listsecrets working in my ARM deployment template. Listsecrets is not recognized.

有谁知道如何使用 ARM 模板和/或 powershell 获取此密钥?

Does anyone know how to get this key with an ARM template and/or powershell?

推荐答案

更新 Microsoft 的 ARM API 后,现在可以直接从 ARM 部署输出中检索 Azure 功能键.

After updates to Microsoft's ARM APIs it is now possible to retrieve Azure Function keys directly from the ARM deployment outputs.

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "appServiceName": {
    "type": "string"
    }
  },
  "variables": {
    "appServiceId": "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]"
  },
//... implementation omitted
  "outputs": {
    "functionKeys": {
      "type": "object",
      "value": "[listkeys(concat(variables('appServiceId'), '/host/default'), '2018-11-01')]"
    }
  }
}

输出

Outputs 属性将包含一个 Newtonsoft.Json.Linq.JObject 条目,其中包含 Azure 函数的所有键,即主键、系统键和功能键(其中包括默认键).不幸的是,JObject 与部署变量类型结合起来有点曲折,应该警告您,它是区分大小写的.(如果您在 PowerShell 中工作,可以将其按摩到 hashtables 中以供使用.请参阅下面的奖励.)

Outputs

The Outputs property will contain a Newtonsoft.Json.Linq.JObject entry that contains all of the keys for the Azure Function i.e., master, system keys, and function keys (which includes the default key). Unfortunately, the JObject combined with the deployment variable type is a bit tortuous to get into and you should be warned, is case sensitive. (If you're working in PowerShell it can be massaged into hashtables for consumption. See Bonus below.)

$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value

奖金

下面的代码去掉了额外的 .Value 调用.

function Convert-OutputsToHashtable {
  param (
    [ValidateNotNull()]
    [object]$Outputs
  )

  $Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
    if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
      $ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
    } else {
      $ht[$_.Key] = $_.Value.Value
    }
  } { $ht }

}

这篇关于使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆