package org.wikimedia.elasticsearch.swift.repositories;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.javaswift.joss.client.factory.AccountConfig;
import org.javaswift.joss.client.factory.AccountFactory;
import org.javaswift.joss.client.factory.AuthenticationMethod;
import org.javaswift.joss.exception.CommandException;
import org.javaswift.joss.model.Account;

public class SwiftService extends AbstractLifecycleComponent<SwiftService> {
	// The account we'll be connecting to Swift with
	private Account swiftUser;

	/**
	 * Constructor
	 * 
	 * @param settings
	 *            Settings for our repository. Injected.
	 */
	@Inject
	public SwiftService(Settings settings) {
		super(settings);
	}

	/**
	 * Create a Swift account object and connect it to Swift
	 * 
	 * @param url
	 *            The auth url (eg: localhost:8080/auth/v1.0/)
	 * @param username
	 *            The username
	 * @param password
	 *            The password
	 */
	public synchronized Account swiftBasic(String url, String username, String password) {
		if (swiftUser != null) {
			return swiftUser;
		}

		try {
			AccountConfig conf = new AccountConfig();
			conf.setUsername(username);
			conf.setPassword(password);
			conf.setAuthUrl(url);
			conf.setAuthenticationMethod(AuthenticationMethod.BASIC);
			swiftUser = new AccountFactory(conf).createAccount();
		} catch (CommandException ce) {
			throw new ElasticsearchIllegalArgumentException("Unable to authenticate to Swift Basic " + url + "/" + username + "/" + password, ce);
		}
		return swiftUser;
	}

	public synchronized Account swiftKeyStone(String url, String username, String password, String tenantName) {
		if (swiftUser != null) {
			return swiftUser;
		}

		try {
			AccountConfig conf = new AccountConfig();
			conf.setUsername(username);
			conf.setPassword(password);
			conf.setAuthUrl(url);
			conf.setTenantName(tenantName);
			conf.setAuthenticationMethod(AuthenticationMethod.KEYSTONE);
			swiftUser = new AccountFactory(conf).createAccount();
		} catch (CommandException ce) {
			throw new ElasticsearchIllegalArgumentException(
					"Unable to authenticate to Swift Keystone " + url + "/" + username + "/" + password + "/" + tenantName, ce);
		}
		return swiftUser;
	}

	public synchronized Account swiftTempAuth(String url, String username, String password) {
		if (swiftUser != null) {
			return swiftUser;
		}

		try {
			AccountConfig conf = new AccountConfig();
			conf.setUsername(username);
			conf.setPassword(password);
			conf.setAuthUrl(url);
			conf.setAuthenticationMethod(AuthenticationMethod.TEMPAUTH);
			swiftUser = new AccountFactory(conf).createAccount();
		} catch (CommandException ce) {
			throw new ElasticsearchIllegalArgumentException("Unable to authenticate to Swift Temp", ce);
		}
		return swiftUser;
	}

	/**
	 * Start the service. No-op here.
	 */
	@Override
	protected void doStart() throws ElasticsearchException {
	}

	/**
	 * Stop the service. No-op here.
	 */
	@Override
	protected void doStop() throws ElasticsearchException {
	}

	/**
	 * Close the service. No-op here.
	 */
	@Override
	protected void doClose() throws ElasticsearchException {
	}
}

@@ -1359,148 +1359,51 @@
 f = 
-new AccountConfig();%0A%09%09%09conf.setUsername(username);%0A%09%09%09conf.setPassword(password);%0A%09%09%09conf.setAuthUrl(url);%0A%09%09%09conf.setAuthenticationMethod(
+getStandardConfig(url, username, password, 
 Auth
@@ -1883,212 +1883,115 @@
 f = 
-new AccountConfig();%0A%09%09%09conf.setUsername(username);%0A%09%09%09conf.setPassword(password);%0A%09%09%09conf.setAuthUrl(url);%0A%09%09%09conf.setTenantName(tenantName);%0A%09%09%09conf.setAuthenticationMethod(AuthenticationMethod.KEYSTONE
+getStandardConfig(url, username, password, AuthenticationMethod.KEYSTONE);%0A%09%09%09conf.setTenantName(tenantName
 );%0A%09
@@ -2454,148 +2454,51 @@
 f = 
-new AccountConfig();%0A%09%09%09conf.setUsername(username);%0A%09%09%09conf.setPassword(password);%0A%09%09%09conf.setAuthUrl(url);%0A%09%09%09conf.setAuthenticationMethod(
+getStandardConfig(url, username, password, 
 Auth
@@ -2736,24 +2736,329 @@
 ftUser;%0A%09%7D%0A%0A
+%09private AccountConfig getStandardConfig(String url, String username, String password, AuthenticationMethod method) %7B%0A%09%09AccountConfig conf = new AccountConfig();%0A%09%09conf.setAuthUrl(url);%0A%09%09conf.setUsername(username);%0A%09%09conf.setPassword(password);%0A%09%09conf.setAuthenticationMethod(method);%0A%09%09return conf;%0A%09%7D%0A%0A
 %09/**%0A%09 * Sta

Reduce config duplication