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

Commit 39a1f45

Browse files
author
Praful Makani
authored
docs(samples): add create and delete connection (#151)
1 parent 1f90adc commit 39a1f45

File tree

4 files changed

+330
-0
lines changed

4 files changed

+330
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_create_connection]
20+
import com.google.cloud.bigquery.connection.v1.CloudSqlCredential;
21+
import com.google.cloud.bigquery.connection.v1.CloudSqlProperties;
22+
import com.google.cloud.bigquery.connection.v1.Connection;
23+
import com.google.cloud.bigquery.connection.v1.CreateConnectionRequest;
24+
import com.google.cloud.bigquery.connection.v1.LocationName;
25+
import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;
26+
import java.io.IOException;
27+
28+
// Sample to create a connection with cloud MySql database
29+
public class CreateConnection {
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 database = "MY_DATABASE";
37+
String instance = "MY_INSTANCE";
38+
String instanceLocation = "MY_INSTANCE_LOCATION";
39+
String username = "MY_USERNAME";
40+
String password = "MY_PASSWORD";
41+
String instanceId = String.format("%s:%s:%s", projectId, instanceLocation, instance);
42+
CloudSqlCredential cloudSqlCredential =
43+
CloudSqlCredential.newBuilder().setUsername(username).setPassword(password).build();
44+
CloudSqlProperties cloudSqlProperties =
45+
CloudSqlProperties.newBuilder()
46+
.setType(CloudSqlProperties.DatabaseType.MYSQL)
47+
.setDatabase(database)
48+
.setInstanceId(instanceId)
49+
.setCredential(cloudSqlCredential)
50+
.build();
51+
Connection connection = Connection.newBuilder().setCloudSql(cloudSqlProperties).build();
52+
createConnection(projectId, location, connectionId, connection);
53+
}
54+
55+
public static void createConnection(
56+
String projectId, String location, String connectionId, Connection connection)
57+
throws IOException {
58+
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
59+
LocationName parent = LocationName.of(projectId, location);
60+
CreateConnectionRequest request =
61+
CreateConnectionRequest.newBuilder()
62+
.setParent(parent.toString())
63+
.setConnection(connection)
64+
.setConnectionId(connectionId)
65+
.build();
66+
Connection response = client.createConnection(request);
67+
System.out.println("Connection created successfully :" + response.getName());
68+
}
69+
}
70+
}
71+
// [END bigqueryconnection_create_connection]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_delete_connection]
20+
import com.google.cloud.bigquery.connection.v1.ConnectionName;
21+
import com.google.cloud.bigquery.connection.v1.DeleteConnectionRequest;
22+
import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;
23+
import java.io.IOException;
24+
25+
// Sample to delete a connection
26+
public class DeleteConnection {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String location = "MY_LOCATION";
32+
String connectionName = "MY_CONNECTION_NAME";
33+
deleteConnection(projectId, location, connectionName);
34+
}
35+
36+
public static void deleteConnection(String projectId, String location, String connectionName)
37+
throws IOException {
38+
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
39+
ConnectionName name = ConnectionName.of(projectId, location, connectionName);
40+
DeleteConnectionRequest request =
41+
DeleteConnectionRequest.newBuilder().setName(name.toString()).build();
42+
client.deleteConnection(request);
43+
System.out.println("Connection deleted successfully");
44+
}
45+
}
46+
}
47+
// [END bigqueryconnection_delete_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 CreateConnectionIT {
37+
38+
private static final Logger LOG = Logger.getLogger(CreateConnectionIT.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() {
71+
connectionId = "MY_CONNECTION_TEST_" + UUID.randomUUID().toString().substring(0, 8);
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
originalPrintStream = System.out;
75+
System.setOut(out);
76+
}
77+
78+
@After
79+
public void tearDown() throws IOException {
80+
// Clean up
81+
DeleteConnection.deleteConnection(PROJECT_ID, LOCATION, connectionId);
82+
// restores print statements in the original method
83+
System.out.flush();
84+
System.setOut(originalPrintStream);
85+
LOG.log(Level.INFO, bout.toString());
86+
}
87+
88+
@Test
89+
public void testCreateConnection() throws IOException {
90+
String instanceId = String.format("%s:%s:%s", PROJECT_ID, REGION, MY_SQL_INSTANCE);
91+
CloudSqlCredential cloudSqlCredential =
92+
CloudSqlCredential.newBuilder().setUsername(DB_USER).setPassword(DB_PWD).build();
93+
CloudSqlProperties cloudSqlProperties =
94+
CloudSqlProperties.newBuilder()
95+
.setType(CloudSqlProperties.DatabaseType.MYSQL)
96+
.setDatabase(MY_SQL_DATABASE)
97+
.setInstanceId(instanceId)
98+
.setCredential(cloudSqlCredential)
99+
.build();
100+
Connection connection =
101+
Connection.newBuilder()
102+
.setFriendlyName(connectionId)
103+
.setCloudSql(cloudSqlProperties)
104+
.build();
105+
CreateConnection.createConnection(PROJECT_ID, LOCATION, connectionId, connection);
106+
assertThat(bout.toString()).contains("Connection created successfully :");
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 DeleteConnectionIT {
37+
38+
private static final Logger LOG = Logger.getLogger(DeleteConnectionIT.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() {
93+
// restores print statements in the original method
94+
System.out.flush();
95+
System.setOut(originalPrintStream);
96+
LOG.log(Level.INFO, bout.toString());
97+
}
98+
99+
@Test
100+
public void testDeleteConnection() throws IOException {
101+
DeleteConnection.deleteConnection(PROJECT_ID, LOCATION, connectionId);
102+
assertThat(bout.toString()).contains("Connection deleted successfully");
103+
}
104+
}

0 commit comments

Comments
 (0)