Exemplo n.º 1
0
        /// <summary>
        /// Gets the name of a parameter which accepts a resource URL.
        /// </summary>
        /// <param name="type">The ODCM type of the object that will be referenced</param>
        /// <returns>The name of the parameter which accepts a resource URL.</returns>
        public static string GetResourceUrlParameterName(this OdcmType type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(ODataTypeUtils.GetReferenceUrlParameterName(type.FullName));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Processes a result object.
        /// </summary>
        /// <param name="obj">The result object to process</param>
        internal void ProcessResultObject(object obj)
        {
            if (obj is PSObject psObj)
            {
                // Track the properties to hide
                ICollection <string> propertiesToHide = new List <string>();

                // Add alias for the "id" property if it exists on the result object
                string objectId = psObj.Properties[ODataConstants.SearchResultProperties.Id]?.Value as string;
                if (objectId != null)
                {
                    // Get the name of the alias property
                    string idPropertyName = this.GetResourceIdPropertyName();
                    if (idPropertyName != null)
                    {
                        // Create the alias
                        psObj.Properties.Add(new PSAliasProperty(idPropertyName, ODataConstants.SearchResultProperties.Id));

                        // Hide this property
                        propertiesToHide.Add(idPropertyName);
                    }
                }

                // ID parameters for parent resources
                foreach (var entry in this.GetIdParameterNamesAndValues())
                {
                    // Create the property
                    string propertyName  = entry.Key;
                    string propertyValue = entry.Value;
                    psObj.Properties.Add(new PSNoteProperty(propertyName, propertyValue));

                    // Hide this property
                    propertiesToHide.Add(propertyName);
                }

                // Type cast parameters for parent resources
                foreach (var entry in this.GetTypeCastParametersAndValues())
                {
                    // Add a property to store the type name
                    string propertyName  = entry.Key;
                    string propertyValue = entry.Value;
                    psObj.Properties.Add(new PSNoteProperty(propertyName, propertyValue));

                    // Hide this property
                    propertiesToHide.Add(propertyName);
                }

                // Get the type name without the leading "#"
                string typeName = (psObj.Properties[ODataConstants.SearchResultProperties.ODataType]?.Value as string)?.TrimStart('#');
                if (string.IsNullOrEmpty(typeName))
                {
                    typeName = this.GetType().GetCustomAttribute <ODataTypeAttribute>(false)?.TypeFullName;
                }

                if (typeName != null)
                {
                    // Add the type name to the list of PowerShell type names for this object
                    psObj.TypeNames.Insert(0, typeName);

                    // Add the type cast property
                    string typeCastPropertyName = this.GetResourceTypePropertyName();
                    psObj.Properties.Add(new PSNoteProperty(typeCastPropertyName, typeName));

                    // Hide the type cast property
                    propertiesToHide.Add(typeCastPropertyName);

                    // If this cmdlet retrieves resources that can be referenced, add a property to represent the URL which can be used to retrieve this object
                    if (ReferencePathGenerator.TryGetFromCache(this.GetCmdletNoun(), out ReferencePathGenerator pathGenerator))
                    {
                        // Get the full resource URL
                        string referencePath = pathGenerator.GenerateResourcePath(this, objectId);
                        string referenceUrl  = this.BuildUrl(referencePath);

                        // Add this reference URL as a property
                        string referenceUrlPropertyName = ODataTypeUtils.GetReferenceUrlParameterName(typeName);
                        psObj.Properties.Add(new PSNoteProperty(referenceUrlPropertyName, referenceUrl));

                        // Hide this property
                        propertiesToHide.Add(referenceUrlPropertyName);
                    }
                }

                // Hide the properties we have been tracking
                psObj.SetDefaultDisplayProperties(prop => !propertiesToHide.Contains(prop.Name));
            }
        }