|
| 1 | +/* |
| 2 | + * Copyright 2020 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 com.example.servicedirectory; |
| 18 | + |
| 19 | +// [START servicedirectory_quickstart] |
| 20 | + |
| 21 | +import com.google.cloud.servicedirectory.v1beta1.LocationName; |
| 22 | +import com.google.cloud.servicedirectory.v1beta1.Namespace; |
| 23 | +import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; |
| 24 | +import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient.ListNamespacesPagedResponse; |
| 25 | +import java.io.IOException; |
| 26 | + |
| 27 | +public class Quickstart { |
| 28 | + |
| 29 | + public static void quickstart() throws IOException { |
| 30 | + // TODO(developer): Replace these variables before running the sample. |
| 31 | + String projectId = "your-project-id"; |
| 32 | + String locationId = "your-region"; |
| 33 | + quickstart(projectId, locationId); |
| 34 | + } |
| 35 | + |
| 36 | + public static void quickstart(String projectId, String locationId) throws IOException { |
| 37 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 38 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 39 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 40 | + try (RegistrationServiceClient client = RegistrationServiceClient.create()) { |
| 41 | + |
| 42 | + // The project and location that hold the namespace to list. |
| 43 | + LocationName parent = LocationName.of(projectId, locationId); |
| 44 | + |
| 45 | + // Call the API. |
| 46 | + ListNamespacesPagedResponse response = client.listNamespaces(parent); |
| 47 | + |
| 48 | + // Iterate over each namespace and print its name. |
| 49 | + System.out.println("Namespaces:"); |
| 50 | + for (Namespace namespace : response.iterateAll()) { |
| 51 | + System.out.println(namespace.getName()); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +// [END servicedirectory_quickstart] |
0 commit comments