![](/img/trans.png)
[英]How to retrieve bytes data from request body in Azure Function App
[英]Pulumi: How can I retrieve function names from a Function App in Azure?
使用 Pulumi,如何从 Azure 中的 Function 应用程序中检索 function 名称?
客观的:
我的目标是为给定的 Function 应用程序检索所有 Azure 函数及其关联的 API 键。
问题:
我找不到关于如何以编程方式从 Function 应用程序检索 function 名称的 Pulumi 文档。
然后我可以依靠以下 function 来满足我的需要:
await ListWebAppFunctionKeys.InvokeAsync(new ListWebAppFunctionKeysArgs {
Name = appName,
FunctionName = "?",
ResourceGroupName = resourceGroupName
});
替代方法:
我试图依赖 HTTP GET 请求(作为解决方法),但观察到一个未经授权的错误:
var current = Pulumi.Azure.Core.GetClientConfig.InvokeAsync().Result;
var subscriptionId = current.SubscriptionId;
var appName = functionApp.Name;
var url = $"GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{appName}/functions?api-version=2022-03-01";
var httpClient = new HttpClient();
var result = await httpClient.GetAsync(url);
if (!result.IsSuccessStatusCode) throw new Exception($"Error: Failed to retrive Azure function names from {appName}");
var json = result.Content.ReadAsStringAsync();
我无法解决错误。 我想我需要创建一个不记名令牌但不知道所需的步骤。
要使用 Pulumi 在 Azure 中检索 function 名称及其关联的 API 键,用于 Azure 中的 Azure 应用程序,您可以使用azure.appservice.FunctionApp
资源和azure.appservice.Function
这是一个示例,说明如何使用 Pulumi 检索 Azure 中的 Azure 应用程序的 function 名称及其关联的 API 键:
import * as azure from "@pulumi/azure";
import * as pulumi from "@pulumi/pulumi";
// Create a Function App
const functionApp = new azure.appservice.FunctionApp("my-function-app", {
resourceGroupName: "my-resource-group",
appServicePlanId: "my-app-service-plan",
storageAccountName: "mystorageaccount",
storageAccountAccessKey: "storageaccountaccesskey",
});
// Create a function
const function1 = new azure.appservice.Function("my-function-1", {
resourceGroupName: functionApp.resourceGroupName,
appServicePlanId: functionApp.appServicePlanId,
functionAppId: functionApp.id,
name: "my-function-1",
scriptRoot: "./function1",
runtime: "node",
});
// Retrieve the function names and their associated API keys
const functionNames = pulumi.all([function1.name]).apply(names => names.join(", "));
const apiKeys = pulumi.all([function1.defaultHostKey]).apply(keys => keys.join(", "));
// Print the function names and their associated API keys
console.log(`Function names: ${functionNames}`);
console.log(`API keys: ${apiKeys}`);
此代码创建一个 Function 应用程序和一个 function 在该 Function 应用程序中,然后使用pulumi.all
和apply
检索函数的名称及其关联的 API 键。 pulumi.all
function 等待指定的资源可用,然后将它们的属性传递给apply
function,它将名称和 API 键连接成字符串。
我希望这有帮助!
首先建立授权令牌:
var httpAuthClient = new HttpClient() { Timeout = new TimeSpan(0, 2, 0) };
httpAuthClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthToken.Value);
var functionNames = await FunctionNames.Get(resourceGroupName, functionAppName, httpAuthClient);
建立授权令牌后,请考虑以下代码:
public static class FunctionNames
{
public static async Task<IEnumerable<string>> Get(string resourceGroupName, string appName, HttpClient httpAuthClient)
{
var url = $"https://management.azure.com/subscriptions/{SubscriptionId.Value}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{appName}/functions?api-version=2022-03-01";
var result = await httpAuthClient.GetAsync(url);
if (!result.IsSuccessStatusCode) throw new Exception($"Error: Failed to retrive Azure function names from {appName}");
var json = await result.Content.ReadAsStringAsync();
var root = JsonConvert.DeserializeObject<JsonSupport.AzureFunctionItems.Root>(json);
var functionNames = root.value.Select(v => v.properties.name);
return functionNames;
}
下面是一些用于创建授权令牌的代码:
public static class BearerToken
{
public async static Task<string> Create(string tenantId, string clientId, string clientSecret, string scope)
{
var tokenRequestBody = new Dictionary<string, string> {
{ "grant_type" , "client_credentials" },
{ "client_id" , clientId },
{ "client_secret", clientSecret },
{ "scope" , scope }
};
var url = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token";
var client = new HttpClient() { BaseAddress = new Uri(url) };
var content = new FormUrlEncodedContent(tokenRequestBody);
var response = await client.PostAsync("", content);
if (response.IsSuccessStatusCode)
{
var tokenResponse = await response.Content.ReadAsStringAsync();
var valueFor = JsonConvert.DeserializeObject<JsonSupport.AccessToken.Root>(tokenResponse);
return valueFor.access_token;
}
throw new Exception(response.ReasonPhrase);
}
}
这是用于反序列化的 JSON root class:
public class Root
{
public string token_type { get; set; }
public int expires_in { get; set; }
public int ext_expires_in { get; set; }
public string access_token { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.