Search in sources :

Example 71 with JsonObject

use of com.hazelcast.internal.json.JsonObject in project sonarqube by SonarSource.

the class InstalledActionTest method empty_fields_are_not_serialized_to_json.

@Test
public void empty_fields_are_not_serialized_to_json() throws IOException {
    when(serverPluginRepository.getPlugins()).thenReturn(singletonList(newInstalledPlugin(new PluginInfo("foo").setName(null).setDescription(null).setLicense(null).setOrganizationName(null).setOrganizationUrl(null).setImplementationBuild(null).setHomepageUrl(null).setIssueTrackerUrl(null))));
    db.pluginDbTester().insertPlugin(p -> p.setKee("foo"), p -> p.setType(Type.EXTERNAL), p -> p.setUpdatedAt(100L));
    String response = tester.newRequest().execute().getInput();
    JsonObject json = Json.parse(response).asObject().get("plugins").asArray().get(0).asObject();
    assertThat(json.get("key")).isNotNull();
    assertThat(json.get("name")).isNotNull();
    assertThat(json.get("description")).isNull();
    assertThat(json.get("license")).isNull();
    assertThat(json.get("organizationName")).isNull();
    assertThat(json.get("organizationUrl")).isNull();
    assertThat(json.get("homepageUrl")).isNull();
    assertThat(json.get("issueTrackerUrl")).isNull();
}
Also used : JsonObject(com.hazelcast.internal.json.JsonObject) PluginInfo(org.sonar.core.platform.PluginInfo) Test(org.junit.Test)

Example 72 with JsonObject

use of com.hazelcast.internal.json.JsonObject in project hazelcast by hazelcast.

the class AwsEcsApi method createBodyListTasks.

private String createBodyListTasks(String cluster) {
    JsonObject body = new JsonObject();
    body.add("cluster", cluster);
    if (!StringUtil.isNullOrEmptyAfterTrim(awsConfig.getFamily())) {
        body.add("family", awsConfig.getFamily());
    }
    if (!StringUtil.isNullOrEmptyAfterTrim(awsConfig.getServiceName())) {
        body.add("serviceName", awsConfig.getServiceName());
    }
    return body.toString();
}
Also used : JsonObject(com.hazelcast.internal.json.JsonObject)

Example 73 with JsonObject

use of com.hazelcast.internal.json.JsonObject in project hazelcast by hazelcast.

the class AwsMetadataApi method parseEcsMetadata.

private EcsMetadata parseEcsMetadata(String response) {
    JsonObject metadata = Json.parse(response).asObject();
    JsonObject labels = metadata.get("Labels").asObject();
    String taskArn = labels.get("com.amazonaws.ecs.task-arn").asString();
    String clusterArn = labels.get("com.amazonaws.ecs.cluster").asString();
    return new EcsMetadata(taskArn, clusterArn);
}
Also used : JsonObject(com.hazelcast.internal.json.JsonObject)

Example 74 with JsonObject

use of com.hazelcast.internal.json.JsonObject in project hazelcast by hazelcast.

the class KubernetesApiEndpointProvider method extractNodes.

public Map<EndpointAddress, String> extractNodes(JsonObject endpointsListJson, List<EndpointAddress> privateAddresses) {
    Map<EndpointAddress, String> result = new HashMap<>();
    Set<EndpointAddress> left = new HashSet<>(privateAddresses);
    for (JsonValue item : toJsonArray(endpointsListJson.get("items"))) {
        for (JsonValue subset : toJsonArray(item.asObject().get("subsets"))) {
            JsonObject subsetObject = subset.asObject();
            List<Integer> ports = new ArrayList<>();
            for (JsonValue port : toJsonArray(subsetObject.get("ports"))) {
                ports.add(port.asObject().get("port").asInt());
            }
            Map<EndpointAddress, String> nodes = new HashMap<>();
            nodes.putAll(extractNodes(subsetObject.get("addresses"), ports));
            nodes.putAll(extractNodes(subsetObject.get("notReadyAddresses"), ports));
            for (Map.Entry<EndpointAddress, String> nodeEntry : nodes.entrySet()) {
                EndpointAddress address = nodeEntry.getKey();
                if (privateAddresses.contains(address)) {
                    result.put(address, nodes.get(address));
                    left.remove(address);
                }
            }
        }
    }
    if (!left.isEmpty()) {
        // At least one Hazelcast Member POD does not have 'nodeName' assigned.
        throw noNodeNameAssignedException(left);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) JsonValue(com.hazelcast.internal.json.JsonValue) ArrayList(java.util.ArrayList) JsonObject(com.hazelcast.internal.json.JsonObject) KubernetesApiProvider.convertToString(com.hazelcast.kubernetes.KubernetesApiProvider.convertToString) HashMap(java.util.HashMap) Map(java.util.Map) EndpointAddress(com.hazelcast.kubernetes.KubernetesClient.EndpointAddress) HashSet(java.util.HashSet)

Example 75 with JsonObject

use of com.hazelcast.internal.json.JsonObject in project hazelcast by hazelcast.

the class KubernetesClient method enrichWithPublicAddresses.

/**
 * Tries to add public addresses to the endpoints.
 * <p>
 * If it's not possible, then returns the input parameter.
 * <p>
 * Assigning public IPs must meet one of the following requirements:
 * <ul>
 * <li>Each POD must be exposed with a separate LoadBalancer service OR</li>
 * <li>Each POD must be exposed with a separate NodePort service and Kubernetes nodes must have external IPs</li>
 * </ul>
 * <p>
 * The algorithm to fetch public IPs is as follows:
 * <ol>
 * <li>Use Kubernetes API (/endpoints) to find dedicated services for each POD</li>
 * <li>For each POD:
 * <ol>
 * <li>Use Kubernetes API (/services) to find the LoadBalancer External IP and Service Port</li>
 * <li>If not found, then use Kubernetes API (/nodes) to find External IP of the Node</li>
 * </ol>
 * </li>
 * </ol>
 */
private List<Endpoint> enrichWithPublicAddresses(List<Endpoint> endpoints) {
    if (exposeExternallyMode == ExposeExternallyMode.DISABLED) {
        return endpoints;
    }
    try {
        String endpointsUrl = String.format(apiProvider.getEndpointsUrlString(), kubernetesMaster, namespace);
        if (!StringUtil.isNullOrEmptyAfterTrim(servicePerPodLabelName) && !StringUtil.isNullOrEmptyAfterTrim(servicePerPodLabelValue)) {
            endpointsUrl += String.format("?labelSelector=%s=%s", servicePerPodLabelName, servicePerPodLabelValue);
        }
        JsonObject endpointsJson = callGet(endpointsUrl);
        List<EndpointAddress> privateAddresses = privateAddresses(endpoints);
        Map<EndpointAddress, String> services = apiProvider.extractServices(endpointsJson, privateAddresses);
        Map<EndpointAddress, String> nodes = apiProvider.extractNodes(endpointsJson, privateAddresses);
        Map<EndpointAddress, String> publicIps = new HashMap<>();
        Map<EndpointAddress, Integer> publicPorts = new HashMap<>();
        Map<String, String> cachedNodePublicIps = new HashMap<>();
        for (Map.Entry<EndpointAddress, String> serviceEntry : services.entrySet()) {
            EndpointAddress privateAddress = serviceEntry.getKey();
            String service = serviceEntry.getValue();
            String serviceUrl = String.format("%s/api/v1/namespaces/%s/services/%s", kubernetesMaster, namespace, service);
            JsonObject serviceJson = callGet(serviceUrl);
            try {
                String loadBalancerAddress = extractLoadBalancerAddress(serviceJson);
                Integer servicePort = extractServicePort(serviceJson);
                publicIps.put(privateAddress, loadBalancerAddress);
                publicPorts.put(privateAddress, servicePort);
            } catch (Exception e) {
                // Load Balancer public IP cannot be found, try using NodePort.
                Integer nodePort = extractNodePort(serviceJson);
                String node = nodes.get(privateAddress);
                String nodePublicAddress;
                if (cachedNodePublicIps.containsKey(node)) {
                    nodePublicAddress = cachedNodePublicIps.get(node);
                } else {
                    nodePublicAddress = externalAddressForNode(node);
                    cachedNodePublicIps.put(node, nodePublicAddress);
                }
                publicIps.put(privateAddress, nodePublicAddress);
                publicPorts.put(privateAddress, nodePort);
            }
        }
        return createEndpoints(endpoints, publicIps, publicPorts);
    } catch (Exception e) {
        if (exposeExternallyMode == ExposeExternallyMode.ENABLED) {
            throw e;
        }
        // If expose-externally not set (exposeExternallyMode == ExposeExternallyMode.AUTO), silently ignore any exception
        LOGGER.finest(e);
        // Log warning only once.
        if (!isNoPublicIpAlreadyLogged) {
            LOGGER.warning("Cannot fetch public IPs of Hazelcast Member PODs, you won't be able to use Hazelcast Smart Client from " + "outside of the Kubernetes network");
            isNoPublicIpAlreadyLogged = true;
        }
        return endpoints;
    }
}
Also used : HashMap(java.util.HashMap) JsonObject(com.hazelcast.internal.json.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map) RestClientException(com.hazelcast.spi.exception.RestClientException)

Aggregations

JsonObject (com.hazelcast.internal.json.JsonObject)151 Test (org.junit.Test)56 JsonArray (com.hazelcast.internal.json.JsonArray)41 QuickTest (com.hazelcast.test.annotation.QuickTest)38 JsonValue (com.hazelcast.internal.json.JsonValue)34 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)26 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)23 HazelcastInstance (com.hazelcast.core.HazelcastInstance)11 JsonUtil.getString (com.hazelcast.internal.util.JsonUtil.getString)10 SlowTest (com.hazelcast.test.annotation.SlowTest)9 HashMap (java.util.HashMap)8 CPMember (com.hazelcast.cp.CPMember)7 ArrayList (java.util.ArrayList)7 ConnectionResponse (com.hazelcast.internal.ascii.HTTPCommunicator.ConnectionResponse)6 NavigableJsonInputAdapter (com.hazelcast.internal.serialization.impl.NavigableJsonInputAdapter)6 JsonUtil.fromJsonObject (com.hazelcast.internal.util.JsonUtil.fromJsonObject)5 JsonUtil.toJsonObject (com.hazelcast.internal.util.JsonUtil.toJsonObject)5 Map (java.util.Map)5 ClusterService (com.hazelcast.internal.cluster.ClusterService)4 Address (com.hazelcast.cluster.Address)3