/** * Returns true if any of the condition sets match an inventoried Resource. * * @param context * @return */ public boolean isActive(GlobalActivationContext context) { ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); Subject subject = context.getSubject(); for (ResourceConditionSet rcs : this.resourceConditionSets) { ResourceCriteria criteria = new ResourceCriteria(); criteria.addFilterPluginName(rcs.getPluginName()); criteria.addFilterResourceTypeName(rcs.getResourceTypeName()); Set<Permission> requiredPermissions = rcs.getPermissions(); if (!((null == requiredPermissions) || requiredPermissions.isEmpty())) { Permission[] arr = requiredPermissions.toArray(new Permission[requiredPermissions.size()]); criteria.addRequiredPermissions(arr); } PageList<Resource> resources = resourceManager.findResourcesByCriteria(context.getSubject(), criteria); if (!((null == resources) || resources.isEmpty())) { return ActivatorHelper.areTraitsSatisfied( subject, rcs.getTraitMatchers(), resources, false); } } return false; }
private Resource getPlatform(String hostname) { ResourceCriteria criteria = new ResourceCriteria(); criteria.setFiltersOptional(true); criteria.addFilterResourceKey(hostname); criteria.addFilterName(hostname); criteria.addFilterResourceCategories(ResourceCategory.PLATFORM); criteria.fetchResourceType(true); criteria.fetchExplicitGroups(true); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); PageList<Resource> resources = resourceManager.findResourcesByCriteria(overlord, criteria); if (resources.isEmpty()) { String msg = "Could not find platform with hostname " + hostname + ". The value that you specify for the " + "host argument should match either a platform's resource name and/or its resource key."; throw new ResourceNotFoundException(msg); } return resources.get(0); }
@Override public List<Resource> getResourcesAsList(Integer... resourceIds) { long start = System.currentTimeMillis(); ResourceCriteria criteria = new ResourceCriteria(); // get all of the resources for the supplied ids criteria.addFilterIds(resourceIds); // filter out any that are not actually in inventory criteria.addFilterInventoryStatuses( new ArrayList<InventoryStatus>(InventoryStatus.getInInventorySet())); // get all of them, don't limit to default paging criteria.clearPaging(); criteria.fetchResourceType(true); criteria.fetchPluginConfiguration(true); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); Subject overlord = LookupUtil.getSubjectManager().getOverlord(); List<Resource> result = resourceManager.findResourcesByCriteria(overlord, criteria); if (log.isDebugEnabled()) { log.debug( "Performance: get ResourcesAsList [" + resourceIds + "], timing (" + (System.currentTimeMillis() - start) + ")ms"); } // Now do some clean out of stuff the agent does not need // Perhaps we should limit the query above to only return relevant stuff for (Resource resource : result) { cleanoutResource(resource); } return result; }
@Test(enabled = ENABLE_TESTS) public void testDriftDef() throws Exception { Configuration config = new Configuration(); DriftDefinition driftDefPojo = new DriftDefinition(config); driftDefPojo.setName("testDriftDef"); driftDefPojo.setInterval(60L); driftDefPojo.setBasedir(new BaseDirectory(BaseDirValueContext.fileSystem, "foo/bar")); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); ResourceManagerLocal resourceManager = LookupUtil.getResourceManager(); ResourceCriteria c = new ResourceCriteria(); c.addFilterId(newResource.getId()); c.fetchDriftDefinitions(true); List<Resource> resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); Set<DriftDefinition> driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); DriftDefinition driftDef = null; for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getName(), driftDef.getName()); assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); driftDefPojo.setInterval(120L); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); driftDef = null; for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertEquals(driftDefPojo.getName(), driftDef.getName()); assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(120L, driftDef.getInterval()); driftDefPojo.setName("testDriftDef-2"); driftDefPojo.setInterval(30L); driftDefPojo.setBasedir(new BaseDirectory(BaseDirValueContext.fileSystem, "foo/baz")); driftManager.updateDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), driftDefPojo); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(4, driftDefs.size()); for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if ("testDriftDef".equals(driftDef.getName())) { assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals("foo/bar", driftDef.getBasedir().getValueName()); assertEquals(BaseDirValueContext.fileSystem, driftDef.getBasedir().getValueContext()); assertEquals(120L, driftDef.getInterval()); } else if ("testDriftDef-2".equals(driftDef.getName())) { assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); } else if (!"test-1".equals(driftDef.getName()) && !"test-2".equals(driftDef.getName())) { fail("Unexpected drift def name: " + driftDef.getName()); } } driftManager.deleteDriftDefinition( overlord, EntityContext.forResource(newResource.getId()), "testDriftDef"); resources = resourceManager.findResourcesByCriteria(overlord, c); assertEquals(1, resources.size()); driftDefs = resources.get(0).getDriftDefinitions(); assertNotNull(driftDefs); assertEquals(3, driftDefs.size()); for (Iterator<DriftDefinition> i = driftDefs.iterator(); i.hasNext(); ) { driftDef = i.next(); if (driftDefPojo.getName().equals(driftDef.getName())) break; } assertTrue(driftDef.getConfiguration().getId() > 0); // persisted assertEquals(driftDefPojo.getName(), driftDef.getName()); assertEquals(driftDefPojo.getBasedir(), driftDef.getBasedir()); assertEquals(driftDefPojo.getInterval(), driftDef.getInterval()); }