-
Notifications
You must be signed in to change notification settings - Fork 63
[b406163439] Verify if specified Cloudera cluster exists before processing tasks #1050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dominik-burdzy
merged 1 commit into
main
from
fix/b406163439/verify-cloudera-cluster-existence
Jan 5, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...ation/dumper/application/dumper/connector/cloudera/manager/ClouderaConnectorVerifier.java
This file contains hidden or 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,84 @@ | ||
| /* | ||
| * Copyright 2022-2025 Google LLC | ||
| * Copyright 2013-2021 CompilerWorks | ||
| * | ||
| * 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.google.edwmigration.dumper.application.dumper.connector.cloudera.manager; | ||
|
|
||
| import com.google.edwmigration.dumper.application.dumper.ConnectorArguments; | ||
| import java.io.IOException; | ||
| import javax.annotation.Nonnull; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.util.EntityUtils; | ||
|
|
||
| /** Utility class for verifying the preconditions and configuration of a Cloudera Connector. */ | ||
| public final class ClouderaConnectorVerifier { | ||
|
|
||
| private ClouderaConnectorVerifier() {} | ||
|
|
||
| /** | ||
| * Verifies the validity of the connector configuration and environment. | ||
| * | ||
| * <p>This involves checking connectivity to the API, verifying the existence of the specified | ||
| * cluster (if provided), and ensuring other preconditions are met. | ||
| * | ||
| * @param handle The handle to the Cloudera Manager API. | ||
| * @param arguments The connector arguments containing target cluster details. | ||
| * @throws ClouderaConnectorException If verification fails due to missing resources, API errors, | ||
| * or connectivity issues. | ||
| */ | ||
| public static void verify( | ||
| @Nonnull ClouderaManagerHandle handle, @Nonnull ConnectorArguments arguments) | ||
| throws ClouderaConnectorException { | ||
| verifyClusterExists(handle, arguments.getCluster()); | ||
| } | ||
|
|
||
| private static void verifyClusterExists(ClouderaManagerHandle handle, String clusterName) | ||
| throws ClouderaConnectorException { | ||
| if (clusterName == null) { | ||
| return; | ||
| } | ||
| String endpoint = String.format("%s/clusters/%s", handle.getApiURI(), clusterName); | ||
| HttpGet httpGet = new HttpGet(endpoint); | ||
|
|
||
| try (CloseableHttpResponse response = handle.getHttpClient().execute(httpGet)) { | ||
|
|
||
| int statusCode = response.getStatusLine().getStatusCode(); | ||
|
|
||
| if (statusCode == 404) { | ||
| throw new ClouderaConnectorException( | ||
| String.format("Specified cluster '%s' not found.", clusterName)); | ||
| } | ||
|
|
||
| if (!isHttpStatusSuccess(statusCode)) { | ||
| String errorMsg = EntityUtils.toString(response.getEntity()); | ||
| throw new ClouderaConnectorException( | ||
| String.format( | ||
| "Unexpected API error checking cluster '%s'. Code: %d. Message: %s", | ||
| clusterName, statusCode, errorMsg)); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new ClouderaConnectorException( | ||
| String.format( | ||
| "Failed to communicate with Cloudera Manager API while checking cluster '%s'.", | ||
| clusterName), | ||
| e); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isHttpStatusSuccess(int statusCode) { | ||
| return statusCode >= 200 && statusCode < 300; | ||
| } | ||
| } |
This file contains hidden or 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
132 changes: 132 additions & 0 deletions
132
...n/dumper/application/dumper/connector/cloudera/manager/ClouderaConnectorVerifierTest.java
This file contains hidden or 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,132 @@ | ||
| /* | ||
| * Copyright 2022-2025 Google LLC | ||
| * Copyright 2013-2021 CompilerWorks | ||
| * | ||
| * 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.google.edwmigration.dumper.application.dumper.connector.cloudera.manager; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.containsString; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.edwmigration.dumper.application.dumper.ConnectorArguments; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import org.apache.http.StatusLine; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class ClouderaConnectorVerifierTest { | ||
|
|
||
| private final CloseableHttpClient httpClient = mock(CloseableHttpClient.class); | ||
| private final ClouderaManagerHandle handle = mock(ClouderaManagerHandle.class); | ||
| private final ConnectorArguments arguments = mock(ConnectorArguments.class); | ||
|
|
||
| @Before | ||
| public void setUp() throws URISyntaxException { | ||
| when(handle.getHttpClient()).thenReturn(httpClient); | ||
| when(handle.getApiURI()).thenReturn(new URI("http://localhost:7183/api/v57")); | ||
| } | ||
|
|
||
| @Test | ||
| public void verify_clusterIsNull_doesNothing() { | ||
| when(arguments.getCluster()).thenReturn(null); | ||
|
|
||
| ClouderaConnectorVerifier.verify(handle, arguments); | ||
|
|
||
| // No exception thrown expected | ||
| } | ||
|
|
||
| @Test | ||
| public void verify_clusterExists_doesNothing() throws Exception { | ||
| when(arguments.getCluster()).thenReturn("cluster-1"); | ||
| CloseableHttpResponse response = mock(CloseableHttpResponse.class); | ||
| StatusLine statusLine = mock(StatusLine.class); | ||
| when(statusLine.getStatusCode()).thenReturn(200); | ||
| when(response.getStatusLine()).thenReturn(statusLine); | ||
| when(httpClient.execute(any())).thenReturn(response); | ||
|
|
||
| ClouderaConnectorVerifier.verify(handle, arguments); | ||
|
|
||
| // No exception thrown expected | ||
| } | ||
|
|
||
| @Test | ||
| public void verify_clusterNotFound_throwsException() throws Exception { | ||
| String clusterName = "cluster-1"; | ||
| when(arguments.getCluster()).thenReturn(clusterName); | ||
| CloseableHttpResponse response = mock(CloseableHttpResponse.class); | ||
| StatusLine statusLine = mock(StatusLine.class); | ||
| when(statusLine.getStatusCode()).thenReturn(404); | ||
| when(response.getStatusLine()).thenReturn(statusLine); | ||
| when(httpClient.execute(any())).thenReturn(response); | ||
|
|
||
| ClouderaConnectorException e = | ||
| assertThrows( | ||
| ClouderaConnectorException.class, | ||
| () -> ClouderaConnectorVerifier.verify(handle, arguments)); | ||
|
|
||
| assertThat(e.getMessage(), containsString("Specified cluster 'cluster-1' not found.")); | ||
| } | ||
|
|
||
| @Test | ||
| public void verify_unexpectedApiError_throwsException() throws Exception { | ||
| // Arrange | ||
| String clusterName = "cluster-1"; | ||
| when(arguments.getCluster()).thenReturn(clusterName); | ||
| CloseableHttpResponse response = mock(CloseableHttpResponse.class); | ||
| StatusLine statusLine = mock(StatusLine.class); | ||
| when(statusLine.getStatusCode()).thenReturn(500); | ||
| when(response.getStatusLine()).thenReturn(statusLine); | ||
| when(response.getEntity()).thenReturn(new StringEntity("Internal Server Error")); | ||
| when(httpClient.execute(any())).thenReturn(response); | ||
|
|
||
| ClouderaConnectorException e = | ||
| assertThrows( | ||
| ClouderaConnectorException.class, | ||
| () -> ClouderaConnectorVerifier.verify(handle, arguments)); | ||
|
|
||
| assertThat( | ||
| e.getMessage(), | ||
| containsString( | ||
| "Unexpected API error checking cluster 'cluster-1'. Code: 500. Message: Internal Server Error")); | ||
| } | ||
|
|
||
| @Test | ||
| public void verify_ioException_throwsException() throws Exception { | ||
| String clusterName = "cluster-1"; | ||
| when(arguments.getCluster()).thenReturn(clusterName); | ||
| when(httpClient.execute(any())).thenThrow(new IOException("some error")); | ||
|
|
||
| ClouderaConnectorException e = | ||
| assertThrows( | ||
| ClouderaConnectorException.class, | ||
| () -> ClouderaConnectorVerifier.verify(handle, arguments)); | ||
|
|
||
| assertThat( | ||
| e.getMessage(), | ||
| containsString( | ||
| "Failed to communicate with Cloudera Manager API while checking cluster 'cluster-1'.")); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.