|
| 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_create_endpoint] |
| 20 | + |
| 21 | +import com.google.cloud.servicedirectory.v1beta1.Endpoint; |
| 22 | +import com.google.cloud.servicedirectory.v1beta1.RegistrationServiceClient; |
| 23 | +import com.google.cloud.servicedirectory.v1beta1.ServiceName; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +public class EndpointsCreate { |
| 27 | + |
| 28 | + public static void createEndpoint() throws IOException { |
| 29 | + // TODO(developer): Replace these variables before running the sample. |
| 30 | + // These variables should refer to an existing Service Directory service. |
| 31 | + String projectId = "your-project-id"; |
| 32 | + String locationId = "your-region"; |
| 33 | + String namespaceId = "your-namespace"; |
| 34 | + String serviceId = "your-service"; |
| 35 | + // This is user-created; must be unique within the service above. |
| 36 | + String endpointId = "your-endpoint"; |
| 37 | + createEndpoint(projectId, locationId, namespaceId, serviceId, endpointId); |
| 38 | + } |
| 39 | + |
| 40 | + // Create a new endpoint. |
| 41 | + public static void createEndpoint( |
| 42 | + String projectId, String locationId, String namespaceId, String serviceId, String endpointId) |
| 43 | + throws IOException { |
| 44 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 45 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 46 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 47 | + try (RegistrationServiceClient client = RegistrationServiceClient.create()) { |
| 48 | + |
| 49 | + // The service to create the endpoint in. |
| 50 | + ServiceName parent = ServiceName.of(projectId, locationId, namespaceId, serviceId); |
| 51 | + |
| 52 | + // The endpoint to create, with fields filled in. |
| 53 | + // Optionally set an IP address and port for the endpoint. |
| 54 | + Endpoint endpoint = Endpoint.newBuilder().setAddress("10.0.0.1").setPort(443).build(); |
| 55 | + |
| 56 | + // Send the request to create the endpoint. |
| 57 | + Endpoint createdEndpoint = client.createEndpoint(parent, endpoint, endpointId); |
| 58 | + |
| 59 | + // Process the response. |
| 60 | + System.out.println("Created Endpoint: " + createdEndpoint.getName()); |
| 61 | + System.out.println("IP Address: " + createdEndpoint.getAddress()); |
| 62 | + System.out.println("Port: " + createdEndpoint.getPort()); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +// [END servicedirectory_create_endpoint] |
0 commit comments