private void cleanupPolicyAlertsChannels(AlertsPolicy policy, Set<Integer> policyChannelsToCleanup) { for (Integer policyChannelId : policyChannelsToCleanup) { cleanupPolicyAlertsChannel(policy, policyChannelId); } }
void sync(@NonNull PolicyConfiguration config) { if (!config.getChannels().isPresent()) { LOG.info("No alerts channels for policy {} - skipping...", config.getPolicyName()); return; } LOG.info("Synchronizing alerts channels for policy {}...", config.getPolicyName()); AlertsPolicy policy = api.getAlertsPoliciesApi().getByName(config.getPolicyName()).orElseThrow( () -> new NewRelicSyncException(format("Policy %s does not exist", config.getPolicyName()))); Set<Integer> policyChannelsToCleanup = createOrUpdatePolicyAlertsChannels(policy, config.getChannels().get()); cleanupPolicyAlertsChannels(policy, policyChannelsToCleanup); LOG.info("Alerts channels for policy {} synchronized", config.getPolicyName()); }
private AlertsChannel findSameOrCreate(AlertsChannel alertsChannelFromConfig, Collection<AlertsChannel> sameInstanceChannels) { return sameInstanceChannels.stream() .filter(alertsChannelFromConfig::equals) .findAny() .orElseGet(() -> createAlertsChannel(alertsChannelFromConfig)); }
private Set<Integer> createOrUpdatePolicyAlertsChannels(AlertsPolicy policy, Collection<Channel> channelsFromConfig) { List<AlertsChannel> allAlertsChannels = api.getAlertsChannelsApi().list(); List<AlertsChannel> userChannels = allAlertsChannels.stream() .filter(ChannelConfigurator::isUserChannel) .collect(Collectors.toList()); List<AlertsChannel> nonUserChannels = allAlertsChannels.stream() .filter(c -> !isUserChannel(c)) .collect(Collectors.toList()); Set<Integer> orphanedChannels = getAllPolicyAlertsChannelsIds(policy, allAlertsChannels); Set<Integer> policyChannels = new LinkedHashSet<>(); for (Channel channelFromConfig : channelsFromConfig) { AlertsChannel requestedChannel = toAlertsChannel(channelFromConfig); AlertsChannel syncedChannel; if (isUserChannel(requestedChannel)) { syncedChannel = findUserChannel(requestedChannel, userChannels); } else { syncedChannel = findSameOrCreate(requestedChannel, nonUserChannels); } policyChannels.add(syncedChannel.getId()); } orphanedChannels.removeAll(policyChannels); api.getAlertsPoliciesApi().updateChannels( AlertsPolicyChannels.builder() .policyId(policy.getId()) .channelIds(policyChannels) .build() ); return orphanedChannels; }
@Test public void shouldThrowException_whenPolicyDoesNotExist() { // given when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.empty()); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); // then - exception expectedException.expect(NewRelicSyncException.class); expectedException.expectMessage(format("Policy %s does not exist", POLICY_NAME)); // when testee.sync(policyConfiguration); }
@Before public void setUp() { testee = new ChannelConfigurator(apiMock); when(alertsChannelsApiMock.create(configuredEmailChannel)).thenReturn(savedEmailChannel); when(alertsChannelsApiMock.create(configuredSlackChannel)).thenReturn(savedSlackChannel); when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.of(POLICY)); User user = User.builder() .id(USER_ID) .email(USER_EMAIL) .build(); when(usersApiMock.getByEmail(USER_EMAIL)).thenReturn(Optional.of(user)); savedUserChannel = createAlertChannel(3, USER_CHANNEL); }
private void cleanupPolicyAlertsChannel(AlertsPolicy policy, Integer policyChannelId) { AlertsChannel removed = api.getAlertsChannelsApi().deleteFromPolicy(policy.getId(), policyChannelId); LOG.info("Alerts channel {} (id: {}) removed from policy {} (id: {})", removed.getName(), removed.getId(), policy.getName(), policy.getId()); if (isUserChannel(removed)) { LOG.info("Skipping user channel removal ({})", removed.getName()); } else { List<Integer> currentChannelPolicyIds = new ArrayList<>(removed.getLinks().getPolicyIds()); currentChannelPolicyIds.remove(policy.getId()); if (currentChannelPolicyIds.isEmpty()) { api.getAlertsChannelsApi().delete(removed.getId()); LOG.info("Alerts channel {} (id: {}) removed", removed.getName(), removed.getId()); } } }
@Test public void shouldDoNothing_whenEmptyChannelsInConfiguration() { // given PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); // when testee.sync(policyConfiguration); // then InOrder order = inOrder(alertsChannelsApiMock); order.verify(alertsChannelsApiMock).list(); order.verifyNoMoreInteractions(); }
/** * NewRelic Alerts configurator constructor * * @param apiKey API Key for given NewRelic account */ public Configurator(@NonNull String apiKey) { NewRelicApi api = new NewRelicApi(apiKey); applicationConfigurator = new ApplicationConfigurator(api); policyConfigurator = new PolicyConfigurator(api); conditionConfigurator = new ConditionConfigurator(api, EntityResolver.defaultInstance()); externalServiceConditionConfigurator = new ExternalServiceConditionConfigurator(api, EntityResolver.defaultInstance()); nrqlConditionConfigurator = new NrqlConditionConfigurator(api); syntheticsConditionConfigurator = new SyntheticsConditionConfigurator(api, EntityResolver.defaultInstance()); channelConfigurator = new ChannelConfigurator(api); }
/** * Synchronizes configurations (see {@link Configurator#setApplicationConfigurations(Collection)} * and {@link Configurator#setPolicyConfigurations(Collection)}) */ public void sync() { for (ApplicationConfiguration applicationConfiguration : applicationConfigurations) { applicationConfigurator.sync(applicationConfiguration); } for (PolicyConfiguration configuration : policyConfigurations) { policyConfigurator.sync(configuration); conditionConfigurator.sync(configuration); externalServiceConditionConfigurator.sync(configuration); nrqlConditionConfigurator.sync(configuration); syntheticsConditionConfigurator.sync(configuration); channelConfigurator.sync(configuration); } }
@Test public void shouldSynchronizeAllConfigurationsForPolicy_whenFullPolicyConfiguration() { // given testee.setPolicyConfigurations(ImmutableList.of(policyConfigurationMock)); // when testee.sync(); // then InOrder order = inOrder(applicationConfiguratorMock, policyConfiguratorMock, conditionConfiguratorMock, externalServiceConditionConfiguratorMock, nrqlConditionConfiguratorMock, syntheticsConditionConfiguratorMock, channelConfiguratorMock); order.verify(policyConfiguratorMock).sync(policyConfigurationMock); order.verify(conditionConfiguratorMock).sync(policyConfigurationMock); order.verify(externalServiceConditionConfiguratorMock).sync(policyConfigurationMock); order.verify(nrqlConditionConfiguratorMock).sync(policyConfigurationMock); order.verify(syntheticsConditionConfiguratorMock).sync(policyConfigurationMock); order.verify(channelConfiguratorMock).sync(policyConfigurationMock); order.verifyNoMoreInteractions(); } }
@Test public void shouldDoNothing_whenNullChannelsInConfiguration() { // given PolicyConfiguration policyConfiguration = PolicyConfiguration.builder() .policyName(POLICY_NAME) .incidentPreference(INCIDENT_PREFERENCE) .build(); // when testee.sync(policyConfiguration); // then verifyNoMoreInteractions(alertsChannelsApiMock); }
@Test public void shouldThrowException_whenUserChannelDosNotExist() { // given PolicyConfiguration policyConfiguration = buildPolicyConfiguration(USER_CHANNEL); // then - exception expectedException.expect(NewRelicSyncException.class); expectedException.expectMessage("Alerts channel with configuration"); // when testee.sync(policyConfiguration); }
@Test public void shouldNotCreateUserChannel() { // given when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(savedUserChannel)); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(USER_CHANNEL); AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY.getId()) .channelIds(ImmutableSet.of(savedUserChannel.getId())) .build(); // when testee.sync(policyConfiguration); // then InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verifyNoMoreInteractions(); }
@Test public void shouldNotRemoveUnusedUserChannel() { // given AlertsChannel userChannelInPolicy = channelInPolicy(savedUserChannel, POLICY_ID); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(userChannelInPolicy)); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, userChannelInPolicy.getId())) .thenReturn(userChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(emptySet()) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedUserChannel.getId()); order.verify(alertsChannelsApiMock, never()).delete(savedUserChannel.getId()); order.verifyNoMoreInteractions(); }
@Test public void shouldCreateRequiredChannels() { // given when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(savedUserChannel)); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(EMAIL_CHANNEL, SLACK_CHANNEL); // when testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedEmailChannel.getId(), savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsChannelsApiMock).create(configuredEmailChannel); order.verify(alertsChannelsApiMock).create(configuredSlackChannel); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verifyNoMoreInteractions(); }
@Test public void shouldRemoveUnusedPolicyChannel() { // given AlertsChannel emailChannelInPolicy = channelInPolicy(savedEmailChannel, POLICY_ID); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of( savedUserChannel, emailChannelInPolicy, channelInPolicy(savedSlackChannel, POLICY_ID) )); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, emailChannelInPolicy.getId())) .thenReturn(emailChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(SLACK_CHANNEL); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedEmailChannel.getId()); order.verify(alertsChannelsApiMock).delete(savedEmailChannel.getId()); order.verifyNoMoreInteractions(); }
@Test public void shouldNotRemoveUnsuedChannel_whenChannelBelongsToDifferentPolicy() { // given AlertsChannel emailChannelInPolicy = channelInPolicy(savedEmailChannel, POLICY_ID, POLICY_ID + 1); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of( savedUserChannel, emailChannelInPolicy, channelInPolicy(savedSlackChannel, POLICY_ID) )); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, emailChannelInPolicy.getId())) .thenReturn(emailChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(SLACK_CHANNEL); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedEmailChannel.getId()); order.verify(alertsChannelsApiMock, never()).delete(savedEmailChannel.getId()); order.verifyNoMoreInteractions(); }
testee.sync(policyConfiguration);