-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add java samples for Service Directory #2538
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Service Directory | ||
|
||
[Service Directory](https://cloud.google.com/service-directory/) is a platform | ||
for discovering, publishing, and connecting services. It offers customers a | ||
single place to register and discover their services in a consistent and | ||
reliable way, regardless of their environment. These sample Java applications | ||
demonstrate how to access the Service Directory API using the Google Java API | ||
Client Libraries. | ||
|
||
## Prerequisites | ||
|
||
### Enable the API | ||
|
||
You must enable the Service Directory API for your project in order to use these | ||
samples. | ||
|
||
### Set Environment Variables | ||
|
||
You must set your project ID in order to run the tests | ||
|
||
`$export GOOGLE_CLOUD_PROJECT=<your-project-id-here>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
### Grant Permissions | ||
|
||
You must ensure that the | ||
[user account or service account](https://cloud.google.com/iam/docs/service-accounts#differences_between_a_service_account_and_a_user_account) | ||
you used to authorize your gcloud session has the proper permissions to edit KMS | ||
resources for your project. In the Cloud Console under IAM, add the `Service | ||
Directory Admin` role to the project whose service account you're using to test. | ||
|
||
More information can be found in the | ||
[Authentication docs](https://cloud.google.com/docs/authentication/production). | ||
|
||
## Quickstart | ||
|
||
Install [Maven](http://maven.apache.org/). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https |
||
|
||
Build your project with: | ||
|
||
mvn clean compile assembly:single | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are you having them run things? You can use the exec:java plugin. |
||
|
||
You can run all tests with: | ||
|
||
mvn clean verify |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||||||||||||||||||||||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add License Header:
Suggested change
|
||||||||||||||||||||||||||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||||||||||||||||||||||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||||||||||||||||||||||||||||||
<modelVersion>4.0.0</modelVersion> | ||||||||||||||||||||||||||||||
<packaging>jar</packaging> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<groupId>com.google.cloud.servicedirectory.samples</groupId> | ||||||||||||||||||||||||||||||
makk94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
<artifactId>servicedirectory-samples</artifactId> | ||||||||||||||||||||||||||||||
<version>1.0</version> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<!-- | ||||||||||||||||||||||||||||||
The parent pom defines common style checks and testing strategies for our samples. | ||||||||||||||||||||||||||||||
Removing or replacing it should not affect the execution of the samples in anyway. | ||||||||||||||||||||||||||||||
--> | ||||||||||||||||||||||||||||||
<parent> | ||||||||||||||||||||||||||||||
<groupId>com.google.cloud.samples</groupId> | ||||||||||||||||||||||||||||||
<artifactId>shared-configuration</artifactId> | ||||||||||||||||||||||||||||||
<version>1.0.13</version> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.0.14 |
||||||||||||||||||||||||||||||
</parent> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<properties> | ||||||||||||||||||||||||||||||
<maven.compiler.source>1.8</maven.compiler.source> | ||||||||||||||||||||||||||||||
<maven.compiler.target>1.8</maven.compiler.target> | ||||||||||||||||||||||||||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||||||||||||||||||||||||||||||
</properties> | ||||||||||||||||||||||||||||||
<dependencyManagement> | ||||||||||||||||||||||||||||||
<dependencies> | ||||||||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||||||||
<groupId>com.google.cloud</groupId> | ||||||||||||||||||||||||||||||
<artifactId>libraries-bom</artifactId> | ||||||||||||||||||||||||||||||
<version>4.1.0</version> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4.3.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4.4.0 |
||||||||||||||||||||||||||||||
<type>pom</type> | ||||||||||||||||||||||||||||||
<scope>import</scope> | ||||||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||||||
</dependencies> | ||||||||||||||||||||||||||||||
</dependencyManagement> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<dependencies> | ||||||||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||||||||
<groupId>com.google.cloud</groupId> | ||||||||||||||||||||||||||||||
<artifactId>google-cloud-servicedirectory</artifactId> | ||||||||||||||||||||||||||||||
<version>0.1.0</version> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elharo, are new libraries automatically added to the bom or is there a process to request them to be added? |
||||||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||||||||
<groupId>args4j</groupId> | ||||||||||||||||||||||||||||||
<artifactId>args4j</artifactId> | ||||||||||||||||||||||||||||||
<version>2.33</version> | ||||||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||||||
<!-- test dependencies --> | ||||||||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||||||||
<groupId>junit</groupId> | ||||||||||||||||||||||||||||||
<artifactId>junit</artifactId> | ||||||||||||||||||||||||||||||
<version>4.13</version> | ||||||||||||||||||||||||||||||
<scope>test</scope> | ||||||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||||||
<dependency> | ||||||||||||||||||||||||||||||
<groupId>com.google.truth</groupId> | ||||||||||||||||||||||||||||||
<artifactId>truth</artifactId> | ||||||||||||||||||||||||||||||
<version>1.0</version> | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.0.1 |
||||||||||||||||||||||||||||||
<scope>test</scope> | ||||||||||||||||||||||||||||||
</dependency> | ||||||||||||||||||||||||||||||
</dependencies> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
<build> | ||||||||||||||||||||||||||||||
<sourceDirectory>src/main/java</sourceDirectory> | ||||||||||||||||||||||||||||||
<plugins> | ||||||||||||||||||||||||||||||
<plugin> | ||||||||||||||||||||||||||||||
<groupId>org.apache.maven.plugins</groupId> | ||||||||||||||||||||||||||||||
<artifactId>maven-compiler-plugin</artifactId> | ||||||||||||||||||||||||||||||
</plugin> | ||||||||||||||||||||||||||||||
<plugin> | ||||||||||||||||||||||||||||||
<artifactId>maven-assembly-plugin</artifactId> | ||||||||||||||||||||||||||||||
<configuration> | ||||||||||||||||||||||||||||||
<descriptorRefs> | ||||||||||||||||||||||||||||||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||||||||||||||||||||||||||||||
</descriptorRefs> | ||||||||||||||||||||||||||||||
</configuration> | ||||||||||||||||||||||||||||||
</plugin> | ||||||||||||||||||||||||||||||
</plugins> | ||||||||||||||||||||||||||||||
</build> | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.servicedirectory.snippets; | ||
|
||
// [START servicedirectory_create_endpoint] | ||
|
||
import com.google.cloud.servicedirectory.v1beta1.Endpoint; | ||
import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; | ||
import com.google.cloud.servicedirectory.v1beta1.ServiceName; | ||
import java.io.IOException; | ||
|
||
public class EndpointsCreate { | ||
|
||
public static void createEndpoint() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-region"; | ||
String namespaceId = "your-namespace"; | ||
String serviceId = "your-service"; | ||
String endpointId = "your-endpoint"; | ||
String address = "10.0.0.1"; | ||
int port = 443; | ||
Comment on lines
+30
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can any of these be inlined below or will the user likely need to set these every time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only project id needs to be set by the user for the call to go through. I can make the executive decision for the other fields since it's just an example - is that preferred? (In general, the port will not usually be 443, it could be anything) |
||
createEndpoint(projectId, locationId, namespaceId, serviceId, endpointId, address, port); | ||
} | ||
|
||
// Create a new endpoint. | ||
public static void createEndpoint( | ||
String projectId, | ||
String locationId, | ||
String namespaceId, | ||
String serviceId, | ||
String endpointId, | ||
String address, | ||
int port) | ||
throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (RegistrationServiceClient client = RegistrationServiceClient.create()) { | ||
|
||
// The service to create the endpoint in. | ||
ServiceName parent = ServiceName.of(projectId, locationId, namespaceId, serviceId); | ||
|
||
// The endpoint to create, with fields filled in. | ||
Endpoint endpoint = Endpoint.newBuilder().setAddress(address).setPort(port).build(); | ||
|
||
// Send the request to create the endpoint. | ||
Endpoint createdEndpoint = client.createEndpoint(parent, endpoint, endpointId); | ||
|
||
// Process the response. | ||
System.out.println("Created Endpoint: " + createdEndpoint.getName()); | ||
System.out.println("IP Address: " + createdEndpoint.getAddress()); | ||
System.out.println("Port: " + createdEndpoint.getPort()); | ||
} | ||
} | ||
} | ||
// [END servicedirectory_create_endpoint] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.servicedirectory.snippets; | ||
|
||
// [START servicedirectory_delete_endpoint] | ||
|
||
import com.google.cloud.servicedirectory.v1beta1.EndpointName; | ||
import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; | ||
import java.io.IOException; | ||
|
||
public class EndpointsDelete { | ||
|
||
public static void deleteEndpoint() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-region"; | ||
String namespaceId = "your-namespace"; | ||
String serviceId = "your-service"; | ||
String endpointId = "your-endpoint"; | ||
deleteEndpoint(projectId, locationId, namespaceId, serviceId, endpointId); | ||
} | ||
|
||
// Delete an endpoint. | ||
public static void deleteEndpoint( | ||
String projectId, String locationId, String namespaceId, String serviceId, String endpointId) | ||
throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (RegistrationServiceClient client = RegistrationServiceClient.create()) { | ||
|
||
// The endpoint to delete. | ||
EndpointName endpointName = | ||
EndpointName.of(projectId, locationId, namespaceId, serviceId, endpointId); | ||
|
||
// Send the request to delete the endpoint. | ||
client.deleteEndpoint(endpointName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a long running operation or does it just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it returns Empty; it's not a LRO |
||
|
||
// Log the action. | ||
System.out.println("Deleted Endpoint: " + endpointName.toString()); | ||
} | ||
} | ||
} | ||
// [END servicedirectory_delete_endpoint] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.servicedirectory.snippets; | ||
|
||
// [START servicedirectory_create_namespace] | ||
|
||
import com.google.cloud.servicedirectory.v1beta1.LocationName; | ||
import com.google.cloud.servicedirectory.v1beta1.Namespace; | ||
import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; | ||
import java.io.IOException; | ||
|
||
public class NamespacesCreate { | ||
|
||
public static void createNamespace() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-region"; | ||
String namespaceId = "your-namespace"; | ||
createNamespace(projectId, locationId, namespaceId); | ||
} | ||
|
||
// Create a new namespace. | ||
public static void createNamespace(String projectId, String locationId, String namespaceId) | ||
throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (RegistrationServiceClient client = RegistrationServiceClient.create()) { | ||
|
||
// The project and location to create the namespace in. | ||
LocationName parent = LocationName.of(projectId, locationId); | ||
|
||
// The namespace object to create. Here, we use the default instance. | ||
Namespace namespace = Namespace.newBuilder().build(); | ||
|
||
// Send the request to create the namespace. | ||
Namespace createdNamespace = client.createNamespace(parent, namespace, namespaceId); | ||
|
||
// Process the response. | ||
System.out.println("Created Namespace: " + createdNamespace.getName()); | ||
} | ||
} | ||
} | ||
// [END servicedirectory_create_namespace] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.servicedirectory.snippets; | ||
|
||
// [START servicedirectory_delete_namespace] | ||
|
||
import com.google.cloud.servicedirectory.v1beta1.NamespaceName; | ||
import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; | ||
import java.io.IOException; | ||
|
||
public class NamespacesDelete { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a strange classname. Classes are not usually verbs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is in accordance with what the canonical example looks like at https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/dlp/src/main/java/dlp/snippets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. |
||
|
||
public static void deleteNamespace() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "your-project-id"; | ||
String locationId = "your-region"; | ||
String namespaceId = "your-namespace"; | ||
deleteNamespace(projectId, locationId, namespaceId); | ||
} | ||
|
||
// Delete a namespace. | ||
public static void deleteNamespace(String projectId, String locationId, String namespaceId) | ||
throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (RegistrationServiceClient client = RegistrationServiceClient.create()) { | ||
|
||
// The namespace to delete. | ||
NamespaceName namespaceName = NamespaceName.of(projectId, locationId, namespaceId); | ||
|
||
// Send the request to delete the namespace. | ||
client.deleteNamespace(namespaceName); | ||
|
||
// Log the action. | ||
System.out.println("Deleted Namespace: " + namespaceName.toString()); | ||
} | ||
} | ||
} | ||
// [END servicedirectory_delete_namespace] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Link or gcloud command to enable the API?