componentNames.get(request.getClusterName())
.get(request.getServiceName()).add(request.getComponentName());
if (request.getDesiredState() != null
&& !request.getDesiredState().isEmpty()) {
State state = State.valueOf(request.getDesiredState());
if (!state.isValidDesiredState()
|| state != State.INIT) {
throw new IllegalArgumentException("Invalid desired state"
+ " only INIT state allowed during creation"
+ ", providedDesiredState=" + request.getDesiredState());
}
}
Service s;
try {
s = cluster.getService(request.getServiceName());
} catch (ServiceNotFoundException e) {
throw new ParentObjectNotFoundException(
"Attempted to add a component to a service which doesn't exist:", e);
}
try {
ServiceComponent sc = s.getServiceComponent(request.getComponentName());
if (sc != null) {
// throw error later for dup
duplicates.add("[clusterName=" + request.getClusterName() + ", serviceName=" + request.getServiceName() +
", componentName=" + request.getComponentName() + "]");
continue;
}
} catch (AmbariException e) {
// Expected
}
StackId stackId = s.getDesiredStackVersion();
if (!ambariMetaInfo.isValidServiceComponent(stackId.getStackName(),
stackId.getStackVersion(), s.getName(), request.getComponentName())) {
throw new IllegalArgumentException("Unsupported or invalid component"
+ " in stack"
+ ", clusterName=" + request.getClusterName()
+ ", serviceName=" + request.getServiceName()
+ ", componentName=" + request.getComponentName()
+ ", stackInfo=" + stackId.getStackId());
}
}
// ensure only a single cluster update
if (componentNames.size() != 1) {
throw new IllegalArgumentException("Invalid arguments, updates allowed"
+ "on only one cluster at a time");
}
// Validate dups
if (!duplicates.isEmpty()) {
StringBuilder names = new StringBuilder();
boolean first = true;
for (String cName : duplicates) {
if (!first) {
names.append(",");
}
first = false;
names.append(cName);
}
String msg;
if (duplicates.size() == 1) {
msg = "Attempted to create a component which already exists: ";
} else {
msg = "Attempted to create components which already exist: ";
}
throw new DuplicateResourceException(msg + names.toString());
}
// now doing actual work
for (ServiceComponentRequest request : requests) {
Cluster cluster = clusters.getCluster(request.getClusterName());
Service s = cluster.getService(request.getServiceName());
ServiceComponent sc = serviceComponentFactory.createNew(s,
request.getComponentName());
sc.setDesiredStackVersion(s.getDesiredStackVersion());
if (request.getDesiredState() != null
&& !request.getDesiredState().isEmpty()) {
State state = State.valueOf(request.getDesiredState());
sc.setDesiredState(state);
} else {
sc.setDesiredState(s.getDesiredState());
}