Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously creates an entity from a Splunk atom feed
        /// <see cref= "Response"/>.
        /// </summary>
        /// <typeparam name="TEntity">
        /// Type of the entity.
        /// </typeparam>
        /// <param name="context">
        /// An object representing a Splunk server session.
        /// </param>
        /// <param name="response">
        /// An object representing a Splunk atom feed response.
        /// </param>
        /// <returns>
        /// The <see cref="BaseEntity&lt;TResource&gt;"/> created.
        /// </returns>
        internal static async Task <TEntity> CreateAsync <TEntity>(Context context, Response response)
            where TEntity : BaseEntity <TResource>, new()
        {
            var     reader = response.XmlReader;
            TEntity resourceEndpoint;

            reader.Requires(await reader.MoveToDocumentElementAsync("feed", "entry").ConfigureAwait(false));

            if (reader.Name == "entry")
            {
                var entry = new AtomEntry();

                await entry.ReadXmlAsync(reader).ConfigureAwait(false);

                resourceEndpoint = new TEntity();
                resourceEndpoint.Initialize(context, entry, new Version(0, 0));
            }
            else
            {
                var feed = new AtomFeed();

                await feed.ReadXmlAsync(reader).ConfigureAwait(false);

                resourceEndpoint = new TEntity();
                resourceEndpoint.Initialize(context, feed);
            }

            return(resourceEndpoint);
        }
        public void CanReadFeed()
        {
            using (var stream = new FileStream(AtomFeedPath, FileMode.Open, FileAccess.Read))
            {
                var reader = XmlReader.Create(stream, XmlReaderSettings);
                var feed = new AtomFeed();

                feed.ReadXmlAsync(reader).Wait();
            }
        }
Ejemplo n.º 3
0
        public void CanReadFeed()
        {
            using (var stream = new FileStream(AtomFeedPath, FileMode.Open, FileAccess.Read))
            {
                var reader = XmlReader.Create(stream, XmlReaderSettings);
                var feed   = new AtomFeed();

                feed.ReadXmlAsync(reader).Wait();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Asynchronously updates the content of the current
        /// <see cref="BaseEntity&lt;TResource&gt;"/>.
        /// </summary>
        /// <param name="response">
        /// A Splunk atom feed <see cref="Response"/>.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the operation.
        /// </returns>
        protected internal virtual async Task <bool> ReconstructSnapshotAsync(Response response)
        {
            Contract.Requires <ArgumentNullException>(response != null);
            var feed = new AtomFeed();

            await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);

            this.CreateSnapshot(feed);

            return(true);
        }
Ejemplo n.º 5
0
        protected async Task UpdateSnapshotAsync(Response response)
        {
            var feed = new AtomFeed();
            await feed.ReadXmlAsync(response.XmlReader);

            if (feed.Entries.Count != 1)
            {
                throw new InvalidDataException();  // TODO: Diagnostics
            }

            this.Snapshot = new EntitySnapshot(feed.Entries[0]);
        }
Ejemplo n.º 6
0
        public virtual async Task GetAsync()
        {
            using (Response response = await this.Context.GetAsync(this.Namespace, this.ResourceName, this.args))
            {
                await response.EnsureStatusCodeAsync(System.Net.HttpStatusCode.OK);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader);

                this.data = new DataCache(this.Context, feed);
            }
        }
Ejemplo n.º 7
0
        internal static async Task <TResource> CreateAsync <TResource>(Response response)
            where TResource : BaseResource, new()
        {
            var feed = new AtomFeed();

            await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);

            var resource = new TResource();

            resource.Initialize(feed);

            return(resource);
        }
Ejemplo n.º 8
0
        /// <inheritdoc/>
        public virtual async Task <ServerInfo> GetInfoAsync()
        {
            using (var response = await this.Context.GetAsync(this.Namespace, Info).ConfigureAwait(false))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK).ConfigureAwait(false);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);

                var info = new ServerInfo(feed);

                return(info);
            }
        }
Ejemplo n.º 9
0
        /// <inheritdoc/>
        public virtual async Task <ServerSettings> UpdateSettingsAsync(ServerSettingValues values)
        {
            using (var response = await this.Context.PostAsync(this.Namespace, Settings, values).ConfigureAwait(false))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK).ConfigureAwait(false);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);

                var settings = new ServerSettings(feed);

                return(settings);
            }
        }
Ejemplo n.º 10
0
        /// <inheritdoc/>
        public virtual async Task <ReadOnlyCollection <string> > GetCapabilitiesAsync()
        {
            using (var response = await this.Context.GetAsync(this.Namespace, AuthorizationCapabilities).ConfigureAwait(false))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK).ConfigureAwait(false);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);

                if (feed.Entries.Count != 1)
                {
                    throw new InvalidDataException(); // TODO: Diagnostics : cardinality violation
                }

                var entry = feed.Entries[0];

                ReadOnlyCollection <dynamic> capabilities = entry.Content["Capabilities"];
                return(new ReadOnlyCollection <string>(capabilities.Cast <string>().ToList()));
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Asynchronously retrieves the list of all Splunk system capabilities.
        /// </summary>
        /// <returns>
        /// An object representing the list of all Splunk system capabilities.
        /// </returns>
        /// <remarks>
        /// This method uses the <a href="http://goo.gl/kgTKvM">GET 
        /// authorization/capabilities</a> endpoint to construct a list of all 
        /// Splunk system capabilities.
        /// </remarks>
        public async Task<dynamic> GetCapabilitiesAsync()
        {
            using (var response = await this.Context.GetAsync(this.Namespace, AuthorizationCapabilities))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK);
                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader);

                if (feed.Entries.Count != 1)
                {
                    throw new InvalidDataException(); // TODO: Diagnostics
                }

                var entry = feed.Entries[0];
                dynamic capabilities = entry.Content.Capabilities; // TODO: Static type (?)

                return capabilities;
            }
        }
Ejemplo n.º 12
0
        /// <inheritdoc/>
        public virtual async Task<ServerInfo> GetInfoAsync()
        {
            using (var response = await this.Context.GetAsync(this.Namespace, Info).ConfigureAwait(false))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK).ConfigureAwait(false);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);
                var info = new ServerInfo(feed);
                
                return info;
            }
        }
Ejemplo n.º 13
0
        /// <inheritdoc/>
        public virtual async Task<ServerSettings> UpdateSettingsAsync(ServerSettingValues values)
        {
            using (var response = await this.Context.PostAsync(this.Namespace, Settings, values).ConfigureAwait(false))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK).ConfigureAwait(false);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader).ConfigureAwait(false);
                var settings = new ServerSettings(feed);

                return settings;
            }
        }
Ejemplo n.º 14
0
        /// <inheritdoc/>
        public virtual async Task<ServerSettings> GetSettingsAsync()
        {
            using (var response = await this.Context.GetAsync(this.Namespace, Settings))
            {
                await response.EnsureStatusCodeAsync(HttpStatusCode.OK);

                var feed = new AtomFeed();
                await feed.ReadXmlAsync(response.XmlReader);
                var settings = new ServerSettings(feed);

                return settings;
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Refreshes the cached state of the current <see cref="Entity<TEntity>"/>.
        /// </summary>
        public override async Task GetAsync()
        {
            //// TODO: This retry logic is for jobs. Parmeterize it and move it into the Job class

            // FJR: I assume the retry logic is for jobs, since nothing else requires this. I suggest moving it
            // into Job. Also, it's insufficient. If you're just trying to get some state, this will do it, but
            // as of Splunk 6, getting a 200 and content back does not imply you have all the fields. For pivot
            // support, they're now shoving fields in as they become ready, so you have to wait until the dispatchState
            // field of the Atom entry reaches a certain point.

            RequestException requestException = null;

            for (int i = 3; i > 0; --i)
            {
                try
                {
                    //// Guarantee: unique result because entities have specific namespaces

                    using (var response = await this.Context.GetAsync(this.Namespace, this.ResourceName))
                    {
                        //// TODO: Use Response.EnsureStatusCode. Is it true that gets always return HttpStatusCode.OK?

                        if (response.Message.StatusCode == HttpStatusCode.NoContent)
                        {
                            throw new RequestException(response.Message, new Message(MessageType.Warning, string.Format("Resource '{0}/{1}' is not ready.", this.Namespace, this.ResourceName)));
                        }

                        if (!response.Message.IsSuccessStatusCode)
                        {
                            throw new RequestException(response.Message, await Message.ReadMessagesAsync(response.XmlReader));
                        }

                        var reader = response.XmlReader;
                        await reader.ReadAsync();

                        if (reader.NodeType == XmlNodeType.XmlDeclaration)
                        {
                            await response.XmlReader.ReadAsync();
                        }

                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            throw new InvalidDataException(); // TODO: Diagnostics
                        }

                        AtomEntry entry;

                        if (reader.Name == "feed")
                        {
                            AtomFeed feed = new AtomFeed();

                            await feed.ReadXmlAsync(reader);

                            int count = feed.Entries.Count;

                            foreach (var feedEntry in feed.Entries)
                            {
                                string id = feedEntry.Title;
                                id.Trim();
                            }

                            if (feed.Entries.Count != 1)
                            {
                                throw new InvalidDataException(); // TODO: Diagnostics
                            }

                            entry = feed.Entries[0];
                        }
                        else
                        {
                            entry = new AtomEntry();
                            await entry.ReadXmlAsync(reader);
                        }

                        this.Snapshot = new EntitySnapshot(entry);
                    }

                    return;
                }
                catch (RequestException e)
                {
                    if (e.StatusCode != System.Net.HttpStatusCode.NoContent)
                    {
                        throw;
                    }
                    requestException = e;
                }
                await Task.Delay(500);
            }

            throw requestException;
        }