Skip to content

Commit eadb6fa

Browse files
refactor: create a new ExperimentalHostHelper in samples package and update import references
1 parent 0d2152c commit eadb6fa

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

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.spanner;
18+
19+
import com.google.cloud.spanner.SpannerOptions;
20+
import com.google.common.base.Strings;
21+
22+
public class ExperimentalHostHelper {
23+
private static final String EXPERIMENTAL_HOST = "spanner.experimental_host";
24+
private static final String USE_PLAIN_TEXT = "spanner.use_plain_text";
25+
private static final String USE_MTLS = "spanner.mtls";
26+
private static final String CLIENT_CERT_PATH = "spanner.client_cert_path";
27+
private static final String CLIENT_CERT_KEY_PATH = "spanner.client_cert_key_path";
28+
29+
/**
30+
* Checks whether an experimental host is being used. This is done by checking if the
31+
* {@code spanner.experimental_host} system property is set.
32+
*
33+
* @return true if an experimental host is being used. Returns false otherwise.
34+
*/
35+
public static boolean isExperimentalHost() {
36+
return !Strings.isNullOrEmpty(System.getProperty(EXPERIMENTAL_HOST));
37+
}
38+
39+
public static boolean isMtlsSetup() {
40+
return Boolean.getBoolean(USE_MTLS);
41+
}
42+
43+
public static void setExperimentalHostSpannerOptions(SpannerOptions.Builder builder) {
44+
if (!isExperimentalHost()) {
45+
throw new IllegalStateException(EXPERIMENTAL_HOST + " must be set to use this method");
46+
}
47+
String experimentalHost = System.getProperty(EXPERIMENTAL_HOST, "");
48+
boolean usePlainText = Boolean.getBoolean(USE_PLAIN_TEXT);
49+
builder.setExperimentalHost(experimentalHost);
50+
builder.setBuiltInMetricsEnabled(false);
51+
if (usePlainText) {
52+
builder.usePlainText();
53+
}
54+
if (isMtlsSetup()) {
55+
String clientCertificate = System.getProperty(CLIENT_CERT_PATH, "");
56+
String clientKey = System.getProperty(CLIENT_CERT_KEY_PATH, "");
57+
if (Strings.isNullOrEmpty(clientCertificate)) {
58+
throw new IllegalArgumentException(CLIENT_CERT_PATH + " must be set when mTLS is enabled");
59+
}
60+
if (Strings.isNullOrEmpty(clientKey)) {
61+
throw new IllegalArgumentException(CLIENT_CERT_KEY_PATH + " must be set when mTLS is enabled");
62+
}
63+
builder.useClientCert(clientCertificate, clientKey);
64+
}
65+
}
66+
}

java-spanner/samples/snippets/src/main/java/com/example/spanner/PgSpannerSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.example.spanner;
1818

19-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.isExperimentalHost;
20-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
19+
import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
20+
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
2121

2222
import com.google.api.gax.paging.Page;
2323
import com.google.cloud.ByteArray;

java-spanner/samples/snippets/src/main/java/com/example/spanner/QueryInformationSchemaDatabaseOptionsSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
// [START spanner_query_information_schema_database_options]
2020

21-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.isExperimentalHost;
22-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
21+
import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
22+
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
2323

2424
import com.google.cloud.spanner.DatabaseClient;
2525
import com.google.cloud.spanner.DatabaseId;

java-spanner/samples/snippets/src/main/java/com/example/spanner/SpannerGraphSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.example.spanner;
1818

19-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.isExperimentalHost;
20-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
19+
import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
20+
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
2121

2222
import com.google.cloud.Timestamp;
2323
import com.google.cloud.spanner.DatabaseClient;

java-spanner/samples/snippets/src/main/java/com/example/spanner/SpannerSample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.example.spanner;
1818

19-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.isExperimentalHost;
20-
import static com.google.cloud.spanner.testing.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
19+
import static com.example.spanner.ExperimentalHostHelper.isExperimentalHost;
20+
import static com.example.spanner.ExperimentalHostHelper.setExperimentalHostSpannerOptions;
2121

2222
import static com.google.cloud.spanner.Type.StructField;
2323

0 commit comments

Comments
 (0)