-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gradle Helm release exists task (#389)
Signed-off-by: Jeromy Cannon <jeromy@swirldslabs.com>
- Loading branch information
1 parent
53d092f
commit 0b7ae17
Showing
5 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...gradle-plugin/src/main/java/com/hedera/fullstack/gradle/plugin/HelmReleaseExistsTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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 com.hedera.fullstack.helm.client.model.release.ReleaseItem; | ||
import java.util.List; | ||
import java.util.Objects; | ||
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 HelmReleaseExistsTask extends DefaultTask { | ||
@Input | ||
@Option(option = "release", description = "The release to verify exists") | ||
public abstract Property<String> getRelease(); | ||
|
||
@Input | ||
@Optional | ||
@Option(option = "namespace", description = "The namespace to use when listing the releases") | ||
public abstract Property<String> getNamespace(); | ||
|
||
@Input | ||
@Optional | ||
@Option(option = "allNamespaces", description = "True if we should list releases in all namespaces") | ||
public abstract Property<Boolean> getAllNamespaces(); | ||
|
||
@TaskAction | ||
void releaseExists() { | ||
ReleaseItem releaseItem; | ||
try { | ||
final String release = getRelease().getOrNull(); | ||
Objects.requireNonNull(release, "release must not be null"); | ||
|
||
HelmClientBuilder helmClientBuilder = HelmClient.builder(); | ||
if (getNamespace().isPresent()) { | ||
helmClientBuilder.defaultNamespace(getNamespace().get()); | ||
} | ||
|
||
HelmClient helmClient = helmClientBuilder.build(); | ||
List<ReleaseItem> releaseItems = | ||
helmClient.listReleases(getAllNamespaces().getOrElse(false)); | ||
releaseItem = releaseItems.stream() | ||
.filter(item -> item.name().equals(release)) | ||
.findFirst() | ||
.orElse(null); | ||
} catch (Exception e) { | ||
this.getProject() | ||
.getLogger() | ||
.error( | ||
"HelmReleaseExistsTask.releaseExists() An ERROR occurred while listing the releases: " | ||
+ e.getMessage(), | ||
e); | ||
throw e; | ||
} | ||
|
||
if (releaseItem == null) { | ||
final String errorMessage = "HelmReleaseExistsTask.releaseExists(): Release " | ||
+ getRelease().get() + " does not exist"; | ||
this.getProject().getLogger().error(errorMessage); | ||
throw new RuntimeException(errorMessage); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...le-plugin/src/test/java/com/hedera/fullstack/gradle/plugin/HelmReleaseExistsTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
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; | ||
|
||
public class HelmReleaseExistsTaskTest { | ||
private static Project project; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
project = ProjectBuilder.builder().build(); | ||
} | ||
|
||
@Test | ||
@DisplayName("test an error is thrown when the release is not found") | ||
void testErrorThrownWhenReleaseNotFound() { | ||
RuntimeException e = assertThrows(RuntimeException.class, () -> { | ||
HelmReleaseExistsTask helmReleaseExistsTask = project.getTasks() | ||
.create("helmReleaseExistsV1", HelmReleaseExistsTask.class, task -> { | ||
task.getAllNamespaces().set(true); | ||
task.getRelease().set("not-a-release"); | ||
}); | ||
helmReleaseExistsTask.releaseExists(); | ||
}); | ||
assertThat(e.getMessage()) | ||
.isEqualTo("HelmReleaseExistsTask.releaseExists(): Release not-a-release does not exist"); | ||
} | ||
|
||
@Test | ||
@DisplayName("test an error is thrown when the release is not set") | ||
void testErrorThrownWhenReleaseNotSet() { | ||
NullPointerException npe = assertThrows(NullPointerException.class, () -> { | ||
HelmReleaseExistsTask helmReleaseExistsTask = project.getTasks() | ||
.create("helmReleaseExistsV2", HelmReleaseExistsTask.class, task -> { | ||
task.getAllNamespaces().set(true); | ||
}); | ||
helmReleaseExistsTask.releaseExists(); | ||
}); | ||
assertThat(npe.getMessage()).isEqualTo("release must not be null"); | ||
} | ||
} |