/* * Copyright 2012 Splunk, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"): you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ namespace Splunk { using System.Collections.Generic; /// /// Represents the base of all splunk entity and entity collection classes. /// public abstract class Resource { /// /// The dictionary of actions allowed on this resource /// private Dictionary actions; /// /// The resource title. /// private string title; /// /// Initializes a new instance of the class. /// /// The service /// The path of the resource. public Resource(Service service, string path) { this.Path = service.Fullpath(path); this.PartialPath = path; this.Service = service; this.RefreshArgs = new Args("count", "-1"); this.MaybeValid = false; } /// /// Initializes a new instance of the class, /// adding optional arguments for namespace and other endpoint /// arguments. /// /// The service /// The path of this resource /// The variable arguments public Resource(Service service, string path, Args args) { this.Service = service; /* Pull out namespace items (app, owner, sharing) from the args, and * then use to create the full path. */ Args clonedArgs = new Args(args); Args splunkNamespace = new Args(); if (args.ContainsKey("app")) { splunkNamespace.AlternateAdd("app", args["app"].ToString()); clonedArgs.Remove("app"); } if (args.ContainsKey("owner")) { splunkNamespace.AlternateAdd("owner", args["owner"].ToString()); clonedArgs.Remove("owner"); } if (args.ContainsKey("sharing")) { splunkNamespace.AlternateAdd( "sharing", args["sharing"].ToString()); clonedArgs.Remove("sharing"); } if (!clonedArgs.ContainsKey("count")) { clonedArgs.AlternateAdd("count", "-1"); } this.RefreshArgs = clonedArgs; this.Path = service.Fullpath( path, splunkNamespace.Count == 0 ? null : splunkNamespace); this.MaybeValid = false; } /// /// Gets or sets full path of this resource. /// public string Path { get; set; } /// /// Gets or sets the partial path of this resource. /// private string PartialPath { get; set; } /// /// Gets or sets the service this resource is found on. /// public Service Service { get; set; } /// /// Gets or sets the refresh args for this resources. /// public Args RefreshArgs { get; set; } /// /// Gets or sets a value indicating whether this resource is clean or /// dirty. When dirty, we refresh the resource before returning /// any data contained therein. /// public bool MaybeValid { get; set; } /// /// Gets an up-to-date list of actions available for this resource. /// /// Available actions on this endpoint public Dictionary Actions() { return this.Validate().actions; } /// /// Gets the name of this resource /// public virtual string Name { get { return this.Title; } } /// /// Gets or sets the value of the title of this resource. Note that /// getting the property may refresh the local resource if dirty from /// the server. /// public string Title { get { return this.Validate().title; } set { this.title = value; } } /// /// Marks the local state of this resource as no longer current. /// /// The resource public Resource Invalidate() { this.MaybeValid = false; return this; } /// /// Loads the state of this resource from a given Atom object. /// /// The AtomObject to load /// The Resource public Resource Load(AtomObject value) { if (value == null) { this.title = "title"; } else { this.actions = value.Links; this.title = value.Title; } this.MaybeValid = true; return this; } /// /// Refreshes the local state of this resource. /// /// The Resource public abstract Resource Refresh(); /// /// Ensures that the local state of the resource is current, /// calling object specific Refresh method if necessary. /// /// The Resource public virtual Resource Validate() { if (!this.MaybeValid) { this.Refresh(); } return this; } } }