use of com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle 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 com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle in project streamline by hortonworks.
the class EnvironmentService method updateServiceBundle.
public ServiceBundle updateServiceBundle(String serviceName, ServiceBundle serviceBundle) throws EntityNotFoundException {
ServiceBundle persistentServiceBundle = getServiceBundleByName(serviceName);
if (persistentServiceBundle == null)
throw new EntityNotFoundException(String.format("Unable to find a service bundle of name : \"%s\"", serviceName));
persistentServiceBundle.setRegisterClass(serviceBundle.getRegisterClass());
persistentServiceBundle.setServiceUISpecification(serviceBundle.getServiceUISpecification());
persistentServiceBundle.setTimestamp(System.currentTimeMillis());
dao.update(persistentServiceBundle);
return persistentServiceBundle;
}
use of com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle in project streamline by hortonworks.
the class EnvironmentService method getServiceBundle.
public ServiceBundle getServiceBundle(Long serviceBundleId) {
ServiceBundle serviceBundle = new ServiceBundle();
serviceBundle.setId(serviceBundleId);
return this.dao.get(serviceBundle.getStorableKey());
}
use of com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle in project streamline by hortonworks.
the class EnvironmentService method removeServiceBundle.
public ServiceBundle removeServiceBundle(Long id) throws IOException {
ServiceBundle serviceBundle = new ServiceBundle();
serviceBundle.setId(id);
return dao.remove(serviceBundle.getStorableKey());
}
use of com.hortonworks.streamline.streams.cluster.catalog.ServiceBundle in project streamline by hortonworks.
the class ServiceBundleResource method addServiceBundle.
/**
* Add a new service bundle.
*/
@POST
@Path("/servicebundles")
@Timed
public Response addServiceBundle(ServiceBundle serviceBundle) throws IOException, ComponentConfigException {
try {
String serviceName = serviceBundle.getName();
ServiceBundle result = environmentService.getServiceBundleByName(serviceName);
if (result != null) {
throw new AlreadyExistsException("Service bundle for " + serviceName + " is already registered.");
}
ServiceBundle created = environmentService.addServiceBundle(serviceBundle);
return WSUtils.respondEntity(created, CREATED);
} catch (ProcessingException ex) {
throw BadRequestException.of();
}
}
Aggregations