-
Notifications
You must be signed in to change notification settings - Fork 68
/
OpenSearchSpace.java
122 lines (98 loc) · 4.21 KB
/
OpenSearchSpace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2024 nosqlbench
*
* 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 io.nosqlbench.adapter.opensearch;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.internal.BasicProfile;
import com.amazonaws.auth.profile.internal.ProfileKeyConstants;
import com.amazonaws.auth.profile.internal.ProfileStaticCredentialsProvider;
import io.nosqlbench.nb.api.config.standard.ConfigModel;
import io.nosqlbench.nb.api.config.standard.NBConfigModel;
import io.nosqlbench.nb.api.config.standard.NBConfiguration;
import io.nosqlbench.nb.api.config.standard.Param;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.transport.aws.AwsSdk2Transport;
import org.opensearch.client.transport.aws.AwsSdk2TransportOptions;
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient;
import software.amazon.awssdk.regions.Region;
import java.io.IOException;
import java.util.Map;
public class OpenSearchSpace implements AutoCloseable {
private final NBConfiguration cfg;
protected OpenSearchClient client;
public OpenSearchSpace(NBConfiguration cfg) {
this.cfg = cfg;
}
public synchronized OpenSearchClient getClient() {
if (client == null) {
client = createClient();
}
return client;
}
private OpenSearchClient createClient() {
String region = cfg.get("region");
Region selectedRegion = Region.of(region);
String host = cfg.get("host");
SdkAsyncHttpClient httpClient =
AwsCrtAsyncHttpClient.builder()
.build();
AwsSdk2TransportOptions.Builder transportOptionsBuilder
= AwsSdk2TransportOptions.builder();
cfg.getOptional("profile").map(
p -> ProfileCredentialsProvider.builder()
.profileName(p)
.build())
.ifPresent(transportOptionsBuilder::setCredentials);
AwsSdk2TransportOptions transportOptions = transportOptionsBuilder.build();
AwsOsServiceType svctype = AwsOsServiceType.valueOf(cfg.get("svctype"));
AwsSdk2Transport awsSdk2Transport =
new AwsSdk2Transport(
httpClient,
host,
svctype.name(),
selectedRegion,
transportOptions
);
OpenSearchClient client = new OpenSearchClient(awsSdk2Transport);
try {
InfoResponse info = client.info();
System.out.println(info.version().distribution() + ": " + info.version().number());
} catch (IOException e) {
throw new RuntimeException(e);
}
return client;
}
public static NBConfigModel getConfigModel() {
return ConfigModel.of(OpenSearchSpace.class)
.add(Param.required("region", String.class).setDescription("The region to connect to"))
.add(Param.required("host", String.class).setDescription("The Open Search API endpoint host"))
.add(Param.optional("profile")
.setDescription("The AWS auth profile to use. Required to activate profile based auth"))
.add(Param.defaultTo("svctype", "es")
.setDescription("one of es or aoss, defaults to es for OpenSearch domains"))
.add(Param.defaultTo("diag","false")
.setDescription("enable payload diagnostics or not"))
.asReadOnly();
}
@Override
public void close() throws Exception {
if (client != null) {
client.shutdown();
}
}
}