|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2020 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from google.cloud import servicedirectory_v1beta1 |
| 18 | + |
| 19 | + |
| 20 | +def create_namespace(project_id, location_id, namespace_id): |
| 21 | + """Creates a namespace in the given location.""" |
| 22 | + |
| 23 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 24 | + |
| 25 | + namespace = servicedirectory_v1beta1.Namespace( |
| 26 | + name=client.namespace_path(project_id, location_id, namespace_id)) |
| 27 | + |
| 28 | + response = client.create_namespace( |
| 29 | + parent=f'projects/{project_id}/locations/{location_id}', |
| 30 | + namespace=namespace, |
| 31 | + namespace_id=namespace_id, |
| 32 | + ) |
| 33 | + |
| 34 | + print(f'Created namespace {response.name}.') |
| 35 | + |
| 36 | + return response |
| 37 | + |
| 38 | + |
| 39 | +def delete_namespace(project_id, location_id, namespace_id): |
| 40 | + """Deletes a namespace in the given location.""" |
| 41 | + |
| 42 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 43 | + |
| 44 | + namespace_name = client.namespace_path(project_id, location_id, namespace_id) |
| 45 | + |
| 46 | + client.delete_namespace(name=namespace_name) |
| 47 | + |
| 48 | + print(f'Deleted namespace {namespace_name}.') |
| 49 | + |
| 50 | + |
| 51 | +def create_service(project_id, location_id, namespace_id, service_id): |
| 52 | + """Creates a service in the given namespace.""" |
| 53 | + |
| 54 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 55 | + |
| 56 | + service = servicedirectory_v1beta1.Service( |
| 57 | + name=client.service_path(project_id, location_id, namespace_id, |
| 58 | + service_id)) |
| 59 | + |
| 60 | + response = client.create_service( |
| 61 | + parent=client.namespace_path(project_id, location_id, namespace_id), |
| 62 | + service=service, |
| 63 | + service_id=service_id, |
| 64 | + ) |
| 65 | + |
| 66 | + print(f'Created service {response.name}.') |
| 67 | + |
| 68 | + return response |
| 69 | + |
| 70 | + |
| 71 | +def delete_service(project_id, location_id, namespace_id, service_id): |
| 72 | + """Deletes a service in the given namespace.""" |
| 73 | + |
| 74 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 75 | + |
| 76 | + service_name = client.service_path(project_id, location_id, namespace_id, |
| 77 | + service_id) |
| 78 | + |
| 79 | + client.delete_service(name=service_name) |
| 80 | + |
| 81 | + print(f'Deleted service {service_name}.') |
| 82 | + |
| 83 | + |
| 84 | +def resolve_service(project_id, location_id, namespace_id, service_id): |
| 85 | + """Resolves a service in the given namespace.""" |
| 86 | + |
| 87 | + client = servicedirectory_v1beta1.LookupServiceClient() |
| 88 | + |
| 89 | + request = servicedirectory_v1beta1.ResolveServiceRequest( |
| 90 | + name=servicedirectory_v1beta1.RegistrationServiceClient().service_path( |
| 91 | + project_id, location_id, namespace_id, service_id)) |
| 92 | + |
| 93 | + response = client.resolve_service(request=request) |
| 94 | + |
| 95 | + print('Endpoints found:') |
| 96 | + for endpoint in response.service.endpoints: |
| 97 | + print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}') |
| 98 | + |
| 99 | + return response |
| 100 | + |
| 101 | + |
| 102 | +def create_endpoint(project_id, location_id, namespace_id, service_id, |
| 103 | + endpoint_id, address, port): |
| 104 | + """Creates a endpoint in the given service.""" |
| 105 | + |
| 106 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 107 | + |
| 108 | + endpoint = servicedirectory_v1beta1.Endpoint( |
| 109 | + name=client.endpoint_path(project_id, location_id, namespace_id, |
| 110 | + service_id, endpoint_id), |
| 111 | + address=address, |
| 112 | + port=port) |
| 113 | + |
| 114 | + response = client.create_endpoint( |
| 115 | + parent=client.service_path(project_id, location_id, namespace_id, |
| 116 | + service_id), |
| 117 | + endpoint=endpoint, |
| 118 | + endpoint_id=endpoint_id, |
| 119 | + ) |
| 120 | + |
| 121 | + print(f'Created endpoint {response.name}.') |
| 122 | + |
| 123 | + return response |
| 124 | + |
| 125 | + |
| 126 | +def delete_endpoint(project_id, location_id, namespace_id, service_id, |
| 127 | + endpoint_id): |
| 128 | + """Deletes a endpoin in the given service.""" |
| 129 | + |
| 130 | + client = servicedirectory_v1beta1.RegistrationServiceClient() |
| 131 | + |
| 132 | + endpoint_name = client.endpoint_path(project_id, location_id, namespace_id, |
| 133 | + service_id, endpoint_id) |
| 134 | + |
| 135 | + client.delete_endpoint(name=endpoint_name) |
| 136 | + |
| 137 | + print(f'Deleted endpoint {endpoint_name}.') |
0 commit comments