diff --git a/java/cdk-custom-resources/src/main/java/sleeper/cdk/custom/PauseScheduledRuleLambda.java b/java/cdk-custom-resources/src/main/java/sleeper/cdk/custom/PauseScheduledRuleLambda.java deleted file mode 100644 index 4c3214cc079..00000000000 --- a/java/cdk-custom-resources/src/main/java/sleeper/cdk/custom/PauseScheduledRuleLambda.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2022-2025 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package sleeper.cdk.custom; - -import com.amazonaws.services.lambda.runtime.Context; -import com.amazonaws.services.lambda.runtime.events.CloudFormationCustomResourceEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; - -import java.util.Map; - -/** - * Pause a CloudWatch rule. - */ -public class PauseScheduledRuleLambda { - public static final Logger LOGGER = LoggerFactory.getLogger(PauseScheduledRuleLambda.class); - - private final CloudWatchEventsClient cwClient; - - public PauseScheduledRuleLambda() { - this(CloudWatchEventsClient.create()); - } - - public PauseScheduledRuleLambda(CloudWatchEventsClient cwClient) { - this.cwClient = cwClient; - } - - /** - * Handles an event triggered by CloudFormation. - * - * @param event the event to handle - * @param context the context - */ - public void handleEvent( - CloudFormationCustomResourceEvent event, Context context) { - Map resourceProperties = event.getResourceProperties(); - String scheduledRuleName = (String) resourceProperties.get("scheduledRuleName"); - - switch (event.getRequestType()) { - case "Create": - break; - case "Update": - break; - case "Delete": - pauseRule(cwClient, scheduledRuleName); - break; - default: - throw new IllegalArgumentException("Invalid request type: " + event.getRequestType()); - } - } - - private static void pauseRule(CloudWatchEventsClient cwClient, String scheduledRuleName) { - LOGGER.info("Pausing scheduled rule {}", scheduledRuleName); - cwClient.disableRule(request -> request.name(scheduledRuleName)); - LOGGER.info("Paused scheduled rule {}", scheduledRuleName); - } - -} diff --git a/java/cdk-custom-resources/src/test/java/sleeper/cdk/custom/PauseScheduledRuleLambdaIT.java b/java/cdk-custom-resources/src/test/java/sleeper/cdk/custom/PauseScheduledRuleLambdaIT.java deleted file mode 100644 index d959af52d9a..00000000000 --- a/java/cdk-custom-resources/src/test/java/sleeper/cdk/custom/PauseScheduledRuleLambdaIT.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2022-2025 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package sleeper.cdk.custom; - -import com.amazonaws.services.lambda.runtime.events.CloudFormationCustomResourceEvent; -import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; -import com.github.tomakehurst.wiremock.junit5.WireMockTest; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.verify; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static sleeper.cdk.custom.WiremockCweTestHelper.anyRequestedForCloudWatchEvents; -import static sleeper.cdk.custom.WiremockCweTestHelper.disableRuleRequest; -import static sleeper.cdk.custom.WiremockCweTestHelper.disableRuleRequestedFor; -import static sleeper.cdk.custom.WiremockCweTestHelper.wiremockCweClient; - -@WireMockTest -public class PauseScheduledRuleLambdaIT { - - private PauseScheduledRuleLambda lambda; - - @BeforeEach - void setUp(WireMockRuntimeInfo runtimeInfo) { - lambda = lambda(runtimeInfo); - } - - @DisplayName("Stop scheduled cloud watch rule") - @Test - void shouldShutdownCloudWatchRuleWhenSet() throws Exception { - - // Given - String scheduledRuleName = "test-compaction-job-creation-rule"; - - stubFor(disableRuleRequest() - .willReturn(aResponse().withStatus(200))); - - // When - lambda.handleEvent(eventHandlerForCloudWatch(scheduledRuleName, "Delete"), null); - - // Then - verify(1, anyRequestedForCloudWatchEvents()); - verify(1, disableRuleRequestedFor(scheduledRuleName)); - - } - - @Test - @DisplayName("Test unsupported operation") - void shouldRaiseExceptionOnUnsupportedOperation() { - - // Given - String scheduledRuleName = "test-compaction-job-creation-rule"; - - // When / Then - verify(0, anyRequestedForCloudWatchEvents()); - assertThatThrownBy( - () -> lambda.handleEvent( - eventHandlerForCloudWatch(scheduledRuleName, "TagResource"), null)) - .isInstanceOf(IllegalArgumentException.class); - } - - private CloudFormationCustomResourceEvent eventHandlerForCloudWatch( - String scheduledRuleName, String event) { - return CloudFormationCustomResourceEvent.builder() - .withRequestType(event) - .withResourceProperties(Map.of("scheduledRuleName", scheduledRuleName)) - .build(); - } - - private PauseScheduledRuleLambda lambda(WireMockRuntimeInfo runtimeInfo) { - return new PauseScheduledRuleLambda(wiremockCweClient(runtimeInfo)); - } - -}