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

Commit ac78f78

Browse files
author
Praful Makani
authored
docs(samples): add update transfer config (#347)
1 parent 3a901f3 commit ac78f78

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_update_config]
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.TransferConfig;
23+
import com.google.cloud.bigquery.datatransfer.v1.UpdateTransferConfigRequest;
24+
import com.google.protobuf.FieldMask;
25+
import java.io.IOException;
26+
27+
// Sample to update transfer config.
28+
public class UpdateTransferConfig {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String configId = "MY_CONFIG_ID";
33+
updateTransferConfig(configId);
34+
}
35+
36+
public static void updateTransferConfig(String configId) throws IOException {
37+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
38+
TransferConfig transferConfig =
39+
TransferConfig.newBuilder()
40+
.setName(configId)
41+
.setDisplayName("UPDATED_DISPLAY_NAME")
42+
.build();
43+
FieldMask updateMask = FieldMask.newBuilder().addPaths("display_name").build();
44+
UpdateTransferConfigRequest request =
45+
UpdateTransferConfigRequest.newBuilder()
46+
.setTransferConfig(transferConfig)
47+
.setUpdateMask(updateMask)
48+
.build();
49+
TransferConfig updateConfig = dataTransferServiceClient.updateTransferConfig(request);
50+
System.out.println("Transfer config updated successfully :" + updateConfig.getDisplayName());
51+
} catch (ApiException ex) {
52+
System.out.print("Transfer config was not updated." + ex.toString());
53+
}
54+
}
55+
}
56+
// [END bigquerydatatransfer_update_config]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 java.io.ByteArrayOutputStream;
23+
import java.io.IOException;
24+
import java.io.PrintStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class UpdateTransferConfigIT {
33+
34+
private static final Logger LOG = Logger.getLogger(DeleteScheduledQueryIT.class.getName());
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
private PrintStream originalPrintStream;
38+
39+
private static final String CONFIG_NAME = requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
59+
System.setOut(out);
60+
}
61+
62+
@After
63+
public void tearDown() {
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
67+
LOG.log(Level.INFO, bout.toString());
68+
}
69+
70+
@Test
71+
public void testUpdateTransferConfig() throws IOException {
72+
UpdateTransferConfig.updateTransferConfig(CONFIG_NAME);
73+
assertThat(bout.toString()).contains("Transfer config updated successfully");
74+
}
75+
}

0 commit comments

Comments
 (0)