use of org.alien4cloud.alm.service.exceptions.InvalidDeploymentStatusException in project alien4cloud by alien4cloud.
the class ManagedServiceResourceService method create.
/**
* Create a Service resource associated with the given environment.
*
* @param environmentId The environment to create a service for, the service version will be the one of the environment current associated version.
* @param serviceName The name of the service as it should appears.
* @param fromRuntime If we should try to create the service from the runtime topology related to the environment.
* @return the id of the created service
*
* @throws AlreadyExistException if a service with the given name, or related to the given environment already exists
* @throws alien4cloud.exception.NotFoundException if <b>fromRuntime</b> is set to true, but the environment is not deployed
* @throws MissingSubstitutionException if topology related to the environment doesn't define a substitution type
*/
public synchronized String create(String serviceName, String environmentId, boolean fromRuntime) {
ApplicationEnvironment environment = checkAndGetApplicationEnvironment(environmentId);
// check that the service does not exists already for this environment
if (alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environmentId)).count() > 0) {
throw new AlreadyExistException("A service resource for environment <" + environmentId + "> and version <" + environment.getTopologyVersion() + "> already exists.");
}
Topology topology;
String state = ToscaNodeLifecycleConstants.INITIAL;
Deployment deployment = null;
if (fromRuntime) {
deployment = deploymentService.getActiveDeploymentOrFail(environmentId);
topology = deploymentRuntimeStateService.getRuntimeTopology(deployment.getId());
DeploymentStatus currentStatus = deploymentRuntimeStateService.getDeploymentStatus(deployment);
state = managedServiceResourceEventService.getInstanceStateFromDeploymentStatus(currentStatus);
if (state == null) {
// We need a valid deployment state to expose as service.
throw new InvalidDeploymentStatusException("Creating a service out of a running deployment is possible only when it's status is one of [DEPLOYED, FAILURE, UNDEPLOYED] current was <" + currentStatus + ">", currentStatus);
}
} else {
topology = topologyServiceCore.getOrFail(Csar.createId(environment.getApplicationId(), environment.getTopologyVersion()));
}
if (topology.getSubstitutionMapping() == null) {
throw new MissingSubstitutionException("Substitution is required to expose a topology.");
}
// The elementId of the type created out of the substitution is currently the archive name.
String serviceId = serviceResourceService.create(serviceName, environment.getTopologyVersion(), topology.getArchiveName(), environment.getTopologyVersion(), environmentId);
// Update the service relationships definition from the topology substitution
updateServiceRelationship(serviceId, topology);
if (fromRuntime) {
managedServiceResourceEventService.updateRunningService((DeploymentTopology) topology, serviceResourceService.getOrFail(serviceId), deployment, state);
}
ServiceResource serviceResource = serviceResourceService.getOrFail(serviceId);
// trigger a ManagedServiceCreatedEvent
publisher.publishEvent(new ManagedServiceCreatedEvent(this, serviceResource));
return serviceId;
}
Aggregations