pkuwm commented on a change in pull request #849: Add REST APIs for get, set, 
update RestConfig
URL: https://github.com/apache/helix/pull/849#discussion_r390583078
 
 

 ##########
 File path: 
helix-rest/src/main/java/org/apache/helix/rest/server/resources/helix/ClusterAccessor.java
 ##########
 @@ -511,6 +512,121 @@ public Response 
removeClusterStateModelDefinition(@PathParam("clusterId") String
     return OK();
   }
 
+  @PUT
+  @Path("{clusterId}/restconfig")
+  public Response createRESTConfig(@PathParam("clusterId") String clusterId,
+      String content) {
+    ZNRecord record;
+    try {
+      record = toZNRecord(content);
+    } catch (IOException e) {
+      _logger.error("Failed to deserialize user's input " + content + ", 
Exception: " + e);
+      return badRequest("Input is not a valid ZNRecord!");
+    }
+
+    if (!record.getId().equals(clusterId)) {
+      return badRequest("ID does not match the cluster name in input!");
+    }
+
+    RESTConfig config = new RESTConfig(record);
+    ConfigAccessor configAccessor = getConfigAccessor();
+    try {
+      configAccessor.setRESTConfig(clusterId, config);
+    } catch (HelixException ex) {
+      // TODO: Could use a more generic error for HelixException
+      return notFound(ex.getMessage());
+    } catch (Exception ex) {
+      _logger.error("Failed to create rest config, cluster " + clusterId + " 
new config: " + content
+          + ", Exception: " + ex);
+      return serverError(ex);
+    }
+    return OK();
+  }
+
+  @POST
+  @Path("{clusterId}/restconfig")
+  public Response updateRESTConfig(@PathParam("clusterId") String clusterId,
+      @QueryParam("command") String commandStr, String content) {
+    Command command;
+    try {
+      command = getCommand(commandStr);
+    } catch (HelixException ex) {
+      return badRequest(ex.getMessage());
+    }
+
+    ZNRecord record;
+    try {
+      record = toZNRecord(content);
+    } catch (IOException e) {
+      _logger.error("Failed to deserialize user's input " + content + ", 
Exception: " + e);
+      return badRequest("Input is not a valid ZNRecord!");
+    }
+
+    RESTConfig config = new RESTConfig(record);
+    ConfigAccessor configAccessor = getConfigAccessor();
+    try {
+      switch (command) {
+        case update:
+          configAccessor.updateRESTConfig(clusterId, config);
+          break;
+        case delete: {
+          HelixConfigScope scope =
+              new 
HelixConfigScopeBuilder(HelixConfigScope.ConfigScopeProperty.REST)
+                  .forCluster(clusterId).build();
+          configAccessor.remove(scope, config.getRecord());
+        }
+        break;
+        default:
+          return badRequest("Unsupported command " + commandStr);
+      }
+    } catch (HelixException ex) {
+      return notFound(ex.getMessage());
+    } catch (Exception ex) {
+      _logger.error(
+          "Failed to " + command + " rest config, cluster " + clusterId + " 
new config: " + content
+              + ", Exception: " + ex);
+      return serverError(ex);
+    }
+    return OK();
+  }
+
+  @GET
+  @Path("{clusterId}/restconfig")
+  public Response getRESTConfig(@PathParam("clusterId") String clusterId) {
+    ConfigAccessor accessor = getConfigAccessor();
+    RESTConfig config = null;
+    try {
+      config = accessor.getRESTConfig(clusterId);
+    } catch (HelixException ex) {
+      _logger.info(
+          "Failed to get rest config for cluster " + clusterId + ", cluster 
not found, Exception: "
+              + ex);
+    } catch (Exception ex) {
+      _logger.error("Failed to get rest config for cluster " + clusterId + " 
Exception: " + ex);
+      return serverError(ex);
+    }
+    if (config == null) {
+      return notFound();
+    }
+    return JSONRepresentation(config.getRecord());
+  }
+
+  @DELETE
+  @Path("{clusterId}/restconfig")
+  public Response deleteRESTConfig(@PathParam("clusterId") String clusterId) {
+    ConfigAccessor accessor = getConfigAccessor();
+    try {
+      accessor.deleteRESTConfig(clusterId);
+    } catch (HelixException ex) {
+      _logger.info("Failed to delete rest config for cluster " + clusterId
 
 Review comment:
   Failed to delete but finally returning OK response? Would 404 be more 
appropriate?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to