示例#1
0
        /// <summary>
        /// Synchronizes the specified <see cref="AssetDescriptor"/> details with a
        /// <c>Snipt.It</c> asset or creates one if not asset matching the specified
        /// serial exists.
        /// </summary>
        /// <param name="snipe">
        ///     The <see cref="SnipeItApi"/> instance used to access the <c>Snipt.It</c>
        ///     web API.
        /// </param>
        /// <param name="assetDetails">
        ///     The <see cref="AssetDescriptor"/> describing the asset's details.
        /// </param>
        /// <returns>
        ///     The resulting <c>Snipt.It</c> <see cref="Asset"/>.
        /// </returns>
        public static Asset SyncAsset(this SnipeItApi snipe, AssetDescriptor assetDetails)
        {
            // Validate parameters.
            if (snipe == null)
            {
                throw new ArgumentNullException("snipe");
            }
            if (assetDetails == null)
            {
                throw new ArgumentNullException("assetDetails");
            }

            Asset snipeAsset = snipe.AssetManager.FindOne(new SearchFilter {
                Search = assetDetails.Serial
            });
            var    manufacturer  = SnipeApiExtensions.SyncManufacturer(snipe, assetDetails.Manufacturer);
            string snipeCategory = SnipeApiExtensions.DefaultSnipeCategoriesMap[assetDetails.Category];
            var    category      = SnipeApiExtensions.SyncCategory(snipe, snipeCategory, "asset");
            var    model         = SnipeApiExtensions.SyncModel(snipe, manufacturer, category, assetDetails.Model, assetDetails.ModelNumber);

            if (snipeAsset == null)
            {
                snipeAsset =
                    new Asset()
                {
                    Name         = assetDetails.Name,
                    ModelNumber  = assetDetails.ModelNumber,
                    Manufacturer = manufacturer,
                    Category     = category,
                    Model        = model
                };

                var response = snipe.AssetManager.Create(snipeAsset);
                snipeAsset = snipe.AssetManager.FindOne(new SearchFilter {
                    Search = assetDetails.Serial
                });
            }
            else
            {
                snipeAsset.Name         = assetDetails.Name;
                snipeAsset.ModelNumber  = assetDetails.ModelNumber;
                snipeAsset.Manufacturer = manufacturer;
                snipeAsset.Category     = category;
                snipeAsset.Model        = model;

                var response = snipe.AssetManager.Update(snipeAsset);
            }

            return(snipeAsset);
        }
示例#2
0
        /// <summary>
        /// Synchronizes the specified <see cref="ComponentDescriptor"/> details with a
        /// <c>Snipt.It</c> component or creates one if no component matching the specified
        /// serial exists.
        /// </summary>
        /// <param name="snipe">
        ///     The <see cref="SnipeItApi"/> instance used to access the <c>Snipt.It</c>
        ///     web API.
        /// </param>
        /// <param name="component">
        ///     The <see cref="ComponentDescriptor"/> describing the component's details.
        /// </param>
        /// <returns>
        ///      The resulting <c>Snipt.It</c> <see cref="Component"/>.
        /// </returns>
        public static Component SyncComponent(this SnipeItApi snipe, ComponentDescriptor component)
        {
            // Validate parameters.
            if (snipe == null)
            {
                throw new ArgumentNullException("snipe");
            }
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
            if (String.IsNullOrWhiteSpace(component.Serial))
            {
                throw new ArgumentNullException("component.Serial");
            }

            string    snipeCategory  = SnipeApiExtensions.DefaultSnipeCategoriesMap[component.Category];
            Component snipeComponent = snipe.ComponentManager.FindAll(new SearchFilter {
                Search = component.Serial, Limit = 1
            })?.Rows.FirstOrDefault();
            var manufacturer = SnipeApiExtensions.SyncManufacturer(snipe, component.Manufacturer);
            var category     = SnipeApiExtensions.SyncCategory(snipe, snipeCategory, "component");

            if (snipeComponent == null)
            {
                snipeComponent = new Component()
                {
                    Category = category,
                    Name     = component.Name,
                    Serial   = component.Serial,
                    Quantity = 1
                };
                var response = snipe.ComponentManager.Create(snipeComponent);
                snipeComponent = snipe.ComponentManager.FindAll(new SearchFilter {
                    Search = component.Serial, Limit = 1
                })?.Rows.FirstOrDefault();
            }

            return(snipeComponent);
        }