yifan-c commented on code in PR #36:
URL: https://github.com/apache/cassandra-sidecar/pull/36#discussion_r961171392


##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->
+                   {
+                       KeyspaceMetadata ksMetadata;
+                       if (metadata == null)
+                       {
+                           LOGGER.error("Failed to obtain keyspace metadata 
for request '{}'", requestParams);
+                           
context.fail(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
+                       }
+                       else if (requestParams.getKeyspace() == null)
+                       {
+                           // keyspaces request
+                           buildKeyspacesResponse(context, metadata);
+                       }
+                       else if ((ksMetadata = 
metadata.getKeyspace(requestParams.getKeyspace())) == null)
+                       {
+                           // keyspace does not exist
+                           String errorMessage = String.format("Keyspace '%s' 
does not exist.",
+                                                               
requestParams.getKeyspace());
+                           context.fail(new 
HttpException(HttpResponseStatus.NOT_FOUND.code(), errorMessage));
+                       }
+                       else if (requestParams.getTableName() == null)
+                       {
+                           // keyspace request
+                           buildKeyspaceResponse(context, ksMetadata);
+                       }
+                       else
+                       {
+                           TableMetadata tableMetadata = 
ksMetadata.getTable(requestParams.getTableName());
+
+                           if (tableMetadata == null)
+                           {
+                               String errorMessage = String.format("Table '%s' 
does not exist in the '%s' keyspace",
+                                                                   
requestParams.getTableName(),
+                                                                   
requestParams.getKeyspace());
+                               context.fail(new 
HttpException(HttpResponseStatus.NOT_FOUND.code(), errorMessage));
+                           }
+                           else
+                           {
+                               // keyspace / table request
+                               buildTableResponse(context, tableMetadata);

Review Comment:
   and.. `getTable` to be consistent.



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->
+                   {
+                       KeyspaceMetadata ksMetadata;
+                       if (metadata == null)
+                       {
+                           LOGGER.error("Failed to obtain keyspace metadata 
for request '{}'", requestParams);
+                           
context.fail(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());

Review Comment:
   nit: return early and move `ksMetadata` declaration after the check. 



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->
+                   {
+                       KeyspaceMetadata ksMetadata;
+                       if (metadata == null)
+                       {
+                           LOGGER.error("Failed to obtain keyspace metadata 
for request '{}'", requestParams);
+                           
context.fail(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
+                       }
+                       else if (requestParams.getKeyspace() == null)
+                       {
+                           // keyspaces request
+                           buildKeyspacesResponse(context, metadata);
+                       }
+                       else if ((ksMetadata = 
metadata.getKeyspace(requestParams.getKeyspace())) == null)
+                       {
+                           // keyspace does not exist
+                           String errorMessage = String.format("Keyspace '%s' 
does not exist.",
+                                                               
requestParams.getKeyspace());
+                           context.fail(new 
HttpException(HttpResponseStatus.NOT_FOUND.code(), errorMessage));
+                       }
+                       else if (requestParams.getTableName() == null)
+                       {
+                           // keyspace request
+                           buildKeyspaceResponse(context, ksMetadata);
+                       }
+                       else
+                       {
+                           TableMetadata tableMetadata = 
ksMetadata.getTable(requestParams.getTableName());
+
+                           if (tableMetadata == null)
+                           {
+                               String errorMessage = String.format("Table '%s' 
does not exist in the '%s' keyspace",
+                                                                   
requestParams.getTableName(),
+                                                                   
requestParams.getKeyspace());
+                               context.fail(new 
HttpException(HttpResponseStatus.NOT_FOUND.code(), errorMessage));
+                           }
+                           else
+                           {
+                               // keyspace / table request
+                               buildTableResponse(context, tableMetadata);
+                           }
+                       }
+                   });

Review Comment:
   A bit more verbose alternative is the following. It separate the value 
assignment apart from conditional statement to make it easier to read. 
   
   ```java
   void handleWithInstanceMetadata(context, metadata)
   {
     if (metadata == null)
     {
       // set request as failed and return
     }
   
     if (has no keyspace param)
     {
       // list keyspaces and return
     }
   
     // retrive keyspace metadata
   
     if (keyspace metadata does not exist)
     {
       // set request as failed and return
     }
     
     if (has no table param)
     {
       // get keyspace and return
     }
   
     // retrieve table metadata
   
     if (table metadata does not exist)
     {
       // fail and return
     }
   
     // get table and return
   }
   ```



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->
+                   {
+                       KeyspaceMetadata ksMetadata;
+                       if (metadata == null)
+                       {
+                           LOGGER.error("Failed to obtain keyspace metadata 
for request '{}'", requestParams);
+                           
context.fail(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
+                       }
+                       else if (requestParams.getKeyspace() == null)
+                       {
+                           // keyspaces request
+                           buildKeyspacesResponse(context, metadata);
+                       }
+                       else if ((ksMetadata = 
metadata.getKeyspace(requestParams.getKeyspace())) == null)
+                       {
+                           // keyspace does not exist
+                           String errorMessage = String.format("Keyspace '%s' 
does not exist.",
+                                                               
requestParams.getKeyspace());
+                           context.fail(new 
HttpException(HttpResponseStatus.NOT_FOUND.code(), errorMessage));
+                       }
+                       else if (requestParams.getTableName() == null)
+                       {
+                           // keyspace request
+                           buildKeyspaceResponse(context, ksMetadata);

Review Comment:
   let's call it `getKeyspace` or `getTables`. Both have the same meaning.
   
   So that it is much easier to distinguish between `buildKeyspacesResponse` 
and `buildKeyspaceResponse`



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->
+                   {
+                       KeyspaceMetadata ksMetadata;
+                       if (metadata == null)
+                       {
+                           LOGGER.error("Failed to obtain keyspace metadata 
for request '{}'", requestParams);
+                           
context.fail(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
+                       }
+                       else if (requestParams.getKeyspace() == null)
+                       {
+                           // keyspaces request
+                           buildKeyspacesResponse(context, metadata);

Review Comment:
   let's call it `listKeyspaces`



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {
+            LOGGER.error("Failed to obtain keyspace metadata for request 
'{}'", requestParams, throwable);
+            context.fail(new 
HttpException(HttpResponseStatus.SERVICE_UNAVAILABLE.code(),
+                                           "Unable to reach Cassandra service",
+                                           throwable));
+        })
+        .onSuccess(metadata ->

Review Comment:
   How about move it into a method? For a lambda, it is quite long. 



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->

Review Comment:
   nit: indent the chained invocations. 



##########
src/main/java/org/apache/cassandra/sidecar/routes/KeyspacesHandler.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cassandra.sidecar.routes;
+
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.KeyspaceMetadata;
+import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TableMetadata;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.HttpException;
+import org.apache.cassandra.sidecar.cluster.InstancesConfig;
+import org.apache.cassandra.sidecar.cluster.instance.InstanceMetadata;
+import org.apache.cassandra.sidecar.common.data.KeyspaceRequest;
+import org.apache.cassandra.sidecar.common.data.KeyspaceSchema;
+import org.apache.cassandra.sidecar.common.data.TableSchema;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Handler for getting information about keyspace / tables from a specific 
Cassandra instance
+ */
+@Singleton
+public class KeyspacesHandler extends AbstractHandler
+{
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(KeyspacesHandler.class);
+    private final Vertx vertx;
+
+    @Inject
+    public KeyspacesHandler(Vertx vertx, InstancesConfig instancesConfig)
+    {
+        super(instancesConfig);
+        this.vertx = vertx;
+    }
+
+    /**
+     * Handles {@code GET} and {@code HEAD} requests for Keyspaces.
+     * For keyspaces requests only {@code GET} is supported, and it will 
produce a list of keyspaces with table and
+     * schema information. For keyspace and table requests both {@code GET} 
and {@code HEAD} requests are supported.
+     *
+     * @param context the event to handle
+     */
+    @Override
+    public void handle(RoutingContext context)
+    {
+        KeyspaceRequest requestParams = extractParamsOrThrow(context);
+        String host = getHost(context);
+        SocketAddress remoteAddress = context.request().remoteAddress();
+        InstanceMetadata instanceMeta = instancesConfig.instanceFromHost(host);
+        LOGGER.debug("KeyspacesHandler received request: {} from: {}. 
Instance: {}",
+                     requestParams, remoteAddress, host);
+
+        getMetadata(instanceMeta)
+        .onFailure(throwable ->
+        {

Review Comment:
   for lambda, we can have the `{` right after `->`, rather than the style in 
the `onSuccess` block, which waste the heading spaces for each line. 



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to