Skip to content

Commit

Permalink
NCBC-62: Added support for creating buckets
Browse files Browse the repository at this point in the history
Change-Id: Ic6c7de629d7e4ddda703ede13556ec62d32e7ab9
Reviewed-on: http://review.couchbase.org/18792
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: John C. Zablocki <john@couchbase.com>
  • Loading branch information
jzablocki authored and John C. Zablocki committed Aug 28, 2012
1 parent a6f68f4 commit 4410a46
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
74 changes: 74 additions & 0 deletions src/Couchbase.Tests/CouchbaseClusterBucketAdminTests.cs
Expand Up @@ -75,6 +75,80 @@ public void When_Flushing_Bucket_Data_Are_Removed()
Assert.That(getResult.Success, Is.False);
Assert.That(getResult.Value, Is.Null);
}

[Test]
public void When_Creating_New_Bucket_That_Bucket_Is_Listed()
{
Func<Bucket> testExists = () => _Cluster.ListBuckets().Where(b => b.Name == "TestCreateBucket").FirstOrDefault();

if (testExists() != null)
{
Assert.Ignore("TestCreateBucket already exists");
return;
}

_Cluster.CreateBucket(new Bucket {
Name = "TestCreateBucket",
AuthType = AuthTypes.Sasl,
BucketType = BucketTypes.Membase,
RamQuotaMB = 128 }
);

Assert.That(testExists, Is.Not.Null);
}

[Test]
[ExpectedException(typeof(WebException))]
public void When_Creating_New_Bucket_With_Existing_Name_Web_Exception_Is_Thrown()
{
_Cluster.CreateBucket(new Bucket
{
Name = "default",
AuthType = AuthTypes.Sasl,
BucketType = BucketTypes.Membase,
RamQuotaMB = 128
});
}

[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage="ProxyPort", MatchType=MessageMatch.Contains)]
public void When_Creating_New_Bucket_With_Auth_Type_None_And_No_Port_Argument_Exception_Is_Thrown()
{
_Cluster.CreateBucket(new Bucket
{
Name = "default",
AuthType = AuthTypes.None,
BucketType = BucketTypes.Memcached,
RamQuotaMB = 128
});
}

[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "ProxyPort", MatchType = MessageMatch.Contains)]
public void When_Creating_New_Bucket_With_Auth_Type_Sasl_And_Port_Argument_Exception_Is_Thrown()
{
_Cluster.CreateBucket(new Bucket
{
Name = "default",
AuthType = AuthTypes.None,
BucketType = BucketTypes.Memcached,
RamQuotaMB = 128
});
}


[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "RamQuotaMB", MatchType = MessageMatch.Contains)]
public void When_Creating_New_Bucket_With_Ram_Quota_Less_Than_100_Argument_Exception_Is_Thrown()
{
_Cluster.CreateBucket(new Bucket
{
Name = "default",
AuthType = AuthTypes.Sasl,
BucketType = BucketTypes.Memcached,
RamQuotaMB = 99
});
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Couchbase/Couchbase.csproj
Expand Up @@ -61,6 +61,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Management\BucketEnums.cs" />
<Compile Include="Helpers\JsonHelper.cs" />
<Compile Include="Management\Bucket.cs" />
<Compile Include="BucketConfigListener.cs" />
Expand Down
34 changes: 32 additions & 2 deletions src/Couchbase/Management/Bucket.cs
Expand Up @@ -7,12 +7,42 @@ namespace Couchbase.Management
{
public class Bucket
{
private const int MIN_RAM_QUOTA = 100;

public string Name { get; set; }

public string BucketType { get; set; }
public BucketTypes BucketType { get; set; }

public AuthTypes AuthType { get; set; }

public int RamQuotaMB { get; set; }

public int? ProxyPort { get; set; }

public short ReplicaNumber { get; set; }

public string Password { get; set; }

public IDictionary<string, string> ValidationErrors { get; set; }

public bool IsValid()
{
ValidationErrors = new Dictionary<string, string>();

if (string.IsNullOrEmpty(Name))
ValidationErrors["Name"] = "Name must be specified";

if (RamQuotaMB < MIN_RAM_QUOTA)
ValidationErrors["RamQuotaMB"] = "RamQuotaMB must be at least " + MIN_RAM_QUOTA;

if (AuthType == AuthTypes.None && (ProxyPort == null || !ProxyPort.HasValue))
ValidationErrors["ProxyPort"] = "ProxyPort is required when AuthType is 'none'";

public string AuthType { get; set; }
if (AuthType == AuthTypes.Sasl && (ProxyPort != null && ProxyPort.HasValue))
ValidationErrors["ProxyPort"] = "ProxyPort may not be used with AuthType 'sasl'";

return ValidationErrors.Keys.Count == 0;
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/Couchbase/Management/BucketEnums.cs
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Couchbase.Management
{
public enum BucketTypes
{
Memcached,
Membase
}

public enum AuthTypes
{
None,
Sasl
}
}

#region [ License information ]
/* ************************************************************
*
* @author Couchbase <info@couchbase.com>
* @copyright 2012 Couchbase, 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.
*
* ************************************************************/
#endregion
26 changes: 26 additions & 0 deletions src/Couchbase/Management/CouchbaseCluster.cs
Expand Up @@ -69,6 +69,32 @@ public void FlushBucket(string bucketName)
var json = HttpHelper.Post(flushUri, _username, _password, "");
}

public bool CreateBucket(Bucket bucket)
{
if (!bucket.IsValid())
{
var message = string.Join(Environment.NewLine, bucket.ValidationErrors.Values.ToArray());
throw new ArgumentException(message);
}

var sb = new StringBuilder();
sb.AppendFormat("name={0}", bucket.Name);
sb.AppendFormat("&ramQuotaMB={0}", bucket.RamQuotaMB);

if (bucket.AuthType == AuthTypes.None)
sb.AppendFormat("&proxyPort={0}", bucket.ProxyPort);
if (bucket.AuthType == AuthTypes.Sasl && ! string.IsNullOrEmpty(bucket.Password))
sb.AppendFormat("&saslPassword={0}", bucket.Password);

sb.AppendFormat("&authType={0}", Enum.GetName(typeof(AuthTypes), bucket.AuthType).ToLower()); ;
sb.AppendFormat("&bucketType={0}", Enum.GetName(typeof(BucketTypes), bucket.BucketType).ToLower());
sb.AppendFormat("&replicaNumber={0}", bucket.ReplicaNumber);

var response = HttpHelper.Post(_bucketUri, _username, _password, sb.ToString(), HttpHelper.CONTENT_TYPE_FORM);
return response.Contains(bucket.Name);
}


#endregion

#region Bootstrapping methods
Expand Down
7 changes: 7 additions & 0 deletions src/Couchbase/Management/ICouchbaseCluster.cs
Expand Up @@ -21,6 +21,13 @@ interface ICouchbaseCluster
/// </summary>
/// <param name="bucketName">bucket name</param>
void FlushBucket(string bucketName);

/// <summary>
/// Create a new bucket on the server
/// </summary>
/// <param name="bucket">Bucket to create</param>
/// <returns>True if successful</returns>
bool CreateBucket(Bucket bucket);
}
}

Expand Down

0 comments on commit 4410a46

Please sign in to comment.