Skip to content

Commit 49763a3

Browse files
Merged changes from main
2 parents 749b431 + bcecf8f commit 49763a3

File tree

8 files changed

+282
-49
lines changed

8 files changed

+282
-49
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/main/java/tpu/DeleteForceQueuedResource.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
//[START tpu_queued_resources_delete_force]
2020
import com.google.api.gax.retrying.RetrySettings;
21-
import com.google.api.gax.rpc.UnknownException;
2221
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
2322
import com.google.cloud.tpu.v2alpha1.TpuClient;
2423
import com.google.cloud.tpu.v2alpha1.TpuSettings;
@@ -66,14 +65,14 @@ public static void deleteForceQueuedResource(
6665
// once, and can be reused for multiple requests.
6766
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
6867
DeleteQueuedResourceRequest request =
69-
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
68+
DeleteQueuedResourceRequest.newBuilder().setName(name).setForce(true).build();
7069

70+
// Waiting for updates in the library. Until then, the operation will complete successfully,
71+
// but the user will receive an error message with UnknownException and IllegalStateException.
7172
tpuClient.deleteQueuedResourceAsync(request).get();
7273

73-
} catch (UnknownException e) {
74-
System.out.println(e.getMessage());
74+
System.out.printf("Deleted Queued Resource: %s\n", name);
7575
}
76-
System.out.printf("Deleted Queued Resource: %s\n", name);
7776
}
7877
}
7978
//[END tpu_queued_resources_delete_force]

tpu/src/main/java/tpu/DeleteQueuedResource.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
//[START tpu_queued_resources_delete]
2020
import com.google.api.gax.retrying.RetrySettings;
21-
import com.google.api.gax.rpc.UnknownException;
2221
import com.google.cloud.tpu.v2alpha1.DeleteQueuedResourceRequest;
2322
import com.google.cloud.tpu.v2alpha1.TpuClient;
2423
import com.google.cloud.tpu.v2alpha1.TpuSettings;
@@ -28,7 +27,6 @@
2827
// Uncomment this imports for getting node name
2928
//import com.google.cloud.tpu.v2alpha1.GetQueuedResourceRequest;
3029
//import com.google.cloud.tpu.v2alpha1.QueuedResource;
31-
//import java.util.concurrent.TimeUnit;
3230

3331
public class DeleteQueuedResource {
3432
public static void main(String[] args)
@@ -75,19 +73,16 @@ public static void deleteQueuedResource(String projectId, String zone, String qu
7573
//String nodeName = queuedResource.getTpu().getNodeSpec(0).getNode().getName();
7674
// For more information about deleting TPU
7775
// see https://cloud.google.com/tpu/docs/managing-tpus-tpu-vm
78-
//DeleteTpuVm.deleteTpuVm(projectId, zone, nodeName);
79-
// Wait until TpuVm is deleted
80-
//TimeUnit.MINUTES.sleep(3);
8176

8277
DeleteQueuedResourceRequest request =
83-
DeleteQueuedResourceRequest.newBuilder().setName(name).build();
78+
DeleteQueuedResourceRequest.newBuilder().setName(name).build();
8479

80+
// Waiting for updates in the library. Until then, the operation will complete successfully,
81+
// but the user will receive an error message with UnknownException and IllegalStateException.
8582
tpuClient.deleteQueuedResourceAsync(request).get();
8683

87-
} catch (UnknownException e) {
88-
System.out.println(e.getMessage());
84+
System.out.printf("Deleted Queued Resource: %s\n", name);
8985
}
90-
System.out.printf("Deleted Queued Resource: %s\n", name);
9186
}
9287
}
9388
//[END tpu_queued_resources_delete]

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
import java.io.IOException;
3737
import java.io.PrintStream;
3838
import java.util.concurrent.ExecutionException;
39-
import org.junit.Before;
40-
import org.junit.Test;
39+
import org.junit.jupiter.api.BeforeAll;
40+
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 QueuedResourceIT {
4949
private static final String PROJECT_ID = "project-id";
5050
private static final String ZONE = "europe-west4-a";
@@ -53,10 +53,10 @@ public class QueuedResourceIT {
5353
private static final String TPU_SOFTWARE_VERSION = "tpu-vm-tf-2.14.1";
5454
private static final String QUEUED_RESOURCE_NAME = "queued-resource";
5555
private static final String NETWORK_NAME = "default";
56-
private ByteArrayOutputStream bout;
56+
private static ByteArrayOutputStream bout;
5757

58-
@Before
59-
public void setUp() {
58+
@BeforeAll
59+
public static void setUp() {
6060
bout = new ByteArrayOutputStream();
6161
System.setOut(new PrintStream(bout));
6262
}
@@ -101,8 +101,8 @@ public void testCreateQueuedResourceWithSpecifiedNetwork() throws Exception {
101101

102102
QueuedResource returnedQueuedResource =
103103
CreateQueuedResourceWithNetwork.createQueuedResourceWithNetwork(
104-
PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME, NODE_NAME,
105-
TPU_TYPE, TPU_SOFTWARE_VERSION, NETWORK_NAME);
104+
PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME, NODE_NAME,
105+
TPU_TYPE, TPU_SOFTWARE_VERSION, NETWORK_NAME);
106106

107107
verify(mockTpuClient, times(1))
108108
.createQueuedResourceAsync(any(CreateQueuedResourceRequest.class));
@@ -132,23 +132,23 @@ public void testGetQueuedResource() throws IOException {
132132

133133
@Test
134134
public void testDeleteQueuedResource()
135-
throws ExecutionException, InterruptedException, IOException {
135+
throws IOException, ExecutionException, InterruptedException {
136136
try (MockedStatic<TpuClient> mockedTpuClient = mockStatic(TpuClient.class)) {
137137
TpuClient mockTpuClient = mock(TpuClient.class);
138138
OperationFuture mockFuture = mock(OperationFuture.class);
139139

140140
mockedTpuClient.when(() -> TpuClient.create(any(TpuSettings.class)))
141-
.thenReturn(mockTpuClient);
141+
.thenReturn(mockTpuClient);
142142
when(mockTpuClient.deleteQueuedResourceAsync(any(DeleteQueuedResourceRequest.class)))
143-
.thenReturn(mockFuture);
143+
.thenReturn(mockFuture);
144144
when(mockFuture.get()).thenReturn(null);
145145

146146
DeleteQueuedResource.deleteQueuedResource(PROJECT_ID, ZONE, QUEUED_RESOURCE_NAME);
147147
String output = bout.toString();
148148

149149
assertThat(output).contains("Deleted Queued Resource:");
150150
verify(mockTpuClient, times(1))
151-
.deleteQueuedResourceAsync(any(DeleteQueuedResourceRequest.class));
151+
.deleteQueuedResourceAsync(any(DeleteQueuedResourceRequest.class));
152152
}
153153
}
154154

0 commit comments

Comments
 (0)