LocatorClusterManagementService implements ClusterManagementService { @Override public <T extends AbstractConfiguration<?>> ClusterManagementRealizationResult create(T config) { ConfigurationManager configurationManager = getConfigurationManager(config); if (persistenceService == null) { return assertSuccessful(new ClusterManagementRealizationResult(StatusCode.ERROR, "Cluster configuration service needs to be enabled.")); } lockCMS(); try { try { commonValidator.validate(CacheElementOperation.CREATE, config); ConfigurationValidator validator = validators.get(config.getClass()); if (validator != null) { validator.validate(CacheElementOperation.CREATE, config); } if (configurationManager instanceof CacheConfigurationManager) { memberValidator.validateCreate(config, (CacheConfigurationManager) configurationManager); } } catch (EntityExistsException e) { raise(StatusCode.ENTITY_EXISTS, e); } catch (IllegalArgumentException e) { raise(StatusCode.ILLEGAL_ARGUMENT, e); } Set<String> groups = new HashSet<>(); Set<DistributedMember> targetedMembers; if (config instanceof RegionScoped) { String regionName = ((RegionScoped) config).getRegionName(); groups = memberValidator.findGroups(regionName); if (groups.isEmpty()) { raise(StatusCode.ENTITY_NOT_FOUND, "Region provided does not exist: " + regionName); } targetedMembers = memberValidator.findServers(groups.toArray(new String[0])); } else { final String groupName = AbstractConfiguration.getGroupName(config.getGroup()); groups.add(groupName); targetedMembers = memberValidator.findServers(groupName); } ClusterManagementRealizationResult result = new ClusterManagementRealizationResult(); List<RealizationResult> functionResults = executeCacheRealizationFunction( config, CacheElementOperation.CREATE, targetedMembers); functionResults.forEach(result::addMemberStatus); if (result.getStatusCode() != StatusCode.OK) { result.setStatus(StatusCode.ERROR, "Failed to create on all members."); return assertSuccessful(result); } List<String> updatedGroups = new ArrayList<>(); List<String> failedGroups = new ArrayList<>(); for (String groupName : groups) { try { configurationManager.add(config, groupName); updatedGroups.add(groupName); } catch (Exception e) { logger.error(e.getMessage(), e); failedGroups.add(groupName); } } setResultStatus(result, updatedGroups, failedGroups); if (result.isSuccessful()) { result.setLinks(config.getLinks()); } return assertSuccessful(result); } finally { unlockCMS(); } } LocatorClusterManagementService(InternalCache cache,
      InternalConfigurationPersistenceService persistenceService); @VisibleForTesting  LocatorClusterManagementService(
      InternalCache cache,
      InternalConfigurationPersistenceService persistenceService,
      Map<Class, ConfigurationManager> managers,
      Map<Class, ConfigurationValidator> validators,
      MemberValidator memberValidator,
      CommonConfigurationValidator commonValidator,
      OperationManager operationManager); @Override ClusterManagementRealizationResult create(T config); @Override ClusterManagementRealizationResult delete(
      T config); @Override ClusterManagementRealizationResult update(
      T config); @Override ClusterManagementListResult<T, R> list(
      T filter); @Override ClusterManagementGetResult<T, R> get(
      T config); @Override ClusterManagementOperationResult<A, V> start(
      A op); @Override ClusterManagementListOperationsResult<A, V> list(
      A opType); @Override ClusterManagementOperationResult<A, V> get(
      A opType, String opId); @Override CompletableFuture<ClusterManagementOperationResult<A, V>> getFuture(
      A opType, String opId); boolean isConnected(); @Override void close(); @VisibleForTesting
  // the dlock service name used by the CMS
  static final String CMS_DLOCK_SERVICE_NAME; }
@Test public void lockAndUnlockCalledAtCreateWithException() { try { service.create(regionConfig); } catch (Exception ignore) { } verify(dLockService).lock(LocatorClusterManagementService.CMS_DLOCK_SERVICE_NAME, -1, -1); verify(dLockService).unlock(LocatorClusterManagementService.CMS_DLOCK_SERVICE_NAME); }
@Test public void create_persistenceIsNull() { org.apache.geode.cache.Region<Object, Object> region = mock(org.apache.geode.cache.Region.class); when(cache.getRegion(any())).thenReturn(region); service = new LocatorClusterManagementService(cache, null); assertThatThrownBy(() -> service.create(regionConfig)) .hasMessageContaining("Cluster configuration service needs to be enabled"); }
@Test public void create_validatorIsCalledCorrectly() { doReturn(Collections.emptySet()).when(memberValidator).findMembers(eq(false), anyString()); doNothing().when(persistenceService).updateCacheConfig(any(), any()); service.create(regionConfig); verify(cacheElementValidator).validate(CacheElementOperation.CREATE, regionConfig); verify(regionValidator).validate(CacheElementOperation.CREATE, regionConfig); verify(memberValidator).validateCreate(regionConfig, regionManager); }
@Test public void create_non_supportedConfigObject() { Member config = new Member(); assertThatThrownBy(() -> service.create(config)).isInstanceOf(ClusterManagementException.class) .hasMessageContaining("ILLEGAL_ARGUMENT: Member is not supported."); }