Esempio n. 1
0
        static private void SetReservedUnits(string unit_type, int desired_units)
        {
            IEncodingReservedUnit encodingReservedUnits = mediaContext.EncodingReservedUnits.FirstOrDefault();

            switch (unit_type)
            {
            case "Basic":
                encodingReservedUnits.ReservedUnitType = ReservedUnitType.Basic;
                break;

            case "Standard":
                encodingReservedUnits.ReservedUnitType = ReservedUnitType.Standard;
                break;

            case "Premium":
                encodingReservedUnits.ReservedUnitType = ReservedUnitType.Premium;
                break;
            }
            encodingReservedUnits.CurrentReservedUnits = desired_units;
            encodingReservedUnits.Update();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"Reserved Units Set to {encodingReservedUnits.CurrentReservedUnits}, " +
                              $"Type: {encodingReservedUnits.ReservedUnitType}");
            Console.ResetColor();
        }
Esempio n. 2
0
        private void SetProcessorUnits(IJob job, IJobTemplate jobTemplate, ReservedUnitType nodeType, bool newJob)
        {
            int unitCount = jobTemplate != null ? jobTemplate.TaskTemplates.Count : job.Tasks.Count;

            IEncodingReservedUnit[] processorUnits = GetEntities(MediaEntity.ProcessorUnit) as IEncodingReservedUnit[];
            IEncodingReservedUnit   processorUnit  = processorUnits[0];

            processorUnit.ReservedUnitType = nodeType;
            if (newJob)
            {
                processorUnit.CurrentReservedUnits += unitCount;
            }
            else if (processorUnit.CurrentReservedUnits >= unitCount)
            {
                processorUnit.CurrentReservedUnits -= unitCount;
            }
            processorUnit.Update();
        }
        public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info($"Webhook was triggered!");

            string jsonContent = await req.Content.ReadAsStringAsync();

            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            log.Info(jsonContent);

            if (data.ruCount == null && data.ruSpeed == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Please pass ruCount and ruSpeed in the input object"
                }));
            }


            int              targetNbRU = -1;
            int?             nbunits    = null;
            bool             relative   = false;
            string           RUspeed    = "";
            ReservedUnitType?type       = null;

            if (data.ruSpeed != null)
            {
                RUspeed = ((string)data.ruSpeed).ToUpper();
                if (RUspeed == "S1")
                {
                    type = ReservedUnitType.Basic;
                }
                else if (RUspeed == "S2")
                {
                    type = ReservedUnitType.Standard;
                }
                else if (RUspeed == "S3")
                {
                    type = ReservedUnitType.Premium;
                }
                else
                {
                    return(req.CreateResponse(HttpStatusCode.BadRequest, new
                    {
                        error = "Error parsing ruSpeed"
                    }));
                }
            }

            if (data.ruCount != null)
            {
                string RUcount = (string)data.ruCount;
                if (RUcount[0] == '+' || RUcount[0] == '-')
                {
                    relative = true;
                    try
                    {
                        nbunits = int.Parse(RUcount);
                    }
                    catch
                    {
                        return(req.CreateResponse(HttpStatusCode.BadRequest, new
                        {
                            error = "Error (1) parsing ruCount"
                        }));
                    }
                }
                else
                {
                    try
                    {
                        nbunits = int.Parse(RUcount);
                    }
                    catch
                    {
                        return(req.CreateResponse(HttpStatusCode.BadRequest, new
                        {
                            error = "Error (2) parsing ruCount"
                        }));
                    }
                }
            }

            MediaServicesCredentials amsCredentials = new MediaServicesCredentials();

            log.Info($"Using Azure Media Service Rest API Endpoint : {amsCredentials.AmsRestApiEndpoint}");

            try
            {
                AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain,
                                                                                       new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret),
                                                                                       AzureEnvironments.AzureCloudEnvironment);

                AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials);

                _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider);
            }
            catch (Exception ex)
            {
                string message = ex.Message + ((ex.InnerException != null) ? Environment.NewLine + MediaServicesHelper.GetErrorMessage(ex) : "");
                log.Info($"ERROR: Exception {message}");
                return(req.CreateResponse(HttpStatusCode.InternalServerError, new { error = message }));
            }

            IEncodingReservedUnit EncResUnit = _context.EncodingReservedUnits.FirstOrDefault();

            targetNbRU = EncResUnit.CurrentReservedUnits;
            ReservedUnitType targetType = EncResUnit.ReservedUnitType;

            log.Info("Current type of media RU: " + MediaServicesHelper.ReturnNewRUName(EncResUnit.ReservedUnitType));
            log.Info("Current count of media RU: " + EncResUnit.CurrentReservedUnits);
            log.Info("Maximum reservable media RUs: " + EncResUnit.MaxReservableUnits);

            if (nbunits != null)
            {
                if (relative)
                {
                    if (((int)nbunits) > 0)
                    {
                        log.Info($"Adding {nbunits} unit(s)");
                    }
                    else
                    {
                        log.Info($"Removing {nbunits} unit(s)");
                    }
                    targetNbRU = Math.Max(targetNbRU + (int)nbunits, 0);
                }
                else
                {
                    log.Info($"Changing to {nbunits} unit(s)");
                    targetNbRU = (int)nbunits;
                }
            }

            if (type != null)
            {
                string sru = MediaServicesHelper.ReturnNewRUName((ReservedUnitType)type);
                log.Info($"Changing to {sru} speed");
                targetType = (ReservedUnitType)type;
            }

            if (targetNbRU == 0 && targetType != ReservedUnitType.Basic)
            {
                targetType = ReservedUnitType.Basic; // 0 units so we switch to S1
            }

            bool Error = false;

            try
            {
                EncResUnit.CurrentReservedUnits = targetNbRU;
                EncResUnit.ReservedUnitType     = targetType;
                EncResUnit.Update();
                EncResUnit = _context.EncodingReservedUnits.FirstOrDefault(); // Refresh
            }
            catch (Exception ex)
            {
                Error = true;
            }

            log.Info("Media RU unit(s) updated successfully.");
            log.Info("New current speed of media RU  : " + MediaServicesHelper.ReturnNewRUName(EncResUnit.ReservedUnitType));
            log.Info("New current count of media RU : " + EncResUnit.CurrentReservedUnits);

            return(req.CreateResponse(HttpStatusCode.OK, new
            {
                success = (!Error).ToString(),
                maxRu = EncResUnit.MaxReservableUnits,
                newRuCount = EncResUnit.CurrentReservedUnits,
                newRuSpeed = MediaServicesHelper.ReturnNewRUName(EncResUnit.ReservedUnitType)
            }));
        }