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

Commit 7d61025

Browse files
author
Praful Makani
authored
docs(samples): add re-enable transfer config (#385)
1 parent 533a681 commit 7d61025

File tree

2 files changed

+136
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)