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

Commit 068dbc0

Browse files
author
Praful Makani
authored
docs(samples): add update connection (#158)
1 parent d015a4e commit 068dbc0

File tree

5 files changed

+182
-1
lines changed

5 files changed

+182
-1
lines changed

samples/install-without-bom/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
</dependency>
3434
<!-- [END bigqueryconnection_install_without_bom] -->
3535

36+
<dependency>
37+
<groupId>com.google.protobuf</groupId>
38+
<artifactId>protobuf-java-util</artifactId>
39+
<version>3.13.0</version>
40+
</dependency>
3641
<dependency>
3742
<groupId>junit</groupId>
3843
<artifactId>junit</artifactId>

samples/snapshot/pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
</dependency>
3333
<!-- {x-version-update-end} -->
3434

35+
<dependency>
36+
<groupId>com.google.protobuf</groupId>
37+
<artifactId>protobuf-java-util</artifactId>
38+
<version>3.13.0</version>
39+
</dependency>
3540
<dependency>
3641
<groupId>junit</groupId>
3742
<artifactId>junit</artifactId>
@@ -80,4 +85,4 @@
8085
</plugin>
8186
</plugins>
8287
</build>
83-
</project>
88+
</project>

samples/snippets/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<version>0.3.1</version>
4444
</dependency>
4545

46+
<dependency>
47+
<groupId>com.google.protobuf</groupId>
48+
<artifactId>protobuf-java-util</artifactId>
49+
<version>3.13.0</version>
50+
</dependency>
4651
<dependency>
4752
<groupId>junit</groupId>
4853
<artifactId>junit</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.bigqueryconnection;
18+
19+
// [START bigqueryconnection_update_connection]
20+
import com.google.cloud.bigquery.connection.v1.Connection;
21+
import com.google.cloud.bigquery.connection.v1.ConnectionName;
22+
import com.google.cloud.bigquery.connection.v1.UpdateConnectionRequest;
23+
import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;
24+
import com.google.protobuf.FieldMask;
25+
import com.google.protobuf.util.FieldMaskUtil;
26+
import java.io.IOException;
27+
28+
// Sample to update connection
29+
public class UpdateConnection {
30+
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "MY_PROJECT_ID";
34+
String location = "MY_LOCATION";
35+
String connectionId = "MY_CONNECTION_ID";
36+
String description = "MY_DESCRIPTION";
37+
Connection connection = Connection.newBuilder().setDescription(description).build();
38+
updateConnection(projectId, location, connectionId, connection);
39+
}
40+
41+
public static void updateConnection(
42+
String projectId, String location, String connectionId, Connection connection)
43+
throws IOException {
44+
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
45+
ConnectionName name = ConnectionName.of(projectId, location, connectionId);
46+
FieldMask updateMask = FieldMaskUtil.fromString("description");
47+
UpdateConnectionRequest request =
48+
UpdateConnectionRequest.newBuilder()
49+
.setName(name.toString())
50+
.setConnection(connection)
51+
.setUpdateMask(updateMask)
52+
.build();
53+
Connection response = client.updateConnection(request);
54+
System.out.println("Connection updated successfully :" + response.getDescription());
55+
}
56+
}
57+
}
58+
// [END bigqueryconnection_update_connection]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.bigqueryconnection;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.connection.v1.CloudSqlCredential;
23+
import com.google.cloud.bigquery.connection.v1.CloudSqlProperties;
24+
import com.google.cloud.bigquery.connection.v1.Connection;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
27+
import java.io.PrintStream;
28+
import java.util.UUID;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
36+
public class UpdateConnectionIT {
37+
38+
private static final Logger LOG = Logger.getLogger(UpdateConnectionIT.class.getName());
39+
private static final String LOCATION = "US";
40+
private static final String REGION = "us-central1";
41+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
42+
private static final String MY_SQL_DATABASE = requireEnvVar("MY_SQL_DATABASE");
43+
private static final String MY_SQL_INSTANCE = requireEnvVar("MY_SQL_INSTANCE");
44+
private static final String DB_USER = requireEnvVar("DB_USER");
45+
private static final String DB_PWD = requireEnvVar("DB_PWD");
46+
47+
private String connectionId;
48+
private ByteArrayOutputStream bout;
49+
private PrintStream out;
50+
private PrintStream originalPrintStream;
51+
52+
private static String requireEnvVar(String varName) {
53+
String value = System.getenv(varName);
54+
assertNotNull(
55+
"Environment variable " + varName + " is required to perform these tests.",
56+
System.getenv(varName));
57+
return value;
58+
}
59+
60+
@BeforeClass
61+
public static void checkRequirements() {
62+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
63+
requireEnvVar("MY_SQL_DATABASE");
64+
requireEnvVar("MY_SQL_INSTANCE");
65+
requireEnvVar("DB_USER");
66+
requireEnvVar("DB_PWD");
67+
}
68+
69+
@Before
70+
public void setUp() throws IOException {
71+
bout = new ByteArrayOutputStream();
72+
out = new PrintStream(bout);
73+
originalPrintStream = System.out;
74+
System.setOut(out);
75+
// create a temporary connection
76+
connectionId = "MY_CONNECTION_TEST_" + UUID.randomUUID().toString().substring(0, 8);
77+
String instanceId = String.format("%s:%s:%s", PROJECT_ID, REGION, MY_SQL_INSTANCE);
78+
CloudSqlCredential cloudSqlCredential =
79+
CloudSqlCredential.newBuilder().setUsername(DB_USER).setPassword(DB_PWD).build();
80+
CloudSqlProperties cloudSqlProperties =
81+
CloudSqlProperties.newBuilder()
82+
.setType(CloudSqlProperties.DatabaseType.MYSQL)
83+
.setDatabase(MY_SQL_DATABASE)
84+
.setInstanceId(instanceId)
85+
.setCredential(cloudSqlCredential)
86+
.build();
87+
Connection connection = Connection.newBuilder().setCloudSql(cloudSqlProperties).build();
88+
CreateConnection.createConnection(PROJECT_ID, LOCATION, connectionId, connection);
89+
}
90+
91+
@After
92+
public void tearDown() throws IOException {
93+
// Clean up
94+
DeleteConnection.deleteConnection(PROJECT_ID, LOCATION, connectionId);
95+
// restores print statements in the original method
96+
System.out.flush();
97+
System.setOut(originalPrintStream);
98+
LOG.log(Level.INFO, bout.toString());
99+
}
100+
101+
@Test
102+
public void testUpdateConnection() throws IOException {
103+
String description = "MY_DESCRIPTION";
104+
Connection connection = Connection.newBuilder().setDescription(description).build();
105+
UpdateConnection.updateConnection(PROJECT_ID, LOCATION, connectionId, connection);
106+
assertThat(bout.toString()).contains("Connection updated successfully :");
107+
}
108+
}

0 commit comments

Comments
 (0)