Skip to content

Commit

Permalink
Merge pull request #1 from rebeccawshaw/sd-python-doc-samples
Browse files Browse the repository at this point in the history
Service Directory python doc samples
  • Loading branch information
rebeccawshaw authored Apr 21, 2020
2 parents d5afc7c + 9995dc5 commit 80df034
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
25 changes: 25 additions & 0 deletions servicedirectory/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is used to generate README.rst

product:
name: Google Cloud Service Directory
short_name: Service Directory
url: https://cloud.google.com/service-directory/docs/
description: >
`Google Cloud 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.


setup:
- auth
- install_deps

samples:
- name: Snippets
file: snippets.py


folder: servicedirectory
1 change: 1 addition & 0 deletions servicedirectory/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==5.3.2
1 change: 1 addition & 0 deletions servicedirectory/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-service-directory==0.1.0
179 changes: 179 additions & 0 deletions servicedirectory/snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# 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.

from google.cloud import servicedirectory_v1beta1


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='projects/{0}/locations/{1}/namespaces/{2}'.format(
project_id, location_id, namespace_id))

response = client.create_namespace(
parent='projects/{0}/locations/{1}'.format(project_id, location_id),
namespace=namespace,
namespace_id=namespace_id,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
project_id, location_id, namespace_id)
]])

print('Created namespace {0}.'.format(response.name))

return response


def delete_namespace(project_id, location_id, namespace_id):
"""Deletes a namespace in the given location."""

client = servicedirectory_v1beta1.RegistrationServiceClient()

namespace_name = 'projects/{0}/locations/{1}/namespaces/{2}'.format(
project_id, location_id, namespace_id)

client.delete_namespace(
name=namespace_name,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
project_id, location_id, namespace_id)
]])

print('Deleted namespace {0}.'.format(namespace_name))


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='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id))

response = client.create_service(
parent='projects/{0}/locations/{1}/namespaces/{2}'.format(
project_id, location_id, namespace_id),
service=service,
service_id=service_id,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id)
]])

print('Created service {0}.'.format(response.name))

return response


def delete_service(project_id, location_id, namespace_id, service_id):
"""Deletes a service in the given namespace."""

client = servicedirectory_v1beta1.RegistrationServiceClient()

service_name = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id)

client.delete_service(
name=service_name,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id)
]])

print('Deleted service {0}.'.format(service_name))


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='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id))

response = client.resolve_service(
request=request,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id)
]])

print('Endpoints found:')
for endpoint in response.service.endpoints:
print('{0} -- {1}:{2}'.format(endpoint.name, endpoint.address,
endpoint.port))

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='projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
.format(project_id, location_id, namespace_id, service_id, endpoint_id),
address=address,
port=port)

response = client.create_endpoint(
parent='projects/{0}/locations/{1}/namespaces/{2}/services/{3}'.format(
project_id, location_id, namespace_id, service_id),
endpoint=endpoint,
endpoint_id=endpoint_id,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
.format(project_id, location_id, namespace_id, service_id,
endpoint_id)
]])

print('Created endpoint {0}.'.format(response.name))

return response


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 = 'projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'.format(
project_id, location_id, namespace_id, service_id, endpoint_id)

client.delete_endpoint(
name=endpoint_name,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}/services/{3}/endpoints/{4}'
.format(project_id, location_id, namespace_id, service_id,
endpoint_id)
]])

print('Deleted endpoint {0}.'.format(endpoint_name))
92 changes: 92 additions & 0 deletions servicedirectory/snippets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# 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.

from os import environ
import pytest
import snippets
from google.cloud import servicedirectory_v1beta1

PROJECT_ID = environ['GCLOUD_PROJECT']
LOCATION_ID = environ['GCLOUD_LOCATION']
NAMESPACE_ID = 'test-namespace'
SERVICE_ID = 'test-service'
ENDPOINT_ID = 'test-endpoint'
ADDRESS = '1.2.3.4'
PORT = 443


def teardown_module(module):
client = servicedirectory_v1beta1.RegistrationServiceClient()
response = client.list_namespaces(
parent='projects/{0}/locations/{1}'.format(PROJECT_ID, LOCATION_ID))
for namespace in response.namespaces:
client.delete_namespace(
name=namespace.name,
metadata=[[
'x-goog-request-params',
'name=projects/{0}/locations/{1}/namespaces/{2}'.format(
PROJECT_ID, LOCATION_ID, namespace.name)
]])


def test_create_namespace():
response = snippets.create_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)

assert NAMESPACE_ID in response.name


def test_create_service():
response = snippets.create_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
SERVICE_ID)

assert SERVICE_ID in response.name


def test_create_endpoint():
response = snippets.create_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
SERVICE_ID, ENDPOINT_ID, ADDRESS, PORT)

assert ENDPOINT_ID in response.name


def test_resolve_service():
response = snippets.resolve_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
SERVICE_ID)

assert len(response.service.endpoints) == 1
assert ENDPOINT_ID in response.service.endpoints[0].name


def test_delete_endpoint(capsys):
snippets.delete_endpoint(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID,
ENDPOINT_ID)

out, _ = capsys.readouterr()
assert ENDPOINT_ID in out


def test_delete_service(capsys):
snippets.delete_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID)

out, _ = capsys.readouterr()
assert SERVICE_ID in out


def test_delete_namespace(capsys):
snippets.delete_namespace(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)

out, _ = capsys.readouterr()
assert NAMESPACE_ID in out

0 comments on commit 80df034

Please sign in to comment.