diff --git a/fullstack-examples/build.gradle.kts b/fullstack-examples/build.gradle.kts index 4eabfe398..54b447777 100644 --- a/fullstack-examples/build.gradle.kts +++ b/fullstack-examples/build.gradle.kts @@ -15,6 +15,7 @@ */ import com.hedera.fullstack.gradle.plugin.HelmInstallChartTask +import com.hedera.fullstack.gradle.plugin.HelmUninstallChartTask plugins { id("java") @@ -46,9 +47,12 @@ tasks.register("helmInstallNginxChart") { chart.set("oci://ghcr.io/nginxinc/charts/nginx-ingress") } -// TODO: task register helmUninstallNginxChart +tasks.register("helmUninstallNginxChart") { + namespace.set("nginx-ns") + release.set("nginx-release") +} tasks.check { dependsOn("helmInstallNginxChart") - // TODO: depends on helmUninstallNginxChart + dependsOn("helmUninstallNginxChart") } diff --git a/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java new file mode 100644 index 000000000..860d0e8a9 --- /dev/null +++ b/fullstack-gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTask.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2023 Hedera Hashgraph, LLC + * + * 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 com.hedera.fullstack.gradle.plugin; + +import com.hedera.fullstack.helm.client.HelmClient; +import com.hedera.fullstack.helm.client.HelmClientBuilder; +import org.gradle.api.DefaultTask; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.options.Option; + +public abstract class HelmUninstallChartTask extends DefaultTask { + @Input + @Optional + @Option(option = "namespace", description = "The namespace to use when installing the chart") + public abstract Property getNamespace(); + + @Input + @Option(option = "release", description = "The name of the release to install") + public abstract Property getRelease(); + + @TaskAction + void uninstallChart() { + HelmClientBuilder helmClientBuilder = HelmClient.builder(); + if (getNamespace().isPresent()) { + helmClientBuilder.defaultNamespace(getNamespace().get()); + } + HelmClient helmClient = helmClientBuilder.build(); + try { + helmClient.uninstallChart(getRelease().getOrNull()); + } catch (Exception e) { + this.getProject() + .getLogger() + .error( + "HelmUninstallChartTask.uninstallChart() An ERROR occurred while uninstalling the chart: ", + e); + throw e; + } + } +} diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java index e15ca2ee8..101eb4a15 100644 --- a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmInstallChartTaskTest.java @@ -98,8 +98,13 @@ void testHelmInstallChartTaskSimple() { }); assertEquals(RELEASE_NAME, helmInstallChartTask.getRelease().get()); helmInstallChartTask.installChart(); + HelmUninstallChartTask helmUninstallChartTask = project.getTasks() + .create("helmUninstallChart", HelmUninstallChartTask.class, task -> { + task.getNamespace().set("simple-test"); + task.getRelease().set(RELEASE_NAME); + }); + helmUninstallChartTask.uninstallChart(); } finally { - suppressExceptions(() -> helmClient.uninstallChart(RELEASE_NAME)); suppressExceptions(() -> helmClient.removeRepository(REPOSITORY)); } } diff --git a/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java new file mode 100644 index 000000000..4fe613124 --- /dev/null +++ b/fullstack-gradle-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmUninstallChartTaskTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2023 Hedera Hashgraph, LLC + * + * 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 com.hedera.fullstack.gradle.plugin; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.hedera.fullstack.helm.client.HelmExecutionException; +import org.gradle.api.Project; +import org.gradle.testfixtures.ProjectBuilder; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class HelmUninstallChartTaskTest { + private static Project project; + + @BeforeAll + static void beforeAll() { + project = ProjectBuilder.builder().build(); + } + + @Test + @DisplayName("test an error is thrown when the chart is not found") + void testErrorThrownWhenChartNotFound() { + assertThrows(HelmExecutionException.class, () -> { + HelmUninstallChartTask helmUninstallChartTask = project.getTasks() + .create("helmUninstallNonExistingChartChart", HelmUninstallChartTask.class, task -> { + task.getNamespace().set("test-failure"); + task.getRelease().set("not-a-release"); + }); + helmUninstallChartTask.uninstallChart(); + }); + } +}