Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dnoble/Example-of-calling-endpoints-we-do-not-support' …
Browse files Browse the repository at this point in the history
…into develop

Conflicts:
	splunk-sdk-csharp-pcl.sln
	src/Splunk.Client/Splunk/Client/Service.cs
  • Loading branch information
Shakeel Mohamed committed Feb 17, 2015
2 parents 29cd6bc + cdeadfa commit f71e310
Show file tree
Hide file tree
Showing 31 changed files with 824 additions and 722 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Expand Up @@ -110,3 +110,11 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML

# Ignore accidentally tracked files
*.pdf
*.cd

# png files in root dir
/*.png

6 changes: 6 additions & 0 deletions examples/Get-SplunkDataInput/App.config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
109 changes: 109 additions & 0 deletions examples/Get-SplunkDataInput/Program.cs
@@ -0,0 +1,109 @@
/*
* Copyright 2013 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.Examples.GetSplunkDataInputs
{
using Splunk.Client;
using Splunk.Client.Helpers;
using System;
using System.Dynamic;
using System.Net.Http;
using System.Threading.Tasks;

/// <summary>
/// An example program to access the set of data inputs running on a Splunk instance.
/// </summary>
public class Program
{
/// <summary>
/// Main.
/// </summary>
/// <param name="args">The arguments.</param>
static void Main(string[] args)
{
var ns = new Namespace(user: args[0], app: args[1]);

using (var service = new Service(SdkHelper.Splunk.Scheme, SdkHelper.Splunk.Host, SdkHelper.Splunk.Port, ns))
{
Console.WriteLine("Splunk management port: {0}", service.Context);
Console.WriteLine("Namespace: {0}", service.Namespace);
Run(service).Wait();
}

Console.Write("Press enter to exit: ");
Console.ReadLine();
}

/// <summary>
/// Runs the specified service.
/// </summary>
/// <param name="service">The service.</param>
/// <returns>a task</returns>
static async Task Run(Service service)
{
try
{
await service.LogOnAsync(SdkHelper.Splunk.Username, SdkHelper.Splunk.Password);

Console.WriteLine("Data inputs:");

var collection = service.CreateEntityCollection("data", "inputs", "all");
int count = 0;
await collection.GetAllAsync();

//// TODO:
//// [X] 1. Make Entity<TResource>.Content public (It still must be assigned to a dynamic variable before use.)
//// [X] 2. Write a convenience method on the Service object for getting an EntityCollection on some path: service.CreateEntityCollection("data", "inputs", "all")
//// [X] 3. Write a convenience method on the Service object for getting an Entity on some path: something like service.CreateEntity("data", "inputs", "tcp", "ssl")
//// [X] 4. Add service.CreateEntityCollectionCollection for completeness.
//// [X] 5. Revert the access change to EntityCollection<Entity<TResource>, TResource>(service, new ResourceName("data", "inputs", "all")) because (2) makes it unnecessary.
//// [X] 6. Revert the access change to Entity<TResource>(service, new ResourceName("data", "inputs", "all")) because (3) makes it unnecessary.
//// [ ] 7. Enliven the example.
//// [ ] 8. Fill holes in IService (holes are unrelated to this exercise)

foreach (var entity in collection)
{
Console.WriteLine("{0:D5}. {1}", ++count, entity.Id);
dynamic dataInput = entity.Content;

Console.WriteLine(" Disabled: {0}", dataInput.Disabled);
Console.WriteLine(" Index: {0}", dataInput.Index);
Console.WriteLine(" Type: {0}", dataInput.Eai.Type);

if (dataInput.Disabled == "0")
{
try
{
// Restart...
await entity.SendAsync(HttpMethod.Post, "disable");
await entity.SendAsync(HttpMethod.Post, "enable");
}
catch (Exception e)
{
Console.WriteLine("Could not restart '{0}': {1}", entity.Id, e.Message);
}
}
}

await service.LogOffAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
17 changes: 17 additions & 0 deletions examples/Get-SplunkDataInput/Properties/AssemblyInfo.cs
@@ -0,0 +1,17 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Get-SplunkDataInput")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Splunk, Inc")]
[assembly: AssemblyProduct("Splunk SDK for C#")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("Splunk®")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("2.1.2.0")]
[assembly: AssemblyFileVersion("2.1.2.0")]

[assembly: ComVisible(false)]
6 changes: 6 additions & 0 deletions examples/Get-SplunkDataInput/Settings.StyleCop
@@ -0,0 +1,6 @@
<StyleCopSettings Version="105">
<GlobalSettings>
<StringProperty Name="LinkedSettingsFile">..\..\splunk-sdk-csharp-pcl.StyleCop</StringProperty>
<StringProperty Name="MergeSettingsFiles">Linked</StringProperty>
</GlobalSettings>
</StyleCopSettings>
70 changes: 70 additions & 0 deletions examples/Get-SplunkDataInput/get-splunk-data-inputs.csproj
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<ProjectGuid>{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}</ProjectGuid>
<AssemblyName>Get-SplunkDataInput</AssemblyName>
<OutputType>Exe</OutputType>
<Platform>AnyCPU</Platform>
<TargetFrameworkProfile />
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<AppDesignerFolder>Properties</AppDesignerFolder>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<ErrorReport>prompt</ErrorReport>
<FileAlignment>512</FileAlignment>
<RootNamespace />
<RunCodeAnalysis>False</RunCodeAnalysis>
<StyleCopEnabled Condition=" '$(StyleCopEnabled)' == '' ">False</StyleCopEnabled>
<StyleCopTreatErrorsAsWarnings>False</StyleCopTreatErrorsAsWarnings>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Splunk.Client.Helpers\Splunk.Client.Helpers.csproj">
<Project>{0b0c43f2-ea9c-45ee-a8f7-7a2916dd1717}</Project>
<Name>Splunk.Client.Helpers</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Splunk.Client\Splunk.Client.csproj">
<Project>{de65f0d5-7753-483e-8933-e6db22350f04}</Project>
<Name>Splunk.Client</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Empty file modified examples/github-commits/github-commits/github-commits.sh 100755 → 100644
Empty file.
7 changes: 7 additions & 0 deletions splunk-sdk-csharp-pcl.sln
Expand Up @@ -62,6 +62,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modular_input", "Modular_in
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "github-commits", "examples\github-commits\github-commits.csproj", "{35B8F1F4-E7F6-4988-8737-5290BF787199}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "get-splunk-data-inputs", "examples\Get-SplunkDataInput\get-splunk-data-inputs.csproj", "{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -150,6 +152,10 @@ Global
{35B8F1F4-E7F6-4988-8737-5290BF787199}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35B8F1F4-E7F6-4988-8737-5290BF787199}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35B8F1F4-E7F6-4988-8737-5290BF787199}.Release|Any CPU.Build.0 = Release|Any CPU
{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{443CB5CF-1086-40E3-B60C-6B35D7DFCF90}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -177,5 +183,6 @@ Global
{8992B3EF-E2E6-4E5D-B488-986F2A5FE713} = {5A4BA6B9-305F-4E0D-A11A-35AE005C678A}
{97C3A57B-8ED8-42A7-9FB3-8120BA265FCC} = {5A4BA6B9-305F-4E0D-A11A-35AE005C678A}
{35B8F1F4-E7F6-4988-8737-5290BF787199} = {AE98AECD-D551-4DF7-84FA-A6C76B930639}
{443CB5CF-1086-40E3-B60C-6B35D7DFCF90} = {5A4BA6B9-305F-4E0D-A11A-35AE005C678A}
EndGlobalSection
EndGlobal
23 changes: 18 additions & 5 deletions src/Splunk.Client.Helpers/Splunk/Client/Helpers/SdkHelper.cs
Expand Up @@ -34,6 +34,13 @@ public static class SdkHelper
/// </summary>
static SdkHelper()
{
//// TODO: Use WebRequestHandler.ServerCertificateValidationCallback instead
//// 1. Instantiate a WebRequestHandler
//// 2. Set its ServerCertificateValidationCallback
//// 3. Instantiate a Splunk.Client.Context with the WebRequestHandler

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
Expand Down Expand Up @@ -84,20 +91,18 @@ public static async Task ThrowsAsync<T>(Func<Task> testCode)
await testCode();
return;
}
catch (T expectedException)
catch (T)
{
return;
}
catch (Exception unexpectedException)
{
Assert.True(false, string.Format("Expected {0}; found exception {1}: {2}", typeof(T).FullName, unexpectedException.GetType().FullName, unexpectedException.Message));
}

Assert.True(false, string.Format("Expected exception {0}, but not exception raised.", typeof(T)));
}




#region Privates/internals

static readonly SplunkRC splunk;
Expand All @@ -119,7 +124,15 @@ public sealed class SplunkRC
/// </param>
internal SplunkRC(string path)
{
var reader = new StreamReader(path);
StreamReader reader = null;
try
{
reader = new StreamReader(path);
}
catch (FileNotFoundException)
{
return;
}

List<string> argList = new List<string>(4);
string line;
Expand Down
6 changes: 5 additions & 1 deletion src/Splunk.Client/Splunk.Client.csproj
Expand Up @@ -126,7 +126,11 @@
</Choose>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Splunk\**\*" />
<Compile Include="Splunk\Client\*.cs" />
<None Include="Endpoints.cd" />
<None Include="Search streams.cd" />
<None Include="Service class.cd" />
<None Include="Entities, entity collections, and resources.cd" />
<None Include="packages.config" />
<None Include="Splunk.Client.nuspec" />
</ItemGroup>
Expand Down
49 changes: 35 additions & 14 deletions src/Splunk.Client/Splunk/Client/AtomFeed.cs
Expand Up @@ -47,6 +47,15 @@ namespace Splunk.Client
/// </remarks>
public sealed class AtomFeed
{

#region Privates/internals

static readonly ReadOnlyCollection<AtomEntry> emptyAtomEntryCollection = new ReadOnlyCollection<AtomEntry>(new List<AtomEntry>());
static readonly ReadOnlyDictionary<string, Uri> emptyLinksDictionary = new ReadOnlyDictionary<string, Uri>(new Dictionary<string, Uri>());
static readonly ReadOnlyCollection<Message> emptyMessageCollection = new ReadOnlyCollection<Message>(new List<Message>());

#endregion

#region Constructors

/// <summary>
Expand Down Expand Up @@ -194,6 +203,7 @@ public async Task ReadXmlAsync(XmlReader reader)
List<AtomEntry> entries = null;
Dictionary<string, Uri> links = null;
List<Message> messages = null;


await reader.ReadAsync().ConfigureAwait(false);

Expand Down Expand Up @@ -320,20 +330,31 @@ public async Task ReadXmlAsync(XmlReader reader)
reader.EnsureMarkup(XmlNodeType.EndElement, documentElementName);
await reader.ReadAsync().ConfigureAwait(false);

if (entries != null)
{
this.Entries = new ReadOnlyCollection<AtomEntry>(entries);
}

if (links != null)
{
this.Links = new ReadOnlyDictionary<string, Uri>(links);
}

if (messages != null)
{
this.Messages = new ReadOnlyCollection<Message>(messages);
}
this.Entries = entries == null ? emptyAtomEntryCollection : new ReadOnlyCollection<AtomEntry>(entries);
this.Links = links == null ? emptyLinksDictionary : new ReadOnlyDictionary<string, Uri>(links);
this.Messages = messages == null ? emptyMessageCollection : new ReadOnlyCollection<Message>(messages);

//if (entries == null)
//{
// entries = new List<AtomEntry>();
// this.Entries = emptyAtomEntryCollection;
//}
//else
//{
// this.Entries = new ReadOnlyCollection<AtomEntry>(entries);
//}

//if (links == null)
//{
// links = new Dictionary<string, Uri>();
//}
//this.Links = new ReadOnlyDictionary<string, Uri>(links);

//if (messages == null)
//{
// messages = new List<Message>();
//}
//this.Messages = new ReadOnlyCollection<Message>(messages);
}

/// <summary>
Expand Down

0 comments on commit f71e310

Please sign in to comment.