/** * Get a file reference for a specific subscription. This file will use the subscription as a context to isolate it, * and using the related node and the subscription's identifier. The parent directories are created as needed. * * @param subscription * The subscription used a context of the file to create. * @param fragments * The file fragments. * @return The {@link Path} reference. * @throws IOException * When the parent directories creation failed. * @since 2.2.4 */ public Path toPath(final Subscription subscription, final String... fragments) throws IOException { return toPath(toPath(subscription.getNode()).resolve(String.valueOf(subscription.getId())), fragments); }
/** * Write <code>subscription;project;projetKey;projectName;node</code>. * * @param writer * Target output. * @param subscription * Related subscription. */ private void writeCommon(final Writer writer, final Subscription subscription) throws IOException { final Project project = subscription.getProject(); writer.write('\n'); writer.write(String.valueOf(subscription.getId())); writer.write(';'); writer.write(String.valueOf(project.getId())); writer.write(';'); writer.write(project.getPkey()); writer.write(';'); writer.write(project.getName().replaceAll("\"", "'")); writer.write(';'); writer.write(subscription.getNode().getId()); }
/** * {@link SubscriptionEditionVo} to JPA entity transformer. * * @param vo * The object to convert. * @param project * The related project. * @param node * The related node. * @return The mapped entity. */ public static Subscription toEntity(final SubscriptionEditionVo vo, final Project project, final Node node) { final Subscription entity = new Subscription(); entity.setProject(project); entity.setId(vo.getId()); entity.setNode(node); return entity; }
@Override @GET @Path("task") public VmSnapshotStatus getTask(@PathParam("subscription") final int subscription) { final VmSnapshotStatus task = LongTaskRunnerSubscription.super.getTask(subscription); if (task != null) { getSnapshot(task.getLocked().getNode()).completeStatus(task); } return task; }
/** * Return non secured parameters values related to the subscription.The attached project is validated against the * current user to check it is visible. Secured parameters (even the encrypted ones) are not returned. The * visibility of this subscription is checked. * * @param id * The subscription identifier. * @return secured associated parameters values. Key of returned map is the identifier of * {@link org.ligoj.app.model.Parameter} */ @GET @Path("{id:\\d+}") @org.springframework.transaction.annotation.Transactional(readOnly = true) public Map<String, String> getNonSecuredParameters(@PathParam("id") final int id) { return parameterValueResource.getNonSecuredSubscriptionParameters(checkVisible(id).getId()); }
/** * Delete entity and cascaded associations : parameters, events then subscription. * * @param id * the entity identifier. * @param deleteRemoteData * When <code>true</code>, remote data will be also destroyed. * @throws Exception * When the delete fails. Managed at JAX-RS level. */ @Path("{id:\\d+}/{deleteRemoteData}") @DELETE public void delete(@PathParam("id") final int id, @PathParam("deleteRemoteData") final boolean deleteRemoteData) throws Exception { final Subscription entity = checkVisible(id); checkManagedProject(entity.getProject().getId()); // Delete the events eventRepository.deleteAllBy("subscription", entity); // Delegate the deletion deleteWithTasks(entity.getNode().getId(), id, deleteRemoteData); parameterValueResource.deleteBySubscription(id); repository.delete(entity); }
/** * Refresh given subscriptions and return their status. */ private SubscriptionStatusWithData refreshSubscription(final Subscription subscription) { final Map<String, String> parameters = getParameters(subscription.getId()); final SubscriptionStatusWithData statusWithData = nodeResource.checkSubscriptionStatus(subscription, parameters); statusWithData.setId(subscription.getId()); statusWithData.setProject(subscription.getProject().getId()); statusWithData.setParameters(parameterValueResource.getNonSecuredSubscriptionParameters(subscription.getId())); // Update the last event with fresh data eventResource.registerEvent(subscription, EventType.STATUS, statusWithData.getStatus().name()); // Return the fresh statuses return statusWithData; }
entity.setProject(project); log.info("Subscription of project {} to service {}", vo.getProject(), vo.getNode()); return entity.getId();
@Override public Subscription checkVisible(final Integer id) { final Subscription entity = repository.findOneExpected(id); if (projectRepository.findOneVisible(entity.getProject().getId(), securityHelper.getLogin()) == null) { // Associated project is not visible, reject the subscription access throw new EntityNotFoundException(String.valueOf(id)); } return entity; }
/** * Return all snapshots matching to the given criteria and also associated to the given subscription. * * @param subscription * The related subscription identifier. * @param criteria * The optional search criteria. Case is insensitive. May be the name or the identifier for this * snapshot. * @return Matching snapshots ordered by descending creation date. * @throws Exception * Any error while finding the snapshots. */ @GET public List<Snapshot> findAll(@PathParam("subscription") final int subscription, @PathParam("q") @DefaultValue("") final String criteria) throws Exception { // Check the visibility and get the contract implementation return getSnapshot(subscriptionResource.checkVisible(subscription).getNode()).findAllSnapshots(subscription, criteria); }
/** * Build and return the trigger identifier from the schedule and the * subscription. * * @param schedule * The schedule entity. * @return the {@link String} identifier for the trigger. */ protected static String format(final VmSchedule schedule) { return schedule.getId() + "-" + schedule.getSubscription().getId(); }
/** * Return tools specific configuration. Only non secured parameters are returned. * * @param id * The subscription identifier. * @return tools specific configuration. * @throws Exception * When the configuration gathering fails. Managed at JAX-RS level. */ @GET @Path("{id:\\d+}/configuration") @org.springframework.transaction.annotation.Transactional(readOnly = true) public ConfigurationVo getConfiguration(@PathParam("id") final int id) throws Exception { // Copy subscription details final Subscription entity = checkVisible(id); final ConfigurationVo vo = new ConfigurationVo(); vo.setNode(NodeResource.toVo(entity.getNode())); vo.setParameters(getNonSecuredParameters(id)); vo.setSubscription(id); vo.setProject(DescribedBean.clone(entity.getProject())); // Get specific configuration final ConfigurablePlugin servicePlugin = locator.getResource(vo.getNode().getId(), ConfigurablePlugin.class); if (servicePlugin != null) { // Specific configuration is available vo.setConfiguration(servicePlugin.getConfiguration(id)); } return vo; }
/** * Check the visibility of a configured entity. * * @param configured * The requested configured entity. * @param <K> * The {@link Configurable} identifier type. * @param <T> * The {@link Configurable} type. * @return The formal entity parameter. */ protected <K extends Serializable, T extends Configurable<C, K>> T checkConfiguredVisibility(final T configured) { final Subscription entity = subscriptionRepository .findOneExpected(configured.getConfiguration().getSubscription().getId()); if (projectRepository.findOneVisible(entity.getProject().getId(), securityHelper.getLogin()) == null) { // Associated project is not visible, reject the configuration access throw new EntityNotFoundException(configured.getId().toString()); } return configured; }
@Override public void create(final int subscription) { final Map<String, String> parameters = subscriptionResource.getParameters(subscription); final String group = parameters.get(IdentityResource.PARAMETER_GROUP); final String parentGroup = parameters.get(IdentityResource.PARAMETER_PARENT_GROUP); final String ou = parameters.get(IdentityResource.PARAMETER_OU); final Project project = subscriptionRepository.findOne(subscription).getProject(); final String pkey = project.getPkey(); // Check the relationship between group, OU and project validateGroup(group, ou, pkey); // Check the relationship between group, and parent final String parentDn = validateAndCreateParent(group, parentGroup, ou, pkey); // Create the group inside the parent (OU or parent CN) final String groupDn = "cn=" + group + "," + parentDn; log.info("New Group CN would be created {} project {} and subscription {}", group, pkey); final GroupLdapRepository repository = getGroup(); final GroupOrg groupLdap = repository.create(groupDn, group); // Complete as needed the relationship between parent and this new group if (StringUtils.isNotBlank(parentGroup)) { // This group will be added as "uniqueMember" of its parent repository.addGroup(groupLdap, parentGroup); } // Associate the project to this group in the cache final CacheProjectGroup projectGroup = new CacheProjectGroup(); projectGroup.setProject(project); projectGroup.setGroup(repository.getCacheRepository().findOneExpected(groupLdap.getId())); cacheProjectGroupRepository.saveAndFlush(projectGroup); }
/** * Return members of given subscription. */ private Set<UserOrg> getMembersOfSubscription(final Subscription subscription) { final Set<UserOrg> users = new HashSet<>(); final ServicePlugin plugin = servicePluginLocator.getResource(subscription.getNode().getId()); if (plugin instanceof LdapPluginResource) { users.addAll(((LdapPluginResource) plugin).getMembers(subscription.getId())); } return users; }
@GET @Path("{subscription:\\d+}") @Override @Transactional @org.springframework.transaction.annotation.Transactional(readOnly = true) public VmConfigurationVo getConfiguration(@PathParam("subscription") final int subscription) throws ParseException { // Check the subscription is visible final Subscription entity = subscriptionResource.checkVisible(subscription); // Get the details final VmConfigurationVo result = new VmConfigurationVo(); result.setSchedules(scheduleResource.findAll(subscription)); // Add snapshot capability result.setSupportSnapshot(locator.getResource(entity.getNode().getId(), Snapshotting.class) != null); return result; } }
/** * Check the given schedule exists and is related to given subscription. * * @param subscription * The subscription holder. * @param schedule * The target schedule identifier. * @return The resolved schedule when found and owned by the subscription. */ private VmSchedule checkOwnership(final int subscription, final int schedule) { final VmSchedule entity = repository.findOneExpected(schedule); if (entity.getSubscription().getId().intValue() != subscription) { throw new EntityNotFoundException(String.valueOf(schedule)); } return entity; } }
/** * Add activities related to given subscription. * * @param activities * The collected activities. * @param users * The implied users. * @param subscription * The related subscription of theses activities. * @param plugin * The plug-in associated to this subscription. * @param nodes * The nodes that have already been processed. This set will be updated by this function. * @throws Exception * When any technical error occurs. Caught at upper level for the right mapping. */ protected void addSubscriptionActivities(final Map<String, Map<String, Activity>> activities, final Collection<String> users, final Subscription subscription, final ServicePlugin plugin, final Set<INamableBean<String>> nodes) throws Exception { // Collect activities of each subscription of unique node if (plugin instanceof ActivitiesProvider && nodes.add(subscription.getNode())) { final Map<String, Activity> subscriptionActivities = ((ActivitiesProvider) plugin) .getActivities(subscription.getId(), users); for (final Entry<String, Activity> userActivity : subscriptionActivities.entrySet()) { addUserActivities(activities, subscription.getNode(), userActivity); } } }
/** * Return activities associates by given a subscription. */ private ActivitiesComputations getActivities(final int subscription, final boolean global) throws Exception { // Get users from other LDAP subscriptions final Subscription main = subscriptionResource.checkVisible(subscription); final List<Subscription> subscriptions = subscriptionRepository.findAllOnSameProject(subscription); final Set<UserOrg> users = global ? getMembersOfAllSubscriptions(subscriptions) : getMembersOfSubscription(main); // Get the activities from each subscription of the same project, final ActivitiesComputations result = new ActivitiesComputations(); result.setUsers(users); final List<String> userLogins = users.stream().map(UserOrg::getId).collect(Collectors.toList()); final Map<String, Map<String, Activity>> activities = new HashMap<>(); final Set<INamableBean<String>> nodes = new LinkedHashSet<>(); for (final Subscription projectSubscription : subscriptions) { final ServicePlugin resource = servicePluginLocator.getResource(projectSubscription.getNode().getId()); addSubscriptionActivities(activities, userLogins, projectSubscription, resource, nodes); } result.setNodes(nodes); result.setActivities(activities); return result; }
/** * Delete entity. Should be protected with RBAC. * * @param id * The entity identifier. * @throws Exception * When the delete fails. Managed at JAX-RS level. */ @DELETE @Path("{id:\\d+}") public void delete(@PathParam("id") final int id) throws Exception { final Project project = findOneVisible(repository::findOneVisible, id, Function.identity()); for (final Subscription subscription : project.getSubscriptions()) { subscriptionResource.delete(subscription.getId()); } repository.delete(project); }