public async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "viewer/locators")] HttpRequest req, ILogger log) { TelemetryClient.TrackEvent("Locators"); using (LoggerService.Init(log)) { try { ConfigurationModel config = ConfigurationService.GetConfiguration(); AzureMediaServicesClient client = await AuthenticationService.GetClientAsync(config); InputRequestService inputRequestService = new InputRequestService(client, config); LocatorsController locatorsController = new LocatorsController(client, config); InputRequestModel inputModel = await inputRequestService.GetInputRequestModelAsync(req); LocatorsOutputModel outputModel = await locatorsController.GetLocatorsAsync(inputModel); return(SuccessResponseService.CreateResponse(outputModel)); } catch (AppException e) { return(ReportError(e)); } catch (Exception e) { return(ReportError(e)); } } }
private async Task <StatusOutputModel> GetServiceStatusAsync(InputRequestModel input) { StatusOutputModel status = await StatusService.GetStatusAsync(input); LoggerService.Info("Got service status", LoggerService.Stop); return(status); }
public async Task <StatusChangeOutputModel> StopServicesAsync(InputRequestModel input) { LoggerService.Info("Beginning the stop procedure", LoggerService.Stop); StatusOutputModel preRunServiceStatus = await GetServiceStatusAsync(input); await StopStreamingEndpointAsync(preRunServiceStatus, input.StreamingEndpoint); LoggerService.Info($"Stopping {input.LiveEvents.Count} live event(s)", LoggerService.Stop); foreach (string liveEventName in input.LiveEvents) { if (!IsLiveEventRunning(preRunServiceStatus, liveEventName)) { continue; } await StopLiveEventAsync(liveEventName); List <LiveOutput> liveOutputs = await GetAllLiveOutputsForLiveEventAsync(liveEventName); LoggerService.Info($"Stopping {liveOutputs.Count} live output(s)", LoggerService.Stop); foreach (LiveOutput liveOutput in liveOutputs) { await DeleteAllStreamingLocatorsForAssetAsync(liveOutput.AssetName); await DeleteAssetAsync(liveOutput.AssetName); await DeleteLiveOutputAsync(liveEventName, liveOutput.Name); } } StatusOutputModel postRunServiceStatus = await GetServiceStatusAsync(input); return(GenerateStatusChange(preRunServiceStatus, postRunServiceStatus)); }
public async Task <StatusChangeOutputModel> StartServicesAsync(InputRequestModel input) { LoggerService.Info("Beginning the start procedure", LoggerService.Start); StatusOutputModel preRunServiceStatus = await GetServiceStatusAsync(input); await StartStreamingEndpointAsync(preRunServiceStatus, input.StreamingEndpoint); LoggerService.Info($"Starting {input.LiveEvents.Count} live event(s)", LoggerService.Start); foreach (string liveEventName in input.LiveEvents) { if (!IsLiveEventStopped(preRunServiceStatus, liveEventName)) { continue; } ResourceNamesModel resources = GenerateResourceNames(liveEventName); Asset asset = await CreateAssetAsync(resources.AssetName); await CreateLiveOutputAsync(asset, liveEventName, resources.LiveOutputName, resources.ManifestName); await CreateStreamingLocatorAsync(asset, resources.StreamingLocatorName); await StartLiveEventAsync(liveEventName); } StatusOutputModel postRunServiceStatus = await GetServiceStatusAsync(input); return(GenerateStatusChange(preRunServiceStatus, postRunServiceStatus)); }
public async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "broadcaster")] HttpRequest req, ILogger log) { TelemetryClient.TrackEvent("Stop"); using (LoggerService.Init(log)) { ConfigurationModel config = ConfigurationService.GetConfiguration(); try { AzureMediaServicesClient client = await AuthenticationService.GetClientAsync(config); InputRequestService inputRequestService = new InputRequestService(client, config); StopController stopController = new StopController(client, config); InputRequestModel inputModel = await inputRequestService.GetInputRequestModelAsync(req); StatusChangeOutputModel outputModel = await stopController.StopServicesAsync(inputModel); await WebhookService.CallWebhookAsync(config.WebhookStartSuccess, ActionEnum.Stop, outputModel.Status.Summary.Name); return(SuccessResponseService.CreateResponse(outputModel)); } catch (AppException e) { return(await ReportErrorAsync(config, e)); } catch (Exception e) { return(await ReportErrorAsync(config, e)); } } }
public async Task <InputRequestModel> GetInputRequestModelAsync(HttpRequest request) { LoggerService.Info("Beginning validation", LoggerService.Validation); InputValidator.Validate(request); LoggerService.Info("Passed local validation", LoggerService.Validation); InputRequestModel model = new InputRequestModel { LiveEvents = request.Query[EventsQuery] .ToString() .Split(',') .Select(eventName => eventName.Trim()) .Where(eventName => !string.IsNullOrEmpty(eventName)) .ToList(), StreamingEndpoint = request.Query[EndpointQuery] .ToString() .Trim() }; await ServiceValidator.ValidateAsync(model); LoggerService.Info("Passed remote validation", LoggerService.Validation); return(model); }
public async Task ValidateAsync(InputRequestModel input) { // Validate the Streaming Endpoint IPage <StreamingEndpoint> endpointsPage = await Client.StreamingEndpoints.ListAsync( resourceGroupName : Config.ResourceGroup, accountName : Config.AccountName ); List <StreamingEndpoint> endpoints = endpointsPage.ToList(); bool hasTargetEndpoint = endpoints.Any() && endpoints .Where(endpoint => endpoint.Name == input.StreamingEndpoint) .Any(); if (!hasTargetEndpoint) { throw new ServiceValidationException { DeveloperMessage = "The query parameter 'endpoint' does not match the name of any existing streaming endpoints", Message = "The given streaming endpoint does not exist" }; } // Validate the Live Events IPage <LiveEvent> eventsPage = await Client.LiveEvents.ListAsync( resourceGroupName : Config.ResourceGroup, accountName : Config.AccountName ); List <LiveEvent> events = eventsPage.ToList(); bool hasAllTargetEvents = events.Any() && events .Where(liveEvent => input.LiveEvents.Contains(liveEvent.Name)) .Count() == input.LiveEvents.Count(); if (!hasAllTargetEvents) { string nonExistentEvents = ""; if (!events.Any()) { nonExistentEvents = string.Join(", ", input.LiveEvents); } else { List <string> givenLiveEvents = new List <string>(input.LiveEvents); givenLiveEvents .RemoveAll(givenLiveEvent => events.Any(existingLiveEvent => existingLiveEvent.Name == givenLiveEvent)); nonExistentEvents = string.Join(", ", givenLiveEvents); } throw new ServiceValidationException { DeveloperMessage = string.Format("The following live event(s) in the query parameter 'events' does not match the name(s) of any the existing live events: {0}", nonExistentEvents), Message = string.Format("The following live event(s) do not exist: {0}", nonExistentEvents) }; } }
public async Task <LocatorsOutputModel> GetLocatorsAsync(InputRequestModel input) { bool didAbortBuildingUrl = false; LocatorsOutputModel allLocators = new LocatorsOutputModel { IsAllLive = false, IsAnyLive = false, LiveEvents = new List <LocatorsOutputModel.LiveEvent>() }; LoggerService.Info("Beginning the locators procedure", LoggerService.Locators); StreamingEndpoint streamingEndpoint = await GetStreamingEndpointAsync(input.StreamingEndpoint); LoggerService.Info($"Building {input.LiveEvents.Count} locator(s)", LoggerService.Locators); foreach (string liveEventName in input.LiveEvents) { List <LiveOutput> liveOutputs = await GetLiveOutputsAsync(liveEventName); if (!liveOutputs.Any()) { allLocators.LiveEvents.Add(GenerateEmptyLiveEvent(liveEventName)); didAbortBuildingUrl = true; LoggerService.Warn($"Could not find any live outputs for live event '{liveEventName}'", LoggerService.Locators); continue; } AssetStreamingLocator streamingLocator = await GetStreamingLocatorForAssetAsync(liveOutputs.First().AssetName); ListPathsResponse paths = await GetPathsForStreamingLocatorAsync(streamingLocator.Name); if (!paths.StreamingPaths.Any() || !paths.StreamingPaths.First().Paths.Any()) { allLocators.LiveEvents.Add(GenerateEmptyLiveEvent(liveEventName)); didAbortBuildingUrl = true; LoggerService.Warn($"Could not find any paths for the streaming locator '{streamingLocator.Name}' associated with the live event '{liveEventName}'", LoggerService.Locators); continue; } List <LocatorsOutputModel.LiveEvent.Locator> locators = MapStreamingPathsToLocatorUrls(streamingEndpoint.HostName, paths.StreamingPaths); allLocators.IsAnyLive = true; allLocators.LiveEvents.Add(new LocatorsOutputModel.LiveEvent { Name = liveEventName, IsLive = true, Locators = locators }); } LoggerService.Info($"Finished building {input.LiveEvents.Count} locator(s)", LoggerService.Locators); allLocators.IsAllLive = !didAbortBuildingUrl; return(allLocators); }
public async Task <StatusOutputModel> GetStatusAsync(InputRequestModel input) { LoggerService.Info("Beginning the status procedure", LoggerService.Status); StreamingEndpoint endpoint = await GetStreamingEndpointAsync(input.StreamingEndpoint); List <LiveEvent> liveEvents = await GetLiveEventsAsync(input.LiveEvents); StatusOutputModel.Resource mappedEndpoint = MapStreamingResourceToOurResource(endpoint); List <StatusOutputModel.Resource> mappedEvents = MapLiveEventResourceToOurResource(liveEvents); return(new StatusOutputModel { LiveEvents = mappedEvents, StreamingEndpoint = mappedEndpoint, Summary = DetermineSummary(mappedEndpoint, mappedEvents) }); }
public async Task <StatusOutputModel> GetStatusAsync(InputRequestModel input) { return(await StatusService.GetStatusAsync(input)); }