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

Commit 16022ea

Browse files
author
Praful Makani
authored
docs(samples): add update credentials (#425)
1 parent 1807342 commit 16022ea

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_credentials]
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 update credentials in transfer config.
29+
public class UpdateCredentials {
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+
String serviceAccount = "MY_SERVICE_ACCOUNT";
35+
TransferConfig transferConfig = TransferConfig.newBuilder().setName(configId).build();
36+
FieldMask updateMask = FieldMaskUtil.fromString("service_account_name");
37+
updateCredentials(transferConfig, serviceAccount, updateMask);
38+
}
39+
40+
public static void updateCredentials(
41+
TransferConfig transferConfig, String serviceAccount, FieldMask updateMask)
42+
throws IOException {
43+
try (DataTransferServiceClient dataTransferServiceClient = DataTransferServiceClient.create()) {
44+
UpdateTransferConfigRequest request =
45+
UpdateTransferConfigRequest.newBuilder()
46+
.setTransferConfig(transferConfig)
47+
.setUpdateMask(updateMask)
48+
.setServiceAccountName(serviceAccount)
49+
.build();
50+
dataTransferServiceClient.updateTransferConfig(request);
51+
System.out.println("Credentials updated successfully");
52+
} catch (ApiException ex) {
53+
System.out.print("Credentials was not updated." + ex.toString());
54+
}
55+
}
56+
}
57+
// [END bigquerydatatransfer_update_credentials]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.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 UpdateCredentialsIT {
36+
37+
private static final Logger LOG = Logger.getLogger(UpdateCredentialsIT.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+
private static final String SERVICE_ACCOUNT = requireEnvVar("DTS_UPDATED_SERVICE_ACCOUNT");
44+
45+
private static String requireEnvVar(String varName) {
46+
String value = System.getenv(varName);
47+
assertNotNull(
48+
"Environment variable " + varName + " is required to perform these tests.",
49+
System.getenv(varName));
50+
return value;
51+
}
52+
53+
@BeforeClass
54+
public static void checkRequirements() {
55+
requireEnvVar("DTS_TRANSFER_CONFIG_NAME");
56+
requireEnvVar("DTS_UPDATED_SERVICE_ACCOUNT");
57+
}
58+
59+
@Before
60+
public void setUp() {
61+
bout = new ByteArrayOutputStream();
62+
out = new PrintStream(bout);
63+
originalPrintStream = System.out;
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() {
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 testUpdateCredentials() throws IOException {
77+
TransferConfig transferConfig = TransferConfig.newBuilder().setName(CONFIG_NAME).build();
78+
FieldMask updateMask = FieldMaskUtil.fromString("service_account_name");
79+
UpdateCredentials.updateCredentials(transferConfig, SERVICE_ACCOUNT, updateMask);
80+
assertThat(bout.toString()).contains("Credentials updated successfully");
81+
}
82+
}

0 commit comments

Comments
 (0)