/// <summary> /// Emits the usage event to the configured MARKETPLACEAPI_URI. /// </summary> private static async Task <HttpResponseMessage> EmitUsageEvents(IConfigurationRoot config, HttpClient httpClient, DimensionConfig dimensionConfig, string resourceUsageId, string planId) { var usageEvent = new UsageEventDefinition { ResourceId = resourceUsageId, Quantity = dimensionConfig.Quantity, Dimension = dimensionConfig.Dimension, EffectiveStartTime = DateTime.UtcNow, PlanId = planId }; if (ChronJob.IsLocalRun(config)) { return(new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(usageEvent), UnicodeEncoding.UTF8, "application/json"), StatusCode = HttpStatusCode.OK }); } return(await httpClient.PostAsJsonAsync(config["MARKETPLACEAPI_URI"], usageEvent).ConfigureAwait(continueOnCapturedContext: false)); }
/// <summary> /// Gets the token for the system-assigned managed identity. /// </summary> private static async Task <string> GetToken(IConfigurationRoot config, HttpClient httpClient, ILogger log, string resource) { if (ChronJob.IsLocalRun(config)) { return("token"); } // TOKEN_RESOURCE come from the configs using (var request = new HttpRequestMessage(HttpMethod.Get, $"{config["MSI_ENDPOINT"]}/?resource={resource}&api-version=2017-09-01")) { request.Headers.Add("Secret", config["MSI_SECRET"]); var response = await httpClient.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false); if (response?.IsSuccessStatusCode != true) { log.LogError($"Failed to get token for system-assigned MSI. Please check that the MSI is set up properly. Error: {response.Content.ReadAsStringAsync().Result}"); } var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); return(JsonConvert.DeserializeObject <TokenDefinition>(responseBody).Access_token); } }
/// <summary> /// Gets the token for the attached user-assigned managed identity. /// </summary> public static async Task <string> GetToken(HttpClient httpClient, IConfigurationRoot config, ILogger log) { if (ChronJob.IsLocalRun(config)) { return("token"); } // TOKEN_RESOURCE and MSI_CLIENT_ID come from the configs using (var request = new HttpRequestMessage(HttpMethod.Get, $"{config["MSI_ENDPOINT"]}/?resource={config["TOKEN_RESOURCE"]}&clientId={config["MSI_CLIENT_ID"]}&api-version=2017-09-01")) { request.Headers.Add("Secret", config["MSI_SECRET"]); var response = await httpClient.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false); if (response?.IsSuccessStatusCode != true) { log.LogError("Failed to get token for user-assigned MSI. Please check that all the config flags are set properly and the MSI is attached."); } var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); return(JsonConvert.DeserializeObject <TokenDefinition>(responseBody).Access_token); } }