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

lint fix #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions servicedirectory/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@


def list_namespaces(project_id, location_id):
"""Lists all namespaces in the given location."""
"""Lists all namespaces in the given location."""

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

response = client.list_namespaces(
parent=f'projects/{project_id}/locations/{location_id}')
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}')
print(f"Listed namespaces in {location_id}.")
for namespace in response:
print(f"Namespace: {namespace.name}")

return response
return response
37 changes: 19 additions & 18 deletions servicedirectory/quickstart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,38 @@
from os import environ
import uuid

from google.cloud import servicedirectory_v1beta1
import pytest

import quickstart

from google.cloud import servicedirectory_v1beta1

PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT']
LOCATION_ID = 'us-east1'
NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}'
PROJECT_ID = environ["GOOGLE_CLOUD_PROJECT"]
LOCATION_ID = "us-east1"
NAMESPACE_ID = f"test-namespace-{uuid.uuid4().hex}"


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def client():
return servicedirectory_v1beta1.RegistrationServiceClient()
return servicedirectory_v1beta1.RegistrationServiceClient()


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def namespace(client):
namespace = servicedirectory_v1beta1.Namespace(
name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID))
namespace = servicedirectory_v1beta1.Namespace(
name=client.namespace_path(PROJECT_ID, LOCATION_ID, NAMESPACE_ID)
)

client.create_namespace(
parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}',
namespace=namespace,
namespace_id=NAMESPACE_ID,
)
client.create_namespace(
parent=f"projects/{PROJECT_ID}/locations/{LOCATION_ID}",
namespace=namespace,
namespace_id=NAMESPACE_ID,
)

yield namespace
yield namespace

client.delete_namespace(name=namespace.name)
client.delete_namespace(name=namespace.name)


def test_list_namespace(namespace):
assert namespace in quickstart.list_namespaces(PROJECT_ID,
LOCATION_ID).namespaces
assert namespace in quickstart.list_namespaces(PROJECT_ID, LOCATION_ID).namespaces
144 changes: 75 additions & 69 deletions servicedirectory/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,120 +18,126 @@


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

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

namespace = servicedirectory_v1beta1.Namespace(
name=client.namespace_path(project_id, location_id, namespace_id))
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,
)
response = client.create_namespace(
parent=f"projects/{project_id}/locations/{location_id}",
namespace=namespace,
namespace_id=namespace_id,
)

print(f'Created namespace {response.name}.')
print(f"Created namespace {response.name}.")

return response
return response


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

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

namespace_name = client.namespace_path(project_id, location_id, namespace_id)
namespace_name = client.namespace_path(project_id, location_id, namespace_id)

client.delete_namespace(name=namespace_name)
client.delete_namespace(name=namespace_name)

print(f'Deleted namespace {namespace_name}.')
print(f"Deleted namespace {namespace_name}.")


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

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

service = servicedirectory_v1beta1.Service(
name=client.service_path(project_id, location_id, namespace_id,
service_id))
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,
)
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}.')
print(f"Created service {response.name}.")

return response
return response


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

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

service_name = client.service_path(project_id, location_id, namespace_id,
service_id)
service_name = client.service_path(
project_id, location_id, namespace_id, service_id
)

client.delete_service(name=service_name)
client.delete_service(name=service_name)

print(f'Deleted service {service_name}.')
print(f"Deleted service {service_name}.")


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

client = servicedirectory_v1beta1.LookupServiceClient()
client = servicedirectory_v1beta1.LookupServiceClient()

request = servicedirectory_v1beta1.ResolveServiceRequest(
name=servicedirectory_v1beta1.RegistrationServiceClient().service_path(
project_id, location_id, namespace_id, service_id))
request = servicedirectory_v1beta1.ResolveServiceRequest(
name=servicedirectory_v1beta1.RegistrationServiceClient().service_path(
project_id, location_id, namespace_id, service_id
)
)

response = client.resolve_service(request=request)
response = client.resolve_service(request=request)

print('Endpoints found:')
for endpoint in response.service.endpoints:
print(f'{endpoint.name} -- {endpoint.address}:{endpoint.port}')
print("Endpoints found:")
for endpoint in response.service.endpoints:
print(f"{endpoint.name} -- {endpoint.address}:{endpoint.port}")

return response
return response


def create_endpoint(project_id, location_id, namespace_id, service_id,
endpoint_id, address, port):
"""Creates a endpoint in the given service."""
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()
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)
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,
)
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}.')
print(f"Created endpoint {response.name}.")

return response
return response


def delete_endpoint(project_id, location_id, namespace_id, service_id,
endpoint_id):
"""Deletes a endpoin in the given service."""
def delete_endpoint(project_id, location_id, namespace_id, service_id, endpoint_id):
"""Deletes a endpoin in the given service."""

client = servicedirectory_v1beta1.RegistrationServiceClient()
client = servicedirectory_v1beta1.RegistrationServiceClient()

endpoint_name = client.endpoint_path(project_id, location_id, namespace_id,
service_id, endpoint_id)
endpoint_name = client.endpoint_path(
project_id, location_id, namespace_id, service_id, endpoint_id
)

client.delete_endpoint(name=endpoint_name)
client.delete_endpoint(name=endpoint_name)

print(f'Deleted endpoint {endpoint_name}.')
print(f"Deleted endpoint {endpoint_name}.")
74 changes: 40 additions & 34 deletions servicedirectory/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,78 @@
from os import environ
import uuid

from google.cloud import servicedirectory_v1beta1

import snippets

from google.cloud import servicedirectory_v1beta1

PROJECT_ID = environ['GOOGLE_CLOUD_PROJECT']
LOCATION_ID = 'us-east1'
NAMESPACE_ID = f'test-namespace-{uuid.uuid4().hex}'
SERVICE_ID = f'test-service-{uuid.uuid4().hex}'
ENDPOINT_ID = f'test-endpoint-{uuid.uuid4().hex}'
ADDRESS = '1.2.3.4'
PROJECT_ID = environ["GOOGLE_CLOUD_PROJECT"]
LOCATION_ID = "us-east1"
NAMESPACE_ID = f"test-namespace-{uuid.uuid4().hex}"
SERVICE_ID = f"test-service-{uuid.uuid4().hex}"
ENDPOINT_ID = f"test-endpoint-{uuid.uuid4().hex}"
ADDRESS = "1.2.3.4"
PORT = 443


def teardown_module():
client = servicedirectory_v1beta1.RegistrationServiceClient()
response = client.list_namespaces(
parent=f'projects/{PROJECT_ID}/locations/{LOCATION_ID}')
for namespace in response.namespaces:
client.delete_namespace(name=namespace.name)
client = servicedirectory_v1beta1.RegistrationServiceClient()
response = client.list_namespaces(
parent=f"projects/{PROJECT_ID}/locations/{LOCATION_ID}"
)
for namespace in response.namespaces:
client.delete_namespace(name=namespace.name)


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

assert NAMESPACE_ID in response.name
assert NAMESPACE_ID in response.name


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

assert SERVICE_ID in response.name
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)
response = snippets.create_endpoint(
PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID, ADDRESS, PORT
)

assert ENDPOINT_ID in response.name
assert ENDPOINT_ID in response.name


def test_resolve_service():
response = snippets.resolve_service(PROJECT_ID, LOCATION_ID, NAMESPACE_ID,
SERVICE_ID)
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
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)
snippets.delete_endpoint(
PROJECT_ID, LOCATION_ID, NAMESPACE_ID, SERVICE_ID, ENDPOINT_ID
)

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


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

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


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

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