Search in sources :

Example 11 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConsulPropertySourceLocatorTests method setup.

@Before
public void setup() {
    this.properties = new ConsulProperties();
    this.client = new ConsulClient(properties.getHost(), properties.getPort());
    this.client.deleteKVValues(PREFIX);
    this.client.setKVValue(KEY1, VALUE1);
    this.client.setKVValue(KEY2, VALUE2);
    this.context = new SpringApplicationBuilder(Config.class).web(false).run("--SPRING_APPLICATION_NAME=" + APP_NAME, "--spring.cloud.consul.config.prefix=" + ROOT, "spring.cloud.consul.config.watch.delay=10");
    this.client = context.getBean(ConsulClient.class);
    this.properties = context.getBean(ConsulProperties.class);
    this.environment = context.getEnvironment();
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Before(org.junit.Before)

Example 12 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project spring-cloud-consul by spring-cloud.

the class ConsulPropertySourceTests method setup.

@Before
public void setup() {
    properties = new ConsulProperties();
    prefix = "consulPropertySourceTests" + new Random().nextInt(Integer.MAX_VALUE);
    client = new ConsulClient(properties.getHost(), properties.getPort());
}
Also used : Random(java.util.Random) ConsulClient(com.ecwid.consul.v1.ConsulClient) ConsulProperties(org.springframework.cloud.consul.ConsulProperties) Before(org.junit.Before)

Example 13 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project cloudbreak by hortonworks.

the class ConsulHostServiceTypeCheckerTaskTest method checkStatusForOneNodeResponse.

@Test
@SuppressWarnings("unchecked")
public void checkStatusForOneNodeResponse() {
    ConsulRawClient raw1 = mock(ConsulRawClient.class);
    RawResponse rawResponse = new RawResponse(200, null, SERVICE_RESPONSE, null, null, null);
    ConsulClient client1 = new ConsulClient(raw1);
    when(raw1.makeGetRequest(SERVICE_ENDPOINT + AMBARI_SERVICE, null, QueryParams.DEFAULT)).thenReturn(rawResponse);
    boolean result = task.checkStatus(new ConsulContext(stack, client1, Collections.singletonList(AMBARI_SERVICE)));
    assertTrue(result);
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) ConsulRawClient(com.ecwid.consul.v1.ConsulRawClient) RawResponse(com.ecwid.consul.transport.RawResponse) Test(org.junit.Test)

Example 14 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project cloudbreak by hortonworks.

the class ConsulKeyValueService method addAlert.

public PrometheusAlert addAlert(Cluster cluster, PrometheusAlert alert) {
    Ambari ambari = cluster.getAmbari();
    try {
        if (RUNNING.equals(cluster.getState())) {
            TlsConfiguration tlsConfig = tlsSecurityService.getConfiguration(cluster);
            ConsulClient consulClient = ConsulUtils.createClient(ambari.getHost(), cluster.getPort(), tlsConfig);
            String alertKey = getKeyNameForAlert(alert);
            consulClient.setKVValue(alertKey, alert.getAlertRule());
            LOGGER.info("Alert has been added to Consul KV store with name: '{}' on host: '{}'.", alertKey, ambari.getHost());
        }
    } catch (Exception e) {
        LOGGER.warn("Alert could not be added to Consul KV store:", e);
    }
    return alert;
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) TlsConfiguration(com.sequenceiq.periscope.model.TlsConfiguration) Ambari(com.sequenceiq.periscope.domain.Ambari)

Example 15 with ConsulClient

use of com.ecwid.consul.v1.ConsulClient in project cloudbreak by hortonworks.

the class ConsulKVCheckerTask method checkStatus.

@Override
public boolean checkStatus(ConsulKVCheckerContext context) {
    List<String> keys = context.getKeys();
    String expectedValue = context.getExpectedValue();
    String failValue = context.getFailValue();
    ConsulClient client = context.getConsulClient();
    LOGGER.info("Checking if keys in Consul's key-value store have the expected value '{}'", expectedValue);
    Set<String> failedKeys = new HashSet<>();
    int matchingKeys = 0;
    int notFoundKeys = 0;
    for (String key : keys) {
        String value = ConsulUtils.getKVValue(Collections.singletonList(client), key, null);
        if (value != null) {
            if (value.equals(failValue)) {
                failedKeys.add(key);
            } else if (value.equals(expectedValue)) {
                matchingKeys++;
            }
        } else {
            notFoundKeys++;
        }
    }
    LOGGER.info("Keys: [Total: {}, {}: {}, Not {}: {}, Not found: {}, {}: {}]", keys.size(), expectedValue, matchingKeys, expectedValue, keys.size() - matchingKeys - notFoundKeys - failedKeys.size(), notFoundKeys, failValue, failedKeys.size());
    if (!failedKeys.isEmpty()) {
        throw new PluginFailureException(String.format("Found failure signal at keys: %s", failedKeys));
    }
    return matchingKeys == keys.size();
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) PluginFailureException(com.sequenceiq.cloudbreak.service.cluster.PluginFailureException) HashSet(java.util.HashSet)

Aggregations

ConsulClient (com.ecwid.consul.v1.ConsulClient)17 Before (org.junit.Before)5 ConsulProperties (org.springframework.cloud.consul.ConsulProperties)5 Test (org.junit.Test)4 ConsulRawClient (com.ecwid.consul.v1.ConsulRawClient)3 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)3 QueryParams (com.ecwid.consul.v1.QueryParams)2 Response (com.ecwid.consul.v1.Response)2 GetValue (com.ecwid.consul.v1.kv.model.GetValue)2 Ambari (com.sequenceiq.periscope.domain.Ambari)2 TlsConfiguration (com.sequenceiq.periscope.model.TlsConfiguration)2 List (java.util.List)2 Matchers.anyString (org.mockito.Matchers.anyString)2 RawResponse (com.ecwid.consul.transport.RawResponse)1 NewService (com.ecwid.consul.v1.agent.model.NewService)1 CatalogService (com.ecwid.consul.v1.catalog.model.CatalogService)1 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 PluginFailureException (com.sequenceiq.cloudbreak.service.cluster.PluginFailureException)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 Field (java.lang.reflect.Field)1