-
Notifications
You must be signed in to change notification settings - Fork 9
samples: add code snippets for Service Directory. #28
Changes from 1 commit
c238cc5
98d371f
207ed6b
6fdee70
34007b0
21c8e5a
46ce0fc
3dd8ec2
a5ea611
5c99182
53862d1
dedd57d
aa2efb9
8f2256a
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,45 @@ | ||
| # 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. You can do so | ||
| [here](https://console.cloud.google.com/flows/enableapi?apiid=servicedirectory.googleapis.com&_ga=2.140387959.57242806.1585772225-360187285.1585772225). | ||
|
|
||
| ### Set Environment Variables | ||
|
|
||
| You must set your project ID in order to run the tests | ||
|
|
||
| `$ export GOOGLE_CLOUD_PROJECT=<your-project-id-here>` | ||
|
|
||
| ### 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](https://maven.apache.org/). | ||
|
|
||
| Build your project with: | ||
|
|
||
| mvn clean package -DskipTests | ||
|
|
||
| You can run all tests with: | ||
|
|
||
| mvn clean verify | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| // [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 { | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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"; | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| createEndpoint(projectId, locationId, namespaceId, serviceId, endpointId); | ||
| } | ||
|
|
||
| // Create a new endpoint. | ||
| public static void createEndpoint( | ||
| 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 service to create the endpoint in. | ||
| ServiceName parent = ServiceName.of(projectId, locationId, namespaceId, serviceId); | ||
|
|
||
| // Optionally set an IP address and port for the endpoint. | ||
| String address = "10.0.0.1"; | ||
| int port = 443; | ||
|
|
||
| // The endpoint to create, with fields filled in. | ||
| Endpoint endpoint = Endpoint.newBuilder().setAddress(address).setPort(port).build(); | ||
makk94 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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; | ||
|
|
||
| // [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); | ||
|
|
||
| // 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; | ||
|
|
||
| // [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(); | ||
|
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. Will the default instance typically be used or is it worth showing of some of the options in the sample?
Contributor
Author
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. I think the default instance will be pretty typical (there's only one field in this object that I don't think is too interesting) |
||
|
|
||
| // 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; | ||
|
|
||
| // [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 { | ||
|
|
||
| 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] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| // [START servicedirectory_create_service] | ||
|
|
||
| import com.google.cloud.servicedirectory.v1beta1.NamespaceName; | ||
| import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; | ||
| import com.google.cloud.servicedirectory.v1beta1.Service; | ||
| import java.io.IOException; | ||
|
|
||
| public class ServicesCreate { | ||
|
|
||
| public static void createService() 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"; | ||
| createService(projectId, locationId, namespaceId, serviceId); | ||
| } | ||
|
|
||
| // Create a new service. | ||
| public static void createService( | ||
| String projectId, String locationId, String namespaceId, String serviceId) | ||
| 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 create the service in. | ||
| NamespaceName parent = NamespaceName.of(projectId, locationId, namespaceId); | ||
|
|
||
| // The service object to create. Here, we use the default instance. | ||
| Service service = Service.newBuilder().build(); | ||
|
||
|
|
||
| // Send the request to create the namespace. | ||
| Service createdService = client.createService(parent, service, serviceId); | ||
|
|
||
| // Process the response. | ||
| System.out.println("Created Service: " + createdService.getName()); | ||
| } | ||
| } | ||
| } | ||
| // [END servicedirectory_create_service] | ||
Uh oh!
There was an error while loading. Please reload this page.