diff --git a/java-servicedirectory/README.md b/java-servicedirectory/README.md index 510e03b3b935..beb5dd58a0d0 100644 --- a/java-servicedirectory/README.md +++ b/java-servicedirectory/README.md @@ -107,22 +107,6 @@ use this Service Directory Client Library. -## Samples - -Samples are in the [`samples/`](https://github.com/googleapis/java-servicedirectory/tree/main/samples) directory. - -| Sample | Source Code | Try it | -| --------------------------- | --------------------------------- | ------ | -| Endpoints Create | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsCreate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/EndpointsCreate.java) | -| Endpoints Delete | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsDelete.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/EndpointsDelete.java) | -| Namespaces Create | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesCreate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/NamespacesCreate.java) | -| Namespaces Delete | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesDelete.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/NamespacesDelete.java) | -| Quickstart | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/Quickstart.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/Quickstart.java) | -| Services Create | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/ServicesCreate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/ServicesCreate.java) | -| Services Delete | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/ServicesDelete.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/ServicesDelete.java) | -| Services Resolve | [source code](https://github.com/googleapis/java-servicedirectory/blob/main/samples/snippets/src/main/java/com/example/servicedirectory/ServicesResolve.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-servicedirectory&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/servicedirectory/ServicesResolve.java) | - - ## Troubleshooting diff --git a/java-servicedirectory/samples/snippets/README.md b/java-servicedirectory/samples/snippets/README.md deleted file mode 100644 index fe368e2a2b48..000000000000 --- a/java-servicedirectory/samples/snippets/README.md +++ /dev/null @@ -1,46 +0,0 @@ -# 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=` - -### 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 -Service Directory 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 diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsCreate.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsCreate.java deleted file mode 100644 index ba164213554a..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsCreate.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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.v1.Endpoint; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.ServiceName; -import java.io.IOException; - -public class EndpointsCreate { - - public static void createEndpoint() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory service. - String projectId = "your-project-id"; - String locationId = "your-region"; - String namespaceId = "your-namespace"; - String serviceId = "your-service"; - // This is user-created; must be unique within the service above. - String endpointId = "your-endpoint"; - 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); - - // The endpoint to create, with fields filled in. - // Optionally set an IP address and port for the endpoint. - Endpoint endpoint = Endpoint.newBuilder().setAddress("10.0.0.1").setPort(443).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] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsDelete.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsDelete.java deleted file mode 100644 index 378654e0f3fb..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/EndpointsDelete.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.v1.EndpointName; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import java.io.IOException; - -public class EndpointsDelete { - - public static void deleteEndpoint() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory endpoint. - 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] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesCreate.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesCreate.java deleted file mode 100644 index 492fd9d9c56a..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesCreate.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.v1.LocationName; -import com.google.cloud.servicedirectory.v1.Namespace; -import com.google.cloud.servicedirectory.v1.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"; - // This is user-created; must be unique within the project/region above. - 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] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesDelete.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesDelete.java deleted file mode 100644 index c3414d8013a1..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/NamespacesDelete.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.v1.NamespaceName; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import java.io.IOException; - -public class NamespacesDelete { - - public static void deleteNamespace() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory namespace. - 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] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/Quickstart.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/Quickstart.java deleted file mode 100644 index d9d7ae393a41..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/Quickstart.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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_quickstart] - -import com.google.cloud.servicedirectory.v1.LocationName; -import com.google.cloud.servicedirectory.v1.Namespace; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse; -import java.io.IOException; - -public class Quickstart { - - public static void quickstart() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "your-project-id"; - String locationId = "your-region"; - quickstart(projectId, locationId); - } - - public static void quickstart(String projectId, String locationId) 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 that hold the namespace to list. - LocationName parent = LocationName.of(projectId, locationId); - - // Call the API. - ListNamespacesPagedResponse response = client.listNamespaces(parent); - - // Iterate over each namespace and print its name. - System.out.println("Namespaces:"); - for (Namespace namespace : response.iterateAll()) { - System.out.println(namespace.getName()); - } - } - } -} -// [END servicedirectory_quickstart] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesCreate.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesCreate.java deleted file mode 100644 index 90a812d7d2da..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesCreate.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.v1.NamespaceName; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.Service; -import java.io.IOException; - -public class ServicesCreate { - - public static void createService() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory namespace. - String projectId = "your-project-id"; - String locationId = "your-region"; - String namespaceId = "your-namespace"; - // This is user-created; must be unique within the namespace above. - 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. - // Optionally add some annotations for the service. - Service service = Service.newBuilder().putAnnotations("protocol", "tcp").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()); - System.out.println("Annotations: " + createdService.getAnnotations()); - } - } -} -// [END servicedirectory_create_service] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesDelete.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesDelete.java deleted file mode 100644 index 3871e902a154..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesDelete.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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_service] - -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.ServiceName; -import java.io.IOException; - -public class ServicesDelete { - - public static void deleteService() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory service. - String projectId = "your-project-id"; - String locationId = "your-region"; - String namespaceId = "your-namespace"; - String serviceId = "your-service"; - deleteService(projectId, locationId, namespaceId, serviceId); - } - - // Delete a service. - public static void deleteService( - 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 service to delete. - ServiceName serviceName = ServiceName.of(projectId, locationId, namespaceId, serviceId); - - // Send the request to delete the service. - client.deleteService(serviceName); - - // Log the action. - System.out.println("Deleted Service: " + serviceName.toString()); - } - } -} -// [END servicedirectory_delete_service] diff --git a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesResolve.java b/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesResolve.java deleted file mode 100644 index a170227d3800..000000000000 --- a/java-servicedirectory/samples/snippets/src/main/java/com/example/servicedirectory/ServicesResolve.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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_resolve_service] - -import com.google.cloud.servicedirectory.v1.Endpoint; -import com.google.cloud.servicedirectory.v1.LookupServiceClient; -import com.google.cloud.servicedirectory.v1.ResolveServiceRequest; -import com.google.cloud.servicedirectory.v1.ResolveServiceResponse; -import com.google.cloud.servicedirectory.v1.ServiceName; -import java.io.IOException; - -public class ServicesResolve { - - public static void resolveService() throws IOException { - // TODO(developer): Replace these variables before running the sample. - // These variables should refer to an existing Service Directory service. - String projectId = "your-project-id"; - String locationId = "your-region"; - String namespaceId = "your-namespace"; - String serviceId = "your-service"; - resolveService(projectId, locationId, namespaceId, serviceId); - } - - // Resolve a service. - public static void resolveService( - 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 (LookupServiceClient client = LookupServiceClient.create()) { - // The service to resolve. - ServiceName name = ServiceName.of(projectId, locationId, namespaceId, serviceId); - - // Construct the resolve request to be sent to the client. - ResolveServiceRequest request = - ResolveServiceRequest.newBuilder().setName(name.toString()).build(); - - // Send the request to resolve the service. - ResolveServiceResponse resolveResponse = client.resolveService(request); - - // Process the response. - System.out.println("Resolved Service: " + resolveResponse.getService().getName()); - - System.out.println("Endpoints found:"); - for (Endpoint endpoint : resolveResponse.getService().getEndpointsList()) { - System.out.println( - endpoint.getName() + " -- " + endpoint.getAddress() + ":" + endpoint.getPort()); - } - } - } -} -// [END servicedirectory_resolve_service] diff --git a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/EndpointsTests.java b/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/EndpointsTests.java deleted file mode 100644 index 01f960a2316b..000000000000 --- a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/EndpointsTests.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import com.google.cloud.servicedirectory.v1.EndpointName; -import com.google.cloud.servicedirectory.v1.LocationName; -import com.google.cloud.servicedirectory.v1.Namespace; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class EndpointsTests { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String LOCATION_ID = "us-central1"; - private static final String NAMESPACE_ID = "namespace-" + UUID.randomUUID().toString(); - private static final String SERVICE_ID = "service-" + UUID.randomUUID().toString(); - private static final String ENDPOINT_ID = "endpoint-" + UUID.randomUUID().toString(); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Before - public void setUp() throws Exception { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - - // Setup: create a namespace for the endpoints to live in. - NamespacesCreate.createNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - - // Setup: create a service for the endpoints to live in. - ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - } - - @After - public void tearDown() throws Exception { - System.setOut(null); - bout.reset(); - - // Deletes all resources created during these tests. - try (RegistrationServiceClient client = RegistrationServiceClient.create()) { - // List the namespaces. - String locationPath = LocationName.format(PROJECT_ID, LOCATION_ID); - ListNamespacesPagedResponse response = client.listNamespaces(locationPath); - - // Delete each namespace. - for (Namespace namespace : response.iterateAll()) { - client.deleteNamespace(namespace.getName()); - } - } - } - - @Test - public void testCreateEndpoint() throws Exception { - EndpointsCreate.createEndpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID); - String endpointName = - EndpointName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Created Endpoint: %s", endpointName))); - } - - @Test - public void testDeleteService() throws Exception { - // Setup: create an endpoint. - EndpointsCreate.createEndpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID); - String endpointName = - EndpointName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID); - - // Delete the endpoint with the specified ID. - EndpointsDelete.deleteEndpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Deleted Endpoint: %s", endpointName))); - } -} diff --git a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/NamespacesTests.java b/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/NamespacesTests.java deleted file mode 100644 index 36f54b06d26a..000000000000 --- a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/NamespacesTests.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import com.google.cloud.servicedirectory.v1.LocationName; -import com.google.cloud.servicedirectory.v1.Namespace; -import com.google.cloud.servicedirectory.v1.NamespaceName; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class NamespacesTests { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String LOCATION_ID = "us-central1"; - private static final String NAMESPACE_ID = "namespace-" + UUID.randomUUID().toString(); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @After - public void tearDown() throws Exception { - System.setOut(null); - bout.reset(); - - // Deletes any namespaces created during these tests. - try (RegistrationServiceClient client = RegistrationServiceClient.create()) { - // List the namespaces. - String locationPath = LocationName.format(PROJECT_ID, LOCATION_ID); - ListNamespacesPagedResponse response = client.listNamespaces(locationPath); - - // Delete each namespace. - for (Namespace ns : response.iterateAll()) { - client.deleteNamespace(ns.getName()); - } - } - } - - @Test - public void testCreateNamespace() throws Exception { - NamespacesCreate.createNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - String namespaceName = NamespaceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Created Namespace: %s", namespaceName))); - } - - @Test - public void testDeleteNamespace() throws Exception { - // Setup: create a namespace. - NamespacesCreate.createNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - String namespaceName = NamespaceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - - // Delete the namespace with the specified ID. - NamespacesDelete.deleteNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Deleted Namespace: %s", namespaceName))); - } -} diff --git a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/ServicesTests.java b/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/ServicesTests.java deleted file mode 100644 index b9642866402b..000000000000 --- a/java-servicedirectory/samples/snippets/src/test/java/com/example/servicedirectory/ServicesTests.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import com.google.cloud.servicedirectory.v1.EndpointName; -import com.google.cloud.servicedirectory.v1.LocationName; -import com.google.cloud.servicedirectory.v1.Namespace; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient; -import com.google.cloud.servicedirectory.v1.RegistrationServiceClient.ListNamespacesPagedResponse; -import com.google.cloud.servicedirectory.v1.ServiceName; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ServicesTests { - - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String LOCATION_ID = "us-central1"; - private static final String NAMESPACE_ID = "namespace-" + UUID.randomUUID().toString(); - private static final String SERVICE_ID = "service-" + UUID.randomUUID().toString(); - - private ByteArrayOutputStream bout; - - private static void requireEnvVar(String varName) { - assertNotNull( - String.format("Environment variable '%s' must be set to perform these tests.", varName), - System.getenv(varName)); - } - - @BeforeClass - public static void checkRequirements() { - requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); - requireEnvVar("GOOGLE_CLOUD_PROJECT"); - } - - @Before - public void setUp() throws Exception { - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - - // Setup: create a namespace for the services to live in. - NamespacesCreate.createNamespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID); - } - - @After - public void tearDown() throws Exception { - System.setOut(null); - bout.reset(); - - // Deletes all resources created during these tests. - try (RegistrationServiceClient client = RegistrationServiceClient.create()) { - // List the namespaces. - String locationPath = LocationName.format(PROJECT_ID, LOCATION_ID); - ListNamespacesPagedResponse response = client.listNamespaces(locationPath); - - // Delete each namespace. - for (Namespace namespace : response.iterateAll()) { - client.deleteNamespace(namespace.getName()); - } - } - } - - @Test - public void testCreateService() throws Exception { - ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Created Service: %s", serviceName))); - } - - @Test - public void testResolveService() throws Exception { - // Setup: create a service. - ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - // Setup: Create an endpoint in the service. - EndpointsCreate.createEndpoint( - PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, "default-endpoint"); - String endpointName = - EndpointName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, "default-endpoint"); - - // Resolve the service with the specified ID. - ServicesResolve.resolveService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Resolved Service: %s", serviceName))); - assertThat( - output, CoreMatchers.containsString(String.format("Endpoints found:\n%s", endpointName))); - } - - @Test - public void testDeleteService() throws Exception { - // Setup: create a service. - ServicesCreate.createService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String serviceName = ServiceName.format(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - - // Delete the service with the specified ID. - ServicesDelete.deleteService(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID); - String output = bout.toString(); - assertThat( - output, CoreMatchers.containsString(String.format("Deleted Service: %s", serviceName))); - } -}