Commit 5e7b02a4 authored by Román Sosa's avatar Román Sosa

Merge branch 'base'

parents 9791c1dc 35dd10ae
......@@ -16,8 +16,7 @@
*/
package eu.cloudsocket.paas.client;
import static eu.atos.paas.credentials.ApiUserPasswordCredentials.PASSWORD;
import static eu.atos.paas.credentials.ApiUserPasswordCredentials.USER;
import static eu.atos.paas.credentials.ApiKeyCredentials.API_KEY;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;
......@@ -52,13 +51,11 @@ public class InvoiceNinjaIT {
client = new RestClient(TestConstants.SERVER_URL);
CredentialsMap credentials = CredentialsMap.builder()
.item(USER, TestConfigProperties.getInstance().getHeroku_user())
.item(PASSWORD, TestConfigProperties.getInstance().getHeroku_password())
.item(API_KEY, TestConfigProperties.getInstance().getHeroku_apiKey())
.build();
provider = client.getProvider("heroku", credentials);
}
@Test
public void createApplication() throws IOException {
ApplicationToCreate appToCreate = new ApplicationToCreate(
......
......@@ -29,31 +29,19 @@ public class HerokuClient implements PaasClient {
@Override
public PaasSession getSession(Credentials credentials) {
PaasSession session = null;
if (credentials instanceof UserPasswordCredentials) {
session = getSession((UserPasswordCredentials)credentials);
}
else if (credentials instanceof ApiKeyCredentials) {
if (credentials instanceof ApiKeyCredentials) {
session = getSession((ApiKeyCredentials)credentials);
} else {
throw new UnsupportedOperationException("Credentials of class " + credentials.getClass().getName() + " not supported (Heroku)");
throw new UnsupportedOperationException("Please, login with apikey");
}
return session;
}
private PaasSession getSession(UserPasswordCredentials credentials) {
HerokuConnector connector = new HerokuConnector(credentials.getUser(), credentials.getPassword());
PaasSession session = new HerokuSession(connector);
return session;
}
private PaasSession getSession(ApiKeyCredentials credentials) {
HerokuConnector connector = new HerokuConnector(credentials.getApiKey());
......
......@@ -36,6 +36,7 @@ import com.heroku.api.Heroku;
import com.heroku.api.Addon;
import com.heroku.api.AddonChange;
import com.heroku.api.App;
import com.heroku.api.Formation;
import com.heroku.api.HerokuAPI;
import com.heroku.sdk.deploy.DeployWar;
......@@ -107,9 +108,7 @@ public class HerokuConnector
*/
public HerokuConnector(String login, String passwd)
{
logger.debug(">> Connecting to Heroku ...");
apiKey = HerokuAPI.obtainApiKey(login, passwd);
_hApiClient = connect();
throw new UnsupportedOperationException("You must login using apikey");
}
......@@ -361,7 +360,7 @@ public class HerokuConnector
if (addon != null)
{
AddonChange addChange = _hApiClient.addAddon(applicationName, addon_plan);
logger.debug(">> Addon installed: " + addChange.getStatus());
logger.debug(">> Addon installed: " + addChange.getState());
return true;
}
else
......@@ -402,6 +401,17 @@ public class HerokuConnector
return null;
}
public void addConfig(String applicationName, String key, String value) {
Map<String, String> envValues = _hApiClient.listConfig(applicationName);
envValues.put(key, value);
_hApiClient.updateConfig(applicationName, envValues);
}
public void removeConfig(String applicationName, String key) {
Map<String, String> envValues = _hApiClient.listConfig(applicationName);
envValues.put(key, null);
_hApiClient.updateConfig(applicationName, envValues);
}
/**
*
......@@ -424,6 +434,11 @@ public class HerokuConnector
return false;
}
public void scaleProcess(String applicationName, String applicationType, int n) {
@SuppressWarnings("unused")
Formation f = getHerokuAPIClient().scale(applicationName, applicationType, n);
}
/**
*
......
......@@ -16,7 +16,6 @@
*/
package eu.atos.paas.heroku;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
......@@ -27,6 +26,7 @@ import org.springframework.http.HttpStatus;
import com.heroku.api.Addon;
import com.heroku.api.App;
import com.heroku.api.Formation;
import com.heroku.api.exception.HerokuAPIException;
import com.heroku.api.exception.RequestFailedException;
......@@ -81,7 +81,7 @@ public class HerokuSession implements PaasSession {
throw new AlreadyExistsException(moduleName);
}
connector.createApp(moduleName);
connector.getHerokuAPIClient().addConfig(moduleName, Collections.singletonMap(UNDEPLOYED_FLAG, "1"));
connector.addConfig(moduleName, UNDEPLOYED_FLAG, "1");
return getModule(moduleName);
......@@ -118,7 +118,7 @@ public class HerokuSession implements PaasSession {
if (!deployed) {
throw new PaasException("Application not deployed");
}
connector.getHerokuAPIClient().removeConfig(moduleName, UNDEPLOYED_FLAG);
connector.removeConfig(moduleName, UNDEPLOYED_FLAG);
return getModule(moduleName);
} catch (HerokuAPIException e) {
......@@ -185,11 +185,11 @@ public class HerokuSession implements PaasSession {
switch (command)
{
case START:
connector.getHerokuAPIClient().scaleProcess(module.getName(), module.getAppType(), 1);
connector.scaleProcess(module.getName(), module.getAppType(), 1);
break;
case STOP:
connector.getHerokuAPIClient().scaleProcess(module.getName(), module.getAppType(), 0);
connector.scaleProcess(module.getName(), module.getAppType(), 0);
break;
default:
......@@ -210,12 +210,12 @@ public class HerokuSession implements PaasSession {
switch (command)
{
case SCALE_UP_INSTANCES:
connector.getHerokuAPIClient().scaleProcess(module.getName(), module.getAppType(), module.getRunningInstances() + 1);
connector.scaleProcess(module.getName(), module.getAppType(), module.getRunningInstances() + 1);
break;
case SCALE_DOWN_INSTANCES:
if (module.getRunningInstances() > 1) {
connector.getHerokuAPIClient().scaleProcess(module.getName(), module.getAppType(), module.getRunningInstances() - 1);
connector.scaleProcess(module.getName(), module.getAppType(), module.getRunningInstances() - 1);
}
break;
......@@ -234,7 +234,7 @@ public class HerokuSession implements PaasSession {
switch (command)
{
case SCALE_INSTANCES:
connector.getHerokuAPIClient().scaleProcess(module.getName(), module.getAppType(), scale_value);
connector.scaleProcess(module.getName(), module.getAppType(), scale_value);
break;
case SCALE_MEMORY:
......@@ -273,7 +273,8 @@ public class HerokuSession implements PaasSession {
app = connector.getHerokuAPIClient().getApp(moduleName);
List<Addon> l = connector.getHerokuAPIClient().listAppAddons(moduleName);
Map<String, String> m = connector.getHerokuAPIClient().listConfig(moduleName);
return new ModuleImpl(app, l, m);
List<Formation> formations = connector.getHerokuAPIClient().listFormation(moduleName);
return new ModuleImpl(app, l, m, formations);
} catch (RequestFailedException e) {
......
......@@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map;
import com.heroku.api.Addon;
import com.heroku.api.App;
import com.heroku.api.Formation;
import eu.atos.paas.Module;
......@@ -35,30 +36,33 @@ import eu.atos.paas.Module;
*
*/
public class ModuleImpl implements Module {
private App app;
private List<String> lServices;
private Map<String, String> mEnv;
private URL url;
private URL gitUrl;
private State state;
private static final Formation EMPTY_FORMATION = new Formation();
static {
EMPTY_FORMATION.setQuantity(0);
EMPTY_FORMATION.setType("");
}
private final App app;
private final List<String> lServices;
private final Map<String, String> mEnv;
private final URL url;
private final URL gitUrl;
private final State state;
private final Formation formation;
/**
*
* @param app
*/
public ModuleImpl(App app) {
this(app, Collections.<Addon>emptyList(), Collections.<String, String>emptyMap());
this.app = app;
lServices = new ArrayList<String>(0);
public ModuleImpl(App app, List<Formation> formations) {
this(app, Collections.<Addon>emptyList(), Collections.<String, String>emptyMap(), formations);
}
public ModuleImpl(App app, List<Addon> serviceList, Map<String, String> env) {
public ModuleImpl(App app, List<Addon> serviceList, Map<String, String> env, List<Formation> formations) {
this.app = app;
this.formation = findFormation(formations);
this.url = urlFromString(app.getWebUrl());
this.gitUrl = urlFromString(app.getGitUrl());
......@@ -77,7 +81,7 @@ public class ModuleImpl implements Module {
this.state = State.UNDEPLOYED;
}
else {
this.state = app.getDynos() > 0? State.STARTED : State.STOPPED;
this.state = formation.getQuantity() > 0? State.STARTED : State.STOPPED;
}
}
......@@ -102,14 +106,14 @@ public class ModuleImpl implements Module {
@Override
public String getAppType() {
return "web";
return formation.getType();
}
@Override
public int getRunningInstances()
{
return app.getDynos();
return formation.getQuantity();
}
......@@ -151,4 +155,17 @@ public class ModuleImpl implements Module {
throw new IllegalArgumentException("Error in URL=" + urlStr + " from provider ", e);
}
}
private Formation findFormation(List<Formation> formations) {
if (formations.size() == 0) {
return EMPTY_FORMATION;
}
else if (formations.size() == 1) {
return formations.get(0);
}
/*
* XXX Unsure of the case where an application has two different formations
*/
throw new IllegalStateException("formations.size = " + formations.size() + " for application " + app.getName());
}
}
......@@ -26,7 +26,7 @@ import eu.atos.paas.AbstractProviderIT;
import eu.atos.paas.Groups;
import eu.atos.paas.PaasClient;
import eu.atos.paas.TestConfigProperties;
import eu.atos.paas.credentials.UserPasswordCredentials;
import eu.atos.paas.credentials.ApiKeyCredentials;
@Test(groups = Groups.HEROKU)
public class HerokuIT extends AbstractProviderIT {
......@@ -43,9 +43,7 @@ public class HerokuIT extends AbstractProviderIT {
PaasClient client = new HerokuClient();
session = client.getSession(new UserPasswordCredentials(
TestConfigProperties.getInstance().getHeroku_user(),
TestConfigProperties.getInstance().getHeroku_password()));
session = client.getSession(new ApiKeyCredentials(TestConfigProperties.getInstance().getHeroku_apiKey()));
URL url;
try {
......
......@@ -42,7 +42,7 @@
<javatuples.version>1.2</javatuples.version>
<dropwizard.version>0.8.1</dropwizard.version>
<guava.version>18.0</guava.version>
<heroku.version>0.16</heroku.version>
<heroku.version>0.24</heroku.version>
<jersey.version>2.17</jersey.version>
<cf-version>1.1.3</cf-version>
<openshift2-version>2.7.0.Final</openshift2-version>
......
......@@ -41,9 +41,9 @@ public class ApplicationToCreate {
private InputStream artifact;
private ArtifactType artifactType = ArtifactType.TARGET;
private String programmingLanguage;
private String programmingLanguage = "";
private Map<String, String> properties;
private Map<String, String> properties = Collections.emptyMap();
/**
* Just for deserialization
......
......@@ -26,4 +26,8 @@ public class AuthenticationException extends ResourceException {
public AuthenticationException() {
super(new ErrorEntity(Status.UNAUTHORIZED, "Authentication error"));
}
public AuthenticationException(String message) {
super(new ErrorEntity(Status.UNAUTHORIZED, message));
}
}
......@@ -519,6 +519,8 @@ public abstract class PaasResource
sessionProxy = new PaasSessionProxy(session);
} catch (eu.atos.paas.AuthenticationException e) {
throw new AuthenticationException();
} catch (UnsupportedOperationException e) {
throw new AuthenticationException(e.getMessage());
}
return sessionProxy;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment