/*
 *
 *  Copyright 2013 Justin Santa Barbara.
 *
 *     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.netflix.simianarmy.manic;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.netflix.simianarmy.CloudClient;
import com.netflix.simianarmy.MonkeyConfiguration;
import com.netflix.simianarmy.chaos.ChaosInstance;
import com.netflix.simianarmy.chaos.ChaosType;
import com.netflix.simianarmy.client.gcloud.BasicClient;

/**
 * Shuts down the instance using the cloud instance-termination API.
 *
 * This is the classic chaos-monkey strategy.
 */
public class StartInstanceChaosType extends ChaosType {
	/**
	 * Constructor.
	 *
	 * @param config
	 *            Configuration to use
	 */
	public StartInstanceChaosType(MonkeyConfiguration config) {
		super(config, "StartInstance");
	}

	public boolean canApply(ChaosInstance instance) {
		CloudClient cloudClient = instance.getCloudClient();
		String instanceId = instance.getInstanceId();

		if (cloudClient instanceof BasicClient) {
			try {
				BasicClient gceClient = (BasicClient) cloudClient;
				switch (gceClient.get(instanceId).getStatus()) {
				case TERMINATED:
				case SUSPENDED:
					return true;
				default:
					return false;

				}
			} catch (IOException ex) {
				ex.printStackTrace();

				return super.canApply(instance);
			}
		} else {
			return super.canApply(instance);
		}
	}

	/**
	 * Shuts down the instance.
	 */
	@Override
	public void apply(ChaosInstance instance) {
		CloudClient cloudClient = instance.getCloudClient();
		String instanceId = instance.getInstanceId();

		try {
			Method method = cloudClient.getClass().getMethod("start", String.class);
			if (method != null) {
				method.invoke(cloudClient, instanceId);
			}
		} catch (NoSuchMethodException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}

@@ -711,93 +711,8 @@
 ion;
-%0Aimport java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;
 %0A%0Aim
@@ -2047,198 +2047,149 @@
 ();%0A
+%0A
 %09%09
-String instanceId = instance.getInstanceId();%0A%0A%09%09try %7B%0A%09%09%09Method method = cloudClient.getClass().getMethod(%22start%22, String.class);%0A%09%09%09if (method != null) %7B%0A%09%09%09%09method.invoke(cloudClient, i
+if (cloudClient instanceof BasicClient) %7B%0A%09%09%09BasicClient gceClient = (BasicClient) cloudClient;%0A%09%09%09try %7B%0A%09%09%09%09gceClient.start(instance.getI
 nsta
@@ -2197,27 +2197,25 @@
 ceId
+()
 );%0A%09%09%09%7D
-%0A%09%09%7D
  catch (
 NoSu
@@ -2214,20 +2214,10 @@
 ch (
-NoSuchMethod
+IO
 Exce
@@ -2227,235 +2227,141 @@
 on e
-x
 ) %7B%0A%09%09%09
-ex.printStackTrace();%0A%09%09%7D catch (IllegalAccessException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (IllegalArgumentException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (InvocationTargetException e) %7B%0A%09%09%09e.printStackTrace
+%09// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%09return;%0A%09%09%7D%0A%09%09throw new UnsupportedOperationException
 ();%0A
-%09%09%7D
 %0A%09%7D%0A

fix error in start instance type