public PSCmdletInvoker GetInvoker(NewSensor newSensorCmdlet) { if (invoker == null && makeCmdlet != null) { invoker = new PSCmdletInvoker(newSensorCmdlet, makeCmdlet(), new Lazy <string>(() => QualifiedToUnqualifiedSetName(newSensorCmdlet.ParameterSetName)), valueFromPipeline); } return(invoker); }
public NewSensorDynamicParameterContainer(NewSensor newSensorCmdlet, PrtgClient client) { InitializeParameterCategories(); activeSensorType = new Lazy <NewSensorDynamicParameterCategory>(() => parameterCategories.Single(c => c.HasParameterSet(newSensorCmdlet))); this.newSensorCmdlet = newSensorCmdlet; this.client = client; Parameters = GetDynamicParameters(); }
internal static async Task <Sensor> CreateSensor(IWimdioApiClient client, Device device, bool isVirtual = false) { var guid = Guid.NewGuid(); var random = guid.ToString().Split('-').First(); var newSensor = new NewSensor { RemoteId = guid.ToString(), Name = $"Name {random}", Description = $"Description {random}", Unit = "ppm", Tseoi = 0, IsVirtual = isVirtual }; return(await client.CreateSensor(device.DevKey, newSensor)); }
internal void ValidateParameters(NewSensor newSensorCmdlet, NewSensorParameters parameters) { try { RequestParser.ValidateObjectParameters(parameters); } catch (InvalidOperationException ex) { var property = Regex.Replace(ex.Message, ".*[Pp]roperty '(.+?)'.+", "$1"); var candidates = new List <string>(); string singular; //Build a list of possible parameter values that could have been invoked if (pluralMap.TryGetValue(property, out singular)) { candidates.Add(singular); } if (alternateSet != null) { candidates.AddRange(alternateSet.Parameters.Where(p => p.OriginalName == property).Select(p => p.Name)); } //Filter the list to the parameter that was actually specified candidates = candidates.Where(c => newSensorCmdlet.MyInvocation.BoundParameters.ContainsKey(c)).ToList(); if (candidates.Count == 1) { var newMessage = ex.Message.Replace("Property", "Parameter").Replace($"'{property}'", $"'-{candidates[0]}'"); throw new InvalidOperationException(newMessage, ex); } else { throw; } } }
public void AddDynamicParameters(NewSensor newSensorCmdlet, RuntimeDefinedParameterDictionaryEx dictionary) { GetInvoker(newSensorCmdlet); //Force initialize the invoker for the parameter sets var parametersType = Type.GetEnumAttribute <TypeAttribute>(true); //Don't add parameters for types that don't have them if (parametersType.Class == null) { return; } AddParametersObjectDynamicParameters(dictionary); if (makeCmdlet != null) { AddCmdletDynamicParameters(newSensorCmdlet, dictionary); } AddSensorTypeDynamicParameter(dictionary); AddDestinationParameter(dictionary); }
public bool TryGetSensorTarget(NewSensor newSensorCmdlet, PrtgClient client, ref string propertyName, ref object propertyValue) { if (alternateSet != null) { var name = propertyName; var matchingParameter = alternateSet.Parameters.OfType <AlternateSensorTargetParameter>().FirstOrDefault(p => p.OriginalName == name); //If this is true, we are processing a sensor type that had an unbound parameter ("Services") that also had a replacement //sensor target parameter ("ServiceName"). Invoke Get-SensorTarget using the device the sensor will be created on, //the type of sensor we're creating and the value that was specified for the alternate target parameter (the specified ServiceName wildcards) if (matchingParameter != null) { var invoker = new PSCmdletInvoker( newSensorCmdlet, new GetSensorTarget(), new Lazy <string>(() => ParameterSet.Default), (c, b) => c.Device = (Device)b[nameof(GetSensorTarget.Device)] ); var boundParameters = new Dictionary <string, object>(); boundParameters[nameof(GetSensorTarget.Type)] = Type; boundParameters[nameof(GetSensorTarget.Name)] = newSensorCmdlet.MyInvocation.BoundParameters[matchingParameter.Name]; invoker.BindParameters(boundParameters); invoker.BeginProcessing(boundParameters); invoker.ProcessRecord(newSensorCmdlet.MyInvocation.BoundParameters); propertyName = matchingParameter.Name; propertyValue = invoker.Output; return(true); } } return(false); }
private void AddCmdletDynamicParameters(NewSensor newSensorCmdlet, RuntimeDefinedParameterDictionaryEx dictionary) { var properties = ReflectionCacheManager.Get(GetInvoker(newSensorCmdlet).CmdletToInvoke.GetType()).Properties .Where(p => p.GetAttribute <ParameterAttribute>() != null).ToList(); var highestPosition = GetHighestParameterPosition(dictionary); foreach (var property in properties) { var description = property.GetAttribute <DescriptionAttribute>(); var name = description?.Description ?? property.Property.Name; //Not cached so we can modify it var attributes = Attribute.GetCustomAttributes(property.Property, typeof(ParameterAttribute)).ToList(); foreach (ParameterAttribute attribute in attributes) { attribute.ParameterSetName = $"{Type}{attribute.ParameterSetName}"; if (attribute.Position >= 0 && highestPosition != null) { attribute.Position += highestPosition.Value + 1; } } var aliases = property.GetAttribute <AliasAttribute>(); if (aliases != null && !aliases.AliasNames.Contains(name)) { attributes.Add(aliases); } dictionary.AddOrMerge(name, property.Property.PropertyType, attributes.ToArray()); } }
public ParameterSetDescriptor GetParameterSet(NewSensor newSensorCmdlet) { GetInvoker(newSensorCmdlet); //Force initialize the invoker for the parameter sets return(ParameterSets.First(s => $"{Type}{s.Name}" == newSensorCmdlet.ParameterSetName)); }
/// <summary> /// Indicates whether this category contains the parameter set that is active in a given cmdlet. /// </summary> /// <param name="newSensorCmdlet">The cmdlet to check parameter sets against.</param> /// <returns>If any of the parameter sets in this category matched the cmdlet's parameter set, true. Otherwise, false.</returns> public bool HasParameterSet(NewSensor newSensorCmdlet) { GetInvoker(newSensorCmdlet); //Force initialize the invoker for the parameter sets return(ParameterSets.Any(s => newSensorCmdlet.ParameterSetName == $"{Type}{s.Name}")); }