Skip to content

Commit 2851016

Browse files
Merged changes from main
2 parents 68aa1e0 + bcecf8f commit 2851016

File tree

6 files changed

+263
-20
lines changed

6 files changed

+263
-20
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 dataplex;
18+
19+
// [START dataplex_search_entries]
20+
import com.google.cloud.dataplex.v1.CatalogServiceClient;
21+
import com.google.cloud.dataplex.v1.Entry;
22+
import com.google.cloud.dataplex.v1.SearchEntriesRequest;
23+
import com.google.cloud.dataplex.v1.SearchEntriesResult;
24+
import java.io.IOException;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
28+
public class SearchEntries {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "MY_PROJECT_ID";
33+
// How to write query for search: https://cloud.google.com/dataplex/docs/search-syntax
34+
String query = "MY_QUERY";
35+
36+
List<Entry> entries = searchEntries(projectId, query);
37+
entries.forEach(entry -> System.out.println("Entry name found in search: " + entry.getName()));
38+
}
39+
40+
// Method to search Entries located in projectId and matching query
41+
public static List<Entry> searchEntries(String projectId, String query) throws IOException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (CatalogServiceClient client = CatalogServiceClient.create()) {
45+
SearchEntriesRequest searchEntriesRequest =
46+
SearchEntriesRequest.newBuilder()
47+
.setPageSize(100)
48+
// Required field, will by default limit search scope to organization under which the
49+
// project is located
50+
.setName(String.format("projects/%s/locations/global", projectId))
51+
// Optional field, will further limit search scope only to specified project
52+
.setScope(String.format("projects/%s", projectId))
53+
.setQuery(query)
54+
.build();
55+
56+
CatalogServiceClient.SearchEntriesPagedResponse searchEntriesResponse =
57+
client.searchEntries(searchEntriesRequest);
58+
return searchEntriesResponse.getPage().getResponse().getResultsList().stream()
59+
// Extract Entries nested inside search results
60+
.map(SearchEntriesResult::getDataplexEntry)
61+
.collect(Collectors.toList());
62+
}
63+
}
64+
}
65+
// [END dataplex_search_entries]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 dataplex;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.dataplex.v1.Entry;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.UUID;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class SearchEntriesIT {
31+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
32+
private static final String LOCATION = "us-central1";
33+
private static final String entryGroupId = "test-entry-group-" + ID;
34+
private static final String entryId = "test-entry-" + ID;
35+
private static final String expectedEntry =
36+
String.format("locations/%s/entryGroups/%s/entries/%s", LOCATION, entryGroupId, entryId);
37+
38+
private static final String PROJECT_ID = requireProjectIdEnvVar();
39+
40+
private static String requireProjectIdEnvVar() {
41+
String value = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
assertNotNull(
43+
"Environment variable GOOGLE_CLOUD_PROJECT is required to perform these tests.", value);
44+
return value;
45+
}
46+
47+
@BeforeClass
48+
public static void setUp() throws Exception {
49+
requireProjectIdEnvVar();
50+
CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
51+
CreateEntry.createEntry(PROJECT_ID, LOCATION, entryGroupId, entryId);
52+
Thread.sleep(30000);
53+
}
54+
55+
@Test
56+
public void testSearchEntries() throws IOException {
57+
String query = "name:test-entry- AND description:description AND aspect:generic";
58+
List<Entry> entries = SearchEntries.searchEntries(PROJECT_ID, query);
59+
assertThat(
60+
entries.stream()
61+
.map(Entry::getName)
62+
.map(entryName -> entryName.substring(entryName.indexOf("location"))))
63+
.contains(expectedEntry);
64+
}
65+
66+
@AfterClass
67+
public static void tearDown() throws Exception {
68+
// Entry inside this Entry Group will be deleted automatically
69+
DeleteEntryGroup.deleteEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
70+
}
71+
}

functions/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
# Google Cloud Functions Java Samples
44

5-
[Cloud Functions][functions_docs] is a lightweight, event-based, asynchronous
6-
compute solution that allows you to create small, single-purpose functions that
7-
respond to Cloud events without the need to manage a server or a runtime
8-
environment.
5+
[Cloud Run functions](https://cloud.google.com/functions/docs/concepts/overview) is a lightweight, event-based, asynchronous compute solution that allows you to create small, single-purpose functions that respond to Cloud events without the need to manage a server or a runtime environment.
96

10-
[functions_docs]: https://cloud.google.com/functions/docs/
7+
There are two versions of Cloud Run functions:
8+
9+
* **Cloud Run functions**, formerly known as Cloud Functions (2nd gen), which deploys your function as services on Cloud Run, allowing you to trigger them using Eventarc and Pub/Sub. Cloud Run functions are created using `gcloud functions` or `gcloud run`. Samples for Cloud Run functions can be found in the [`functions/v2`](v2/) folder.
10+
* **Cloud Run functions (1st gen)**, formerly known as Cloud Functions (1st gen), the original version of functions with limited event triggers and configurability. Cloud Run functions (1st gen) are created using `gcloud functions --no-gen2`. Samples for Cloud Run functions (1st generation) can be found in the current `functions/` folder.
1111

1212
## Samples
1313

1414
* [Hello World](helloworld/)
15-
* [Concepts](concepts/)
15+
* [Concepts](v2/concepts/)
1616
* [Datastore](v2/datastore/)
1717
* [Firebase](firebase/)
18-
* [Cloud Pub/Sub](pubsub/)
18+
* [Cloud Pub/Sub](v2/pubsub/)
1919
* [HTTP](http/)
2020
* [Logging & Monitoring](logging/)
2121
* [Slack](slack/)
22-
* [OCR tutorial](ocr/)
23-
* [ImageMagick](imagemagick/)
22+
* [OCR tutorial](v2/ocr/)
23+
* [ImageMagick](v2/imagemagick/)
2424
* [CI/CD setup](ci_cd/)
2525

2626
## Running Functions Locally
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 tpu;
18+
19+
//[START tpu_vm_create_topology]
20+
import com.google.cloud.tpu.v2.AcceleratorConfig;
21+
import com.google.cloud.tpu.v2.AcceleratorConfig.Type;
22+
import com.google.cloud.tpu.v2.CreateNodeRequest;
23+
import com.google.cloud.tpu.v2.Node;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class CreateTpuWithTopologyFlag {
29+
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project you want to create a node.
34+
String projectId = "YOUR_PROJECT_ID";
35+
// The zone in which to create the TPU.
36+
// For more information about supported TPU types for specific zones,
37+
// see https://cloud.google.com/tpu/docs/regions-zones
38+
String zone = "europe-west4-a";
39+
// The name for your TPU.
40+
String nodeName = "YOUR_TPU_NAME";
41+
// The version of the Cloud TPU you want to create.
42+
// Available options: TYPE_UNSPECIFIED = 0, V2 = 2, V3 = 4, V4 = 7
43+
Type tpuVersion = AcceleratorConfig.Type.V2;
44+
// Software version that specifies the version of the TPU runtime to install.
45+
// For more information, see https://cloud.google.com/tpu/docs/runtimes
46+
String tpuSoftwareVersion = "tpu-vm-tf-2.17.0-pod-pjrt";
47+
// The physical topology of your TPU slice.
48+
// For more information about topology for each TPU version,
49+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
50+
String topology = "2x2";
51+
52+
createTpuWithTopologyFlag(projectId, zone, nodeName, tpuVersion, tpuSoftwareVersion, topology);
53+
}
54+
55+
// Creates a TPU VM with the specified name, zone, version and topology.
56+
public static Node createTpuWithTopologyFlag(String projectId, String zone, String nodeName,
57+
Type tpuVersion, String tpuSoftwareVersion, String topology)
58+
throws IOException, ExecutionException, InterruptedException {
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests.
61+
try (TpuClient tpuClient = TpuClient.create()) {
62+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
63+
Node tpuVm =
64+
Node.newBuilder()
65+
.setName(nodeName)
66+
.setAcceleratorConfig(Node.newBuilder()
67+
.getAcceleratorConfigBuilder()
68+
.setType(tpuVersion)
69+
.setTopology(topology)
70+
.build())
71+
.setRuntimeVersion(tpuSoftwareVersion)
72+
.build();
73+
74+
CreateNodeRequest request =
75+
CreateNodeRequest.newBuilder()
76+
.setParent(parent)
77+
.setNodeId(nodeName)
78+
.setNode(tpuVm)
79+
.build();
80+
81+
return tpuClient.createNodeAsync(request).get();
82+
}
83+
}
84+
}
85+
//[END tpu_vm_create_topology]

tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.mockito.MockedStatic;
4747

4848
@RunWith(JUnit4.class)
49-
@Timeout(value = 30)
49+
@Timeout(value = 10)
5050
public class QueuedResourceIT {
5151
private static final String PROJECT_ID = "project-id";
5252
private static final String ZONE = "europe-west4-a";

tpu/src/test/java/tpu/TpuVmIT.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.mockito.Mockito.when;
2727

2828
import com.google.api.gax.longrunning.OperationFuture;
29+
import com.google.cloud.tpu.v2.AcceleratorConfig;
2930
import com.google.cloud.tpu.v2.CreateNodeRequest;
3031
import com.google.cloud.tpu.v2.DeleteNodeRequest;
3132
import com.google.cloud.tpu.v2.GetNodeRequest;
@@ -36,28 +37,22 @@
3637
import java.io.IOException;
3738
import java.io.PrintStream;
3839
import java.util.concurrent.ExecutionException;
39-
import org.junit.jupiter.api.BeforeAll;
4040
import org.junit.jupiter.api.Test;
4141
import org.junit.jupiter.api.Timeout;
4242
import org.junit.runner.RunWith;
4343
import org.junit.runners.JUnit4;
4444
import org.mockito.MockedStatic;
4545

4646
@RunWith(JUnit4.class)
47-
@Timeout(value = 30)
47+
@Timeout(value = 10)
4848
public class TpuVmIT {
4949
private static final String PROJECT_ID = "project-id";
5050
private static final String ZONE = "asia-east1-c";
5151
private static final String NODE_NAME = "test-tpu";
5252
private static final String TPU_TYPE = "v2-8";
53-
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.12.1";
54-
private static ByteArrayOutputStream bout;
55-
56-
@BeforeAll
57-
public static void setUp() {
58-
bout = new ByteArrayOutputStream();
59-
System.setOut(new PrintStream(bout));
60-
}
53+
private static final AcceleratorConfig.Type ACCELERATOR_TYPE = AcceleratorConfig.Type.V2;
54+
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
55+
private static final String TOPOLOGY = "2x2";
6156

6257
@Test
6358
public void testCreateTpuVm() throws Exception {
@@ -102,6 +97,8 @@ public void testGetTpuVm() throws IOException {
10297

10398
@Test
10499
public void testDeleteTpuVm() throws IOException, ExecutionException, InterruptedException {
100+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
101+
System.setOut(new PrintStream(bout));
105102
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
106103
TpuClient mockTpuClient = mock(TpuClient.class);
107104
OperationFuture mockFuture = mock(OperationFuture.class);
@@ -116,6 +113,31 @@ public void testDeleteTpuVm() throws IOException, ExecutionException, Interrupte
116113

117114
assertThat(output).contains("TPU VM deleted");
118115
verify(mockTpuClient, times(1)).deleteNodeAsync(any(DeleteNodeRequest.class));
116+
117+
bout.close();
118+
}
119+
}
120+
121+
@Test
122+
public void testCreateTpuVmWithTopologyFlag()
123+
throws IOException, ExecutionException, InterruptedException {
124+
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
125+
Node mockNode = mock(Node.class);
126+
TpuClient mockTpuClient = mock(TpuClient.class);
127+
OperationFuture mockFuture = mock(OperationFuture.class);
128+
129+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
130+
when(mockTpuClient.createNodeAsync(any(CreateNodeRequest.class)))
131+
.thenReturn(mockFuture);
132+
when(mockFuture.get()).thenReturn(mockNode);
133+
Node returnedNode = CreateTpuWithTopologyFlag.createTpuWithTopologyFlag(
134+
PROJECT_ID, ZONE, NODE_NAME, ACCELERATOR_TYPE,
135+
TPU_SOFTWARE_VERSION, TOPOLOGY);
136+
137+
verify(mockTpuClient, times(1))
138+
.createNodeAsync(any(CreateNodeRequest.class));
139+
verify(mockFuture, times(1)).get();
140+
assertEquals(returnedNode, mockNode);
119141
}
120142
}
121143
}

0 commit comments

Comments
 (0)