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

Commit 59fa9ae

Browse files
author
Praful Makani
authored
docs(samples): add create aws connection (#163)
1 parent 760c488 commit 59fa9ae

File tree

4 files changed

+160
-2
lines changed

4 files changed

+160
-2
lines changed

samples/install-without-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>com.google.cloud</groupId>
3131
<artifactId>google-cloud-bigqueryconnection</artifactId>
32-
<version>0.3.1</version>
32+
<version>0.4.0</version>
3333
</dependency>
3434
<!-- [END bigqueryconnection_install_without_bom] -->
3535

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<groupId>com.google.cloud</groupId>
4141
<artifactId>google-cloud-bigqueryconnection</artifactId>
4242
<!-- TODO: REMOVE VERSION SPECIFIED BELOW WHEN THE CLIENT GOES GA-->
43-
<version>0.3.1</version>
43+
<version>0.4.0</version>
4444
</dependency>
4545

4646
<dependency>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_aws_connection]
20+
import com.google.cloud.bigquery.connection.v1.AwsCrossAccountRole;
21+
import com.google.cloud.bigquery.connection.v1.AwsProperties;
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 aws connection
29+
public class CreateAwsConnection {
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+
// Note: As of now location only supports aws-us-east-1
35+
String location = "MY_LOCATION";
36+
String connectionId = "MY_CONNECTION_ID";
37+
// Example of role id arn:aws:iam::accountId:role/myrole
38+
String iamRoleId = "MY_AWS_ROLE_ID";
39+
AwsCrossAccountRole role = AwsCrossAccountRole.newBuilder().setIamRoleId(iamRoleId).build();
40+
AwsProperties awsProperties = AwsProperties.newBuilder().setCrossAccountRole(role).build();
41+
Connection connection = Connection.newBuilder().setAws(awsProperties).build();
42+
createAwsConnection(projectId, location, connectionId, connection);
43+
}
44+
45+
public static void createAwsConnection(
46+
String projectId, String location, String connectionId, Connection connection)
47+
throws IOException {
48+
try (ConnectionServiceClient client = ConnectionServiceClient.create()) {
49+
LocationName parent = LocationName.of(projectId, location);
50+
CreateConnectionRequest request =
51+
CreateConnectionRequest.newBuilder()
52+
.setParent(parent.toString())
53+
.setConnection(connection)
54+
.setConnectionId(connectionId)
55+
.build();
56+
Connection response = client.createConnection(request);
57+
AwsCrossAccountRole role = response.getAws().getCrossAccountRole();
58+
System.out.println(
59+
"Aws connection created successfully : Aws userId :"
60+
+ role.getIamUserId()
61+
+ " Aws externalId :"
62+
+ role.getExternalId());
63+
}
64+
}
65+
}
66+
// [END bigqueryconnection_create_aws_connection]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.AwsCrossAccountRole;
23+
import com.google.cloud.bigquery.connection.v1.AwsProperties;
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 CreateAwsConnectionIT {
37+
38+
private static final Logger LOG = Logger.getLogger(CreateAwsConnectionIT.class.getName());
39+
private static final String LOCATION = "aws-us-east-1";
40+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
41+
private static final String AWS_ACCOUNT_ID = requireEnvVar("AWS_ACCOUNT_ID");
42+
private static final String AWS_ROLE_ID = requireEnvVar("AWS_ROLE_ID");
43+
44+
private String connectionId;
45+
private ByteArrayOutputStream bout;
46+
private PrintStream out;
47+
private PrintStream originalPrintStream;
48+
49+
private static String requireEnvVar(String varName) {
50+
String value = System.getenv(varName);
51+
assertNotNull(
52+
"Environment variable " + varName + " is required to perform these tests.",
53+
System.getenv(varName));
54+
return value;
55+
}
56+
57+
@BeforeClass
58+
public static void checkRequirements() {
59+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
60+
requireEnvVar("AWS_ACCOUNT_ID");
61+
requireEnvVar("AWS_ROLE_ID");
62+
}
63+
64+
@Before
65+
public void setUp() {
66+
connectionId = "MY_CONNECTION_TEST_" + UUID.randomUUID().toString().substring(0, 8);
67+
bout = new ByteArrayOutputStream();
68+
out = new PrintStream(bout);
69+
originalPrintStream = System.out;
70+
System.setOut(out);
71+
}
72+
73+
@After
74+
public void tearDown() throws IOException {
75+
// Clean up
76+
DeleteConnection.deleteConnection(PROJECT_ID, LOCATION, connectionId);
77+
// restores print statements in the original method
78+
System.out.flush();
79+
System.setOut(originalPrintStream);
80+
LOG.log(Level.INFO, bout.toString());
81+
}
82+
83+
@Test
84+
public void testCreateAwsConnection() throws IOException {
85+
String iamRoleId = String.format("arn:aws:iam::%s:role/%s", AWS_ACCOUNT_ID, AWS_ROLE_ID);
86+
AwsCrossAccountRole role = AwsCrossAccountRole.newBuilder().setIamRoleId(iamRoleId).build();
87+
AwsProperties awsProperties = AwsProperties.newBuilder().setCrossAccountRole(role).build();
88+
Connection connection = Connection.newBuilder().setAws(awsProperties).build();
89+
CreateAwsConnection.createAwsConnection(PROJECT_ID, LOCATION, connectionId, connection);
90+
assertThat(bout.toString()).contains("Aws connection created successfully :");
91+
}
92+
}

0 commit comments

Comments
 (0)