Created
November 7, 2018 14:57
-
-
Save russellseymour/741213453272a412cada1dcb2864e5f8 to your computer and use it in GitHub Desktop.
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 System; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; | |
using Microsoft.Azure.Management.ResourceManager.Fluent; | |
using Microsoft.Azure.Management.Fluent; | |
using Microsoft.Azure.Services.AppAuthentication; | |
using Microsoft.Rest; | |
using Microsoft.Azure.Management.Dns; | |
using Microsoft.Azure.Management.ResourceManager.Fluent.Core; | |
namespace UpdateDNS | |
{ | |
public static class Function1 | |
{ | |
[FunctionName("Function1")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "{subscription}/{rg_name}/{zone_name}/{lib}")] HttpRequest req, | |
string subscription, | |
string rg_name, | |
string zone_name, | |
string lib, | |
ILogger log) | |
{ | |
log.LogInformation("C# HTTP trigger function processed a request."); | |
int count = 0; | |
dynamic records; | |
// determine the lib to use to get the dns data | |
switch (lib) | |
{ | |
case "fluent": | |
AzureCredentialsFactory factory = new AzureCredentialsFactory(); | |
MSILoginInformation msi = new MSILoginInformation(MSIResourceType.AppService); | |
AzureCredentials msiCred = factory.FromMSI(msi, AzureEnvironment.AzureGlobalCloud); | |
var azureAuth = Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders).Authenticate(msiCred); | |
// set the subscription to work with | |
var azure = azureAuth.WithSubscription(subscription); | |
var dnszone = azure.DnsZones.GetByResourceGroup(rg_name, zone_name); | |
records = dnszone.ListRecordSets(); | |
break; | |
default: | |
// get the token from the managed service identity | |
AzureServiceTokenProvider token_provider = new AzureServiceTokenProvider(); | |
string token = await token_provider.GetAccessTokenAsync("https://management.azure.com"); | |
TokenCredentials token_creds = new TokenCredentials(token); | |
// create the dns client | |
DnsManagementClient client = new DnsManagementClient(token_creds); | |
client.SubscriptionId = subscription; | |
records = client.RecordSets.ListAllByDnsZone(rg_name, zone_name); | |
break; | |
} | |
foreach (var record in records) | |
{ | |
Console.WriteLine(record.Name); | |
count++; | |
} | |
return new OkObjectResult($"Records: {count}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment