/// <summary> /// Validates the results of a manual resolution. /// </summary> /// <param name="values">The values to validate.</param> /// <returns>If the validation completed successfully, true. Otherwise, false.</returns> protected override bool VerifyManualResults(NotificationTrigger[] values) { var result = true; if (base.VerifyManualResults(values)) { if (HasParameter(nameof(SubId))) { var actualIds = values.Select(v => v.SubId); foreach (var subId in SubId) { if (!actualIds.Contains(subId)) { result = false; WriteInvalidOperation($"Could not resolve a {IObjectExtensions.GetTypeDescription(typeof(NotificationTrigger))} with SubID {subId}."); } } } } else { result = false; } return(result); }
/// <summary> /// Updates the previous and current progress records for the current input object for scenarios in which a variable was piped into one or more cmdlets. /// </summary> protected void UpdatePreviousAndCurrentVariableProgressOperations(bool writeObject = true, string currentOperation = null) { //PrtgOperationCmdlets use TrySetPreviousOperation, so they won't be caught by the fact PipelineContainsOperation would be true for them //(causing them to SetPreviousOperation, which would fail) if (ProgressManager.PipelineContainsOperation == false || ProgressManager.PreviousOperationDestroyed || ProgressManager.PipeFromPrtgCmdletPostProcessMode) { if (ProgressManager.PipelineIsProgressPure) { ProgressManager.CurrentRecord.Activity = $"PRTG {TypeDescription} Search"; var obj = ProgressManager.CmdletPipeline.Current; var prtgObj = obj as IObject; if (prtgObj != null) { ProgressManager.InitialDescription = $"Processing {prtgObj.GetTypeDescription().ToLower()}"; } else { ProgressManager.InitialDescription = $"Processing all {IObjectExtensions.GetTypeDescription(obj.GetType()).ToLower()}s"; } ProgressManager.CurrentRecord.CurrentOperation = currentOperation ?? $"Retrieving all {GetTypePlural()}"; ProgressManager.RemovePreviousOperation(); ProgressManager.UpdateRecordsProcessed(ProgressManager.CurrentRecord, prtgObj, writeObject); } } else { ProgressManager.SetPreviousOperation(currentOperation ?? $"Retrieving all {GetTypePlural()}"); } }
/// <summary> /// Validates the results of a manual resolution. /// </summary> /// <param name="values">The values to validate.</param> /// <returns>If the validation completed successfully, true. Otherwise, false.</returns> protected override bool VerifyManualResults(TValue[] values) { var result = true; if (base.VerifyManualResults(values)) { if (HasParameter(nameof(Id))) { var actualIds = values.Select(v => v.Id.Value); foreach (var id in Id) { if (!actualIds.Contains(id)) { result = false; WriteInvalidOperation($"Could not resolve a {IObjectExtensions.GetTypeDescription(typeof(TValue))} with ID {id}."); } } } } else { result = false; } return(result); }
/// <summary> /// Validates the results of a manual resolution. /// </summary> /// <param name="values">The values to validate.</param> /// <returns>If the validation completed successfully, true. Otherwise, false.</returns> protected virtual bool VerifyManualResults(TValue[] values) { if (values.Length == 0) { WriteInvalidOperation($"Failed to resolve any {IObjectExtensions.GetTypeDescription(typeof(TValue))} objects. Could not create a {typeof(TNode).Name}."); return(false); } return(true); }
/// <summary> /// Display the initial progress message for the first cmdlet in a chain.<para/> /// If the cmdlet is streaming results, will display "detecting total number of items" and return the total number of items that will be retrieved. /// </summary> /// <returns>If the cmdlet is streaming, the total number of objects that will be retrieved. Otherwise, -1.</returns> protected override int DisplayFirstInChainMessage() { if (StreamProvider.StreamResults) { ProgressManager.WriteProgress($"PRTG {IObjectExtensions.GetTypeDescription(typeof(TObject))} Search", "Detecting total number of items"); StreamProvider.StreamCount = client.GetTotalObjects(content); return(StreamProvider.StreamCount.Value); } return(base.DisplayFirstInChainMessage()); }
private List <TNode> GetNodes <TNode, TObject>( Property property, object value, Func <Property, object, List <TObject> > getResults, Func <TObject, IEnumerable <PrtgNode>, TNode> getNode) { var results = getResults(property, value); if (results.Count == 0) { throw new InvalidOperationException($"Failed to resolve any {IObjectExtensions.GetTypeDescription(typeof(TObject))} objects for query {property} = {value}."); } return(results.Select(r => getNode(r, null)).ToList()); }
public IEnumerable <TRet> StreamResultsWithProgress <TRet>(TParam parameters, int?count, Func <IEnumerable <TRet> > getFiltered = null) { cmdlet.ProgressManager.Scenario = ProgressScenario.StreamProgress; if (!cmdlet.ProgressManager.WatchStream) { cmdlet.ProgressManager.WriteProgress($"PRTG {IObjectExtensions.GetTypeDescription(typeof(TObject))} Search", "Detecting total number of items"); StreamCount = cmdlet.GetStreamTotalObjects(parameters); SetTotalExist(StreamCount.Value); } else { cmdlet.ProgressManager.WriteProgress($"PRTG {IObjectExtensions.GetTypeDescription(typeof(TObject))} Watcher", "Waiting for first event"); StreamCount = 1000; } IEnumerable <TRet> records; //Normally if a filter has been specified PrtgAPI won't stream, and so the custom parameters are not necessary. //However, if a cmdlet has specified it wants to force a stream, we apply our filters and use our custom parameters. if (ForceStream) { records = getFiltered == null?StreamRecords <TRet>(parameters, count) : getFiltered(); } else { records = StreamRecords <TRet>(parameters, count); } if (StreamCount > streamThreshold || cmdlet.ProgressManager.WatchStream) { //We'll be replacing this progress record, so just null it out via a call to CompleteProgress() //We strategically set the TotalRecords AFTER calling this method, to avoid CompleteProgress truly completing the record cmdlet.ProgressManager.CompleteProgress(); cmdlet.SetObjectSearchProgress(ProcessingOperation.Retrieving); cmdlet.ProgressManager.TotalRecords = StreamCount; } else //We won't be showing progress, so complete this record { cmdlet.ProgressManager.TotalRecords = StreamCount; cmdlet.ProgressManager.CompleteProgress(); } return(records); }
public static T SingleObject <T>(this List <T> source, object value, string property = "ID") where T : IObject { if (source.Count == 1) { return(source.Single()); } var desc = IObjectExtensions.GetTypeDescription(typeof(T)); if (source.Count == 0) { throw new InvalidOperationException($"Failed to retrieve {desc.ToLower()} with {property} '{value}': {desc} does not exist"); } var str = source.Select(s => $"{s} ({s.GetId()})"); throw new InvalidOperationException($"Failed to retrieve {desc.ToLower()} with {property} '{value}': Multiple {desc.ToLower()}s were returned: " + string.Join(", ", str)); }
public void OnLevelBegin(ITreeValue value, PrtgNodeType type, int depth) { var typeDescription = IObjectExtensions.GetTypeDescription(value.GetType()); var str = typeDescription.ToLower(); if (value.Id == WellKnownId.Root) { ProgressManager.InitialDescription = "Processing children of 'Root' Group"; } else { ProgressManager.InitialDescription = $"Processing children of {str} '{value}'"; if (value is IPrtgObject) { ProgressManager.InitialDescription += $" (ID: {value.Id})"; } } var activity = depth == 1 ? "PRTG Tree Search" : $"PRTG {typeDescription} Tree Search"; ProgressManager.WriteProgress(activity, $"Retrieving children of {str} '{value}'"); }
protected void Object_GetSingle_Throws_WhenMultipleObjectsReturned(Func <PrtgClient, TObject> getObject) { var client = Initialize_Client(GetResponse(new[] { GetItem(), GetItem() })); AssertEx.Throws <InvalidOperationException>(() => getObject(client), $"Multiple {IObjectExtensions.GetTypeDescription(typeof(TObject)).ToLower()}s were returned"); }
protected void Object_GetSingle_Throws_WhenNoObjectReturned(Func <PrtgClient, TObject> getObject) { var client = Initialize_Client(GetResponse(new TItem[] { })); AssertEx.Throws <InvalidOperationException>(() => getObject(client), $"{IObjectExtensions.GetTypeDescription(typeof(TObject))} does not exist"); }
/// <summary> /// Initializes a new instance of the <see cref="PrtgObjectCmdlet{T}"/> class. /// </summary> protected PrtgObjectCmdlet() : base(IObjectExtensions.GetTypeDescription(typeof(T))) { }
internal void DisplayResolutionError(Type type, int retriesRemaining) { var typeName = IObjectExtensions.GetTypeDescription(type).ToLower(); WriteWarning($"'{MyInvocation.MyCommand}' failed to resolve {typeName}: object is still being created. Retries remaining: {retriesRemaining}"); }