public void resourceUpdate(IPResource resource) { resourceUpdate(resource.getInternalId(), resource); }
public void resourceUpdate(IPResource resource, IPResource updatedResource) { resourceUpdate(resource.getInternalId(), updatedResource); }
protected IPResource retrieveAndUpdateOrCreateResource(IPResourceService resourceService, ChangesContext changes, IPResource resource) { // Search in current changes Optional<IPResource> foundOptional = changes.getResourcesToAdd().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it)) // .findAny(); if (foundOptional.isPresent()) { updateResourceIfDifferent(resource, foundOptional.get()); return foundOptional.get(); } foundOptional = changes.getResourcesToUpdate().stream() // .filter(it -> resourceService.resourceEqualsPk(resource, it.getB())) // .map(it -> it.getB()) // .findAny(); if (foundOptional.isPresent()) { updateResourceIfDifferent(resource, foundOptional.get()); return foundOptional.get(); } // Search in repository foundOptional = resourceService.resourceFindByPk(resource); if (foundOptional.isPresent()) { IPResource foundResource = foundOptional.get(); if (updateResourceIfDifferent(resource, foundResource)) { changes.resourceUpdate(foundResource, foundResource); } return foundResource; } // Create new changes.resourceAdd(resource); return resource; }
private void common(CommonServicesContext services, ChangesContext changes, JunitResource resource) { if (!alwaysUpdate) { return; } List<JunitResource> junitResources = services.getResourceService().resourceFindAll(services.getResourceService().createResourceQuery(JunitResource.class)); if (junitResources.size() < 3) { for (int i = 0; i < 5; ++i) { changes.resourceAdd(new JunitResource(SecureRandomTools.randomHexString(10))); } } else { JunitResource update = junitResources.get(0); JunitResource refresh = junitResources.get(1); JunitResource delete = junitResources.get(2); update.setDate(new Date()); changes.resourceUpdate(update); changes.resourceRefresh(refresh); changes.resourceDelete(delete); } }
@Override protected void commonHandlerExecute(CommonServicesContext services, ChangesContext changes, CommonMethodUpdateEventHandlerContext<LetsencryptConfig> context) { LetsencryptConfig resource = context.getResource(); boolean update = false; // accountKeypairPem if (Strings.isNullOrEmpty(resource.getAccountKeypairPem())) { logger.info("Generating an AccountKeypair"); AsymmetricKeys keys = RSACrypt.RSA_CRYPT.generateKeyPair(4096); String accountPem = RSACrypt.RSA_CRYPT.savePrivateKeyPemAsString(keys) + RSACrypt.RSA_CRYPT.savePublicKeyPemAsString(keys); resource.setAccountKeypairPem(accountPem); update = true; } // tagName if (Strings.isNullOrEmpty(resource.getTagName())) { logger.info("Generating a Tag name"); resource.setTagName("letsencrypt_" + SecureRandomTools.randomHexString(10).toLowerCase()); update = true; } // Update if changed if (update) { changes.resourceUpdate(resource); } }
@Override public void addHandler(CommonServicesContext services, ChangesContext changes, UnixUser resource) { // Unique user name checkUniqueName(services, resource.getName()); // Choose the next id if (resource.getId() == null) { resource.setId(UnixUserAvailableIdHelper.getNextAvailableId()); changes.resourceUpdate(resource.getInternalId(), resource); } common(services, changes, resource); }
@Override public void addHandler(CommonServicesContext services, ChangesContext changes, UnixUser resource) { // Unique user name checkUniqueName(services, resource.getName()); // Choose the next id if (resource.getId() == null) { resource.setId(UnixUserAvailableIdHelper.getNextAvailableId()); changes.resourceUpdate(resource.getInternalId(), resource); } common(services, changes, resource); }
logger.debug("Updating runAs to: {}", neededRunAs); resource.getApplicationDefinition().setRunAs(neededRunAs); changes.resourceUpdate(resource.getInternalId(), resource);
logger.debug("Updating runAs to: {}", neededRunAs); resource.getApplicationDefinition().setRunAs(neededRunAs); changes.resourceUpdate(resource.getInternalId(), resource);
changes.resourceUpdate(resource); changes.resourceUpdate(resource); changes.resourceUpdate(resource); changes.resourceUpdate(resource);
changes.resourceUpdate(resource); changes.resourceUpdate(resource); changes.resourceUpdate(resource); changes.resourceUpdate(resource);
changesContext.resourceAdd(resource); } else { changesContext.resourceUpdate(internalId, resource);
@Test public void testQueryEditorName() { IPResourceService resourceService = getCommonServicesContext().getResourceService(); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); // Get the initial id Optional<JunitResource> junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .propertyContains(JunitResource.PROPERTY_SET_TEXTS, Arrays.asList("two"))// ); Assert.assertTrue(junitResourceOptional.isPresent()); long expectedId = junitResourceOptional.get().getInternalId(); // Modify its editor JunitResource resource = junitResourceOptional.get(); ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); resource.setResourceEditorName("junit"); changes.resourceUpdate(expectedId, resource); internalChangeService.changesExecute(changes); // Get the resource junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .addEditorEquals("junit") // ); Assert.assertTrue(junitResourceOptional.isPresent()); Assert.assertEquals((Long) expectedId, junitResourceOptional.get().getInternalId()); // Don't get the resource if wrong editor junitResourceOptional = resourceService.resourceFind(resourceService.createResourceQuery(JunitResource.class) // .addEditorEquals("not junit") // ); Assert.assertFalse(junitResourceOptional.isPresent()); }
@Override public void timerHandler(CommonServicesContext services, ChangesContext changes, TimerEventContext event) { IPResourceService resourceService = services.getResourceService(); // Check the certs that will expire in 1 week logger.info("Getting self-signed certificates that expire in 1 week"); List<WebsiteCertificate> certificatesToUpdate = resourceService.resourceFindAll( // resourceService.createResourceQuery(WebsiteCertificate.class) // .addEditorEquals(SelfSignedWebsiteCertificateEditor.EDITOR_NAME) // .propertyLesserAndEquals(WebsiteCertificate.PROPERTY_END, DateTools.addDate(Calendar.WEEK_OF_YEAR, 1) // )); // Update them logger.info("Got {} certificates to update", certificatesToUpdate.size()); for (WebsiteCertificate certificate : certificatesToUpdate) { logger.info("Updating certificate {}", certificate.getDomainNames()); try { RSACertificate currentRsaCertificate = CertificateHelper.toRSACertificate(certificate); AsymmetricKeys keys = currentRsaCertificate.getKeysForSigning(); RSACertificate rsaCertificate = new RSACertificate(keys).selfSign( // new CertificateDetails().setCommonName(currentRsaCertificate.getCommonName()) // .addSanDns(currentRsaCertificate.getCommonName()) // .setEndDate(DateTools.addDate(Calendar.MONTH, 1))); CertificateHelper.toWebsiteCertificate(null, rsaCertificate, certificate); changes.resourceUpdate(certificate.getInternalId(), certificate); } catch (Exception e) { logger.error("Problem updating self-signed certificate {}", certificate.getDomainNames(), e); services.getMessagingService().alertingError("Problem updating self-signed certificate " + certificate.getDomainNames(), e.getMessage()); } } }
@Override public void timerHandler(CommonServicesContext services, ChangesContext changes, TimerEventContext event) { IPResourceService resourceService = services.getResourceService(); // Check the certs that will expire in 1 week logger.info("Getting self-signed certificates that expire in 1 week"); List<WebsiteCertificate> certificatesToUpdate = resourceService.resourceFindAll( // resourceService.createResourceQuery(WebsiteCertificate.class) // .addEditorEquals(SelfSignedWebsiteCertificateEditor.EDITOR_NAME) // .propertyLesserAndEquals(WebsiteCertificate.PROPERTY_END, DateTools.addDate(Calendar.WEEK_OF_YEAR, 1) // )); // Update them logger.info("Got {} certificates to update", certificatesToUpdate.size()); for (WebsiteCertificate certificate : certificatesToUpdate) { logger.info("Updating certificate {}", certificate.getDomainNames()); try { RSACertificate currentRsaCertificate = CertificateHelper.toRSACertificate(certificate); AsymmetricKeys keys = currentRsaCertificate.getKeysForSigning(); RSACertificate rsaCertificate = new RSACertificate(keys).selfSign( // new CertificateDetails().setCommonName(currentRsaCertificate.getCommonName()) // .addSanDns(currentRsaCertificate.getCommonName()) // .setEndDate(DateTools.addDate(Calendar.MONTH, 1))); CertificateHelper.toWebsiteCertificate(null, rsaCertificate, certificate); changes.resourceUpdate(certificate.getInternalId(), certificate); } catch (Exception e) { logger.error("Problem updating self-signed certificate {}", certificate.getDomainNames(), e); services.getMessagingService().alertingError("Problem updating self-signed certificate " + certificate.getDomainNames(), e.getMessage()); } } }
changes.resourceUpdate(machine.getInternalId(), machine); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(machine.getInternalId(), machine); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(machine.getInternalId(), machine); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(machine.getInternalId(), machine); try { getInternalServicesContext().getInternalChangeService().changesExecute(changes);
@Test public void testDuplicatePkSameResource_update() { // Common JunitResource resource; ChangesContext changes = new ChangesContext(getCommonServicesContext().getResourceService()); InternalChangeService internalChangeService = getInternalServicesContext().getInternalChangeService(); IPResourceService resourceService = getCommonServicesContext().getResourceService(); // Create 2 items resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(10L); changes.resourceAdd(resource); resource = new JunitResource("t2", JunitResourceEnum.A, 2); resource.setLongNumber(10L); changes.resourceAdd(resource); internalChangeService.changesExecute(changes); // Rename second item to same pk as first thrown.expect(ResourcePrimaryKeyCollisionException.class); resource = new JunitResource("t1", JunitResourceEnum.A, 1); resource.setLongNumber(20L); changes.resourceUpdate(resourceService.resourceFindByPk(new JunitResource("t2", JunitResourceEnum.A, 2)).get().getInternalId(), resource); internalChangeService.changesExecute(changes); }
changes.resourceUpdate(m2.getInternalId(), m2); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(m2.getInternalId(), m2); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(dp.getInternalId(), new DnsPointer("pointer2.example.com")); getInternalServicesContext().getInternalChangeService().changesExecute(changes); dp = resourceService.resourceFindByPk(new DnsPointer("pointer2.example.com")).get();
changes.resourceUpdate(m1.getInternalId(), new Machine(machineName1, m1Ip2)); changes.resourceUpdate(m2.getInternalId(), new Machine(machineName2, m2Ip2)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(m2.getInternalId(), new Machine(machineName2, null)); getInternalServicesContext().getInternalChangeService().changesExecute(changes); changes.resourceUpdate(m2.getInternalId(), new Machine("anotherName.node.example.com")); getInternalServicesContext().getInternalChangeService().changesExecute(changes); Assert.fail("Expecting an exception");
changes.resourceUpdate(c1.getInternalId(), c2); getInternalServicesContext().getInternalChangeService().changesExecute(changes);