mesosphere.marathon.client.MarathonClient Java Examples

The following examples show how to use mesosphere.marathon.client.MarathonClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarathonBuilderImpl.java    From marathon-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Get a Marathon client with Authorization headers using the token within provided credentials. If the content of
 * credentials is JSON, this will use the "jenkins_token" field; if the content is just a string, that will be
 * used as the token value.
 *
 * @param credentials String credentials
 * @return Marathon client with token in auth header
 */
private Marathon getMarathonClient(StringCredentials credentials) {
    String token;

    try {
        final JSONObject json = JSONObject.fromObject(credentials.getSecret().getPlainText());
        if (json.has("jenkins_token")) {
            token = json.getString("jenkins_token");
        } else {
            token = "";
        }
    } catch (JSONException jse) {
        token = credentials.getSecret().getPlainText();
    }

    if (StringUtils.isNotEmpty(token)) {
        return MarathonClient
                .getInstanceWithTokenAuth(getURL(), token);
    }

    return getMarathonClient();
}
 
Example #2
Source File: MesosAutoConfiguration.java    From spring-cloud-deployer-mesos with Apache License 2.0 5 votes vote down vote up
@Bean
@RefreshScope
public Marathon marathon(MarathonAppDeployerProperties marathonProperties, DcosClusterProperties dcosClusterProperties) {
	if (StringUtils.hasText(dcosClusterProperties.getAuthorizationToken())) {
		return MarathonClient.getInstance(marathonProperties.getApiEndpoint(),
				new DcosHeadersInterceptor(dcosClusterProperties.getAuthorizationToken()));
	}
	else {
		return MarathonClient.getInstance(marathonProperties.getApiEndpoint());
	}
}
 
Example #3
Source File: MarathonTestSupport.java    From spring-cloud-deployer-mesos with Apache License 2.0 5 votes vote down vote up
@Bean
public Marathon marathon(MarathonAppDeployerProperties marathonProperties,
                         DcosClusterProperties dcosClusterProperties) {
	if (StringUtils.hasText(dcosClusterProperties.getAuthorizationToken())) {
		return MarathonClient.getInstance(marathonProperties.getApiEndpoint(),
				new DcosHeadersInterceptor(dcosClusterProperties.getAuthorizationToken()));
	}
	else {
		return MarathonClient.getInstance(marathonProperties.getApiEndpoint());
	}
}
 
Example #4
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
/**
 * Deploys a Marathon app by JSON string
 *
 * @param marathonJson JSON string
 */
@Override
public void deployApp(String marathonJson) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        marathon.createApp(constructApp(marathonJson));
    } catch (MarathonException e) {
        throw new MinimesosException("Marathon did not accept the app, error: " + e.toString());
    }
    LOGGER.debug(format("Installed app at '%s'", getMarathonEndpoint()));
}
 
Example #5
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Override
public Result deleteApp(String appId) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        Result result = marathon.deleteApp(appId);
        LOGGER.debug(format("Deleted app '%s' at '%s'", appId, getMarathonEndpoint()));
        return result;
    } catch (MarathonException e) {
        throw new MinimesosException("Could not delete app '" + appId  + "'. " + e.getMessage());
    }
}
 
Example #6
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Override
public void deployGroup(String groupJson) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        Group group = constructGroup(groupJson);
        marathon.createGroup(group);
    } catch (Exception e) {
        throw new MinimesosException("Marathon did not accept the app, error: " + e.toString(), e);
    }
    LOGGER.debug(format("Installing group at %s", getMarathonEndpoint()));
}
 
Example #7
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
@Override
public Result deleteGroup(String groupId) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        Result result = marathon.deleteGroup(groupId);
        LOGGER.debug(format("Deleted app '%s' at '%s'", groupId, getMarathonEndpoint()));
        return result;
    } catch (MarathonException e) {
        throw new MinimesosException("Could not delete group '" + groupId  + "'. " + e.getMessage());
    }
}
 
Example #8
Source File: MarathonContainer.java    From minimesos with Apache License 2.0 5 votes vote down vote up
/**
 * Updates a Marathon app by JSON string
 *
 * @param marathonJson JSON string
 */
@Override
public void updateApp(String marathonJson) {
    mesosphere.marathon.client.Marathon marathon = MarathonClient.getInstance(getMarathonEndpoint());
    try {
        App app = constructApp(marathonJson);
        marathon.updateApp(app.getId(), app, true);
    } catch (MarathonException e) {
        throw new MinimesosException("Marathon could not update the app, error: " + e.toString());
    }
    LOGGER.debug(format("Installing an app on marathon %s", getMarathonEndpoint()));
}
 
Example #9
Source File: ExternalVolumeTest.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testExternalVolumes() throws Exception {
    final MockWebServer server = new MockWebServer();
    final String mockResponse = "{\"container\": {\"volumes\": [" +
            "{\"containerPath\":\"/data/db\",\"mode\":\"RW\",\"external\":" +
            "{\"name\":\"mongodb-testvol\",\"provider\":\"dvdi\",\"options\":" +
            "{\"dvdi/driver\":\"rexray\"}}}]}}";

    try {
        server.enqueue(new MockResponse().setBody(mockResponse));
        server.start();
        Marathon client = MarathonClient.getInstance(server.url("/").toString());

        App app = new App();
        app.setId("mongo");
        app.setCpus(1.0);
        app.setMem(256.0);
        app.setContainer(new Container());
        app.getContainer().setDocker(new Docker());
        app.getContainer().getDocker().setImage("mongo");
        app.getContainer().setVolumes(new ArrayList<Volume>());

        ExternalVolume externalVolume = new ExternalVolume();
        externalVolume.setName("mongodb-testvol");
        externalVolume.setMode("RW");
        externalVolume.setContainerPath("/data/db");
        externalVolume.setProvider("dvdi");
        externalVolume.setDriver("rexray");

        app.getContainer().getVolumes().add(externalVolume);

        final App appRes = client.createApp(app);
        assertFalse(appRes.getContainer().getVolumes().isEmpty());

        ExternalVolume responseVolume = (ExternalVolume) appRes.getContainer().getVolumes().iterator().next();
        assertEquals("mongodb-testvol", responseVolume.getExternalVolumeInfo().getName());

        RecordedRequest request = server.takeRequest();
        assertNotNull(request);

        final String requestBody = request.getBody().readUtf8();
        assertNotNull(requestBody);

        // request to JSON
        JsonObject requestPayload = new Gson().fromJson(requestBody, JsonObject.class);
        assertNotNull(requestPayload);
        JsonObject requestVolume = requestPayload.getAsJsonObject("container").getAsJsonArray("volumes").get(0).getAsJsonObject();
        assertNotNull(requestVolume);
        assertEquals("RW", requestVolume.get("mode").getAsString());
        assertEquals("/data/db", requestVolume.get("containerPath").getAsString());
    } finally {
        server.shutdown();
    }
}
 
Example #10
Source File: MarathonClientTest.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@Test
public void exceptionContainErrorMessage() {
    MarathonClient.MarathonErrorDecoder errorDecoder = new MarathonClient.MarathonErrorDecoder();
    Exception result = errorDecoder.decode("", Response.create(400, "", new HashMap<>(), "{\"message\":\"Invalid JSON\"}".getBytes()));
    assertTrue(result.getMessage() + " should contain text 'Invalid JSON'", result.getMessage().contains("Invalid JSON"));
}
 
Example #11
Source File: MarathonClientTest.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@Test
public void exceptionWithErrorDetails() {
    MarathonClient.MarathonErrorDecoder errorDecoder = new MarathonClient.MarathonErrorDecoder();
    Exception result = errorDecoder.decode("", Response.create(400, "reason", new HashMap<>(), "{\"message\":\"Invalid JSON\", \"details\": [ { \"path\": \"/upgradeStrategy/minimumHealthCapacity\", \"errors\": [ \"is greater than 1\" ] } ]}".getBytes()));
    assertEquals("reason: Invalid JSON (/upgradeStrategy/minimumHealthCapacity: is greater than 1) (http status: 400)", result.getMessage());
}
 
Example #12
Source File: MarathonClientTest.java    From marathon-client with Apache License 2.0 4 votes vote down vote up
@Test
public void exceptionContainsOnlyReasonForDifferentFormattedMessage() {
    MarathonClient.MarathonErrorDecoder errorDecoder = new MarathonClient.MarathonErrorDecoder();
    Exception result = errorDecoder.decode("", Response.create(400, "reason", new HashMap<>(), "{\"nonExistingField\":\"Invalid JSON\"}".getBytes()));
    assertEquals("reason (http status: 400)", result.getMessage());
}
 
Example #13
Source File: MarathonBuilderImpl.java    From marathon-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Get a Marathon client with basic auth using the username and password within the provided credentials.
 *
 * @param credentials Username and password credentials
 * @return Marathon client with basic authentication filled in
 */
private Marathon getMarathonClient(UsernamePasswordCredentials credentials) {
    return MarathonClient
            .getInstanceWithBasicAuth(getURL(), credentials.getUsername(), credentials.getPassword().getPlainText());
}
 
Example #14
Source File: MarathonBuilderImpl.java    From marathon-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Get a default Marathon client. This does not include any authentication headers.
 *
 * @return Marathon client without authentication mechanisms
 */
private Marathon getMarathonClient() {
    return MarathonClient.getInstance(getURL());
}