Ejemplo n.º 1
0
 /// <summary>
 /// Parse renewals from store
 /// </summary>
 protected override IEnumerable <Renewal> ReadRenewals()
 {
     if (_renewalsCache == null)
     {
         var list    = new List <Renewal>();
         var di      = new DirectoryInfo(_settings.Client.ConfigurationPath);
         var postFix = ".renewal.json";
         foreach (var rj in di.EnumerateFiles($"*{postFix}", SearchOption.AllDirectories))
         {
             try
             {
                 var storeConverter = new PluginOptionsConverter <StorePluginOptions>(_plugin.PluginOptionTypes <StorePluginOptions>(), _log);
                 var result         = JsonConvert.DeserializeObject <Renewal>(
                     File.ReadAllText(rj.FullName),
                     new JsonSerializerSettings()
                 {
                     ObjectCreationHandling = ObjectCreationHandling.Replace,
                     Converters             =
                     {
                         new ProtectedStringConverter(_log,                                                                              _settings),
                         new StorePluginOptionsConverter(storeConverter),
                         new PluginOptionsConverter <TargetPluginOptions>(_plugin.PluginOptionTypes <TargetPluginOptions>(),             _log),
                         new PluginOptionsConverter <CsrPluginOptions>(_plugin.PluginOptionTypes <CsrPluginOptions>(),                   _log),
                         new PluginOptionsConverter <OrderPluginOptions>(_plugin.PluginOptionTypes <OrderPluginOptions>(),               _log),
                         storeConverter,
                         new PluginOptionsConverter <ValidationPluginOptions>(_plugin.PluginOptionTypes <ValidationPluginOptions>(),     _log),
                         new PluginOptionsConverter <InstallationPluginOptions>(_plugin.PluginOptionTypes <InstallationPluginOptions>(), _log)
                     }
                 });
                 if (result == null)
                 {
                     throw new Exception("result is empty");
                 }
                 if (result.Id != rj.Name.Replace(postFix, ""))
                 {
                     throw new Exception($"mismatch between filename and id {result.Id}");
                 }
                 if (result.TargetPluginOptions == null || result.TargetPluginOptions.GetType() == typeof(TargetPluginOptions))
                 {
                     throw new Exception("missing TargetPluginOptions");
                 }
                 if (result.ValidationPluginOptions == null || result.ValidationPluginOptions.GetType() == typeof(ValidationPluginOptions))
                 {
                     throw new Exception("missing ValidationPluginOptions");
                 }
                 if (result.StorePluginOptions == null)
                 {
                     throw new Exception("missing StorePluginOptions");
                 }
                 if (result.CsrPluginOptions == null && result.TargetPluginOptions.Name != CsrOptions.NameLabel)
                 {
                     throw new Exception("missing CsrPluginOptions");
                 }
                 if (result.InstallationPluginOptions == null)
                 {
                     throw new Exception("missing InstallationPluginOptions");
                 }
                 if (string.IsNullOrEmpty(result.LastFriendlyName))
                 {
                     result.LastFriendlyName = result.FriendlyName;
                 }
                 if (result.History == null)
                 {
                     result.History = new List <RenewResult>();
                 }
                 result.RenewalDays = _settings.ScheduledTask.RenewalDays;
                 list.Add(result);
             }
             catch (Exception ex)
             {
                 _log.Error("Unable to read renewal {renewal}: {reason}", rj.Name, ex.Message);
             }
         }
         _renewalsCache = list.OrderBy(x => x.GetDueDate()).ToList();
     }
     return(_renewalsCache);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Parse renewals from store
 /// </summary>
 protected override IEnumerable <Renewal> ReadRenewals()
 {
     if (_renewalsCache == null)
     {
         var list         = new List <Renewal>();
         var di           = new DirectoryInfo(_settings.Client.ConfigurationPath);
         var postFix      = ".renewal.json";
         var renewalFiles = di.EnumerateFiles($"*{postFix}", SearchOption.AllDirectories);
         foreach (var rj in renewalFiles)
         {
             try
             {
                 // Just checking if we have write permission
                 using var writeStream = rj.OpenWrite();
             }
             catch (Exception ex)
             {
                 _log.Warning("No write access to all renewals: {reason}", ex.Message);
                 break;
             }
         }
         foreach (var rj in renewalFiles)
         {
             try
             {
                 var storeConverter = new PluginOptionsConverter <StorePluginOptions>(_plugin.PluginOptionTypes <StorePluginOptions>(), _log);
                 var text           = File.ReadAllText(rj.FullName);
                 var result         = JsonConvert.DeserializeObject <Renewal>(
                     text,
                     new JsonSerializerSettings()
                 {
                     ObjectCreationHandling = ObjectCreationHandling.Replace,
                     Converters             =
                     {
                         new ProtectedStringConverter(_log,                                                                              _settings),
                         new StorePluginOptionsConverter(storeConverter),
                         new PluginOptionsConverter <TargetPluginOptions>(_plugin.PluginOptionTypes <TargetPluginOptions>(),             _log),
                         new PluginOptionsConverter <CsrPluginOptions>(_plugin.PluginOptionTypes <CsrPluginOptions>(),                   _log),
                         new PluginOptionsConverter <OrderPluginOptions>(_plugin.PluginOptionTypes <OrderPluginOptions>(),               _log),
                         storeConverter,
                         new PluginOptionsConverter <ValidationPluginOptions>(_plugin.PluginOptionTypes <ValidationPluginOptions>(),     _log),
                         new PluginOptionsConverter <InstallationPluginOptions>(_plugin.PluginOptionTypes <InstallationPluginOptions>(), _log)
                     }
                 });
                 if (result == null)
                 {
                     throw new Exception("result is empty");
                 }
                 dynamic neutralResult = JObject.Parse(text);
                 if (neutralResult == null)
                 {
                     throw new Exception("neutralResult is empty");
                 }
                 if (result.Id != rj.Name.Replace(postFix, ""))
                 {
                     throw new Exception($"mismatch between filename and id {result.Id}");
                 }
                 if (result.TargetPluginOptions == null || result.TargetPluginOptions.GetType() == typeof(TargetPluginOptions))
                 {
                     throw new Exception("missing TargetPluginOptions");
                 }
                 if (result.ValidationPluginOptions == null || result.ValidationPluginOptions.GetType() == typeof(ValidationPluginOptions))
                 {
                     if (neutralResult.ValidationPluginOptions != null)
                     {
                         throw new Exception($"missing ValidationPlugin with {{{neutralResult.ValidationPluginOptions.Plugin}}}");
                     }
                     throw new Exception("missing ValidationPluginOptions");
                 }
                 if (result.StorePluginOptions == null)
                 {
                     throw new Exception("missing StorePluginOptions");
                 }
                 if (result.CsrPluginOptions == null && result.TargetPluginOptions.Name != CsrOptions.NameLabel)
                 {
                     throw new Exception("missing CsrPluginOptions");
                 }
                 if (result.InstallationPluginOptions == null)
                 {
                     throw new Exception("missing InstallationPluginOptions");
                 }
                 if (string.IsNullOrEmpty(result.LastFriendlyName))
                 {
                     result.LastFriendlyName = result.FriendlyName;
                 }
                 if (result.History == null)
                 {
                     result.History = new List <RenewResult>();
                 }
                 list.Add(result);
             }
             catch (Exception ex)
             {
                 _log.Error("Unable to read renewal {renewal}: {reason}", rj.Name, ex.Message);
             }
         }
         _renewalsCache = list.OrderBy(x => _dueDateService.DueDate(x)).ToList();
     }
     return(_renewalsCache);
 }