Skip to content

Commit 124386d

Browse files
hegemonicminherz
andauthored
feat(security-command-center): add sample for creating client with regional endpoint (GoogleCloudPlatform#9547)
* feat(security-command-center): add sample for creating client with regional endpoint The sample also shows how to create a client for the default endpoint. That's because SCC's regional endpoints support only a limited subset of SCC resources that are subject to data residency controls: https://cloud.google.com/security-command-center/docs/data-residency-support#scc-regional-urls-rest As a result, it's likely that customers will need to create two clients: one for their regional endpoint, to call the methods it supports, and another for the default endpoint, to call all other methods. * update package name * address comments * Apply suggestions from code review Co-authored-by: LeoY <minherz@users.noreply.github.com> * streamline sample; fix test Now that we're creating a single client, there's no need to put it in a `Map`. * update comment to reflect code changes --------- Co-authored-by: LeoY <minherz@users.noreply.github.com>
1 parent 51748e8 commit 124386d

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024 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 vtwo.client;
18+
19+
// [START securitycenter_set_client_endpoint_v2]
20+
21+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
22+
import com.google.cloud.securitycenter.v2.SecurityCenterSettings;
23+
import java.io.IOException;
24+
25+
public class CreateClientWithEndpoint {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO: Replace the value with the endpoint for the region in which your
29+
// Security Command Center data resides.
30+
String regionalEndpoint = "securitycenter.me-central2.rep.googleapis.com:443";
31+
SecurityCenterClient client = createClientWithEndpoint(regionalEndpoint);
32+
System.out.println("Client initiated with endpoint: " + client.getSettings().getEndpoint());
33+
}
34+
35+
// Creates Security Command Center client for a regional endpoint.
36+
public static SecurityCenterClient createClientWithEndpoint(String regionalEndpoint)
37+
throws java.io.IOException {
38+
SecurityCenterSettings regionalSettings =
39+
SecurityCenterSettings.newBuilder().setEndpoint(regionalEndpoint).build();
40+
return SecurityCenterClient.create(regionalSettings);
41+
}
42+
}
43+
44+
// [END securitycenter_set_client_endpoint_v2]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2024 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 vtwo;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
22+
import java.io.IOException;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
import vtwo.client.CreateClientWithEndpoint;
27+
28+
// Test v2 Create Client samples.
29+
@RunWith(JUnit4.class)
30+
public class CreateClientIT {
31+
32+
@Test
33+
public void testCreateClientWithEndpoint() throws IOException {
34+
SecurityCenterClient client =
35+
CreateClientWithEndpoint.createClientWithEndpoint(
36+
"securitycenter.me-central2.rep.googleapis.com:443");
37+
assertThat(client.getSettings().getEndpoint())
38+
.isEqualTo("securitycenter.me-central2.rep.googleapis.com:443");
39+
}
40+
}

0 commit comments

Comments
 (0)