diff --git a/vertex-ai/managed_notebook/README.md b/vertex-ai/managed_notebook/README.md new file mode 100644 index 00000000000..ddf152ac9ef --- /dev/null +++ b/vertex-ai/managed_notebook/README.md @@ -0,0 +1,61 @@ +## Vertex AI Workbench with Java Client Library + +This is a small sample to create a managed notebook using the Java SDK + +### Useful information + +Before compiling, open +java-docs-samples/vertex-ai/managed_notebook/src/main/java/com/gcp_aiml/samples/CreateRuntime.java and update the parameters at the top of the `public class CreateRuntime`. You will need to know these values: + +Project ID +Project Number + +You can find these values in the cloud console: + +![in the cloud console](img/project-view.png). + +In order to set a default load balancer provider, this maven package has a file +called io.grpc.LoadBalancerProvider in the java-docs-samples/vertex-ai/managed_notebook/src/main/resources/META-INF/services +directory with the value of $(eval echo +"io.grpc.internal.PickFirstLoadBalancerProvider") + +- https://stackoverflow.com/questions/55484043/how-to-fix-could-not-find-policy-pick-first-with-google-tts-java-client + + ```bash + mkdir -p src/main/resources/META-INF/services + echo "io.grpc.internal.PickFirstLoadBalancerProvider" > src/main/resources/META-INF/services/io.grpc.LoadBalancerProvider + ``` + + To compile the code, run the following command from this directory, which I + have called vertex-ai/managed_notebook + + `mvn clean compile` + +- [To include all dependencies in this .jar + file](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-runnable-jar-with-dependencies-using-maven), + you would run the following: + + `mvn assembly:single` + +Be sure to enable the API: + +```bash +gcloud services enable notebooks.googleapis.com +``` + +Be sure that you have granted the notebooks.runtimes.create permission to your +service account. The roles/notebooks.admin role includes this permission, but +may be too permissive. If this is the case, consider creating a new role with +only the permissions necessary. The following is an example of granting the +notebooks.admin role: + +```bash +gcloud projects add-iam-policy-binding ${PROJECT_ID} \ + --member="serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com" \ + --role="roles/notebooks.admin" +``` + +Once you have your project configured appropriately, you can launch the code +with: + +`java -jar target/managed_notebook-1.0-SNAPSHOT-jar-with-dependencies.jar` diff --git a/vertex-ai/managed_notebook/img/project-view.png b/vertex-ai/managed_notebook/img/project-view.png new file mode 100644 index 00000000000..68293214289 Binary files /dev/null and b/vertex-ai/managed_notebook/img/project-view.png differ diff --git a/vertex-ai/managed_notebook/pom.xml b/vertex-ai/managed_notebook/pom.xml new file mode 100644 index 00000000000..937ef16a507 --- /dev/null +++ b/vertex-ai/managed_notebook/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + com.gcp_aiml.samples + managed_notebook + 1.0-SNAPSHOT + + managed_notebook + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + com.google.cloud + google-cloud-notebooks + 1.4.0 + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + maven-assembly-plugin + + + + com.gcp_aiml.samples.CreateRuntime + + + + jar-with-dependencies + + + + + + + diff --git a/vertex-ai/managed_notebook/src/main/java/com/gcp_aiml/samples/CreateRuntime.java b/vertex-ai/managed_notebook/src/main/java/com/gcp_aiml/samples/CreateRuntime.java new file mode 100644 index 00000000000..32375b3db61 --- /dev/null +++ b/vertex-ai/managed_notebook/src/main/java/com/gcp_aiml/samples/CreateRuntime.java @@ -0,0 +1,78 @@ +package com.gcp_aiml.samples; + +/** Required imports */ +import com.google.cloud.notebooks.v1.CreateRuntimeRequest; +import com.google.cloud.notebooks.v1.LocalDisk; +import com.google.cloud.notebooks.v1.LocalDiskInitializeParams; +import com.google.cloud.notebooks.v1.LocalDiskInitializeParams.DiskType; +import com.google.cloud.notebooks.v1.ManagedNotebookServiceClient; +import com.google.cloud.notebooks.v1.Runtime; +import com.google.cloud.notebooks.v1.RuntimeAccessConfig; +import com.google.cloud.notebooks.v1.RuntimeAccessConfig.RuntimeAccessType; +import com.google.cloud.notebooks.v1.VirtualMachine; +import com.google.cloud.notebooks.v1.VirtualMachineConfig; + +/** Vertex AI Managed Notebook Sample */ +public class CreateRuntime { + // Tune these parameters for your sample + private static String serviceAccount = "000000000007-compute@developer.gserviceaccount.com"; + private static String parentProject = "projects/example-project-id/locations/us-central1"; + private static String runtimeId = "example-vertexai-notebook"; + private static String machineType = "n1-standard-4"; + + public static void main(String[] args) { + System.out.println("Creating Managed Notebook!"); + try { + syncCreateRuntime(); + } catch (Exception e) { + System.out.println(e); + } + } + + public static void syncCreateRuntime() throws Exception { + try (ManagedNotebookServiceClient managedNotebookServiceClient = + ManagedNotebookServiceClient.create()) { + + RuntimeAccessConfig accessConfig = + RuntimeAccessConfig.newBuilder() + .setRuntimeOwner(serviceAccount) + .setAccessType(RuntimeAccessType.SERVICE_ACCOUNT) + .build(); + + LocalDisk datDisk = + LocalDisk.newBuilder() + .setInitializeParams( + LocalDiskInitializeParams.newBuilder() + .setDiskSizeGb(100) + .setDiskType(DiskType.PD_STANDARD) + .build()) + .build(); + + VirtualMachineConfig machineConfig = + VirtualMachineConfig.newBuilder() + .setMachineType(machineType) + .setDataDisk(datDisk) + .build(); + + VirtualMachine virtualMachine = + VirtualMachine.newBuilder().setVirtualMachineConfig(machineConfig).build(); + + Runtime runtime = + Runtime.newBuilder() + .setAccessConfig(accessConfig) + .setVirtualMachine(virtualMachine) + .build(); + + CreateRuntimeRequest request = + CreateRuntimeRequest.newBuilder() + .setParent(parentProject) + .setRuntimeId(runtimeId) + .setRuntime(runtime) + .build(); + + Runtime response = managedNotebookServiceClient.createRuntimeAsync(request).get(); + + System.out.println(response); + } + } +} diff --git a/vertex-ai/managed_notebook/src/main/resources/META-INF/services/io.grpc.LoadBalancerProvider b/vertex-ai/managed_notebook/src/main/resources/META-INF/services/io.grpc.LoadBalancerProvider new file mode 100644 index 00000000000..bbc367f8fc5 --- /dev/null +++ b/vertex-ai/managed_notebook/src/main/resources/META-INF/services/io.grpc.LoadBalancerProvider @@ -0,0 +1 @@ +io.grpc.internal.PickFirstLoadBalancerProvider diff --git a/vertex-ai/managed_notebook/src/test/java/com/gcp_aiml/samples/AppTest.java b/vertex-ai/managed_notebook/src/test/java/com/gcp_aiml/samples/AppTest.java new file mode 100644 index 00000000000..68e746f779c --- /dev/null +++ b/vertex-ai/managed_notebook/src/test/java/com/gcp_aiml/samples/AppTest.java @@ -0,0 +1,20 @@ +package com.gcp_aiml.samples; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +}