private void common(CommonServicesContext services, ChangesContext changes) { IPResourceService resourceService = services.getResourceService(); List<Bind9Server> bind9Servers = resourceService.resourceFindAll(resourceService.createResourceQuery(Bind9Server.class)); bind9Servers.forEach(it -> { changes.resourceRefresh(it); }); }
private <R extends IPResource> void assertLinkToAllNames(Class<R> resourceClass, String linkType, IPResource resourceFrom, String... expectedNames) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); List<String> actualNames = resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resourceFrom, linkType, resourceClass).stream() // .map(it -> it.getResourceName()) // .sorted() // .collect(Collectors.toList()); List<String> eNames = Arrays.asList(expectedNames).stream() // .sorted() // .collect(Collectors.toList()); Assert.assertEquals(eNames, actualNames); }
protected void assertInfraConfigApps(String resourceName) { List<IPResource> resources = new ArrayList<>(); IPResourceService resourceService = getCommonServicesContext().getResourceService(); resources.add(resourceService.resourceFind(resourceService.createResourceQuery(Application.class) // .propertyEquals(Application.PROPERTY_NAME, "infra_login")).get()); resources.add(resourceService.resourceFind(resourceService.createResourceQuery(Application.class) // .propertyEquals(Application.PROPERTY_NAME, "infra_ui")).get()); resources.add(resourceService.resourceFind(resourceService.createResourceQuery(Website.class) // .propertyEquals(Website.PROPERTY_NAME, "infra_login")).get()); resources.add(resourceService.resourceFind(resourceService.createResourceQuery(Website.class) // .propertyEquals(Website.PROPERTY_NAME, "infra_ui")).get()); AssertTools.assertJsonComparison(resourceName, AbstractIPResourceServiceTest.class, resources); }
protected boolean resourceCheckInListIds(IPResource resource, List<Long> resourceIds) { return resourceIds.stream() // .map(it -> resourceService.resourceFind(it).get()) // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .findAny().isPresent(); }
List<Website> websites = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(Website.class, LinkTypeConstants.INSTALLED_ON, machine); websites.addAll(resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(Website.class, Website.LINK_TYPE_INSTALLED_ON_NO_DNS, machine)); haProxyApplication.setName("infra_web-" + machineName.replaceAll("\\.", "_")); Optional<Application> existingHaProxyApplicationOptional = resourceService.resourceFindByPk(haProxyApplication); if (websites.isEmpty()) { logger.info("[{}] No websites to install", machineName); logger.debug("[{}] HA Proxy exists. Will see if needs an update", machineName); existingHaProxyApplication = existingHaProxyApplicationOptional.get(); List<UnixUser> unixUsers = resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(existingHaProxyApplication, LinkTypeConstants.RUN_AS, UnixUser.class); if (unixUsers.size() > 1) { logger.error("[{}] The HA Proxy application has too many unix users: {}", machineName, unixUsers.size()); Optional<UnixUser> optional = resourceService.resourceFind(resourceService.createResourceQuery(UnixUser.class).propertyEquals(UnixUser.PROPERTY_NAME, UNIX_USER_HA_PROXY_NAME)); if (optional.isPresent()) { unixUser = optional.get(); List<Application> pointsToApplications = resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(website, LinkTypeConstants.POINTS_TO, Application.class); .map(app -> new Tuple2<>(app, resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(app, LinkTypeConstants.INSTALLED_ON, Machine.class))) // .collect(Collectors.toList()); if (pointsToApplications.isEmpty()) { List<WebsiteCertificate> websiteCertificates = resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(website, LinkTypeConstants.USES, WebsiteCertificate.class); if (websiteCertificates.size() != 1) { logger.debug("[{}] Cannot configure {} since we are expecting 1 website certificate and got {}", machineName, website, websiteCertificates.size());
entries = resourceService.resourceFindAll(resourceService.createResourceQuery(JunitResource.class)); for (JunitResource entry : entries) { changes.getResourcesToDelete().add(entry.getInternalId()); entries = resourceService.resourceFindAll(resourceService.createResourceQuery(JunitResource.class)); Assert.assertTrue(entries.isEmpty()); resource = resourceService.resourceFindByPk(new JunitResource("theMaster")).get(); masterResource = resourceService.resourceFindByPk(masterResource).get(); List<Tuple2<String, ? extends IPResource>> links = resourceService.linkFindAllByFromResource(masterResource); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkType(resource, "LIKES").size()); Assert.assertEquals(2, resourceService.linkFindAllByFromResourceAndLinkType(resource, "COMMANDS").size()); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resource, "LIKES", JunitResource.class).size()); Assert.assertEquals(2, resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resource, "COMMANDS", JunitResource.class).size()); resource = resourceService.resourceFindByPk(new JunitResource("slave1")).get(); Assert.assertEquals(0, resourceService.linkFindAllByFromResource(resource).size()); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkType(resource, "LIKES").size()); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkType(resource, "COMMANDS").size()); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resource, "LIKES", JunitResource.class).size()); Assert.assertEquals(0, resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resource, "COMMANDS", JunitResource.class).size()); resource = resourceService.resourceFindByPk(new JunitResource("slave2")).get(); Assert.assertEquals(1, resourceService.linkFindAllByFromResource(resource).size()); for (Tuple2<String, ? extends IPResource> usingResource : resourceService.linkFindAllByFromResource(resource)) {
allIds.addAll(resourceService.resourceFindAll(resourceService.createResourceQuery(Machine.class)).stream().map(it -> it.getInternalId()).collect(Collectors.toList())); allIds.addAll(resourceService.resourceFindAll(resourceService.createResourceQuery(DnsEntry.class)).stream().map(it -> it.getInternalId()).collect(Collectors.toList())); Collections.sort(allIds); assertResourceExists(true, new DnsEntry(machineName2, DnsEntryType.A, m2Ip1), DnsEntry.class); Set<String> tags = resourceService.tagFindAllByResource(m2); Assert.assertEquals(1, tags.size()); Assert.assertEquals("extraTag", tags.iterator().next()); List<? extends IPResource> extraLinks = resourceService.linkFindAllByFromResourceAndLinkType(m1, "extraLink"); Assert.assertEquals(1, extraLinks.size()); Assert.assertEquals(m2, extraLinks.get(0)); allActualIds.addAll(resourceService.resourceFindAll(resourceService.createResourceQuery(Machine.class)).stream().map(it -> it.getInternalId()).collect(Collectors.toList())); allActualIds.addAll(resourceService.resourceFindAll(resourceService.createResourceQuery(DnsEntry.class)).stream().map(it -> it.getInternalId()).collect(Collectors.toList())); Collections.sort(allActualIds); Assert.assertEquals(allIds, allActualIds);
@Override protected void commonHandlerExecute(CommonServicesContext services, ChangesContext changes, CommonMethodUpdateEventHandlerContext<EmailDomain> context) { EmailDomain emailDomain = context.getResource(); // Ensure all account names are unique per account and per redirection (can be shared) Set<String> accountNames = new HashSet<>(); services.getResourceService().linkFindAllByFromResourceClassAndLinkTypeAndToResource(EmailAccount.class, LinkTypeConstants.INSTALLED_ON, emailDomain).forEach(account -> { if (!accountNames.add(account.getAccountName())) { throw new IllegalUpdateException("The email account [" + account.getAccountName() + "@" + emailDomain.getDomainName() + "] exists multiple times"); } }); accountNames.clear(); services.getResourceService().linkFindAllByFromResourceClassAndLinkTypeAndToResource(EmailRedirection.class, LinkTypeConstants.INSTALLED_ON, emailDomain).forEach(redirection -> { if (!accountNames.add(redirection.getAccountName())) { throw new IllegalUpdateException("The email redirection [" + redirection.getAccountName() + "@" + emailDomain.getDomainName() + "] exists multiple times"); } }); // Ensure only linked to one EmailServer if (services.getResourceService().linkFindAllByFromResourceAndLinkTypeAndToResourceClass(emailDomain, LinkTypeConstants.INSTALLED_ON, EmailServer.class).size() > 1) { throw new IllegalUpdateException("The email domain [" + emailDomain.getDomainName() + "] is installed on more than one Email server"); } }
List<L> currentLinks = servicesCtx.getResourceService().linkFindAllByFromResourceClassAndLinkTypeAndToResource(fromResourceType, linkType, editedResource); List<? extends IPResource> finalLinks = servicesCtx.getResourceService().resourceFindAll( // servicesCtx.getResourceService().createResourceQuery(fromResourceType) // currentLinks = new ArrayList<>(); } else { currentLinks = servicesCtx.getResourceService().linkFindAllByFromResourceClassAndLinkTypeAndToResource(fromResourceType, linkType, editedResource);
Optional<UnixUser> unixUserOptional = resourceService.resourceFind(resourceService.createResourceQuery(UnixUser.class).propertyEquals(UnixUser.PROPERTY_NAME, unixUserName)); logger.info("Getting unix user {}. Is present: {}", unixUserName, unixUserOptional.isPresent()); if (!unixUserOptional.isPresent()) { List<UrlRedirection> urlRedirections = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(UrlRedirection.class, LinkTypeConstants.INSTALLED_ON, machine); logger.info("Machine {} has {} Url Redirections", machineName, urlRedirections.size()); if (urlRedirections.isEmpty()) {
@Test public void testChanges_linksAndTagsAreNotKeptWhenDeleted() { // Create ChangesContext changes = new ChangesContext(); JunitResource r1 = new JunitResource("1"); JunitResource r2 = new JunitResource("2"); changes.getResourcesToAdd().add(r1); changes.getResourcesToAdd().add(r2); changes.getLinksToAdd().add(new Tuple3<>(r1, "link1", r2)); changes.getTagsToAdd().add(new Tuple2<>(r1, "tag1")); getInternalServicesContext().getInternalChangeService().changesExecute(changes); IPResourceService resourceService = getCommonServicesContext().getResourceService(); r1 = resourceService.resourceFindByPk(r1).get(); r2 = resourceService.resourceFindByPk(r2).get(); Assert.assertEquals(Arrays.asList("link1"), resourceService.linkFindAllByFromResource(r1).stream().map(it -> it.getA()).sorted().collect(Collectors.toList())); Assert.assertEquals(Arrays.asList("tag1"), resourceService.tagFindAllByResource(r1).stream().sorted().collect(Collectors.toList())); // Delete it changes.getResourcesToDelete().add(resourceService.resourceFindByPk(r1).get().getInternalId()); getInternalServicesContext().getInternalChangeService().changesExecute(changes); Assert.assertEquals(0, resourceService.linkFindAllByFromResource(r1).size()); Assert.assertEquals(0, resourceService.tagFindAllByResource(r1).size()); // Recreate it changes.getResourcesToAdd().add(r1); getInternalServicesContext().getInternalChangeService().changesExecute(changes); // Check no links and tags Assert.assertEquals(0, resourceService.linkFindAllByFromResource(r1).size()); Assert.assertEquals(0, resourceService.tagFindAllByResource(r1).size()); }
@Test public void testQueryBoolean_less_equal_no() { thrown.expectMessage("Property [bool] does not support querying lesser or equal"); IPResourceService resourceService = getCommonServicesContext().getResourceService(); resourceService.createResourceQuery(JunitResource.class) // .propertyLesserAndEquals(JunitResource.PROPERTY_BOOL, true); }
public <T extends IPResource> T assertResourceExists(boolean expectedExists, T resourcePk, Class<T> resourceClass) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<T> resourceFindOne = resourceService.resourceFindByPk(resourcePk); Assert.assertEquals(expectedExists, resourceFindOne.isPresent()); if (resourceFindOne.isPresent()) { return resourceFindOne.get(); } else { return null; } }
logger.debug("Getting the previously managed resources"); for (Class<? extends IPResource> managedResourceType : context.getManagedResourceTypes()) { previousManagedResources.addAll(resourceService.linkFindAllByFromResourceAndLinkTypeAndToResourceClass(resource, LinkTypeConstants.MANAGES, managedResourceType)); List<? extends IPResource> managers = resourceService.linkFindAllByLinkTypeAndToResource(LinkTypeConstants.MANAGES, desiredManagedResource); if (managers.size() > 1) { throw new IllegalUpdateException( previousLinks = Collections.emptyList(); } else { previousLinks = resourceService.linkFindAllByFromResourceAndLinkType(desiredManagedResource, linkType); .filter(previous -> !desiredLinks.stream().anyMatch(desired -> resourceService.resourceEqualsPk(previous, desired))) // .forEach(previous -> { logger.debug("Removing link {}/{}/{}", desiredManagedResource, linkType, previous); .filter(desired -> !previousLinks.stream().anyMatch(previous -> resourceService.resourceEqualsPk(previous, desired))) // .forEach(desired -> { logger.debug("Adding link {}/{}/{}", desiredManagedResource, linkType, desired); previousLinks = Collections.emptyList(); } else { previousLinks = resourceService.linkFindAllByLinkTypeAndToResource(linkType, desiredManagedResource); .filter(previous -> !desiredLinks.stream().anyMatch(desired -> resourceService.resourceEqualsPk(previous, desired))) // .forEach(previous -> { logger.debug("Removing link {}/{}/{}", previous, linkType, desiredManagedResource); .filter(desired -> !previousLinks.stream().anyMatch(previous -> resourceService.resourceEqualsPk(previous, desired))) //
List<AttachablePart> attachedParts = new ArrayList<>(); resourceService.linkFindAllByFromResource(apachePhp).stream() // List<Website> websitesFrom = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(Website.class, LinkTypeConstants.POINTS_TO, apachePhp); applicationFinalState.addManagedLinksFromType(LinkTypeConstants.POINTS_TO); websitesFrom.forEach(it -> applicationFinalState.addLinkFrom(it, LinkTypeConstants.POINTS_TO));
private <R extends IPResource> void assertLinkFromAllNames(Class<R> resourceClass, String linkType, IPResource resourceTo, String... expectedNames) { IPResourceService resourceService = getCommonServicesContext().getResourceService(); List<String> actualNames = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(resourceClass, linkType, resourceTo).stream() // .map(it -> it.getResourceName()) // .sorted() // .collect(Collectors.toList()); List<String> eNames = Arrays.asList(expectedNames).stream() // .sorted() // .collect(Collectors.toList()); Assert.assertEquals(eNames, actualNames); }
@Test public void testQueryPK_one() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); Optional<JunitResource> one = resourceService.resourceFindByPk(new JunitResource("www.example.com", JunitResourceEnum.A, 2)); Assert.assertTrue(one.isPresent()); Set<String> tags = resourceService.tagFindAllByResource(one.get()); Assert.assertEquals(1, tags.size()); Assert.assertTrue(tags.contains("asite")); }
@SuppressWarnings({ "unchecked" }) protected <T extends IPResource> T retrieveOrCreateResource(IPResourceService resourceService, ChangesContext changes, T resource, Class<T> resourceClass) { // Search in current changes Optional<T> foundOptional = changes.getResourcesToAdd().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .map(it -> (T) it) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } foundOptional = changes.getResourcesToUpdate().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it.getB())) // .map(it -> (T) it.getB()) // .findAny(); if (foundOptional.isPresent()) { return foundOptional.get(); } // Search in repository foundOptional = resourceService.resourceFindByPk(resource); if (foundOptional.isPresent()) { return foundOptional.get(); } // Create new changes.resourceAdd(resource); return resource; }
currentFromResources = Collections.emptyList(); } else { currentFromResources = resourceService.linkFindAllByFromResourceClassAndLinkTypeAndToResource(resourceFromType, linkType, toResource); Optional<L> existingLink = currentFromResources.stream().filter(it -> resourceService.resourceEqualsPk(fromResource, it)).findAny(); if (existingLink.isPresent()) { currentFromResources.remove(existingLink.get());
public static ResourcesDump dumpExport(CommonServicesContext commonServicesContext, InternalServicesContext internalServicesContext) { ResourcesDump dump = new ResourcesDump(); IPResourceService resourceService = commonServicesContext.getResourceService(); for (IPResource resource : internalServicesContext.getInternalIPResourceService().resourceFindAll()) { String resourceType = resourceService.getResourceDefinition(resource).getResourceType(); String resourceTypeAndName = resourceType + "/" + resource.getResourceName(); // Export resource dump.getResources().add(new ResourcesDumpResource(resourceType, resource)); // Export tags resourceService.tagFindAllByResource(resource).stream() // .forEach(it -> dump.getTags().add(new ResourcesDumpTag(resourceTypeAndName, it))); // Export links resourceService.linkFindAllByFromResource(resource).stream() // .map(it -> { String toResourceType = resourceService.getResourceDefinition(it.getB()).getResourceType(); String toResourceTypeAndName = toResourceType + "/" + it.getB().getResourceName(); return new ResourcesDumpLink(resourceTypeAndName, it.getA(), toResourceTypeAndName); }) // .forEach(it -> dump.getLinks().add(it)); } dump.sort(); return dump; }