use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.
the class GcpClientTest method setZonesUnknownException.
@Test(expected = Exception.class)
public void setZonesUnknownException() {
// given
GcpConfig gcpConfig = GcpConfig.builder().build();
RestClientException exception = new RestClientException("unknown", 500);
given(gcpComputeApi.zones(any(), any(), any())).willThrow(exception);
// when
new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
// then
// throws exception
}
use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.
the class GcpClientTest method getAddressesUnauthorized.
@Test
public void getAddressesUnauthorized() {
// given
Label label = null;
String forbiddenMessage = "\"reason\":\"Request had insufficient authentication scopes\"";
RestClientException exception = new RestClientException(forbiddenMessage, 401);
given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
// when
List<GcpAddress> result = gcpClient.getAddresses();
// then
assertEquals(emptyList(), result);
}
use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.
the class GcpClientTest method getAddressesForbidden.
@Test
public void getAddressesForbidden() {
// given
Label label = null;
String forbiddenMessage = "\"reason\":\"Request had insufficient authentication scopes\"";
RestClientException exception = new RestClientException(forbiddenMessage, 403);
given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
// when
List<GcpAddress> result = gcpClient.getAddresses();
// then
assertEquals(emptyList(), result);
}
use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.
the class AwsDiscoveryStrategy method discoverNodes.
@Override
public Iterable<DiscoveryNode> discoverNodes() {
try {
Map<String, String> addresses = awsClient.getAddresses();
logResult(addresses);
List<DiscoveryNode> result = new ArrayList<>();
for (Map.Entry<String, String> entry : addresses.entrySet()) {
for (int port = portRange.getFromPort(); port <= portRange.getToPort(); port++) {
Address privateAddress = new Address(entry.getKey(), port);
Address publicAddress = new Address(entry.getValue(), port);
result.add(new SimpleDiscoveryNode(privateAddress, publicAddress));
}
}
return result;
} catch (NoCredentialsException e) {
if (!isKnownExceptionAlreadyLogged) {
LOGGER.warning("No AWS credentials found! Starting standalone. To use Hazelcast AWS discovery, configure" + " properties (access-key, secret-key) or assign the required IAM Role to your EC2 instance");
LOGGER.finest(e);
isKnownExceptionAlreadyLogged = true;
}
} catch (RestClientException e) {
if (e.getHttpErrorCode() == HTTP_FORBIDDEN) {
if (!isKnownExceptionAlreadyLogged) {
LOGGER.warning("AWS IAM Role Policy missing 'ec2:DescribeInstances' Action! Starting standalone.");
isKnownExceptionAlreadyLogged = true;
}
LOGGER.finest(e);
} else {
LOGGER.warning("Cannot discover nodes. Starting standalone.", e);
}
} catch (Exception e) {
LOGGER.warning("Cannot discover nodes. Starting standalone.", e);
}
return Collections.emptyList();
}
use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.
the class RestClient method call.
private Response call(String method) {
HttpURLConnection connection = null;
try {
URL urlToConnect = new URL(url);
connection = (HttpURLConnection) urlToConnect.openConnection();
if (connection instanceof HttpsURLConnection && caCertificate != null) {
((HttpsURLConnection) connection).setSSLSocketFactory(buildSslSocketFactory());
}
connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(readTimeoutSeconds));
connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(connectTimeoutSeconds));
connection.setRequestMethod(method);
for (Parameter header : headers) {
connection.setRequestProperty(header.getKey(), header.getValue());
}
if (body != null) {
byte[] bodyData = body.getBytes(StandardCharsets.UTF_8);
connection.setDoOutput(true);
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(bodyData.length));
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
outputStream.write(bodyData);
outputStream.flush();
}
}
checkResponseCode(method, connection);
return new Response(connection.getResponseCode(), read(connection));
} catch (IOException e) {
throw new RestClientException("Failure in executing REST call", e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
Aggregations