Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Directory python snippets #3456

Merged
merged 42 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1490a11
python snippets for service directory
Apr 16, 2020
390ee2d
update requirements.txt to correct version
Apr 16, 2020
29d9d19
fix location constant
Apr 21, 2020
aaa8360
limit scope of import of os, fix create namespace to not create endpo…
Apr 21, 2020
9995dc5
add request metadata for grpc routing
Apr 21, 2020
80df034
Merge pull request #1 from rebeccawshaw/sd-python-doc-samples
rebeccawshaw Apr 21, 2020
83b878d
Merge branch 'master' into master
rebeccawshaw Apr 21, 2020
506c1db
fix: use 0.3.0 and remove extra metadata
busunkim96 Jun 11, 2020
2fc3917
Merge branch 'master' into master
busunkim96 Jun 11, 2020
33440b6
chore(deps): update dependency google-auth-oauthlib to v0.4.1 (#2797)
renovate-bot Apr 18, 2020
a954aa7
undo accidental changes to translate dir
busunkim96 Jun 11, 2020
a429af0
chore: remove extra files
busunkim96 Jun 11, 2020
300370a
remove extra files
busunkim96 Jun 11, 2020
420e8ec
take 2
busunkim96 Jun 11, 2020
89a40fb
remove accidentally committed translate test
busunkim96 Jun 11, 2020
657b38d
fix: use GOOGLE_CLOUD_PROJECT
busunkim96 Jun 11, 2020
cd5febe
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
Jun 16, 2020
322e70b
add quickstart files,generate README.rst, remove metadata from snippe…
Jun 17, 2020
fb971a6
remove README.rst file
Jun 17, 2020
6dedabb
Merge branch 'master' into master
rebeccawshaw Jun 17, 2020
ad957b8
use api for creating resource paths instead of string format
Jun 17, 2020
b3a9282
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
Jun 17, 2020
c3a32fe
change the region tags to be set to 'us-east1', change the namespace …
Jun 24, 2020
36b2da6
change test to fixture
Jun 24, 2020
1210541
Merge branch 'master' into master
busunkim96 Jul 6, 2020
8a991f5
change strings to f-strings, make namespace, service, and endpoint id…
Jul 16, 2020
cd2ef68
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
Jul 16, 2020
a64af6d
missed an f-string, added it to delete namespace
Jul 16, 2020
63e36f3
move list_namespace fixture into namespace fixture, remove unnecessar…
Jul 17, 2020
29d6707
Merge branch 'master' into master
rebeccawshaw Jul 20, 2020
f37d48d
Merge branch 'master' of https://github.com/GoogleCloudPlatform/pytho…
Jul 22, 2020
542910a
add generated README.rst file
Jul 22, 2020
e393114
add requirements to readme and regenerate the file
Jul 23, 2020
ca3d5b3
Merge branch 'master' into master
rebeccawshaw Jul 23, 2020
40334b2
Merge branch 'master' into master
leahecole Jul 23, 2020
c9d52f5
fix linter errors
Jul 24, 2020
4b54713
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
Jul 24, 2020
807730c
Merge branch 'master' into master
rebeccawshaw Jul 24, 2020
3f42d08
Merge branch 'master' into master
rebeccawshaw Jul 24, 2020
6680dc8
only delete the namespace created upon teardown
Jul 24, 2020
083801a
Merge branch 'master' of https://github.com/rebeccawshaw/python-docs-…
Jul 24, 2020
7f0f27b
Merge branch 'master' into master
rebeccawshaw Jul 24, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.


rebeccawshaw marked this conversation as resolved.
Show resolved Hide resolved
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
rebeccawshaw marked this conversation as resolved.
Show resolved Hide resolved
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(
rebeccawshaw marked this conversation as resolved.
Show resolved Hide resolved
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)
]])
rebeccawshaw marked this conversation as resolved.
Show resolved Hide resolved

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']
Copy link
Contributor

@busunkim96 busunkim96 Jun 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required to specify a location to use this library?

We don't currently have a env var for this in CI, so we would have to add one if it is needed

Traceback (most recent call last):
  File "/workspace/servicedirectory/snippets_test.py", line 23, in <module>
    LOCATION_ID = environ['GCLOUD_LOCATION']
  File "/usr/local/lib/python3.7/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'GCLOUD_LOCATION'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user needs to specify the location since its part of the resource path, i.e. i specify a namespace, you need to specify the project and the location that it is in.

Where would we need to add the env var for this? For now, I've just been setting it manually in my environment (export GCLOUD_LOCATION='us-east1')

NAMESPACE_ID = 'test-namespace'
SERVICE_ID = 'test-service'
ENDPOINT_ID = 'test-endpoint'
ADDRESS = '1.2.3.4'
PORT = 443

rebeccawshaw marked this conversation as resolved.
Show resolved Hide resolved

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