Skip to content

Commit 30f4a93

Browse files
committed
fix issues, updqte langchain4j
1 parent 6b7c83e commit 30f4a93

File tree

9 files changed

+208
-27
lines changed

9 files changed

+208
-27
lines changed

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/db/AstraDBOpsClient.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,7 @@ public Stream<AccessList> findAllAccessLists() {
112112
// ---------------------------------
113113
// ---- CRUD ----
114114
// ---------------------------------
115-
116-
/**
117-
* Returns list of databases with default filter.
118-
* (include=non terminated, provider=ALL,limit=25)
119-
*
120-
* @return
121-
* matching db
122-
*/
123-
public Stream<Database> findAll() {
124-
return search(DatabaseFilter.builder()
125-
.include(Include.ALL)
126-
.provider(CloudProviderType.ALL)
127-
.limit(1000)
128-
.build());
129-
}
130-
115+
131116
/**
132117
* Default Filter to find databases.
133118
*

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/org/TokensClient.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dtsx.astra.sdk.org;
22

33
import com.dtsx.astra.sdk.AbstractApiClient;
4+
import com.dtsx.astra.sdk.org.domain.CreateTokenRequest;
45
import com.dtsx.astra.sdk.org.domain.CreateTokenResponse;
56
import com.dtsx.astra.sdk.org.domain.DefaultRoles;
67
import com.dtsx.astra.sdk.org.domain.IamToken;
@@ -12,7 +13,15 @@
1213
import com.dtsx.astra.sdk.utils.AstraEnvironment;
1314
import com.dtsx.astra.sdk.utils.JsonUtils;
1415

16+
import java.time.Instant;
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
1522
import java.util.Optional;
23+
import java.util.UUID;
24+
import java.util.stream.Collectors;
1625
import java.util.stream.Stream;
1726

1827
/**
@@ -130,6 +139,59 @@ public CreateTokenResponse create(String role) {
130139
return JsonUtils.unmarshallBean(res.getBody(), CreateTokenResponse.class);
131140
}
132141

142+
/**
143+
* Create token
144+
*
145+
* @param ctr
146+
* request to create a token with all the elements
147+
* @return
148+
* created token
149+
*/
150+
public CreateTokenResponse create(CreateTokenRequest ctr) {
151+
152+
Assert.notNull(ctr, "request");
153+
Map<String, Object> tokenCreationPayload = new HashMap<>();
154+
155+
// Role should exist
156+
157+
Map<String, String> availableRoles =
158+
rolesClient.findAll().collect(Collectors.toMap(Role::getId, Role::getName));
159+
160+
List<String> roleIds = new ArrayList<>();
161+
ctr.getRoles().forEach(role -> {
162+
if (availableRoles.containsKey(role)) {
163+
roleIds.add(role);
164+
} else if (availableRoles.containsValue(role)) {
165+
roleIds.add(availableRoles.entrySet().stream()
166+
.filter(e -> role.equalsIgnoreCase(e.getValue()))
167+
.map(Map.Entry::getKey)
168+
.findFirst()
169+
.orElse(null));
170+
} else {
171+
throw new IllegalArgumentException("Role '" + role + "' does not exist");
172+
}
173+
});
174+
tokenCreationPayload.put("roles", roleIds);
175+
176+
if (ctr.getDescription() != null && !ctr.getDescription().isBlank()) {
177+
tokenCreationPayload.put("description", ctr.getDescription());
178+
}
179+
if (ctr.getOrgId() != null) {
180+
tokenCreationPayload.put("orgId", ctr.getOrgId());
181+
}
182+
if (ctr.getExpirationDate() != null) {
183+
tokenCreationPayload.put("tokenExpiry", ctr.getExpirationDate().toString());
184+
}
185+
186+
// Invoke endpoint
187+
ApiResponseHttp res = POST(
188+
getEndpointTokens(),
189+
JsonUtils.marshall(tokenCreationPayload),
190+
getOperationName("create"));
191+
// Marshall response
192+
return JsonUtils.unmarshallBean(res.getBody(), CreateTokenResponse.class);
193+
}
194+
133195
/**
134196
* Create token
135197
*
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.dtsx.astra.sdk.org.domain;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
import java.time.Instant;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
import java.util.UUID;
10+
11+
public class CreateTokenRequest implements Serializable {
12+
13+
private String description;
14+
private UUID orgId;
15+
private Instant expirationDate;
16+
private Set<String> roles;
17+
18+
public CreateTokenRequest() {
19+
roles = new HashSet<>();
20+
}
21+
22+
/**
23+
* Gets description
24+
*
25+
* @return value of description
26+
*/
27+
public String getDescription() {
28+
return description;
29+
}
30+
31+
/**
32+
* Set value for description
33+
*
34+
* @param description new value for description
35+
*/
36+
public void setDescription(String description) {
37+
this.description = description;
38+
}
39+
40+
/**
41+
* Gets orgId
42+
*
43+
* @return value of orgId
44+
*/
45+
public UUID getOrgId() {
46+
return orgId;
47+
}
48+
49+
/**
50+
* Set value for orgId
51+
*
52+
* @param orgId new value for orgId
53+
*/
54+
public void setOrgId(UUID orgId) {
55+
this.orgId = orgId;
56+
}
57+
58+
/**
59+
* Gets expirationDate
60+
*
61+
* @return value of expirationDate
62+
*/
63+
public Instant getExpirationDate() {
64+
return expirationDate;
65+
}
66+
67+
/**
68+
* Set value for expirationDate
69+
*
70+
* @param expirationDate new value for expirationDate
71+
*/
72+
public void setExpirationDate(Instant expirationDate) {
73+
this.expirationDate = expirationDate;
74+
}
75+
76+
/**
77+
* Gets roles
78+
*
79+
* @return value of roles
80+
*/
81+
public Set<String> getRoles() {
82+
return roles;
83+
}
84+
85+
/**
86+
* Set value for roles
87+
*
88+
* @param roles new value for roles
89+
*/
90+
public void setRoles(Set<String> roles) {
91+
this.roles = roles;
92+
}
93+
}

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/streaming/TenantCdcClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public void create(String databaseId, String keyspace, String table, int topicPa
9393
createCdc.setKeyspace(keyspace);
9494
createCdc.setTableName(table);
9595
createCdc.setTopicPartitions(topicPartition);
96-
HttpClientWrapper.getInstance(getOperationName("create")).POST_PULSAR(getEndpointTenantCdc(),
96+
HttpClientWrapper.getInstance(getOperationName("create"))
97+
.POST_PULSAR(getEndpointTenantCdc(),
9798
tenant.getPulsarToken(),
9899
JsonUtils.marshall(createCdc),
99100
tenant.getClusterName(),

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/utils/HttpClientWrapper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public ApiResponseHttp DELETE_PULSAR(String url, String token, String body, Stri
243243
* organization
244244
*/
245245
private void updatePulsarHttpRequest(HttpUriRequestBase request, String pulsarToken, String pulsarCluster, String organizationId) {
246-
request.addHeader(HEADER_AUTHORIZATION, pulsarToken);
246+
request.addHeader(HEADER_AUTHORIZATION, "Bearer " + pulsarToken);
247247
request.addHeader(HEADER_CURRENT_ORG, organizationId);
248248
request.addHeader(HEADER_CURRENT_PULSAR_CLUSTER, pulsarCluster);
249249
}
@@ -372,7 +372,6 @@ public ApiResponseHttp executeHttp(HttpUriRequestBase req, boolean mandatory) {
372372
.withHttpRequest(req);
373373

374374
try(CloseableHttpResponse response = httpClient.execute(req)) {
375-
376375
ApiResponseHttp res;
377376
if (response == null) {
378377
res = new ApiResponseHttp("Response is empty, please check url",
@@ -404,7 +403,6 @@ public ApiResponseHttp executeHttp(HttpUriRequestBase req, boolean mandatory) {
404403
processErrors(res, mandatory);
405404
LOGGER.error("An HTTP Error occurred. The HTTP CODE Return is {}", res.getCode());
406405
}
407-
408406
executionInfo.withHttpResponse(res);
409407
return res;
410408
// do not swallow the exception
@@ -485,7 +483,7 @@ private void processErrors(ApiResponseHttp res, boolean mandatory) {
485483
break;
486484
// 409
487485
case HttpURLConnection.HTTP_CONFLICT:
488-
throw new AuthenticationException("HTTP_CONFLICT (code=" + res.getCode() +
486+
throw new IllegalStateException("HTTP_CONFLICT (code=" + res.getCode() +
489487
"): Object may already exist with same name or id " +
490488
body);
491489
case 422:

astra-sdk-devops/src/test/java/com/dtsx/astra/sdk/db/DatabasesClientTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.junit.jupiter.api.TestMethodOrder;
1515

1616
import java.util.Optional;
17+
import java.util.UUID;
1718

1819
/**
1920
* Tests Operations on Databases level.
@@ -60,9 +61,26 @@ public void shouldCreateServerlessDb() {
6061

6162
@Test
6263
@Order(3)
63-
@DisplayName("findAll() retrieve some data")
64-
public void shouldFindAll() {
65-
Assertions.assertTrue(getDatabasesClient().findAll().findAny().isPresent());
64+
@DisplayName("find Non Terminated dbs")
65+
public void shouldFindNonTerminated() {
66+
Assertions.assertTrue(getDatabasesClient().findAllNonTerminated().findAny().isPresent());
67+
}
68+
69+
70+
@Test
71+
public void shouldSendGoodErrorMessage() throws InterruptedException {
72+
// create ephemeral db to force error
73+
// String dbId = getDatabasesClient().create(DatabaseCreationRequest
74+
// .builder()
75+
// .name(SDK_TEST_DB_NAME)
76+
// .keyspace(SDK_TEST_KEYSPACE)
77+
// .cloudRegion(SDK_TEST_DB_REGION)
78+
// .build());
79+
//
80+
// Thread.sleep(2000);
81+
82+
// try to delete immediately
83+
getDatabasesClient().database("6aee3ef4-76ac-4848-88b8-92a01bbe2fe7").delete();
6684
}
6785

6886
@Test

astra-sdk-devops/src/test/java/com/dtsx/astra/sdk/iam/TokensClientTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dtsx.astra.sdk.iam;
22

33
import com.dtsx.astra.sdk.AbstractDevopsApiTest;
4+
import com.dtsx.astra.sdk.org.domain.CreateTokenRequest;
45
import com.dtsx.astra.sdk.org.domain.CreateTokenResponse;
56
import com.dtsx.astra.sdk.org.domain.DefaultRoles;
67
import org.junit.jupiter.api.Assertions;
@@ -9,6 +10,10 @@
910
import org.junit.jupiter.api.Test;
1011
import org.junit.jupiter.api.TestMethodOrder;
1112

13+
import java.time.Duration;
14+
import java.time.Instant;
15+
import java.time.temporal.TemporalAmount;
16+
1217
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1318
public class TokensClientTest extends AbstractDevopsApiTest {
1419

@@ -22,6 +27,21 @@ public void shouldCreateToken() {
2227
// Then it should be found
2328
Assertions.assertTrue(getApiDevopsClient().tokens().exist(res.getClientId()));
2429
createClientId = res.getClientId();
30+
System.out.println(createClientId);
31+
}
32+
33+
@Test
34+
@Order(1)
35+
public void shouldCreateTokenComposite() {
36+
// When creating a token
37+
CreateTokenRequest req = new CreateTokenRequest();
38+
req.setDescription("Token created by Junit");
39+
req.getRoles().add(DefaultRoles.DATABASE_ADMINISTRATOR.getName());
40+
req.setExpirationDate(Instant.now().plus(Duration.ofDays(10)));
41+
42+
CreateTokenResponse res = getApiDevopsClient().tokens().create(req);
43+
Assertions.assertTrue(getApiDevopsClient().tokens().exist(res.getClientId()));
44+
createClientId = res.getClientId();
2545
}
2646

2747
@Test

astra-sdk-devops/src/test/java/com/dtsx/astra/sdk/streaming/TenantClientTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public void shouldCreateTenant() throws InterruptedException {
3434
.tenantName(tmpTenant)
3535
.userEmail("[email protected]").build());
3636
// Then
37-
Thread.sleep(1000);
37+
long timeout = System.currentTimeMillis() + 20000;
38+
while (!sc.exist(tmpTenant) && System.currentTimeMillis() < timeout) {
39+
LOGGER.info("Waiting for tenant creation ...");
40+
Thread.sleep(1000);
41+
}
3842
Assertions.assertTrue(sc.exist(tmpTenant));
3943
}
4044

langchain4j-astradb/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
</parent>
1313

1414
<properties>
15-
<langchain4j.version>1.3.0</langchain4j.version>
16-
<langchain4j-beta.version>1.3.0-beta9</langchain4j-beta.version>
15+
<langchain4j.version>1.4.0</langchain4j.version>
16+
<langchain4j-beta.version>1.4.0-beta10</langchain4j-beta.version>
1717
<awaitility.version>4.3.0</awaitility.version>
1818
</properties>
1919

0 commit comments

Comments
 (0)