/**
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * Licensed 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 com.linkedin.pinot.tools.admin.command;

import com.linkedin.pinot.tools.Command;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.methods.GetMethod;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ChangeTableState extends AbstractBaseAdminCommand implements Command {
  private static final Logger LOGGER = LoggerFactory.getLogger(ChangeTableState.class);

  @Option (name = "-controllerHost", required = false, metaVar = "<String>", usage = "host name for controller")
  private String _controllerHost;

  @Option (name = "-controllerPort", required = false, metaVar = "<int>", usage = "Port number for controller.")
  private String _controllerPort = DEFAULT_CONTROLLER_PORT;

  @Option (name = "-tableName", required = true, metaVar = "<String>", usage = "Table name to disable")
  private String _tableName;

  @Option (name = "-state", required = true, metaVar = "<String>", usage = "Change Table State(enable|disable|drop)")
  private String _state;

  @Option (name = "-help", required = false, help = true, aliases = { "-h", "--h", "--help" },
      usage = "Print this message.")
  private boolean _help = false;

  @Override
  public boolean execute()
      throws Exception {
    String stateValue = _state.toLowerCase();
    if ( ! stateValue.equals("enable") &&
         ! stateValue.equals("disable") &&
         ! stateValue.equals("drop")) {
      throw new IllegalArgumentException("Invalid value for state: " + _state
      + "\n Value must be one of enable|disable|drop");
    }
    HttpClient httpClient = new HttpClient();
    HttpURL url = new HttpURL(_controllerHost,
        Integer.parseInt(_controllerPort),
        URI_TABLES_PATH + _tableName);
    url.setQuery("state", stateValue);
    GetMethod httpGet = new GetMethod(url.getEscapedURI());
    int status = httpClient.executeMethod(httpGet);
    if (status != 200) {
      throw new RuntimeException(
          "Failed to change table state, error: " + httpGet.getResponseBodyAsString());
    }
    return true;
  }

  @Override
  public String description() {
    return "Change the state (enable|disable|drop) of Pinot table";
  }

  public boolean getHelp() {
    return _help;
  }

  @Override
  public String getName() {
    return "ChangeTableState";
  }

  @Override
  public String toString() {
    return ("ChangeTableState -controllerHost " + _controllerHost
        + " -controllerPort " + _controllerPort
        + " -tableName" + _tableName
        + " -state" + _state);
  }

  @Override
  public void cleanup() {

  }

}

@@ -681,16 +681,64 @@
 mmand;%0A%0A
+import com.linkedin.pinot.common.utils.NetUtil;%0A
 import c
@@ -1991,22 +1991,16 @@
 xecute()
-%0A     
  throws 
@@ -2011,16 +2011,108 @@
 ption %7B%0A
+    if (_controllerHost == null) %7B%0A      _controllerHost = NetUtil.getHostAddress();%0A    %7D%0A%0A
     Stri
@@ -2157,19 +2157,17 @@
     if (
- ! 
+!
 stateVal
@@ -2181,27 +2181,24 @@
 ls(%22enable%22)
- &&
 %0A         ! 
@@ -2186,35 +2186,36 @@
 nable%22)%0A        
+&&
  !
- 
 stateValue.equal
@@ -2226,19 +2226,16 @@
 isable%22)
- &&
 %0A       
@@ -2239,11 +2239,12 @@
     
+&&
  !
- 
 stat
@@ -2346,16 +2346,20 @@
  _state%0A
+    
       + 

Add default hostname to ChangeTableState class