Permalink
Browse files

NCBC-63: Added support for removing buckets

Change-Id: Id03b17e054c5f64c66d5b9e1abc1429e490b058c
Reviewed-on: http://review.couchbase.org/18793
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: John C. Zablocki <john@couchbase.com>
  • Loading branch information...
johnzablocki authored and John C. Zablocki committed Jul 25, 2012
1 parent 4410a46 commit 86d736503531d68674085b2cada1eaf3e8a889af
@@ -79,22 +79,20 @@ public void When_Flushing_Bucket_Data_Are_Removed()
[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;
- }
+ var bucketName = "Bucket-" + DateTime.Now.Ticks;
_Cluster.CreateBucket(new Bucket {
- Name = "TestCreateBucket",
+ Name = bucketName,
AuthType = AuthTypes.Sasl,
BucketType = BucketTypes.Membase,
RamQuotaMB = 128 }
);
- Assert.That(testExists, Is.Not.Null);
+ var bucket = _Cluster.ListBuckets().Where(b => b.Name == bucketName).FirstOrDefault();
+ Assert.That(bucket, Is.Not.Null);
+
+ _Cluster.DeleteBucket(bucketName);
+
}
[Test]
@@ -149,6 +147,39 @@ public void When_Creating_New_Bucket_With_Ram_Quota_Less_Than_100_Argument_Excep
RamQuotaMB = 99
});
}
+
+ [Test]
+ public void When_Deleting_Bucket_Bucket_Is_No_Longer_Listed()
+ {
+ var bucketName = "Bucket-" + DateTime.Now.Ticks;
+
+ _Cluster.CreateBucket(new Bucket
+ {
+ Name = bucketName,
+ AuthType = AuthTypes.Sasl,
+ BucketType = BucketTypes.Membase,
+ RamQuotaMB = 128
+ });
+
+ var bucket = _Cluster.ListBuckets().Where(b => b.Name == bucketName).FirstOrDefault();
+
+ Assert.That(bucket, Is.Not.Null, "New bucket was null");
+
+ _Cluster.DeleteBucket(bucketName);
+
+ bucket = _Cluster.ListBuckets().Where(b => b.Name == bucketName).FirstOrDefault();
+
+ Assert.That(bucket, Is.Null, "Deleted bucket still exists");
+ }
+
+ [Test]
+ [ExpectedException(typeof(WebException), ExpectedMessage="404", MatchType=MessageMatch.Contains)]
+ public void When_Deleting_Bucket_That_Does_Not_Exist_404_Web_Exception_Is_Thrown()
+ {
+ var bucketName = "Bucket-" + DateTime.Now.Ticks;
+
+ _Cluster.DeleteBucket(bucketName);
+ }
}
}
@@ -56,6 +56,12 @@ public void When_Performing_Post_Response_Is_OK()
Assert.That(output, Is.StringMatching(_output));
}
+ [Test]
+ public void When_Performing_Delete_Response_Is_OK()
+ {
+ var output = HttpHelper.Delete(new Uri(_url), "", "");
+ Assert.That(output, Is.StringMatching(_output));
+ }
}
}
@@ -19,25 +19,7 @@ public static string Get(Uri uri)
public static string Get(Uri uri, string username, string password)
{
- if (uri == null) throw new ArgumentNullException("uri");
-
- var request = WebRequest.Create(uri) as HttpWebRequest;
- request.Method = "GET";
-
- if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
- {
- buildAuthorizationHeader(request, username, password);
- }
-
- var response = request.GetResponse() as HttpWebResponse;
-
- using (var responseStream = response.GetResponseStream())
- {
- using (var reader = new StreamReader(responseStream))
- {
- return reader.ReadToEnd();
- }
- }
+ return doRequest(uri, "GET", username, password);
}
public static string Post(Uri uri, string username, string password, string postData)
@@ -46,23 +28,40 @@ public static string Post(Uri uri, string username, string password, string post
}
public static string Post(Uri uri, string username, string password, string postData, string contentType)
+ {
+ return doRequest(uri, "POST", username, password, postData, contentType);
+ }
+
+ public static string Delete(Uri uri, string username, string password)
+ {
+ return doRequest(uri, "DELETE", username, password);
+ }
+
+ private static string doRequest(Uri uri, string verb, string username, string password, string postData, string contentType)
{
if (uri == null) throw new ArgumentNullException("uri");
var request = WebRequest.Create(uri) as HttpWebRequest;
- request.Method = "POST";
- request.ContentType = contentType ?? CONTENT_TYPE_JSON;
- request.ContentLength = postData.Length;
+ request.Method = verb.ToUpper();
+
+ if (verb == "POST")
+ {
+ request.ContentType = contentType ?? CONTENT_TYPE_JSON;
+ request.ContentLength = postData.Length;
+ }
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
buildAuthorizationHeader(request, username, password);
}
- using (var requestStream = request.GetRequestStream())
+ if (!string.IsNullOrEmpty(postData))
{
- var bytesToWrite = Encoding.UTF8.GetBytes(postData);
- requestStream.Write(bytesToWrite, 0, bytesToWrite.Length);
+ using (var requestStream = request.GetRequestStream())
+ {
+ var bytesToWrite = Encoding.UTF8.GetBytes(postData);
+ requestStream.Write(bytesToWrite, 0, bytesToWrite.Length);
+ }
}
var response = request.GetResponse() as HttpWebResponse;
@@ -75,6 +74,11 @@ public static string Post(Uri uri, string username, string password, string post
}
}
+ private static string doRequest(Uri uri, string verb, string username, string password)
+ {
+ return doRequest(uri, verb, username, password, null, null);
+ }
+
private static void buildAuthorizationHeader(HttpWebRequest request, string username, string password)
{
request.Credentials = new NetworkCredential(username, password);
@@ -66,10 +66,10 @@ public bool TryListBuckets(out Bucket[] buckets)
public void FlushBucket(string bucketName)
{
var flushUri = UriHelper.Combine(_bucketUri, bucketName, "controller", "doFlush");
- var json = HttpHelper.Post(flushUri, _username, _password, "");
+ HttpHelper.Post(flushUri, _username, _password, "");
}
- public bool CreateBucket(Bucket bucket)
+ public void CreateBucket(Bucket bucket)
{
if (!bucket.IsValid())
{
@@ -90,10 +90,13 @@ public bool CreateBucket(Bucket bucket)
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);
+ HttpHelper.Post(_bucketUri, _username, _password, sb.ToString(), HttpHelper.CONTENT_TYPE_FORM);
}
+ public void DeleteBucket(string bucketName)
+ {
+ HttpHelper.Delete(UriHelper.Combine(_bucketUri, bucketName), _username, _password);
+ }
#endregion
@@ -27,7 +27,14 @@ interface ICouchbaseCluster
/// </summary>
/// <param name="bucket">Bucket to create</param>
/// <returns>True if successful</returns>
- bool CreateBucket(Bucket bucket);
+ void CreateBucket(Bucket bucket);
+
+ /// <summary>
+ /// Delete a bucket on the server
+ /// </summary>
+ /// <param name="bucket">Bucket to create</param>
+ /// <returns>True if successful</returns>
+ void DeleteBucket(string bucketName);
}
}

0 comments on commit 86d7365

Please sign in to comment.