Skip to content

Commit ece369b

Browse files
author
kravi
committed
Merge pull request #6 from kravi/SwiftProvider_Container
Added functionality for Create Container.
2 parents 77debce + 58f26fa commit ece369b

File tree

10 files changed

+251
-13
lines changed

10 files changed

+251
-13
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace net.openstack.Core.Domain
7+
{
8+
public enum ObjectStore
9+
{
10+
Unknown,
11+
ContainerCreated,
12+
ContainerExists
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace net.openstack.Core.Exceptions
7+
{
8+
public class ContainerNameException : Exception
9+
{
10+
public ContainerNameException() { }
11+
public ContainerNameException(string message) : base(message) { }
12+
}
13+
}

src/corelib/Core/IObjectStoreProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public interface IObjectStoreProvider
1111
#region Container
1212

1313
IEnumerable<Container> ListContainers(CloudIdentity identity, int? limit = null, string markerId = null, string markerEnd = null, string format = "json", string region = null);
14+
ObjectStore CreateContainer(CloudIdentity identity, string container, string region = null);
1415

1516
#endregion
1617
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace net.openstack.Core
7+
{
8+
public interface IObjectStoreValidator
9+
{
10+
void ValidateContainerName(string containerName);
11+
}
12+
}

src/corelib/Providers/Rackspace/ObjectStoreProvider.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ namespace net.openstack.Providers.Rackspace
99
{
1010
public class ObjectStoreProvider : ProviderBase, IObjectStoreProvider
1111
{
12+
private readonly IObjectStoreValidator _objectStoreValidator;
1213
#region Constructors
1314

1415
public ObjectStoreProvider()
15-
: this(new IdentityProvider(), new JsonRestServices()) { }
16+
: this(new IdentityProvider(), new JsonRestServices(), new ObjectStoreValidator()) { }
17+
18+
public ObjectStoreProvider(IIdentityProvider identityProvider, IRestService restService, IObjectStoreValidator objectStoreValidator)
19+
: base(identityProvider, restService)
20+
{
21+
_objectStoreValidator = objectStoreValidator;
22+
}
1623

17-
public ObjectStoreProvider(IIdentityProvider identityProvider, IRestService restService)
18-
: base(identityProvider, restService) { }
1924

2025
#endregion
2126

@@ -46,6 +51,21 @@ public IEnumerable<Container> ListContainers(CloudIdentity identity, int? limit
4651

4752
}
4853

54+
public ObjectStore CreateContainer(CloudIdentity identity, string container, string region = null)
55+
{
56+
_objectStoreValidator.ValidateContainerName(container);
57+
var urlPath = new Uri(string.Format("{0}/{1}", GetServiceEndpoint(identity, region), container));
58+
59+
var response = ExecuteRESTRequest(identity, urlPath, HttpMethod.PUT);
60+
61+
if (response.StatusCode == 201)
62+
return ObjectStore.ContainerCreated;
63+
if (response.StatusCode == 202)
64+
return ObjectStore.ContainerExists;
65+
66+
return ObjectStore.Unknown;
67+
}
68+
4969
#endregion
5070

5171
#region Private methods
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Web;
6+
using net.openstack.Core;
7+
using net.openstack.Core.Exceptions;
8+
9+
namespace net.openstack.Providers.Rackspace
10+
{
11+
public class ObjectStoreValidator : IObjectStoreValidator
12+
{
13+
public void ValidateContainerName(string containerName)
14+
{
15+
var containerNameString = string.Format("Container Name:[{0}]", containerName);
16+
if (string.IsNullOrWhiteSpace(containerName))
17+
throw new ArgumentNullException("ContainerName", "ERROR: Container Name cannot be Null.");
18+
if (HttpUtility.UrlEncode(containerName).Length > 256)
19+
throw new ContainerNameException(string.Format("ERROR: encoded URL Length greater than 256 char's. {0}", containerNameString));
20+
if (containerName.Contains("/"))
21+
throw new ContainerNameException(string.Format("ERROR: Container Name contains a /. {0}", containerNameString));
22+
}
23+
}
24+
}

src/corelib/corelib.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Reference Include="System" />
4242
<Reference Include="System.Core" />
4343
<Reference Include="System.Runtime.Serialization" />
44+
<Reference Include="System.Web" />
4445
</ItemGroup>
4546
<ItemGroup>
4647
<Compile Include="Core\Domain\Address.cs" />
@@ -55,12 +56,14 @@
5556
<Compile Include="Core\Domain\Mapping\MetaDataJsonMapper.cs" />
5657
<Compile Include="Core\Domain\Network.cs" />
5758
<Compile Include="Core\Domain\NewUser.cs" />
59+
<Compile Include="Core\Domain\ObjectStore.cs" />
5860
<Compile Include="Core\Domain\Personality.cs" />
5961
<Compile Include="Core\Domain\RebootType.cs" />
6062
<Compile Include="Core\Domain\ServerAddresses.cs" />
6163
<Compile Include="Core\Domain\ServerState.cs" />
6264
<Compile Include="Core\Domain\Tenant.cs" />
6365
<Compile Include="Core\Domain\UserCredential.cs" />
66+
<Compile Include="Core\Exceptions\ContainerNameException.cs" />
6467
<Compile Include="Core\Exceptions\Response\BadServiceRequestException.cs" />
6568
<Compile Include="Core\Exceptions\Response\ItemNotFoundException.cs" />
6669
<Compile Include="Core\Exceptions\Response\MethodNotImplementedException.cs" />
@@ -69,10 +72,12 @@
6972
<Compile Include="Core\Exceptions\Response\ServiceUnavailableException.cs" />
7073
<Compile Include="Core\Exceptions\Response\UserNotAuthorizedException.cs" />
7174
<Compile Include="Core\IObjectStoreProvider.cs" />
75+
<Compile Include="Core\IObjectStoreValidator.cs" />
7276
<Compile Include="Providers\Rackspace\IExtendedIdentityProvider.cs" />
7377
<Compile Include="Providers\Rackspace\ObjectStoreProvider.cs" />
7478
<Compile Include="Providers\Rackspace\ComputeProvider.cs" />
7579
<Compile Include="Core\Exceptions\InvalidCloudIdentityException.cs" />
80+
<Compile Include="Providers\Rackspace\ObjectStoreValidator.cs" />
7681
<Compile Include="Providers\Rackspace\Objects\Request\AddRoleRequest.cs" />
7782
<Compile Include="Providers\Rackspace\Objects\Request\AddUserRequest.cs" />
7883
<Compile Include="Providers\Rackspace\Objects\Request\ConfirmServerResizeRequest.cs" />
@@ -151,6 +156,7 @@
151156
<Compile Include="Providers\Rackspace\Objects\Response\UserImpersonationResponse.cs" />
152157
<Compile Include="Providers\Rackspace\Exceptions\UnknownGeographyException.cs" />
153158
</ItemGroup>
159+
<ItemGroup />
154160
<ItemGroup>
155161
<None Include="packages.config" />
156162
</ItemGroup>

src/testing/integration/Providers/Rackspace/ObjectStoreTests.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,16 @@ namespace Net.OpenStack.Testing.Integration.Providers.Rackspace
1313
public class ObjectStoreTests
1414
{
1515

16+
private static RackspaceCloudIdentity _testIdentity;
17+
private static RackspaceCloudIdentity _testAdminIdentity;
18+
1619
public ObjectStoreTests()
1720
{
1821
CloudInstance cloudInstance;
19-
CloudInstance.TryParse(ConfigurationManager.AppSettings["TestIdentityGeo"], true, out cloudInstance);
20-
21-
_testIdentity = new RackspaceCloudIdentity
22-
{
23-
APIKey = ConfigurationManager.AppSettings["TestIdentityAPIKey"],
24-
Password = ConfigurationManager.AppSettings["TestIdentityPassword"],
25-
CloudInstance = cloudInstance,
26-
Username = ConfigurationManager.AppSettings["TestIdentityUserName"],
27-
};
2822
}
2923

3024
private TestContext testContextInstance;
31-
private static CloudIdentity _testIdentity;
25+
3226

3327
/// <summary>
3428
///Gets or sets the test context which provides
@@ -47,6 +41,13 @@ public TestContext TestContext
4741
}
4842

4943

44+
[ClassInitialize]
45+
public static void Init(TestContext context)
46+
{
47+
_testIdentity = new RackspaceCloudIdentity(Bootstrapper.Settings.TestIdentity);
48+
}
49+
50+
5051
[TestMethod]
5152
public void Should_Return_Container_List()
5253
{
@@ -108,6 +109,26 @@ public void Should_Return_Container_List_With_End_Marker_Lower_Case()
108109
Assert.IsTrue(containerList.Any());
109110
}
110111

112+
[TestMethod]
113+
public void Should_Create_Container()
114+
{
115+
const string containerName = "TestContainer";
116+
var provider = new ObjectStoreProvider();
117+
var containerCreatedResponse = provider.CreateContainer(_testIdentity, containerName);
118+
119+
Assert.AreEqual(ObjectStore.ContainerCreated, containerCreatedResponse);
120+
}
121+
122+
[TestMethod]
123+
public void Should_Not_Create_Container_Already_Exists()
124+
{
125+
const string containerName = "TestContainer";
126+
var provider = new ObjectStoreProvider();
127+
var containerCreatedResponse = provider.CreateContainer(_testIdentity, containerName);
128+
129+
Assert.AreEqual(ObjectStore.ContainerExists, containerCreatedResponse);
130+
}
131+
111132

112133
}
113134
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Text;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Moq;
7+
using JSIStudios.SimpleRESTServices.Client;
8+
using net.openstack;
9+
using net.openstack.Core;
10+
using net.openstack.Core.Domain;
11+
using net.openstack.Core.Exceptions;
12+
using net.openstack.Providers.Rackspace;
13+
using net.openstack.Providers.Rackspace.Objects.Response;
14+
15+
namespace OpenStackNet.Testing.Unit.Providers.Rackspace
16+
{
17+
[TestClass]
18+
public class ObjectProviderValidatorTests
19+
{
20+
[TestMethod]
21+
public void Should_Pass_Validation_For_Container_Name()
22+
{
23+
const string containerName = "DarkKnight";
24+
var validatorMock = new Mock<IObjectStoreValidator>();
25+
26+
validatorMock.Setup(v => v.ValidateContainerName(containerName));
27+
28+
var objectStoreValidator = new ObjectStoreValidator();
29+
objectStoreValidator.ValidateContainerName(containerName);
30+
31+
}
32+
33+
//[ExpectedException(typeof(ArgumentNullException),"ERROR: Container Name cannot be Null.")]
34+
[TestMethod]
35+
public void Should_Throw_Exception_When_Passing_Empty_Container_Name()
36+
{
37+
const string containerName = "";
38+
var validatorMock = new Mock<IObjectStoreValidator>();
39+
40+
validatorMock.Setup(v => v.ValidateContainerName(containerName));
41+
42+
try
43+
{
44+
var objectStoreValidator = new ObjectStoreValidator();
45+
objectStoreValidator.ValidateContainerName(containerName);
46+
Assert.Fail("Expected exception was not thrown.");
47+
}
48+
catch (Exception ex)
49+
{
50+
51+
Assert.AreEqual("ERROR: Container Name cannot be Null.\r\nParameter name: ContainerName", ex.Message);
52+
}
53+
}
54+
55+
[TestMethod]
56+
public void Should_Throw_Exception_When_Passing_Null_Container_Name()
57+
{
58+
const string containerName = null;
59+
var validatorMock = new Mock<IObjectStoreValidator>();
60+
61+
validatorMock.Setup(v => v.ValidateContainerName(containerName));
62+
63+
try
64+
{
65+
var objectStoreValidator = new ObjectStoreValidator();
66+
objectStoreValidator.ValidateContainerName(containerName);
67+
Assert.Fail("Expected exception was not thrown.");
68+
}
69+
catch (Exception ex)
70+
{
71+
72+
Assert.AreEqual("ERROR: Container Name cannot be Null.\r\nParameter name: ContainerName", ex.Message);
73+
}
74+
}
75+
76+
[TestMethod]
77+
public void Should_Throw_Exception_When_Passing_256_Characters_In_Container_Name()
78+
{
79+
string containerName = "AaAaAaAaAa";
80+
81+
while (containerName.Length <= 256)
82+
{
83+
containerName += containerName;
84+
}
85+
var validatorMock = new Mock<IObjectStoreValidator>();
86+
87+
validatorMock.Setup(v => v.ValidateContainerName(containerName));
88+
89+
try
90+
{
91+
var objectStoreValidator = new ObjectStoreValidator();
92+
objectStoreValidator.ValidateContainerName(containerName);
93+
Assert.Fail("Expected exception was not thrown.");
94+
}
95+
catch (ContainerNameException ex)
96+
{
97+
98+
Assert.AreEqual(string.Format("ERROR: encoded URL Length greater than 256 char's. Container Name:[{0}]",containerName), ex.Message);
99+
}
100+
}
101+
102+
[TestMethod]
103+
public void Should_Throw_Exception_When_Passing_Forwar_Slash_In_Container_Name()
104+
{
105+
string containerName = "/";
106+
107+
var validatorMock = new Mock<IObjectStoreValidator>();
108+
109+
validatorMock.Setup(v => v.ValidateContainerName(containerName));
110+
111+
try
112+
{
113+
var objectStoreValidator = new ObjectStoreValidator();
114+
objectStoreValidator.ValidateContainerName(containerName);
115+
Assert.Fail("Expected exception was not thrown.");
116+
}
117+
catch (ContainerNameException ex)
118+
{
119+
120+
Assert.AreEqual(string.Format("ERROR: Container Name contains a /. Container Name:[{0}]", containerName), ex.Message);
121+
}
122+
}
123+
124+
125+
}
126+
}

src/testing/unit/unit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<ItemGroup>
5858
<Compile Include="Properties\AssemblyInfo.cs" />
5959
<Compile Include="Providers\Rackspace\IdentityProviderCacheTests.cs" />
60+
<Compile Include="Providers\Rackspace\ObjectProviderValidatorTests.cs" />
6061
<Compile Include="UnitTest1.cs" />
6162
</ItemGroup>
6263
<ItemGroup>

0 commit comments

Comments
 (0)