use of javax.ws.rs.core.Response.Status.CREATED in project streamline by hortonworks.
the class NamespaceCatalogResource method setServicesToClusterInNamespace.
@POST
@Path("/namespaces/{id}/mapping/bulk")
@Timed
public Response setServicesToClusterInNamespace(@PathParam("id") Long namespaceId, List<NamespaceServiceClusterMap> mappings, @Context SecurityContext securityContext) {
SecurityUtil.checkRoleOrPermissions(authorizer, securityContext, Roles.ROLE_ENVIRONMENT_SUPER_ADMIN, Namespace.NAMESPACE, namespaceId, WRITE);
Namespace namespace = environmentService.getNamespace(namespaceId);
if (namespace == null) {
throw EntityNotFoundException.byId(namespaceId.toString());
}
String streamingEngine = namespace.getStreamingEngine();
String timeSeriesDB = namespace.getTimeSeriesDB();
Collection<NamespaceServiceClusterMap> existing = environmentService.listServiceClusterMapping(namespaceId);
Optional<NamespaceServiceClusterMap> existingStreamingEngine = existing.stream().filter(m -> m.getServiceName().equals(streamingEngine)).findFirst();
// indicates that mapping of streaming engine has been changed or removed
if (existingStreamingEngine.isPresent() && !mappings.contains(existingStreamingEngine.get())) {
assertNoTopologyReferringNamespaceIsRunning(namespaceId, WSUtils.getUserFromSecurityContext(securityContext));
}
// we're OK to just check with new mappings since we will remove existing mappings
assertServiceIsUnique(mappings, streamingEngine);
assertServiceIsUnique(mappings, timeSeriesDB);
// remove any existing mapping for (namespace, service name) pairs
Collection<NamespaceServiceClusterMap> existingMappings = environmentService.listServiceClusterMapping(namespaceId);
if (existingMappings != null) {
existingMappings.forEach(m -> environmentService.removeServiceClusterMapping(m.getNamespaceId(), m.getServiceName(), m.getClusterId()));
}
List<NamespaceServiceClusterMap> newMappings = mappings.stream().map(environmentService::addOrUpdateServiceClusterMapping).collect(toList());
return WSUtils.respondEntities(newMappings, CREATED);
}
use of javax.ws.rs.core.Response.Status.CREATED in project streamline by hortonworks.
the class ServiceCatalogResource method registerService.
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/clusters/{clusterId}/services/register/{serviceName}")
@Timed
public Response registerService(@PathParam("clusterId") Long clusterId, @PathParam("serviceName") String serviceName, FormDataMultiPart form) {
ServiceBundle serviceBundle = environmentService.getServiceBundleByName(serviceName);
if (serviceBundle == null) {
throw BadRequestException.message("Not supported service: " + serviceName);
}
ManualServiceRegistrar registrar;
try {
Class<?> clazz = Class.forName(serviceBundle.getRegisterClass());
registrar = (ManualServiceRegistrar) clazz.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new RuntimeException(e);
}
Cluster cluster = environmentService.getCluster(clusterId);
if (cluster == null) {
throw EntityNotFoundException.byId("Cluster " + clusterId);
}
Service service = environmentService.getServiceByName(clusterId, serviceName);
if (service != null) {
throw EntityAlreadyExistsException.byName("Service " + serviceName + " is already exist in Cluster " + clusterId);
}
registrar.init(environmentService);
ComponentUISpecification specification = serviceBundle.getServiceUISpecification();
List<String> fileFieldNames = specification.getFields().stream().filter(uiField -> uiField.getType().equals(ComponentUISpecification.UIFieldType.FILE)).map(uiField -> uiField.getFieldName()).collect(toList());
Map<String, List<FormDataBodyPart>> fields = form.getFields();
List<FormDataBodyPart> cfgFormList = fields.getOrDefault("config", Collections.emptyList());
Config config;
if (!cfgFormList.isEmpty()) {
String jsonConfig = cfgFormList.get(0).getEntityAs(String.class);
try {
config = objectMapper.readValue(jsonConfig, Config.class);
} catch (IOException e) {
throw BadRequestException.message("config is missing");
}
} else {
config = new Config();
}
List<ManualServiceRegistrar.ConfigFileInfo> configFileInfos = fields.entrySet().stream().filter(entry -> fileFieldNames.contains(entry.getKey())).flatMap(entry -> {
String key = entry.getKey();
List<FormDataBodyPart> values = entry.getValue();
return values.stream().map(val -> new ManualServiceRegistrar.ConfigFileInfo(key, val.getEntityAs(InputStream.class)));
}).collect(toList());
try {
Service registeredService = registrar.register(cluster, config, configFileInfos);
return WSUtils.respondEntity(buildManualServiceRegisterResult(registeredService), CREATED);
} catch (IllegalArgumentException e) {
throw BadRequestException.message(e.getMessage());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of javax.ws.rs.core.Response.Status.CREATED in project pravega by pravega.
the class RemoteSequential method startTestExecution.
@Override
public CompletableFuture<Void> startTestExecution(Method testMethod) {
Exceptions.handleInterrupted(() -> TimeUnit.SECONDS.sleep(60));
// This will be removed once issue https://github.com/pravega/pravega/issues/1665 is resolved.
log.debug("Starting test execution for method: {}", testMethod);
final Metronome client = AuthEnabledMetronomeClient.getClient();
String className = testMethod.getDeclaringClass().getName();
String methodName = testMethod.getName();
// All jobIds should have lowercase for metronome.
String jobId = (methodName + ".testJob").toLowerCase();
return CompletableFuture.runAsync(() -> {
client.createJob(newJob(jobId, className, methodName));
Response response = client.triggerJobRun(jobId);
if (response.status() != CREATED.getStatusCode()) {
throw new TestFrameworkException(TestFrameworkException.Type.ConnectionFailed, "Error while starting " + "test " + testMethod);
} else {
log.info("Created job succeeded with: " + response.toString());
}
}).thenCompose(v2 -> waitForJobCompletion(jobId, client)).<Void>thenApply(v1 -> {
if (client.getJob(jobId).getHistory().getFailureCount() != 0) {
throw new AssertionError("Test failed, detailed logs can be found at " + "https://MasterIP/mesos, under metronome framework tasks. MethodName: " + methodName);
}
return null;
}).whenComplete((v, ex) -> {
// deletejob once execution is complete.
deleteJob(jobId, client);
if (ex != null) {
log.error("Error while executing the test. ClassName: {}, MethodName: {}", className, methodName);
}
});
}
Aggregations