Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.

Commit 8824cc5

Browse files
author
Praful Makani
authored
docs(samples): add run details (#334)
* docs(samples): add run details * docs(samples): address feedback * docs(samples): add envvar
1 parent 2b86803 commit 8824cc5

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquerydatatransfer;
18+
19+
// [START bigquerydatatransfer_get_run_details]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
22+
import com.google.cloud.bigquery.datatransfer.v1.GetTransferRunRequest;
23+
import com.google.cloud.bigquery.datatransfer.v1.TransferRun;
24+
import java.io.IOException;
25+
26+
// Sample to get run details from transfer config.
27+
public class RunDetails {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// runId examples:
32+
// `projects/{project_id}/transferConfigs/{config_id}/runs/{run_id}` or
33+
// `projects/{project_id}/locations/{location_id}/transferConfigs/{config_id}/runs/{run_id}`
34+
String runId = "MY_RUN_ID";
35+
runDetails(runId);
36+
}
37+
38+
public static void runDetails(String runId) throws IOException {
39+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
40+
GetTransferRunRequest request = GetTransferRunRequest.newBuilder().setName(runId).build();
41+
TransferRun run = dataTransferServiceClient.getTransferRun(request);
42+
System.out.print("Run details retrieved successfully :" + run.getName() + "\n");
43+
} catch (ApiException ex) {
44+
System.out.print("Run details not found." + ex.toString());
45+
}
46+
}
47+
}
48+
// [END bigquerydatatransfer_get_run_details]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquerydatatransfer;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.PrintStream;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class RunDetailsIT {
34+
35+
private static final Logger LOG = Logger.getLogger(GetTransferConfigInfoIT.class.getName());
36+
private ByteArrayOutputStream bout;
37+
private String runName;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
41+
private static final String CONFIG_NAME = requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
54+
}
55+
56+
@Before
57+
public void setUp() throws IOException {
58+
bout = new ByteArrayOutputStream();
59+
out = new PrintStream(bout);
60+
originalPrintStream = System.out;
61+
System.setOut(out);
62+
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
63+
client.listTransferRuns(CONFIG_NAME).iterateAll().forEach(run -> runName = run.getName());
64+
}
65+
}
66+
67+
@After
68+
public void tearDown() throws IOException {
69+
// restores print statements in the original method
70+
System.out.flush();
71+
System.setOut(originalPrintStream);
72+
LOG.log(Level.INFO, bout.toString());
73+
}
74+
75+
@Test
76+
public void testRunDetails() throws IOException {
77+
RunDetails.runDetails(runName);
78+
assertThat(bout.toString()).contains("Run details retrieved successfully :");
79+
}
80+
}

0 commit comments

Comments
 (0)