Last active
March 9, 2021 15:42
-
-
Save martinjt/50405714daec583ebc1902dd3fe25e19 to your computer and use it in GitHub Desktop.
Http request from pulumi component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Pulumi; | |
using Microsoft.Azure.Management.AppService; | |
using Pulumi.AzureNative.Authorization; | |
using Microsoft.Rest; | |
using System.Threading.Tasks; | |
using Pulumi.AzureNative.Web; | |
using System.Net.Http; | |
using System; | |
using System.Text.Json; | |
using System.Text; | |
class FunctionAppKey : ComponentResource | |
{ | |
[Output] | |
public Output<string> Key { get; set; } | |
public FunctionAppKey(string name, FunctionAppKeyArgs args, ComponentResourceOptions? options = null) | |
: base("newday:cardplatform:example:functionapp-key", name, args, options) | |
{ | |
Key = Output.Tuple(args.Name, args.ResourceGroup, args.KeyName, args.KeyValue) | |
.Apply(tuple => | |
{ | |
return CreateKey(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); | |
}); | |
} | |
private async Task<string> CreateKey(string functionAppName, string functionAppResourceGroup, string keyName, string keyValue) | |
{ | |
var keys = await ListWebAppHostKeys.InvokeAsync(new ListWebAppHostKeysArgs | |
{ | |
Name = functionAppName, | |
ResourceGroupName = functionAppResourceGroup | |
}); | |
var masterKey = keys.MasterKey; | |
Console.WriteLine($"MasterKey: {masterKey}"); | |
var keydata = new | |
{ | |
name = keyName, | |
value = keyValue | |
}; | |
Console.WriteLine($"got 1"); | |
var content = new StringContent(JsonSerializer.Serialize(keydata), Encoding.UTF8, "application/json"); | |
Console.WriteLine($"got 2"); | |
var httpClient = new HttpClient(); | |
Console.WriteLine($"got 3"); | |
httpClient.BaseAddress = new Uri($"https://{functionAppName}.azurewebsites.net"); | |
Console.WriteLine($"got 4"); | |
httpClient.DefaultRequestHeaders.Add("x-functions-key", masterKey); | |
Console.WriteLine($"got 5"); | |
var response = await httpClient.PutAsync("/admin/host/keys", content); | |
Console.WriteLine($"responseReason: {response.ReasonPhrase}"); | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine($"got 6"); | |
this.Key = Output.Create(keyValue); | |
return keyValue; | |
} | |
Console.WriteLine($"got 7"); | |
throw new ResourceException("Error adding the API Key", this); | |
} | |
public class FunctionAppKeyArgs : ResourceArgs | |
{ | |
public Input<string> Name { get; set; } | |
public Input<string> ResourceGroup { get; set; } | |
public Input<string> KeyName { get; set; } | |
public Input<string> KeyValue { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment