From 59fa9aed468c9fa13e538ecdcff4c452d870ef49 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Fri, 9 Oct 2020 21:16:10 +0530 Subject: [PATCH] docs(samples): add create aws connection (#163) --- samples/install-without-bom/pom.xml | 2 +- samples/snippets/pom.xml | 2 +- .../CreateAwsConnection.java | 66 +++++++++++++ .../CreateAwsConnectionIT.java | 92 +++++++++++++++++++ 4 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/bigqueryconnection/CreateAwsConnection.java create mode 100644 samples/snippets/src/test/java/com/example/bigqueryconnection/CreateAwsConnectionIT.java diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index bdabf37d..44c3b4e0 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -29,7 +29,7 @@ com.google.cloud google-cloud-bigqueryconnection - 0.3.1 + 0.4.0 diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 7fc0dd66..9df94882 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -40,7 +40,7 @@ com.google.cloud google-cloud-bigqueryconnection - 0.3.1 + 0.4.0 diff --git a/samples/snippets/src/main/java/com/example/bigqueryconnection/CreateAwsConnection.java b/samples/snippets/src/main/java/com/example/bigqueryconnection/CreateAwsConnection.java new file mode 100644 index 00000000..072025b3 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigqueryconnection/CreateAwsConnection.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigqueryconnection; + +// [START bigqueryconnection_create_aws_connection] +import com.google.cloud.bigquery.connection.v1.AwsCrossAccountRole; +import com.google.cloud.bigquery.connection.v1.AwsProperties; +import com.google.cloud.bigquery.connection.v1.Connection; +import com.google.cloud.bigquery.connection.v1.CreateConnectionRequest; +import com.google.cloud.bigquery.connection.v1.LocationName; +import com.google.cloud.bigqueryconnection.v1.ConnectionServiceClient; +import java.io.IOException; + +// Sample to create aws connection +public class CreateAwsConnection { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + // Note: As of now location only supports aws-us-east-1 + String location = "MY_LOCATION"; + String connectionId = "MY_CONNECTION_ID"; + // Example of role id arn:aws:iam::accountId:role/myrole + String iamRoleId = "MY_AWS_ROLE_ID"; + AwsCrossAccountRole role = AwsCrossAccountRole.newBuilder().setIamRoleId(iamRoleId).build(); + AwsProperties awsProperties = AwsProperties.newBuilder().setCrossAccountRole(role).build(); + Connection connection = Connection.newBuilder().setAws(awsProperties).build(); + createAwsConnection(projectId, location, connectionId, connection); + } + + public static void createAwsConnection( + String projectId, String location, String connectionId, Connection connection) + throws IOException { + try (ConnectionServiceClient client = ConnectionServiceClient.create()) { + LocationName parent = LocationName.of(projectId, location); + CreateConnectionRequest request = + CreateConnectionRequest.newBuilder() + .setParent(parent.toString()) + .setConnection(connection) + .setConnectionId(connectionId) + .build(); + Connection response = client.createConnection(request); + AwsCrossAccountRole role = response.getAws().getCrossAccountRole(); + System.out.println( + "Aws connection created successfully : Aws userId :" + + role.getIamUserId() + + " Aws externalId :" + + role.getExternalId()); + } + } +} +// [END bigqueryconnection_create_aws_connection] diff --git a/samples/snippets/src/test/java/com/example/bigqueryconnection/CreateAwsConnectionIT.java b/samples/snippets/src/test/java/com/example/bigqueryconnection/CreateAwsConnectionIT.java new file mode 100644 index 00000000..a8c809d1 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigqueryconnection/CreateAwsConnectionIT.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.bigqueryconnection; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.connection.v1.AwsCrossAccountRole; +import com.google.cloud.bigquery.connection.v1.AwsProperties; +import com.google.cloud.bigquery.connection.v1.Connection; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class CreateAwsConnectionIT { + + private static final Logger LOG = Logger.getLogger(CreateAwsConnectionIT.class.getName()); + private static final String LOCATION = "aws-us-east-1"; + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + private static final String AWS_ACCOUNT_ID = requireEnvVar("AWS_ACCOUNT_ID"); + private static final String AWS_ROLE_ID = requireEnvVar("AWS_ROLE_ID"); + + private String connectionId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("AWS_ACCOUNT_ID"); + requireEnvVar("AWS_ROLE_ID"); + } + + @Before + public void setUp() { + connectionId = "MY_CONNECTION_TEST_" + UUID.randomUUID().toString().substring(0, 8); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // Clean up + DeleteConnection.deleteConnection(PROJECT_ID, LOCATION, connectionId); + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + LOG.log(Level.INFO, bout.toString()); + } + + @Test + public void testCreateAwsConnection() throws IOException { + String iamRoleId = String.format("arn:aws:iam::%s:role/%s", AWS_ACCOUNT_ID, AWS_ROLE_ID); + AwsCrossAccountRole role = AwsCrossAccountRole.newBuilder().setIamRoleId(iamRoleId).build(); + AwsProperties awsProperties = AwsProperties.newBuilder().setCrossAccountRole(role).build(); + Connection connection = Connection.newBuilder().setAws(awsProperties).build(); + CreateAwsConnection.createAwsConnection(PROJECT_ID, LOCATION, connectionId, connection); + assertThat(bout.toString()).contains("Aws connection created successfully :"); + } +}