[Groonga-commit] groonga/gcs [master] Support DescribeDomains Action on the configuration API

Zurück zum Archiv-Index

null+****@clear***** null+****@clear*****
2012年 8月 3日 (金) 15:20:36 JST


SHIMODA Hiroshi	2012-08-03 15:20:36 +0900 (Fri, 03 Aug 2012)

  New Revision: 125c03a8f2843f8e3cba2ad597defad4f2d614fe
  https://github.com/groonga/gcs/commit/125c03a8f2843f8e3cba2ad597defad4f2d614fe

  Log:
    Support DescribeDomains Action on the configuration API

  Modified files:
    lib/api/2011-02-01/configuration.js
    test/api-configuration.test.js

  Modified: lib/api/2011-02-01/configuration.js (+51 -7)
===================================================================
--- lib/api/2011-02-01/configuration.js    2012-08-03 15:03:01 +0900 (2095f90)
+++ lib/api/2011-02-01/configuration.js    2012-08-03 15:20:36 +0900 (e7244cc)
@@ -77,12 +77,10 @@ handlers.CreateDomain = function(context, request, response) {
   var domain = new Domain(request.query.DomainName, context);
   try {
     domain.createSync();
-    var host = getBaseDomain(request.headers.host);
-    host = host ? '.' + host : '';
     response.contentType('application/xml');
     response.send(createCreateDomainResponse({
       domain: domain,
-      hostname: host,
+      hostname: getBaseDomain(request.headers.host),
       created: true
     }));
   } catch (error) {
@@ -109,12 +107,10 @@ handlers.DeleteDomain = function(context, request, response) {
   var domain = new Domain(request.query.DomainName, context);
   try {
     domain.deleteSync();
-    var host = getBaseDomain(request.headers.host);
-    host = host ? '.' + host : '';
     response.contentType('application/xml');
     response.send(createDeleteDomainResponse({
       domain: domain,
-      hostname: host,
+      hostname: getBaseDomain(request.headers.host),
       deleted: true
     }));
   } catch (error) {
@@ -124,6 +120,55 @@ handlers.DeleteDomain = function(context, request, response) {
   }
 };
 
+function createDomainStatusList(options) {
+  var doc = xmlbuilder.create();
+  var domainStatusList = doc.begin('DomainStatusList', {version: '1.0'});
+  options.domains.forEach(function(domain) {
+    domainStatusList.element('member')
+                    .importXMLBuilder(createDomainStatus({
+                      domain:   domain,
+                      hostname: options.hostname
+                    }));
+  });
+  return domainStatusList;
+}
+
+function createDescribeDomainsResponse(options) {
+  var doc = xmlbuilder.create();
+  doc.begin('DescribeDomainsResponse', { version: '1.0' })
+    .attribute('xmlns', XMLNS)
+    .element('DescribeDomainsResult')
+      .importXMLBuilder(createDomainStatusList(options))
+    .up()
+    .element('ResponseMetadata')
+      .element('RequestId').text(options.requestId || '').up()
+    .up();
+  return doc.toString();
+}
+
+handlers.DescribeDomains = function(context, request, response) {
+  try {
+    var keys = Object.keys(request.query).filter(function(key) {
+          return /^DomainNames\.member\.\d+$/.test(key);
+        });
+    var domainNames = keys.sort().map(function(key) {
+          return request.query[key];
+        });
+    var domains = domainNames.map(function(name) {
+          return new Domain(name, context);
+        });
+    response.contentType('application/xml');
+    response.send(createDescribeDomainsResponse({
+      domains: domains,
+      hostname: getBaseDomain(request.headers.host)
+    }));
+  } catch (error) {
+    var body = createCommonErrorResponse('InternalFailure', error.message);
+    response.contentType('application/xml');
+    response.send(body, 400);
+  }
+};
+
 function createIndexFieldOptionStatus(options) {
   switch (options.field.type) {
     case 'text':
@@ -328,7 +373,6 @@ function getClientIp(request) {
   return request.connection.remoteAddress;
 };
 
-
 exports.createHandler = function(context, config) {
   var privilegedRanges = config &&
                          config.privilegedRanges &&

  Modified: test/api-configuration.test.js (+63 -0)
===================================================================
--- test/api-configuration.test.js    2012-08-03 15:03:01 +0900 (bce9276)
+++ test/api-configuration.test.js    2012-08-03 15:20:36 +0900 (6475458)
@@ -46,6 +46,24 @@ var PATTERN_DeleteDomainResponse = {
       }
     };
 
+function PATTERN_DescribeDomainsResponse(members) {
+  return {
+    DescribeDomainsResponse: {
+      '@': { xmlns: '' },
+      DescribeDomainsResult: {
+        DomainStatusList: (function() {
+          var pattern = {};
+          members.forEach(function(member, index) {
+            pattern[index] = PATTERN_DomainStatus;
+          });
+          return { member: pattern };
+        })()
+      },
+      ResponseMetadata: PATTERN_ResponseMetadata
+    }
+  };
+}
+
 var PATTERN_OptionStatus = {
       CreationDate: '',
       State: '',
@@ -312,6 +330,51 @@ suite('Configuration API', function() {
       });
   });
 
+  test('Get, Action=DescribeDomains', function(done) {
+    var domain;
+    utils
+      .get('/?DomainName=domain3&Action=CreateDomain&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .get('/?DomainName=domain1&Action=CreateDomain&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .get('/?DomainName=domain2&Action=CreateDomain&Version=2011-02-01', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .get('/?Action=DescribeDomains&Version=2011-02-01' +
+             '&DomainNames.member.1=domain2' +
+             '&DomainNames.member.2=domain1', {
+        'Host': 'cloudsearch.localhost'
+      })
+      .next(function(response) {
+        response = toParsedResponse(response);
+        var expectedDomains = ['domain2', 'domain1'];
+        assert.deepEqual(response.pattern,
+                         { statusCode: 200,
+                           body: PATTERN_DescribeDomainsResponse(expectedDomains) });
+
+        var actualDomains = response.body.DescribeDomainsResponse
+                                         .DescribeDomainsResult
+                                         .DomainStatus
+                                         .member;
+        actualDomains = (function() {
+          var domains = [];
+          for (var i in actualDomains) {
+            if (actualDomains.hasOwnProperty(i))
+              domains.push(actualDomains[i]).name;
+          }
+          return domains;
+        })();
+        assert.deepEqual(actualDomains, expectedDomains);
+
+        done();
+      })
+      .error(function(error) {
+        done(error);
+      });
+  });
+
   test('Get, Action=DefineIndexField (text)', function(done) {
     utils
       .get('/?DomainName=companies&Action=CreateDomain&Version=2011-02-01', {
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



Groonga-commit メーリングリストの案内
Zurück zum Archiv-Index