Search in sources :

Example 1 with CREATED

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);
}
Also used : Topology(com.hortonworks.streamline.streams.catalog.Topology) Roles(com.hortonworks.streamline.streams.security.Roles) Produces(javax.ws.rs.Produces) BadRequestException(com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException) QueryParam(com.hortonworks.registries.common.QueryParam) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) StringUtils(org.apache.commons.lang3.StringUtils) BooleanUtils(org.apache.commons.lang.BooleanUtils) MediaType(javax.ws.rs.core.MediaType) WSUtils(com.hortonworks.streamline.common.util.WSUtils) READ(com.hortonworks.streamline.streams.security.Permission.READ) StreamlineAuthorizer(com.hortonworks.streamline.streams.security.StreamlineAuthorizer) EnumSet(java.util.EnumSet) DELETE(javax.ws.rs.DELETE) SecurityUtil(com.hortonworks.streamline.streams.security.SecurityUtil) Namespace(com.hortonworks.streamline.streams.cluster.catalog.Namespace) Context(javax.ws.rs.core.Context) Permission(com.hortonworks.streamline.streams.security.Permission) DELETE(com.hortonworks.streamline.streams.security.Permission.DELETE) OK(javax.ws.rs.core.Response.Status.OK) AlreadyExistsException(com.hortonworks.registries.storage.exception.AlreadyExistsException) Collection(java.util.Collection) Objects(java.util.Objects) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Response(javax.ws.rs.core.Response) ProcessingException(javax.ws.rs.ProcessingException) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) CREATED(javax.ws.rs.core.Response.Status.CREATED) NamespaceServiceClusterMap(com.hortonworks.streamline.streams.cluster.catalog.NamespaceServiceClusterMap) PathParam(javax.ws.rs.PathParam) EntityNotFoundException(com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException) GET(javax.ws.rs.GET) ArrayList(java.util.ArrayList) EnvironmentService(com.hortonworks.streamline.streams.cluster.service.EnvironmentService) TopologyNotAliveException(com.hortonworks.streamline.streams.exception.TopologyNotAliveException) WRITE(com.hortonworks.streamline.streams.security.Permission.WRITE) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) TopologyActionsService(com.hortonworks.streamline.streams.actions.topology.service.TopologyActionsService) IOException(java.io.IOException) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Collectors.toList(java.util.stream.Collectors.toList) StreamCatalogService(com.hortonworks.streamline.streams.catalog.service.StreamCatalogService) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) NamespaceServiceClusterMap(com.hortonworks.streamline.streams.cluster.catalog.NamespaceServiceClusterMap) Namespace(com.hortonworks.streamline.streams.cluster.catalog.Namespace) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with 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);
    }
}
Also used : Produces(javax.ws.rs.Produces) BadRequestException(com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException) QueryParam(com.hortonworks.registries.common.QueryParam) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ComponentUISpecification(com.hortonworks.streamline.common.ComponentUISpecification) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) WSUtils(com.hortonworks.streamline.common.util.WSUtils) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) READ(com.hortonworks.streamline.streams.security.Permission.READ) StreamlineAuthorizer(com.hortonworks.streamline.streams.security.StreamlineAuthorizer) DELETE(javax.ws.rs.DELETE) Config(com.hortonworks.streamline.common.Config) SecurityUtil(com.hortonworks.streamline.streams.security.SecurityUtil) Context(javax.ws.rs.core.Context) ServiceWithComponents(com.hortonworks.streamline.streams.cluster.model.ServiceWithComponents) OK(javax.ws.rs.core.Response.Status.OK) Collection(java.util.Collection) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Service(com.hortonworks.streamline.streams.cluster.catalog.Service) Response(javax.ws.rs.core.Response) UriInfo(javax.ws.rs.core.UriInfo) Cluster(com.hortonworks.streamline.streams.cluster.catalog.Cluster) CREATED(javax.ws.rs.core.Response.Status.CREATED) PathParam(javax.ws.rs.PathParam) EntityNotFoundException(com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException) GET(javax.ws.rs.GET) ServiceBundle(com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) ArrayList(java.util.ArrayList) EnvironmentService(com.hortonworks.streamline.streams.cluster.service.EnvironmentService) ServiceConfiguration(com.hortonworks.streamline.streams.cluster.catalog.ServiceConfiguration) WRITE(com.hortonworks.streamline.streams.security.Permission.WRITE) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) EntityAlreadyExistsException(com.hortonworks.streamline.common.exception.service.exception.request.EntityAlreadyExistsException) Component(com.hortonworks.streamline.streams.cluster.catalog.Component) ManualServiceRegistrar(com.hortonworks.streamline.streams.cluster.register.ManualServiceRegistrar) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Collectors.toList(java.util.stream.Collectors.toList) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) InputStream(java.io.InputStream) Config(com.hortonworks.streamline.common.Config) InputStream(java.io.InputStream) Cluster(com.hortonworks.streamline.streams.cluster.catalog.Cluster) Service(com.hortonworks.streamline.streams.cluster.catalog.Service) EnvironmentService(com.hortonworks.streamline.streams.cluster.service.EnvironmentService) IOException(java.io.IOException) ManualServiceRegistrar(com.hortonworks.streamline.streams.cluster.register.ManualServiceRegistrar) ServiceBundle(com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) ComponentUISpecification(com.hortonworks.streamline.common.ComponentUISpecification) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Timed(com.codahale.metrics.annotation.Timed)

Example 3 with CREATED

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);
        }
    });
}
Also used : Response(feign.Response) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Response(feign.Response) Job(io.pravega.test.system.framework.metronome.model.v1.Job) Exceptions(io.pravega.common.Exceptions) Restart(io.pravega.test.system.framework.metronome.model.v1.Restart) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Metronome(io.pravega.test.system.framework.metronome.Metronome) Run(io.pravega.test.system.framework.metronome.model.v1.Run) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) MetronomeException(io.pravega.test.system.framework.metronome.MetronomeException) Duration(java.time.Duration) Map(java.util.Map) Artifact(io.pravega.test.system.framework.metronome.model.v1.Artifact) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Method(java.lang.reflect.Method) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) AuthEnabledMetronomeClient(io.pravega.test.system.framework.metronome.AuthEnabledMetronomeClient) CREATED(javax.ws.rs.core.Response.Status.CREATED) Metronome(io.pravega.test.system.framework.metronome.Metronome)

Aggregations

Collections (java.util.Collections)3 CREATED (javax.ws.rs.core.Response.Status.CREATED)3 Timed (com.codahale.metrics.annotation.Timed)2 QueryParam (com.hortonworks.registries.common.QueryParam)2 BadRequestException (com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException)2 EntityNotFoundException (com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException)2 WSUtils (com.hortonworks.streamline.common.util.WSUtils)2 EnvironmentService (com.hortonworks.streamline.streams.cluster.service.EnvironmentService)2 READ (com.hortonworks.streamline.streams.security.Permission.READ)2 WRITE (com.hortonworks.streamline.streams.security.Permission.WRITE)2 SecurityUtil (com.hortonworks.streamline.streams.security.SecurityUtil)2 StreamlineAuthorizer (com.hortonworks.streamline.streams.security.StreamlineAuthorizer)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 DELETE (javax.ws.rs.DELETE)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2