Adding example code to show how to build a new service client from the ground up.

Updated the .gitignore file to do a better job of ignoring bin,obj, and package folders.

Partially implements blueprint initial-docs
Change-Id: Ia5225027f5ad2972facabbbe5458b814ef09b6be
This commit is contained in:
Wayne Foley 2014-05-01 10:45:20 -07:00
parent 33d59efcb6
commit dc3288ab8d
19 changed files with 605 additions and 9 deletions

11
.gitignore vendored
View File

@ -2,13 +2,8 @@
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################
/Openstack/Openstack/obj
/Openstack/Openstack.Common/obj
/Openstack/Openstack.Test/obj
/Openstack/Bin
*.user
*.suo
/Openstack/packages
/Openstack/Openstack/obj
/Openstack/Openstack.Common/obj
/Openstack/Openstack.Test/obj
packages/
obj/
bin/

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>

View File

@ -0,0 +1,78 @@
<?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>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CustomServiceClientExample</RootNamespace>
<AssemblyName>CustomServiceClientExample</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\Bin\Debug\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="OpenStack">
<HintPath>..\..\Bin\Debug\OpenStack.dll</HintPath>
</Reference>
<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="EchoPocoClient.cs" />
<Compile Include="EchoPocoClientFactory.cs" />
<Compile Include="EchoResponse.cs" />
<Compile Include="EchoRestClient.cs" />
<Compile Include="EchoRestClientFactory.cs" />
<Compile Include="EchoServiceClient.cs" />
<Compile Include="EchoServiceClientDefinition.cs" />
<Compile Include="EchoServiceRegistrar.cs" />
<Compile Include="IEchoPocoClient.cs" />
<Compile Include="IEchoPocoClientFactory.cs" />
<Compile Include="IEchoRestClient.cs" />
<Compile Include="IEchoRestClientFactory.cs" />
<Compile Include="IEchoServiceClient.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</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>

View File

@ -0,0 +1,41 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
internal class EchoPocoClient : IEchoPocoClient
{
internal IServiceLocator ServiceLocator;
public EchoPocoClient(IServiceLocator serviceLocator)
{
this.ServiceLocator = serviceLocator;
}
public async Task<EchoResponse> Echo(string message)
{
var restClient = this.ServiceLocator.Locate<IEchoRestClientFactory>().Create(this.ServiceLocator);
var resp = await restClient.Echo(message);
var payload = await resp.ReadContentAsStringAsync();
var obj = JObject.Parse(payload);
return new EchoResponse((string)obj["args"]["m"], (string)obj["url"]);
}
}
}

View File

@ -0,0 +1,28 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
internal class EchoPocoClientFactory : IEchoPocoClientFactory
{
public IEchoPocoClient Create(IServiceLocator serviceLocator)
{
return new EchoPocoClient(serviceLocator);
}
}
}

View File

@ -0,0 +1,31 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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 CustomServiceClientExample
{
public class EchoResponse
{
public string Message { get; private set; }
public string Url { get; private set; }
public EchoResponse(string message, string url)
{
this.Message = message;
this.Url = url;
}
}
}

View File

@ -0,0 +1,45 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using OpenStack.Common.Http;
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
internal class EchoRestClient : IEchoRestClient
{
internal const string serviceEndpoint = "http://httpbin.org/get";
internal IServiceLocator ServiceLocator;
public EchoRestClient(IServiceLocator serviceLocator)
{
this.ServiceLocator = serviceLocator;
}
public async Task<IHttpResponseAbstraction> Echo(string message)
{
var client = this.ServiceLocator.Locate<IHttpAbstractionClientFactory>().Create(CancellationToken.None);
client.Method = HttpMethod.Get;
client.Uri = new Uri(serviceEndpoint + "?m=" + message);
return await client.SendAsync();
}
}
}

View File

@ -0,0 +1,28 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
internal class EchoRestClientFactory : IEchoRestClientFactory
{
public IEchoRestClient Create(IServiceLocator serviceLocator)
{
return new EchoRestClient(serviceLocator);
}
}
}

View File

@ -0,0 +1,39 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Threading;
using System.Threading.Tasks;
using OpenStack.Common.ServiceLocation;
using OpenStack.Identity;
namespace CustomServiceClientExample
{
internal class EchoServiceClient : IEchoServiceClient
{
internal IServiceLocator ServiceLocator;
public EchoServiceClient(ICredential credential, CancellationToken token, IServiceLocator serviceLocator)
{
this.ServiceLocator = serviceLocator;
}
public async Task<EchoResponse> Echo(string message)
{
var pocoClient = this.ServiceLocator.Locate<IEchoPocoClientFactory>().Create(this.ServiceLocator);
return await pocoClient.Echo(message);
}
}
}

View File

@ -0,0 +1,49 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Collections.Generic;
using System.Threading;
using OpenStack;
using OpenStack.Common.ServiceLocation;
using OpenStack.Identity;
namespace CustomServiceClientExample
{
internal class EchoServiceClientDefinition : IOpenStackServiceClientDefinition
{
public string Name { get; private set; }
public EchoServiceClientDefinition()
{
this.Name = typeof(EchoServiceClient).Name;
}
public IOpenStackServiceClient Create(ICredential credential, CancellationToken cancellationToken, IServiceLocator serviceLocator)
{
return new EchoServiceClient(credential, cancellationToken, serviceLocator);
}
public IEnumerable<string> ListSupportedVersions()
{
return new List<string>();
}
public bool IsSupported(ICredential credential)
{
return true;
}
}
}

View File

@ -0,0 +1,32 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using OpenStack;
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
public class EchoServiceRegistrar : IServiceLocationRegistrar
{
public void Register(IServiceLocationManager manager, IServiceLocator locator)
{
manager.RegisterServiceInstance(typeof(IEchoPocoClientFactory), new EchoPocoClientFactory());
manager.RegisterServiceInstance(typeof(IEchoRestClientFactory), new EchoRestClientFactory());
var serviceManager = locator.Locate<IOpenStackServiceClientManager>();
serviceManager.RegisterServiceClient<EchoServiceClient>(new EchoServiceClientDefinition());
}
}
}

View File

@ -0,0 +1,25 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Threading.Tasks;
namespace CustomServiceClientExample
{
public interface IEchoPocoClient
{
Task<EchoResponse> Echo(string message);
}
}

View File

@ -0,0 +1,25 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
public interface IEchoPocoClientFactory
{
IEchoPocoClient Create(IServiceLocator serviceLocator);
}
}

View File

@ -0,0 +1,26 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Threading.Tasks;
using OpenStack.Common.Http;
namespace CustomServiceClientExample
{
public interface IEchoRestClient
{
Task<IHttpResponseAbstraction> Echo(string message);
}
}

View File

@ -0,0 +1,25 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using OpenStack.Common.ServiceLocation;
namespace CustomServiceClientExample
{
public interface IEchoRestClientFactory
{
IEchoRestClient Create(IServiceLocator serviceLocator);
}
}

View File

@ -0,0 +1,26 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System.Threading.Tasks;
using OpenStack;
namespace CustomServiceClientExample
{
public interface IEchoServiceClient : IOpenStackServiceClient
{
Task<EchoResponse> Echo(string message);
}
}

View File

@ -0,0 +1,55 @@
// /* ============================================================================
// Copyright 2014 Hewlett Packard
//
// 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.
// ============================================================================ */
using System;
using System.Threading.Tasks;
using OpenStack;
using OpenStack.Identity;
namespace CustomServiceClientExample
{
class Program
{
static void Main(string[] args)
{
var authUri = new Uri("https://region.identity.host.com:12345/v2.0/tokens");
var userName = "user name";
var password = "password";
var tenantId = "XXXXXXXXXXXXXX-Project";
var echoMessage = "Hello world!";
Console.WriteLine("Calling remote service to echo the following message: '{0}'", echoMessage);
var echoMessageTask = EchoMessage(echoMessage, authUri, userName, password, tenantId);
echoMessageTask.Wait();
Console.WriteLine("Response from remote service: '{0}'", echoMessageTask.Result);
Console.ReadLine();
}
public static async Task<string> EchoMessage(string message, Uri authUri, string userName, string password, string tenantId)
{
var credential = new OpenStackCredential(authUri, userName, password, tenantId);
var client = OpenStackClientFactory.CreateClient(credential);
await client.Connect();
var echoServiceClient = client.CreateServiceClient<EchoServiceClient>();
var resp = await echoServiceClient.Echo(message);
return resp.Message;
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CustomServiceClientExample")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("CustomServiceClientExample")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4cc5544b-fdb5-43ef-8108-9ed8e995d114")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
VisualStudioVersion = 12.0.30110.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleStorageExample", "SimpleStorageExample\SimpleStorageExample.csproj", "{A8BB096A-9E6F-4D84-B091-F1FCF4DDCBDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomServiceClientExample", "CustomServiceClientExample\CustomServiceClientExample.csproj", "{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{A8BB096A-9E6F-4D84-B091-F1FCF4DDCBDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8BB096A-9E6F-4D84-B091-F1FCF4DDCBDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8BB096A-9E6F-4D84-B091-F1FCF4DDCBDC}.Release|Any CPU.Build.0 = Release|Any CPU
{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83B16A76-A3DF-42E5-95A6-5BBE41FC7854}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE