Skip to content

Commit 0fe291b

Browse files
authored
add api for fetching federated query configs (#339)
* add api for fetching federated query configs * fix * fix * fix * fix format --------- Co-authored-by: tiana528 <tiana528@users.noreply.github.com>
1 parent 8d75808 commit 0fe291b

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed

src/main/java/com/treasuredata/client/TDClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.treasuredata.client.model.TDDatabase;
3737
import com.treasuredata.client.model.TDExportJobRequest;
3838
import com.treasuredata.client.model.TDExportResultJobRequest;
39+
import com.treasuredata.client.model.TDFederatedQueryConfig;
3940
import com.treasuredata.client.model.TDImportResult;
4041
import com.treasuredata.client.model.TDJob;
4142
import com.treasuredata.client.model.TDJobList;
@@ -1137,4 +1138,11 @@ public TDImportResult importBytes(String databaseName, String tableName, byte[]
11371138
{
11381139
return doPut(buildUrl(String.format("/v3/table/import_with_id/%s/%s/%s/%s", databaseName, tableName, id, "msgpack.gz")), Collections.emptyMap(), content, offset, length, TDImportResult.class);
11391140
}
1141+
1142+
@Override
1143+
public List<TDFederatedQueryConfig> getFederatedQueryConfigs()
1144+
throws TDClientException
1145+
{
1146+
return doGet("/v4/federated_query_configs", new TypeReference<List<TDFederatedQueryConfig>>() {});
1147+
}
11401148
}

src/main/java/com/treasuredata/client/TDClientApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.treasuredata.client.model.TDDatabase;
3232
import com.treasuredata.client.model.TDExportJobRequest;
3333
import com.treasuredata.client.model.TDExportResultJobRequest;
34+
import com.treasuredata.client.model.TDFederatedQueryConfig;
3435
import com.treasuredata.client.model.TDImportResult;
3536
import com.treasuredata.client.model.TDJob;
3637
import com.treasuredata.client.model.TDJobList;
@@ -501,4 +502,10 @@ default <Result> Result getBulkImportErrorRecords(String sessionName, com.google
501502
* @return TDImportResult which contains a unique import id and md5
502503
*/
503504
TDImportResult importBytes(String database, String table, byte[] content, int offset, int length, String id);
505+
506+
/**
507+
* Fetch a list of federated query configurations
508+
* @return List<TDFederatedQueryConfig> which contains a list of federated query configs
509+
*/
510+
List<TDFederatedQueryConfig> getFederatedQueryConfigs();
504511
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.treasuredata.client.model;
2+
3+
import java.util.Map;
4+
import java.util.Objects;
5+
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
public class TDFederatedQueryConfig
10+
{
11+
private final int id;
12+
private final String type;
13+
private final int userId;
14+
private final int accountId;
15+
private final String name;
16+
private final int connectionId;
17+
private final String createdAt;
18+
private final String updatedAt;
19+
private final Map<String, Object> settings;
20+
21+
@JsonCreator
22+
public TDFederatedQueryConfig(
23+
@JsonProperty("id") int id,
24+
@JsonProperty("type") String type,
25+
@JsonProperty("user_id") int userId,
26+
@JsonProperty("account_id") int accountId,
27+
@JsonProperty("name") String name,
28+
@JsonProperty("connection_id") int connectionId,
29+
@JsonProperty("created_at") String createdAt,
30+
@JsonProperty("updated_at") String updatedAt,
31+
@JsonProperty("settings") Map<String, Object> settings)
32+
{
33+
this.id = id;
34+
this.type = type;
35+
this.userId = userId;
36+
this.accountId = accountId;
37+
this.name = name;
38+
this.connectionId = connectionId;
39+
this.createdAt = createdAt;
40+
this.updatedAt = updatedAt;
41+
this.settings = settings;
42+
}
43+
44+
@JsonProperty
45+
public int getId()
46+
{
47+
return id;
48+
}
49+
50+
@JsonProperty
51+
public String getType()
52+
{
53+
return type;
54+
}
55+
56+
@JsonProperty("user_id")
57+
public int getUserId()
58+
{
59+
return userId;
60+
}
61+
62+
@JsonProperty("account_id")
63+
public int getAccountId()
64+
{
65+
return accountId;
66+
}
67+
68+
@JsonProperty
69+
public String getName()
70+
{
71+
return name;
72+
}
73+
74+
@JsonProperty("connection_id")
75+
public int getConnectionId()
76+
{
77+
return connectionId;
78+
}
79+
80+
@JsonProperty("created_at")
81+
public String getCreatedAt()
82+
{
83+
return createdAt;
84+
}
85+
86+
@JsonProperty("updated_at")
87+
public String getUpdatedAt()
88+
{
89+
return updatedAt;
90+
}
91+
92+
@JsonProperty
93+
public Map<String, Object> getSettings()
94+
{
95+
return settings;
96+
}
97+
98+
@Override
99+
public final boolean equals(Object o)
100+
{
101+
if (this == o) {
102+
return true;
103+
}
104+
if (!(o instanceof TDFederatedQueryConfig)) {
105+
return false;
106+
}
107+
TDFederatedQueryConfig that = (TDFederatedQueryConfig) o;
108+
return id == that.id
109+
&& userId == that.userId
110+
&& accountId == that.accountId
111+
&& connectionId == that.connectionId
112+
&& Objects.equals(type, that.type)
113+
&& Objects.equals(name, that.name)
114+
&& Objects.equals(createdAt, that.createdAt)
115+
&& Objects.equals(updatedAt, that.updatedAt)
116+
&& Objects.equals(settings, that.settings);
117+
}
118+
119+
@Override
120+
public int hashCode()
121+
{
122+
return Objects.hash(id, type, userId, accountId, name, connectionId, createdAt, updatedAt, settings);
123+
}
124+
125+
@Override
126+
public String toString()
127+
{
128+
return "TDFederatedQueryConfig{"
129+
+ "id=" + id
130+
+ ", type='" + type + '\''
131+
+ ", userId=" + userId
132+
+ ", accountId=" + accountId
133+
+ ", name='" + name + '\''
134+
+ ", connectionId=" + connectionId
135+
+ ", createdAt='" + createdAt + '\''
136+
+ ", updatedAt='" + updatedAt + '\''
137+
+ ", settings=" + settings
138+
+ '}';
139+
}
140+
}

src/test/java/com/treasuredata/client/TestTDClient.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.treasuredata.client.model.TDExportFileFormatType;
3434
import com.treasuredata.client.model.TDExportJobRequest;
3535
import com.treasuredata.client.model.TDExportResultJobRequest;
36+
import com.treasuredata.client.model.TDFederatedQueryConfig;
3637
import com.treasuredata.client.model.TDImportResult;
3738
import com.treasuredata.client.model.TDJob;
3839
import com.treasuredata.client.model.TDJob.EngineVersion;
@@ -111,6 +112,7 @@
111112
import static org.hamcrest.MatcherAssert.assertThat;
112113
import static org.junit.jupiter.api.Assertions.assertEquals;
113114
import static org.junit.jupiter.api.Assertions.assertFalse;
115+
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
114116
import static org.junit.jupiter.api.Assertions.assertThrows;
115117
import static org.junit.jupiter.api.Assertions.assertTrue;
116118
import static org.junit.jupiter.api.Assertions.fail;
@@ -1958,6 +1960,178 @@ public void testImportBytesWithId()
19581960
assertEquals(result.getUniqueId(), "4288048cf8f811e88b560a87157ac806");
19591961
}
19601962

1963+
@Test
1964+
public void testGetFederatedQueryConfigsWhenEmpty()
1965+
throws Exception
1966+
{
1967+
client = mockClient();
1968+
server.enqueue(new MockResponse().setBody("[]"));
1969+
1970+
List<TDFederatedQueryConfig> result = client.getFederatedQueryConfigs();
1971+
1972+
assertTrue(result.isEmpty());
1973+
}
1974+
1975+
@Test
1976+
public void testGetFederatedQueryConfigsWhenOnlyOne()
1977+
throws Exception
1978+
{
1979+
client = mockClient();
1980+
final String federatedQueryConfigs = "[\n" +
1981+
" {\n" +
1982+
" \"id\": 12345,\n" +
1983+
" \"type\": \"custom_type\",\n" +
1984+
" \"user_id\": 67890,\n" +
1985+
" \"account_id\": 54321,\n" +
1986+
" \"name\": \"test_name\",\n" +
1987+
" \"connection_id\": 112233,\n" +
1988+
" \"created_at\": \"2023-06-01T00:00:00Z\",\n" +
1989+
" \"updated_at\": \"2023-06-15T00:00:00Z\",\n" +
1990+
" \"settings\": {\n" +
1991+
" \"account_name\": \"snowflake_account\",\n" +
1992+
" \"auth_method\": \"password\",\n" +
1993+
" \"private_key\": \"encrypted_key\",\n" +
1994+
" \"passphrase\": \"secret_phrase\",\n" +
1995+
" \"options\": {\n" +
1996+
" \"key1\": \"val1\",\n" +
1997+
" \"key2\": \"val2\"\n" +
1998+
" },\n" +
1999+
" \"user\": \"snowflake_user\",\n" +
2000+
" \"password\": \"snowflake_password\",\n" +
2001+
" \"database\": \"sample_database\"\n" +
2002+
" }\n" +
2003+
" }\n" +
2004+
"]";
2005+
server.enqueue(new MockResponse().setBody(federatedQueryConfigs));
2006+
2007+
List<TDFederatedQueryConfig> result = client.getFederatedQueryConfigs();
2008+
2009+
Map<String, Object> settings = new HashMap<>();
2010+
settings.put("account_name", "snowflake_account");
2011+
settings.put("auth_method", "password");
2012+
settings.put("private_key", "encrypted_key");
2013+
settings.put("passphrase", "secret_phrase");
2014+
Map<String, Object> options = new HashMap<>();
2015+
options.put("key1", "val1");
2016+
options.put("key2", "val2");
2017+
settings.put("options", options);
2018+
settings.put("user", "snowflake_user");
2019+
settings.put("password", "snowflake_password");
2020+
settings.put("database", "sample_database");
2021+
2022+
List<TDFederatedQueryConfig> expected = Arrays.asList(
2023+
new TDFederatedQueryConfig(12345, "custom_type", 67890, 54321, "test_name", 112233, "2023-06-01T00:00:00Z", "2023-06-15T00:00:00Z", settings)
2024+
);
2025+
assertIterableEquals(result, expected);
2026+
}
2027+
2028+
@Test
2029+
public void testGetFederatedQueryConfigsWhenMultiple()
2030+
throws Exception
2031+
{
2032+
client = mockClient();
2033+
final String federatedQueryConfigs = "[\n" +
2034+
" {\n" +
2035+
" \"id\": 12345,\n" +
2036+
" \"type\": \"custom_type\",\n" +
2037+
" \"user_id\": 67890,\n" +
2038+
" \"account_id\": 54321,\n" +
2039+
" \"name\": \"test_name\",\n" +
2040+
" \"connection_id\": 112233,\n" +
2041+
" \"created_at\": \"2023-06-01T00:00:00Z\",\n" +
2042+
" \"updated_at\": \"2023-06-15T00:00:00Z\",\n" +
2043+
" \"settings\": {\n" +
2044+
" \"account_name\": \"snowflake_account\",\n" +
2045+
" \"auth_method\": \"password\",\n" +
2046+
" \"private_key\": \"encrypted_key\",\n" +
2047+
" \"passphrase\": \"secret_phrase\",\n" +
2048+
" \"options\": {\n" +
2049+
" \"key1\": \"val1\",\n" +
2050+
" \"key2\": \"val2\"\n" +
2051+
" },\n" +
2052+
" \"user\": \"snowflake_user\",\n" +
2053+
" \"password\": \"snowflake_password\",\n" +
2054+
" \"database\": \"sample_database\"\n" +
2055+
" }\n" +
2056+
" },\n" +
2057+
" {\n" +
2058+
" \"id\": 67890,\n" +
2059+
" \"type\": \"another_type\",\n" +
2060+
" \"user_id\": 12345,\n" +
2061+
" \"account_id\": 98765,\n" +
2062+
" \"name\": \"another_test_name\",\n" +
2063+
" \"connection_id\": 445566,\n" +
2064+
" \"created_at\": \"2023-07-01T00:00:00Z\",\n" +
2065+
" \"updated_at\": \"2023-07-15T00:00:00Z\",\n" +
2066+
" \"settings\": {\n" +
2067+
" \"account_name\": \"another_snowflake_account\",\n" +
2068+
" \"auth_method\": \"oauth\",\n" +
2069+
" \"private_key\": \"another_encrypted_key\",\n" +
2070+
" \"passphrase\": \"another_secret_phrase\",\n" +
2071+
" \"options\": {\n" +
2072+
" \"keyA\": \"valA\",\n" +
2073+
" \"keyB\": \"valB\"\n" +
2074+
" },\n" +
2075+
" \"user\": \"another_snowflake_user\",\n" +
2076+
" \"password\": \"another_snowflake_password\",\n" +
2077+
" \"database\": \"another_sample_database\"\n" +
2078+
" }\n" +
2079+
" }\n" +
2080+
"]";
2081+
server.enqueue(new MockResponse().setBody(federatedQueryConfigs));
2082+
2083+
List<TDFederatedQueryConfig> result = client.getFederatedQueryConfigs();
2084+
2085+
Map<String, Object> settings1 = new HashMap<>();
2086+
settings1.put("account_name", "snowflake_account");
2087+
settings1.put("auth_method", "password");
2088+
settings1.put("private_key", "encrypted_key");
2089+
settings1.put("passphrase", "secret_phrase");
2090+
Map<String, Object> options1 = new HashMap<>();
2091+
options1.put("key1", "val1");
2092+
options1.put("key2", "val2");
2093+
settings1.put("options", options1);
2094+
settings1.put("user", "snowflake_user");
2095+
settings1.put("password", "snowflake_password");
2096+
settings1.put("database", "sample_database");
2097+
2098+
Map<String, Object> settings2 = new HashMap<>();
2099+
settings2.put("account_name", "another_snowflake_account");
2100+
settings2.put("auth_method", "oauth");
2101+
settings2.put("private_key", "another_encrypted_key");
2102+
settings2.put("passphrase", "another_secret_phrase");
2103+
Map<String, Object> options2 = new HashMap<>();
2104+
options2.put("keyA", "valA");
2105+
options2.put("keyB", "valB");
2106+
settings2.put("options", options2);
2107+
settings2.put("user", "another_snowflake_user");
2108+
settings2.put("password", "another_snowflake_password");
2109+
settings2.put("database", "another_sample_database");
2110+
2111+
List<TDFederatedQueryConfig> expected = Arrays.asList(
2112+
new TDFederatedQueryConfig(12345, "custom_type", 67890, 54321, "test_name", 112233, "2023-06-01T00:00:00Z", "2023-06-15T00:00:00Z", settings1),
2113+
new TDFederatedQueryConfig(67890, "another_type", 12345, 98765, "another_test_name", 445566, "2023-07-01T00:00:00Z", "2023-07-15T00:00:00Z", settings2)
2114+
);
2115+
assertIterableEquals(result, expected);
2116+
}
2117+
2118+
@Test
2119+
public void testGetFederatedQueryConfigsWhenAuthenticationFail()
2120+
throws Exception
2121+
{
2122+
TDClient client = TDClient.newBuilder().setApiKey("1/xxfasdfafd").build(); // Set a wrong API key
2123+
server.enqueue(new MockResponse().setBody("[]"));
2124+
2125+
try {
2126+
List<TDFederatedQueryConfig> result = client.getFederatedQueryConfigs();
2127+
fail("should not reach here");
2128+
}
2129+
catch (TDClientHttpUnauthorizedException e) {
2130+
// OK
2131+
assertEquals(HttpStatus.UNAUTHORIZED_401, e.getStatusCode());
2132+
}
2133+
}
2134+
19612135
private File createTempMsgpackGz(String prefix, int numRows)
19622136
throws IOException
19632137
{

0 commit comments

Comments
 (0)