Search in sources :

Example 1 with ConfigFileSpec

use of com.mesosphere.sdk.specification.ConfigFileSpec in project dcos-commons by mesosphere.

the class ArtifactQueries method getTemplate.

/**
 * Produces the content of the requested configuration template, or returns an error if that template doesn't exist
 * or the data couldn't be read. Configuration templates are versioned against configuration IDs, which (despite the
 * similar naming) are not directly related. See also {@link ConfigQueries} for more information on configuration
 * IDs.
 *
 * @param configurationId the id of the configuration set to be retrieved from -- this should match the
 *     configuration the task is on. this allows old tasks to continue retrieving old configurations
 * @param podType the name/type of the pod, eg 'index' or 'data'
 * @param taskName the name of the task
 * @param configurationName the name of the configuration to be retrieved
 * @return an HTTP response containing the content of the requested configuration, or an HTTP error
 * @see ConfigQueries
 */
public static Response getTemplate(ConfigStore<ServiceSpec> configStore, String configurationId, String podType, String taskName, String configurationName) {
    LOGGER.info("Attempting to fetch template '{}' from config '{}' with pod '{}', task '{}'", configurationName, configurationId, podType, taskName);
    UUID uuid;
    try {
        uuid = UUID.fromString(configurationId);
    } catch (IllegalArgumentException ex) {
        LOGGER.warn(String.format("Failed to parse requested configuration id as a UUID: '%s'", configurationId), ex);
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    ServiceSpec serviceSpec;
    try {
        serviceSpec = configStore.fetch(uuid);
    } catch (ConfigStoreException ex) {
        if (ex.getReason() == Reason.NOT_FOUND) {
            LOGGER.warn(String.format("Requested configuration '%s' doesn't exist", configurationId), ex);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        LOGGER.error(String.format("Failed to fetch requested configuration with id '%s'", configurationId), ex);
        return Response.serverError().build();
    }
    try {
        ConfigFileSpec config = getConfigFile(getTask(getPod(serviceSpec, podType), taskName), configurationName);
        return plainOkResponse(config.getTemplateContent());
    } catch (Exception ex) {
        LOGGER.warn(String.format("Couldn't find requested template in config '%s'", configurationId), ex);
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : ConfigFileSpec(com.mesosphere.sdk.specification.ConfigFileSpec) ConfigStoreException(com.mesosphere.sdk.state.ConfigStoreException) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) UUID(java.util.UUID) ConfigStoreException(com.mesosphere.sdk.state.ConfigStoreException)

Example 2 with ConfigFileSpec

use of com.mesosphere.sdk.specification.ConfigFileSpec in project dcos-commons by mesosphere.

the class ArtifactQueriesTest method testGetTemplateSuccess.

@Test
public void testGetTemplateSuccess() throws ConfigStoreException {
    UUID uuid = UUID.randomUUID();
    when(mockConfigStore.fetch(uuid)).thenReturn(mockServiceSpec);
    when(mockServiceSpec.getPods()).thenReturn(Arrays.asList(mockPodSpec));
    when(mockPodSpec.getType()).thenReturn("pod");
    when(mockPodSpec.getTasks()).thenReturn(Arrays.asList(mockTaskSpec));
    when(mockTaskSpec.getName()).thenReturn("task");
    ConfigFileSpec configSpec = new DefaultConfigFileSpec("conffile", "../conf/confpath.xml", "content goes here");
    when(mockTaskSpec.getConfigFiles()).thenReturn(Arrays.asList(configSpec));
    Response r = ArtifactQueries.getTemplate(mockConfigStore, uuid.toString(), "pod", "task", "conffile");
    assertEquals(200, r.getStatus());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, r.getMediaType());
    assertEquals(configSpec.getTemplateContent(), r.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) DefaultConfigFileSpec(com.mesosphere.sdk.specification.DefaultConfigFileSpec) ConfigFileSpec(com.mesosphere.sdk.specification.ConfigFileSpec) UUID(java.util.UUID) DefaultConfigFileSpec(com.mesosphere.sdk.specification.DefaultConfigFileSpec) Test(org.junit.Test)

Aggregations

ConfigFileSpec (com.mesosphere.sdk.specification.ConfigFileSpec)2 UUID (java.util.UUID)2 DefaultConfigFileSpec (com.mesosphere.sdk.specification.DefaultConfigFileSpec)1 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)1 ConfigStoreException (com.mesosphere.sdk.state.ConfigStoreException)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1