String appId = requestedObj.getAppId();
String programTypeStr = requestedObj.getProgramType();
String programId = requestedObj.getProgramId();
// these values will be overwritten later
int requested, provisioned;
ApplicationSpecification spec = store.getApplication(Id.Application.from(accountId, appId));
if (spec == null) {
addCodeError(requestedObj, HttpResponseStatus.NOT_FOUND.getCode(), "App: " + appId + " not found");
continue;
}
ProgramType programType = ProgramType.valueOfPrettyName(programTypeStr);
String runnableId;
if (programType == ProgramType.PROCEDURE) {
// the "runnable" for procedures has the same id as the procedure name
runnableId = programId;
if (spec.getProcedures().containsKey(programId)) {
requested = store.getProcedureInstances(Id.Program.from(accountId, appId, programId));
} else {
addCodeError(requestedObj, HttpResponseStatus.NOT_FOUND.getCode(),
"Procedure: " + programId + " not found");
continue;
}
} else {
// cant get instances for things that are not flows, services, or procedures
if (programType != ProgramType.FLOW && programType != ProgramType.SERVICE) {
addCodeError(requestedObj, HttpResponseStatus.BAD_REQUEST.getCode(),
"Program type: " + programType + " is not a valid program type to get instances");
continue;
}
// services and flows must have runnable id
if (requestedObj.getRunnableId() == null) {
responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Must provide a string runnableId for flows/services");
return;
}
runnableId = requestedObj.getRunnableId();
if (programType == ProgramType.FLOW) {
FlowSpecification flowSpec = spec.getFlows().get(programId);
if (flowSpec != null) {
Map<String, FlowletDefinition> flowletSpecs = flowSpec.getFlowlets();
if (flowletSpecs != null && flowletSpecs.containsKey(runnableId)) {
requested = flowletSpecs.get(runnableId).getInstances();
} else {
addCodeError(requestedObj, HttpResponseStatus.NOT_FOUND.getCode(),
"Flowlet: " + runnableId + " not found");
continue;
}
} else {
addCodeError(requestedObj, HttpResponseStatus.NOT_FOUND.getCode(), "Flow: " + programId + " not found");
continue;
}
} else {
// Services
ServiceSpecification serviceSpec = spec.getServices().get(programId);
if (serviceSpec != null) {
Map<String, RuntimeSpecification> runtimeSpecs = serviceSpec.getRunnables();
if (runtimeSpecs != null && runtimeSpecs.containsKey(runnableId)) {
requested = runtimeSpecs.get(runnableId).getResourceSpecification().getInstances();
} else {