Repository: ambari
Updated Branches:
  refs/heads/trunk 21697822f -> 8e646eb2d


AMBARI-19403 : Hive View 2.0 : add REST endpoint for deleting database 
(nitirajrathore)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8e646eb2
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8e646eb2
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8e646eb2

Branch: refs/heads/trunk
Commit: 8e646eb2da863cca896b515b14e980c9c778ed05
Parents: 2169782
Author: Nitiraj Rathore <[email protected]>
Authored: Tue Jan 10 15:48:05 2017 +0530
Committer: Nitiraj Rathore <[email protected]>
Committed: Tue Jan 10 15:48:51 2017 +0530

----------------------------------------------------------------------
 .../DeleteDatabaseQueryGenerator.java           | 48 +++++++++++
 .../view/hive20/resources/browser/DDLProxy.java | 27 +++++++
 .../hive20/resources/browser/DDLService.java    | 15 ++++
 .../DeleteDatabaseQueryGeneratorSpecTest.groovy | 65 +++++++++++++++
 .../rest/postman/hive20.postman_collection.json | 83 ++++++++++++++++++++
 5 files changed, 238 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/8e646eb2/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGenerator.java
----------------------------------------------------------------------
diff --git 
a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGenerator.java
 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGenerator.java
new file mode 100644
index 0000000..6e1e1a5
--- /dev/null
+++ 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGenerator.java
@@ -0,0 +1,48 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you 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.
+*/
+
+package org.apache.ambari.view.hive20.internal.query.generators;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Strings;
+import org.apache.ambari.view.hive20.exceptions.ServiceException;
+
+public class DeleteDatabaseQueryGenerator implements QueryGenerator{
+  private String databaseName;
+
+  public DeleteDatabaseQueryGenerator(String databaseName) {
+    this.databaseName = databaseName;
+  }
+
+  public String getDatabaseName() {
+    return databaseName;
+  }
+
+  public void setDatabaseName(String databaseName) {
+    this.databaseName = databaseName;
+  }
+
+  @Override
+  public Optional<String> getQuery() throws ServiceException {
+    if(Strings.isNullOrEmpty(this.getDatabaseName())){
+      throw new ServiceException("Database name cannot be null or empty.");
+    }
+
+    return Optional.of("drop database `" + this.getDatabaseName() + "`");
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/8e646eb2/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLProxy.java
----------------------------------------------------------------------
diff --git 
a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLProxy.java
 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLProxy.java
index 8c37a96..8d995dd 100644
--- 
a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLProxy.java
+++ 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLProxy.java
@@ -42,6 +42,7 @@ import 
org.apache.ambari.view.hive20.internal.dto.TableResponse;
 import org.apache.ambari.view.hive20.internal.parsers.TableMetaParserImpl;
 import 
org.apache.ambari.view.hive20.internal.query.generators.AlterTableQueryGenerator;
 import 
org.apache.ambari.view.hive20.internal.query.generators.CreateTableQueryGenerator;
+import 
org.apache.ambari.view.hive20.internal.query.generators.DeleteDatabaseQueryGenerator;
 import 
org.apache.ambari.view.hive20.internal.query.generators.DeleteTableQueryGenerator;
 import 
org.apache.ambari.view.hive20.internal.query.generators.RenameTableQueryGenerator;
 import org.apache.ambari.view.hive20.resources.jobs.JobServiceInternal;
@@ -330,4 +331,30 @@ public class DDLProxy {
         oldTableName);
     }
   }
+
+  public Job deleteDatabase(String databaseName, JobResourceManager 
resourceManager) throws ServiceException {
+    DeleteDatabaseQueryGenerator queryGenerator = new 
DeleteDatabaseQueryGenerator(databaseName);
+    Optional<String> deleteDatabase = queryGenerator.getQuery();
+    if(deleteDatabase.isPresent()) {
+      String deleteQuery = deleteDatabase.get();
+      LOG.info("Creating job for : {}", deleteQuery );
+      Map jobInfo = new HashMap<>();
+      jobInfo.put("title", "Delete database " + databaseName);
+      jobInfo.put("forcedContent", deleteQuery);
+      jobInfo.put("dataBase", databaseName);
+
+      try {
+        Job job = new JobImpl(jobInfo);
+        JobController createdJobController = new 
JobServiceInternal().createJob(job, resourceManager);
+        Job returnableJob = createdJobController.getJobPOJO();
+        LOG.info("returning job with id {} for deleting database {}", 
returnableJob.getId(), databaseName);
+        return returnableJob;
+      } catch (Throwable e) {
+        LOG.error("Exception occurred while renaming the table for rename 
Query : {}", deleteQuery, e);
+        throw new ServiceException(e);
+      }
+    }else{
+      throw new ServiceException("Failed to generate delete database query for 
database " + databaseName);
+    }
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/8e646eb2/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLService.java
----------------------------------------------------------------------
diff --git 
a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLService.java
 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLService.java
index 65647ee..b82dd3f 100644
--- 
a/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLService.java
+++ 
b/contrib/views/hive20/src/main/java/org/apache/ambari/view/hive20/resources/browser/DDLService.java
@@ -94,6 +94,21 @@ public class DDLService extends BaseService {
     return Response.ok(response).build();
   }
 
+  @DELETE
+  @Path("databases/{database_id}")
+  @Produces(MediaType.APPLICATION_JSON)
+  public Response deleteDatabase(@PathParam("database_id") String databaseId) {
+    Job job = null;
+    try {
+      job = proxy.deleteDatabase(databaseId, getResourceManager());
+      JSONObject response = new JSONObject();
+      response.put("job", job);
+      return Response.status(Response.Status.ACCEPTED).entity(job).build();
+    } catch (ServiceException e) {
+      LOG.error("Exception occurred while delete database {}", databaseId, e);
+      throw new ServiceFormattedException(e);
+    }
+  }
 
   @GET
   @Path("databases/{database_id}/tables")

http://git-wip-us.apache.org/repos/asf/ambari/blob/8e646eb2/contrib/views/hive20/src/test/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGeneratorSpecTest.groovy
----------------------------------------------------------------------
diff --git 
a/contrib/views/hive20/src/test/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGeneratorSpecTest.groovy
 
b/contrib/views/hive20/src/test/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGeneratorSpecTest.groovy
new file mode 100644
index 0000000..418e190
--- /dev/null
+++ 
b/contrib/views/hive20/src/test/java/org/apache/ambari/view/hive20/internal/query/generators/DeleteDatabaseQueryGeneratorSpecTest.groovy
@@ -0,0 +1,65 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements.  See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership.  The ASF licenses this file
+* to you 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.
+*/
+package org.apache.ambari.view.hive20.internal.query.generators
+
+import com.google.common.base.Optional
+import org.apache.ambari.view.hive20.exceptions.ServiceException
+import spock.lang.Specification
+
+class DeleteDatabaseQueryGeneratorSpecTest extends Specification {
+  def "correct drop database"() {
+    setup:
+    DeleteDatabaseQueryGenerator generator = new 
DeleteDatabaseQueryGenerator("databaseName");
+
+    when:
+    Optional<String> databaseDeleteQuery = generator.getQuery()
+
+    then:
+    databaseDeleteQuery.isPresent()
+
+    when:
+    String query = databaseDeleteQuery.get();
+
+    then:
+    query == "drop database `databaseName`"
+  }
+
+  def "sending null as database name throws exception"() {
+    setup:
+    DeleteDatabaseQueryGenerator generator = new 
DeleteDatabaseQueryGenerator(null);
+
+    when:
+    Optional<String> databaseDeleteQuery = generator.getQuery()
+
+    then:
+    def exception = thrown(ServiceException)
+    exception.message == "Database name cannot be null or empty."
+  }
+
+  def "sending enpty string as database name throws exception"() {
+    setup:
+    DeleteDatabaseQueryGenerator generator = new 
DeleteDatabaseQueryGenerator(null);
+
+    when:
+    Optional<String> databaseDeleteQuery = generator.getQuery()
+
+    then:
+    def exception = thrown(ServiceException)
+    exception.message == "Database name cannot be null or empty."
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/8e646eb2/contrib/views/hive20/src/test/rest/postman/hive20.postman_collection.json
----------------------------------------------------------------------
diff --git 
a/contrib/views/hive20/src/test/rest/postman/hive20.postman_collection.json 
b/contrib/views/hive20/src/test/rest/postman/hive20.postman_collection.json
index b6d84b3..d674944 100644
--- a/contrib/views/hive20/src/test/rest/postman/hive20.postman_collection.json
+++ b/contrib/views/hive20/src/test/rest/postman/hive20.postman_collection.json
@@ -354,6 +354,89 @@
                                "description": "RENAME TABLE"
                        },
                        "response": []
+               },
+               {
+                       "name": "fetch job",
+                       "request": {
+                               "auth": {
+                                       "type": "basic",
+                                       "basic": {
+                                               "username": "admin",
+                                               "password": "admin",
+                                               "saveHelperData": true,
+                                               "showPassword": false
+                                       }
+                               },
+                               "url": "{{APP_BASE_URL}}/resources/jobs/202",
+                               "method": "GET",
+                               "header": [
+                                       {
+                                               "key": "X-Requested-By",
+                                               "value": "ambari",
+                                               "description": ""
+                                       },
+                                       {
+                                               "key": "Authorization",
+                                               "value": "Basic 
YWRtaW46YWRtaW4=",
+                                               "description": ""
+                                       },
+                                       {
+                                               "key": "Content-Type",
+                                               "value": 
"application/x-www-form-urlencoded",
+                                               "description": ""
+                                       }
+                               ],
+                               "body": {
+                                       "mode": "urlencoded",
+                                       "urlencoded": [
+                                               {
+                                                       "key": 
"new_database_id",
+                                                       "value": "default",
+                                                       "type": "text",
+                                                       "enabled": true
+                                               },
+                                               {
+                                                       "key": "new_table_id",
+                                                       "value": "t2",
+                                                       "type": "text",
+                                                       "enabled": true
+                                               }
+                                       ]
+                               },
+                               "description": "fetches the job"
+                       },
+                       "response": []
+               },
+               {
+                       "name": "delete  database",
+                       "request": {
+                               "auth": {
+                                       "type": "basic",
+                                       "basic": {
+                                               "username": "admin",
+                                               "password": "admin",
+                                               "saveHelperData": true,
+                                               "showPassword": false
+                                       }
+                               },
+                               "url": 
"{{APP_BASE_URL}}/resources/ddl/databases/d2",
+                               "method": "DELETE",
+                               "header": [
+                                       {
+                                               "key": "X-Requested-By",
+                                               "value": "ambari",
+                                               "description": ""
+                                       },
+                                       {
+                                               "key": "Authorization",
+                                               "value": "Basic 
YWRtaW46YWRtaW4=",
+                                               "description": ""
+                                       }
+                               ],
+                               "body": {},
+                               "description": "drop database "
+                       },
+                       "response": []
                }
        ]
 }
\ No newline at end of file

Reply via email to