Skip to content

Instantly share code, notes, and snippets.

@pfsscott
Created January 4, 2022 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfsscott/2499a1d69c8acb364d49ef52971990e6 to your computer and use it in GitHub Desktop.
Save pfsscott/2499a1d69c8acb364d49ef52971990e6 to your computer and use it in GitHub Desktop.
var webJobsAppService = new WebApp(...yadda yadda yadda...);
// Midnight Client Local
var someTrigger = CreateRecurringTriggeredWebJobWorkflow(
webJobName: "SomeJobName",
resourceGroup: resourceGroup,
appService: webJobsAppService,
state: AzureNative.Logic.WorkflowState.Disabled,
interval: 1,
frequency: "Hour",
startTime: DateTime.Parse("2019-10-01T00:00:00"),
timeZoneId: someTimeZoneId);
private AzureNative.Logic.Workflow CreateRecurringTriggeredWebJobWorkflow(
string webJobName,
AzureNative.Logic.WorkflowState state,
ResourceGroup resourceGroup,
WebApp appService,
string frequency,
int interval,
DateTimeOffset startTime,
TimeZoneId timeZoneId
)
{
return new AzureNative.Logic.Workflow($"workflow-{webJobName}", new AzureNative.Logic.WorkflowArgs
{
ResourceGroupName = resourceGroup.Name,
Location = resourceGroup.Location,
WorkflowName = $"Trigger-{webJobName}",
State = state,
Definition = Output.Tuple(appService.Name, resourceGroup.Name).Apply<object>(async data =>
{
var webJobsAppServiceName = data.Item1;
var resourceGroupName = data.Item2;
var (username, password) = await GetPublishProfileUserPass(resourceGroupName, webJobsAppServiceName);
# ... use the username and password here...
return new JsonDocument();
})
});
}
private async Task<(string Username, string Password)> GetPublishProfileUserPass(string resourceGroupName, string appServiceName)
{
var config = await GetClientConfig.InvokeAsync();
var token = await GetClientToken.InvokeAsync();
var httpClient = new HttpClient();
var url = $"https://management.azure.com/subscriptions/{config.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{appServiceName}/publishxml?api-version=2021-02-01";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);
var emptyContent = new StringContent(string.Empty);
emptyContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(url, emptyContent);
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Request failed with {response.StatusCode}");
}
var xmlResponseBody = await response.Content.ReadAsStringAsync();
var xmlElement = XElement.Parse(xmlResponseBody);
return xmlElement.Descendants().Select(publishProfileElement => (publishProfileElement.Attribute("userName").Value, publishProfileElement.Attribute("userPWD").Value)).FirstOrDefault();
}
@pfsscott
Copy link
Author

pfsscott commented Jan 4, 2022

This fails during pulumi up when CreateRecurringTriggeredWebJobWorkflow is called. GetPublishProfileUserPass is called, but fails because the appService doesn't exist yet, causing the Azure API call in GetPublishProfileUserPass to fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment