private Provider getProvider(ClusterTemplate template, ClusterCreateRequest request, EntityStoreView entityStore) throws IOException, MissingEntityException { // make sure the provider exists String providerName = request.getProvider(); if (providerName == null || providerName.isEmpty()) { providerName = template.getClusterDefaults().getProvider(); } Provider provider = entityStore.getProvider(providerName); if (provider == null) { throw new MissingEntityException("provider " + providerName + " does not exist."); } return provider; }
private BodyConsumer uploadResource(HttpResponder responder, Account account, PluginType type, String pluginName, String resourceType, String resourceName) { ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); try { validateTypeExists(account, pluginResourceType); return resourceService.createResourceBodyConsumer(account, pluginResourceType, resourceName, responder); } catch (IOException e) { LOG.error("Exception uploading resource.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error uploading resource"); return null; } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); return null; } }
/** * Recall the given resource for the given account. A no-op if there is no staged version. * * @param account Account that contains the resource * @param resourceType Type of resource to deactivate * @param name Name of the resource to deactivate * @param version Version of resource to stage * @throws MissingEntityException if there is no such module * @throws IOException if there was an error deactivating all versions of the module */ public void recall(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("Recalling version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource " + name + " does not exist."); } view.recall(name, version); } finally { lock.unlock(); } }
private void deleteResource(HttpResponder responder, Account account, PluginType type, String pluginName, String resourceType, String resourceName) { ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); try { validateTypeExists(account, pluginResourceType); resourceService.delete(account, pluginResourceType, resourceName); responder.sendStatus(HttpResponseStatus.OK); } catch (IllegalStateException e) { responder.sendError(HttpResponseStatus.CONFLICT, "Resource not in a deletable state."); } catch (IOException e) { LOG.error("Exception deleting all versions of resource.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error deleting all versions of resource."); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } }
/** * Atomically stage the specified resource version for the given account and recall the previous staged version. * A staged version will get pushed to provisioners during a sync, and will stay staged unless explicitly recalled. * * @param account Account that contains the resource * @param resourceType Type of resource to stage * @param name Name of resource to stage * @param version Version of resource to stage * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error staging the resource */ public void stage(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("staging version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource does not exist."); } view.stage(name, version); } finally { lock.unlock(); } }
private void deleteResource(HttpResponder responder, Account account, PluginType type, String pluginName, String resourceType, String resourceName, String versionStr) { ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); try { validateTypeExists(account, pluginResourceType); int version = Integer.parseInt(versionStr); resourceService.delete(account, pluginResourceType, resourceName, version); responder.sendStatus(HttpResponseStatus.OK); } catch (NumberFormatException e) { responder.sendError(HttpResponseStatus.BAD_REQUEST, "Invalid version " + versionStr); } catch (IllegalStateException e) { responder.sendError(HttpResponseStatus.CONFLICT, "Resource not in a deletable state."); } catch (IOException e) { LOG.error("Exception deleting resource version.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error deleting resource version."); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } }
private void validateTypeExists(Account account, ResourceType resourceType) throws MissingEntityException, IOException { PluginType pluginType = resourceType.getPluginType(); String pluginName = resourceType.getPluginName(); String resourceTypeName = resourceType.getTypeName(); AbstractPluginSpecification plugin; if (pluginType == PluginType.AUTOMATOR) { plugin = entityStoreService.getView(account).getAutomatorType(pluginName); } else if (pluginType == PluginType.PROVIDER) { plugin = entityStoreService.getView(account).getProviderType(pluginName); } else { throw new MissingEntityException("Unknown plugin type " + pluginType); } if (plugin == null) { throw new MissingEntityException(pluginType.name().toLowerCase() + " plugin " + pluginName + " not found."); } if (!plugin.getResourceTypes().containsKey(resourceTypeName)) { throw new MissingEntityException(resourceTypeName + " for " + pluginType.name().toLowerCase() + " plugin " + pluginName + " not found."); } }
responder.sendError(HttpResponseStatus.CONFLICT, "Cluster is not in a state where the template can by synced"); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (InvalidClusterException e) { responder.sendError(HttpResponseStatus.BAD_REQUEST, e.getMessage());
/** * Get an input stream for reading the plugin resource. * * @param account Account the resource belongs to * @param resourceType Type of resource * @param name Name of resource to get an input stream for * @param version Version of resource to get an input stream for * @return Input stream for reading the given plugin resource * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error getting the input stream for the resource */ public InputStream getResourceInputStream(final Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { // no lock needed since each resource uploaded gets its own id. ResourceMeta meta = metaStoreService.getResourceTypeView(account, resourceType).get(name, version); if (meta == null) { throw new MissingEntityException("Resource not found."); } LOG.debug("getting input stream for version {} of resource {} of type {} for account {}.", version, name, resourceType, account); return pluginStore.getResourceInputStream(account, resourceType, meta.getName(), meta.getVersion()); }
responder.sendError(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (IllegalStateException e) { responder.sendError(HttpResponseStatus.CONFLICT,
/** * Handle the heartbeat of a provisioner, updating the last heartbeat time of the provisioner and updating the number * of live workers running on the provisioner for each tenant it is responsible for. * * @param provisionerId Id of the provisioner that sent the heartbeat * @param heartbeat The heartbeat containing live worker information * @throws IOException if there was an exception persisting the data * @throws MissingEntityException if there is no provisioner for the given id */ public void handleHeartbeat(String provisionerId, ProvisionerHeartbeat heartbeat) throws IOException, MissingEntityException { // no lock required here. Simply getting a provisioner and writing worker usage. Would only expect one provisioner // to be calling this at a time, and even if it is calling it concurrently for some reason, only the usage can // change and for that its ok for one of them to win. Provisioner provisioner = provisionerStore.getProvisioner(provisionerId); if (provisioner == null) { throw new MissingEntityException("Provisioner " + provisionerId + " not found."); } if (!provisioner.getUsage().equals(heartbeat.getUsage())) { provisioner.setUsage(heartbeat.getUsage()); provisionerStore.writeProvisioner(provisioner); } provisionerStore.setHeartbeat(provisionerId, System.currentTimeMillis()); }
responder.sendError(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (IllegalStateException e) { responder.sendError(HttpResponseStatus.CONFLICT, "Cluster is not in a configurable state.");
ProviderType providerType = entityStoreService.getView(account).getProviderType(providerTypeName); if (providerType == null) { throw new MissingEntityException("provider type " + providerTypeName + " does not exist.");
responder.sendError(HttpResponseStatus.BAD_REQUEST, e.getMessage()); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (IOException e) { LOG.error("Exception while trying to create cluster.", e);
LOG.info("tried to sync template {} for cluster {}, but the template no longer exists", templateName, clusterId); throw new MissingEntityException("template " + templateName + " no longer exists"); throw new MissingEntityException("could not find cluster nodes");
responder.sendStatus(HttpResponseStatus.OK); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } catch (IllegalStateException e) { responder.sendError(HttpResponseStatus.CONFLICT,
Cluster cluster = getCluster(clusterId, account); if (service != null && !cluster.getServices().contains(service)) { throw new MissingEntityException("service " + service + " does not exist on cluster " + clusterId);
private void getResources(HttpRequest request, HttpResponder responder, Account account, PluginType type, String pluginName, String resourceType) { ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); try { validateTypeExists(account, pluginResourceType); ResourceStatus statusFilter = getStatusParam(request); responder.sendJson(HttpResponseStatus.OK, resourceService.getAll(account, pluginResourceType, statusFilter), new TypeToken<Map<String, Set<ResourceMeta>>>() { }.getType(), gson); } catch (IllegalArgumentException e) { responder.sendError(HttpResponseStatus.BAD_REQUEST, "invalid status filter."); } catch (IOException e) { LOG.error("Exception getting resources.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error getting resources."); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } }
throw new MissingEntityException("provisioner " + provisionerId + " not found.");
private void getResources(HttpRequest request, HttpResponder responder, Account account, PluginType type, String pluginName, String resourceType, String resourceName) { ResourceType pluginResourceType = new ResourceType(type, pluginName, resourceType); try { validateTypeExists(account, pluginResourceType); ResourceStatus statusFilter = getStatusParam(request); responder.sendJson(HttpResponseStatus.OK, resourceService.getAll(account, pluginResourceType, resourceName, statusFilter), new TypeToken<Set<ResourceMeta>>() { }.getType(), gson); } catch (IllegalArgumentException e) { responder.sendError(HttpResponseStatus.BAD_REQUEST, "invalid status filter."); } catch (IOException e) { LOG.error("Exception getting resources.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error getting resources."); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.NOT_FOUND, e.getMessage()); } }