def teardown_module(): client = servicedirectory_v1beta1.RegistrationServiceClient() namespace_name = client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID) try: namespace = client.get_namespace(name=namespace_name) client.delete_namespace(name=namespace.name) except exceptions.NotFound: pass
def delete_service(project_id, location_id, namespace_id, service_id): """Deletes a service in the given namespace.""" client = servicedirectory_v1beta1.RegistrationServiceClient() service_name = client.service_path(project_id, location_id, namespace_id, service_id) client.delete_service(name=service_name) print(f'Deleted service {service_name}.')
def delete_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id): """Deletes a endpoin in the given service.""" client = servicedirectory_v1beta1.RegistrationServiceClient() endpoint_name = client.endpoint_path(project_id, location_id, namespace_id, service_id, endpoint_id) client.delete_endpoint(name=endpoint_name) print(f'Deleted endpoint {endpoint_name}.')
def list_namespaces(project_id, location_id): """Lists all namespaces in the given location.""" client = servicedirectory_v1beta1.RegistrationServiceClient() response = client.list_namespaces( parent=f'projects/{project_id}/locations/{location_id}') print(f'Listed namespaces in {location_id}.') for namespace in response: print(f'Namespace: {namespace.name}') return response
def resolve_service(project_id, location_id, namespace_id, service_id): """Resolves a service in the given namespace.""" client = servicedirectory_v1beta1.LookupServiceClient() request = servicedirectory_v1beta1.ResolveServiceRequest( name=servicedirectory_v1beta1.RegistrationServiceClient().service_path( project_id, location_id, namespace_id, service_id)) response = client.resolve_service(request=request) print('Endpoints found:') for endpoint in response.service.endpoints: print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}') return response
def create_service(project_id, location_id, namespace_id, service_id): """Creates a service in the given namespace.""" client = servicedirectory_v1beta1.RegistrationServiceClient() service = servicedirectory_v1beta1.Service(name=client.service_path( project_id, location_id, namespace_id, service_id)) response = client.create_service( parent=client.namespace_path(project_id, location_id, namespace_id), service=service, service_id=service_id, ) print(f'Created service {response.name}.') return response
def create_namespace(project_id, location_id, namespace_id): """Creates a namespace in the given location.""" client = servicedirectory_v1beta1.RegistrationServiceClient() namespace = servicedirectory_v1beta1.Namespace( name=client.namespace_path(project_id, location_id, namespace_id)) response = client.create_namespace( parent=f'projects/{project_id}/locations/{location_id}', namespace=namespace, namespace_id=namespace_id, ) print(f'Created namespace {response.name}.') return response
def create_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id, address, port): """Creates a endpoint in the given service.""" client = servicedirectory_v1beta1.RegistrationServiceClient() endpoint = servicedirectory_v1beta1.Endpoint(name=client.endpoint_path( project_id, location_id, namespace_id, service_id, endpoint_id), address=address, port=port) response = client.create_endpoint( parent=client.service_path(project_id, location_id, namespace_id, service_id), endpoint=endpoint, endpoint_id=endpoint_id, ) print(f'Created endpoint {response.name}.') return response
def client(): return servicedirectory_v1beta1.RegistrationServiceClient()