private static Resource convertToPojoResource(Resource resource, boolean includeDescendants) {
    Resource pojoResource = new Resource(resource.getId());
    pojoResource.setUuid(resource.getUuid());
    pojoResource.setResourceKey(resource.getResourceKey());
    pojoResource.setResourceType(resource.getResourceType());
    pojoResource.setMtime(resource.getMtime());
    pojoResource.setInventoryStatus(resource.getInventoryStatus());
    Configuration pcCopy = resource.getPluginConfiguration();
    if (pcCopy != null) {
      pcCopy = pcCopy.deepCopy();
    }
    pojoResource.setPluginConfiguration(pcCopy);
    pojoResource.setName(resource.getName());
    pojoResource.setDescription(resource.getDescription());
    pojoResource.setLocation(resource.getLocation());
    pojoResource.setVersion(resource.getVersion());

    if (resource.getParentResource() != null) {
      pojoResource.setParentResource(convertToPojoResource(resource.getParentResource(), false));
    }
    if (includeDescendants) {
      for (Resource childResource : resource.getChildResources()) {
        if (isVisibleInInventory(childResource)) {
          pojoResource.addChildResource(convertToPojoResource(childResource, true));
        }
      }
    }
    return pojoResource;
  }
Esempio n. 2
0
  private Resource createResource(ResourceType resourceType, String host) throws Exception {
    Resource resource =
        new Resource(
            TEST_PREFIX + "CassandraDaemon", TEST_PREFIX + "CassandraDaemon", resourceType);
    resource.setUuid(UUID.randomUUID().toString());
    resource.getPluginConfiguration().setSimpleValue("host", host);
    getEntityManager().persist(resource);
    getEntityManager().flush();

    return resource;
  }
  private Resource createNewResource() throws Exception {
    getTransactionManager().begin();
    Resource resource;

    try {
      try {
        ResourceType resourceType =
            new ResourceType(
                "plat" + System.currentTimeMillis(), "test", ResourceCategory.PLATFORM, null);
        DriftDefinitionTemplate template = new DriftDefinitionTemplate();
        template.setName("test-template");
        DriftDefinition templateDef = new DriftDefinition(new Configuration());
        templateDef.setName("test-template-def");
        template.setTemplateDefinition(templateDef);
        template.setUserDefined(true);
        resourceType.addDriftDefinitionTemplate(template);
        em.persist(resourceType);

        Agent agent = new Agent("testagent", "testaddress", 1, "", "testtoken");
        em.persist(agent);
        em.flush();

        DriftDefinition test1Def = new DriftDefinition(new Configuration());
        test1Def.setName("test-1");

        DriftDefinition test2Def = new DriftDefinition(new Configuration());
        test2Def.setName("test-2");

        resource = new Resource("reskey" + System.currentTimeMillis(), "resname", resourceType);
        resource.setUuid("" + new Random().nextInt());
        resource.setAgent(agent);
        resource.setInventoryStatus(InventoryStatus.COMMITTED);
        resource.addDriftDefinition(test1Def);
        resource.addDriftDefinition(test2Def);
        em.persist(resource);

      } catch (Exception e) {
        System.out.println("CANNOT PREPARE TEST: " + e);
        getTransactionManager().rollback();
        throw e;
      }

      em.flush();
      getTransactionManager().commit();
    } finally {
    }

    return resource;
  }
    public MockInventoryManager() {
      super(pcConfig, null, pluginManager, new EventManager(pcConfig));
      platformType =
          new ResourceType(
              "platformResourceTypeName", "pluginName", ResourceCategory.PLATFORM, null);
      bundleHandlerType =
          new ResourceType("bhRTypeName", "pluginName", ResourceCategory.SERVER, platformType);
      serverTypeFS =
          new ResourceType(
              "typeName-fileSystem", "pluginName", ResourceCategory.SERVER, platformType);
      serverTypePC =
          new ResourceType(
              "typeName-plugConfig", "pluginName", ResourceCategory.SERVER, platformType);
      serverTypeRC =
          new ResourceType(
              "typeName-reSconfig", "pluginName", ResourceCategory.SERVER, platformType);
      serverTypeMT =
          new ResourceType("typeName-trait", "pluginName", ResourceCategory.SERVER, platformType);

      int id = 1;
      platform = new Resource("platformKey", "platformName", platformType);
      platform.setId(id++);
      bundleHandler = new Resource("bhKey", "bhName", bundleHandlerType);
      bundleHandler.setId(id++);
      bundleHandler.setParentResource(platform);
      bundleHandler.setUuid(UUID.randomUUID().toString());
      serverFS = new Resource("serverKey-fileSystem", "serverName-fileSystem", serverTypeFS);
      serverFS.setId(id++);
      serverFS.setParentResource(platform);
      serverPC = new Resource("serverKey-plugConfig", "serverName-plugConfig", serverTypePC);
      serverPC.setId(id++);
      serverPC.setParentResource(platform);
      serverRC = new Resource("serverKey-resConfig", "serverName-resConfig", serverTypeRC);
      serverRC.setId(id++);
      serverRC.setParentResource(platform);
      serverMT = new Resource("serverKey-traitConfig", "serverName-traitConfig", serverTypeMT);
      serverMT.setId(id++);
      serverMT.setParentResource(platform);

      typeResourceMap.put(platformType, platform);
      typeResourceMap.put(bundleHandlerType, bundleHandler);
      typeResourceMap.put(serverTypeFS, serverFS);
      typeResourceMap.put(serverTypePC, serverPC);
      typeResourceMap.put(serverTypeRC, serverRC);
      typeResourceMap.put(serverTypeMT, serverMT);

      ResourceContainer platformContainer = new ResourceContainer(platform, null);
      ResourceContainer bundleHandlerContainer = new ResourceContainer(bundleHandler, null);
      ResourceContainer serverContainerFS = new ResourceContainer(serverFS, null);
      ResourceContainer serverContainerPC = new ResourceContainer(serverPC, null);
      ResourceContainer serverContainerRC = new ResourceContainer(serverRC, null);
      ResourceContainer serverContainerMT = new ResourceContainer(serverMT, null);
      idResourceContainerMap.put(platform.getId(), platformContainer);
      idResourceContainerMap.put(bundleHandler.getId(), bundleHandlerContainer);
      idResourceContainerMap.put(serverFS.getId(), serverContainerFS);
      idResourceContainerMap.put(serverPC.getId(), serverContainerPC);
      idResourceContainerMap.put(serverRC.getId(), serverContainerRC);
      idResourceContainerMap.put(serverMT.getId(), serverContainerMT);

      bundleHandlerContainer.setResourceContext(new MockResourceContext(bundleHandler));

      // each different resource type that supports bundle deployments needs to define its
      // bundle configuration to denote where the base directory location is found.
      // Today we support four ways: via plugin config property, resource config property,
      // measurement trait value, or strictly on the root file system (using no resource specific
      // value)
      ResourceTypeBundleConfiguration rtbc =
          new ResourceTypeBundleConfiguration(new Configuration());
      rtbc.addBundleDestinationBaseDirectory(
          BUNDLE_CONFIG_NAME_FS, Context.fileSystem.name(), BUNDLE_CONFIG_CONTEXT_VALUE_FS, null);
      serverTypeFS.setResourceTypeBundleConfiguration(rtbc);

      rtbc = new ResourceTypeBundleConfiguration(new Configuration());
      rtbc.addBundleDestinationBaseDirectory(
          BUNDLE_CONFIG_NAME_PC,
          Context.pluginConfiguration.name(),
          BUNDLE_CONFIG_CONTEXT_VALUE_PC,
          null);
      serverTypePC.setResourceTypeBundleConfiguration(rtbc);

      rtbc = new ResourceTypeBundleConfiguration(new Configuration());
      rtbc.addBundleDestinationBaseDirectory(
          BUNDLE_CONFIG_NAME_RC,
          Context.resourceConfiguration.name(),
          BUNDLE_CONFIG_CONTEXT_VALUE_RC,
          null);
      serverTypeRC.setResourceTypeBundleConfiguration(rtbc);

      rtbc = new ResourceTypeBundleConfiguration(new Configuration());
      rtbc.addBundleDestinationBaseDirectory(
          BUNDLE_CONFIG_NAME_MT,
          Context.measurementTrait.name(),
          BUNDLE_CONFIG_CONTEXT_VALUE_MT,
          null);
      serverTypeMT.setResourceTypeBundleConfiguration(rtbc);

      // each different resource needs to specify where exactly it wants the bundles deployed
      // using the different contexts that are supported.
      Configuration pluginConfiguration = new Configuration();
      pluginConfiguration.put(
          new PropertySimple(BUNDLE_CONFIG_CONTEXT_VALUE_PC, BUNDLE_CONFIG_LOCATION_PC));
      serverPC.setPluginConfiguration(pluginConfiguration);

      Configuration resourceConfiguration = new Configuration();
      resourceConfiguration.put(
          new PropertySimple(BUNDLE_CONFIG_CONTEXT_VALUE_RC, BUNDLE_CONFIG_LOCATION_RC));
      serverRC.setResourceConfiguration(resourceConfiguration);

      MeasurementDefinition definition =
          new MeasurementDefinition(serverTypeMT, BUNDLE_CONFIG_CONTEXT_VALUE_MT);
      definition.setDataType(DataType.TRAIT);
      definition.setId(123);
      MeasurementSchedule schedule = new MeasurementSchedule(definition, serverMT);
      schedule.setId(123123);
      MeasurementScheduleRequest scheduleRequest = new MeasurementScheduleRequest(schedule);
      Set<MeasurementScheduleRequest> schedules = new HashSet<MeasurementScheduleRequest>(1);
      schedules.add(scheduleRequest);
      serverContainerMT.setMeasurementSchedule(schedules);
    }