public async Task OpenLiveStreamInBrowser(Site functionApp, ArmSubscriptionsArray allSubscriptions) { if (!functionApp.AzureAppSettings.ContainsKey(ApplicationInsightsIKeySetting)) { throw new CliException($"Missing {ApplicationInsightsIKeySetting} App Setting. " + $"Please make sure you have Application Insights configured with you function app."); } var iKey = functionApp.AzureAppSettings[ApplicationInsightsIKeySetting]; if (string.IsNullOrEmpty(iKey)) { throw new CliException("Invalid Instrumentation Key found. Please make sure that the Application Insights is configured correctly."); } ColoredConsole.WriteLine("Retrieving Application Insights information..."); var appId = await AzureHelper.GetApplicationInsightIDFromIKey(iKey, AccessToken, ManagementURL, allSubs : allSubscriptions); var armResourceId = AzureHelper.ParseResourceId(appId); var componentId = $@"{{""Name"":""{armResourceId.Name}"",""SubscriptionId"":""{armResourceId.Subscription}"",""ResourceGroup"":""{armResourceId.ResourceGroup}""}}"; var liveMetricsUrl = string.Format(LiveMetricsUriTemplate, WebUtility.UrlEncode(componentId), WebUtility.UrlEncode(appId)); ColoredConsole.WriteLine("Launching web browser..."); if (StaticSettings.IsDebug) { ColoredConsole.WriteLine(VerboseColor($"Launching browser with URL- {liveMetricsUrl}")); } Utilities.OpenBrowser(liveMetricsUrl); }
internal static async Task <string> GetApplicationInsightIDFromIKey(string iKey, string accessToken, string managementURL, ArmSubscriptionsArray allSubs = null) { var allArmSubscriptions = (allSubs ?? await GetSubscriptions(accessToken, managementURL)).value; var allSubscriptionIds = allArmSubscriptions.Select(sub => sub.subscriptionId); var query = $"where type =~ 'Microsoft.Insights/components' and properties.InstrumentationKey == '{iKey}' | project id"; try { return(await GetResourceIDFromArg(allSubscriptionIds, query, accessToken, managementURL)); } catch { throw new CliException("Could not find the Application Insights using the configured Instrumentation Key."); } }
internal static async Task <Site> GetFunctionApp(string name, string accessToken, string managementURL, string slot = null, string defaultSubscription = null, ArmSubscriptionsArray allSubs = null) { IEnumerable <string> allSubscriptionIds; if (defaultSubscription != null) { allSubscriptionIds = new string[] { defaultSubscription }; } else { var subscriptions = (allSubs ?? await GetSubscriptions(accessToken, managementURL)).value; allSubscriptionIds = subscriptions.Select(sub => sub.subscriptionId); } var result = await TryGetFunctionAppFromArg(name, allSubscriptionIds, accessToken, managementURL, slot); if (result != null) { return(result); } var errorMsg = slot == null ? $"Can't find app with name \"{name}\"" : $"Can't find the function app slot with name \"{slot}\""; throw new ArmResourceNotFoundException(errorMsg); }
internal static async Task <string> GetApplicationInsightIDFromIKey(string iKey, string accessToken, string managementURL, ArmSubscriptionsArray allSubs = null) { const string appInsightsError = "Could not find the Application Insights using the configured Instrumentation Key."; var allArmSubscriptions = (allSubs ?? await GetSubscriptions(accessToken, managementURL)).value; var allSubscriptionIds = allArmSubscriptions.Select(sub => sub.subscriptionId); var url = new Uri($"{managementURL}/{ArmUriTemplates.ArgUri}?api-version={ArmUriTemplates.ArgApiVersion}"); var bodyObject = new { subscriptions = allSubscriptionIds, query = $"where type =~ 'Microsoft.Insights/components' and properties.InstrumentationKey == '{iKey}' | project id" }; var response = await ArmClient.HttpInvoke(HttpMethod.Post, url, accessToken, objectPayload : bodyObject); if (!response.IsSuccessStatusCode) { throw new CliException(appInsightsError); } var result = await response.Content.ReadAsStringAsync(); var argResponse = JsonConvert.DeserializeObject <ArgResponse>(result); // we need the first item of the first row return(argResponse.Data?.Rows?.FirstOrDefault()?.FirstOrDefault() ?? throw new CliException(appInsightsError)); }
internal static async Task <Site> GetFunctionApp(string name, string accessToken, string managementURL, string defaultSubscription = null, ArmSubscriptionsArray allSubs = null) { if (!string.IsNullOrEmpty(defaultSubscription)) { var result = await TryGetFunctionApp(name, accessToken, managementURL, defaultSubscription); if (result != null) { return(result); } } var subscriptions = allSubs ?? await GetSubscriptions(accessToken, managementURL); foreach (var subscription in subscriptions.value) { var result = await TryGetFunctionApp(name, accessToken, managementURL, subscription.subscriptionId); if (result != null) { return(result); } } throw new ArmResourceNotFoundException($"Can't find app with name \"{name}\""); }