diff --git a/dns/api/README.rst b/dns/api/README.rst index 1069a05dec3d..96c40d6217f6 100644 --- a/dns/api/README.rst +++ b/dns/api/README.rst @@ -42,7 +42,7 @@ Install Dependencies .. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup -#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. +#. Create a virtualenv. Samples are compatible with Python 3.9+. .. code-block:: bash diff --git a/dns/api/conftest.py b/dns/api/conftest.py new file mode 100644 index 000000000000..2b3480185797 --- /dev/null +++ b/dns/api/conftest.py @@ -0,0 +1,83 @@ +# Copyright 2025 Google LLC +# +# 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. + +import os +import time +import uuid + +from google.cloud.dns import Client, ManagedZone +from google.cloud.exceptions import NotFound + +import pytest + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +TEST_ZONE_NAME_BASE = "test-zone" +TEST_ZONE_NAME_WITH_SUFFIX = TEST_ZONE_NAME_BASE + "-" + str(uuid.uuid4()) +TEST_ZONE_DNS_NAME = "theadora.is." +TEST_ZONE_DESCRIPTION = "Test zone" + + +def delay_rerun() -> bool: + time.sleep(5) + return True + + +@pytest.fixture(scope="module") +def client() -> Client: + client = Client(PROJECT_ID) + + yield client + + # Delete anything created during the test. + for zone in client.list_zones(): + try: + zone.delete() + except NotFound: # May have been in process + pass + + +@pytest.fixture +def zone(client: Client) -> ManagedZone: + zone_name = TEST_ZONE_NAME_BASE + "-" + str(uuid.uuid4()) + zone = client.zone(zone_name, TEST_ZONE_DNS_NAME) + zone.description = TEST_ZONE_DESCRIPTION + zone.create() + + yield zone + + if zone.exists(): + try: + zone.delete() + except NotFound: # May have been in process + pass + + +@pytest.fixture +def project_id() -> str: + return PROJECT_ID + + +@pytest.fixture +def zone_name() -> str: + return TEST_ZONE_NAME_WITH_SUFFIX + + +@pytest.fixture +def zone_dns_name() -> str: + return TEST_ZONE_DNS_NAME + + +@pytest.fixture +def zone_description() -> str: + return TEST_ZONE_DESCRIPTION diff --git a/dns/api/create_zone.py b/dns/api/create_zone.py new file mode 100644 index 000000000000..c743f44f2a97 --- /dev/null +++ b/dns/api/create_zone.py @@ -0,0 +1,42 @@ +# Copyright 2025 Google LLC +# +# 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.dns import ManagedZone + + +def create_zone( + project_id: str, zone_name: str, dns_name: str, description: str +) -> ManagedZone: + # [START dns_create_zone] + from google.cloud.dns import Client + + # TODO(developer): Uncomment the following lines: + # project_id = "my_project_id" + # zone_name = "my_zone_name" + # dns_name = "example.com." + # description = "Description for your ManagedZone, at most 1024 characters." + + client = Client(project_id) + + # Find more information about the ManagedZone object at: + # https://cloud.google.com/python/docs/reference/dns/latest/zone + zone = client.zone( + zone_name, + dns_name, + description=description, + ) + zone.create() + # [END dns_create_zone] + + return zone diff --git a/dns/api/create_zone_test.py b/dns/api/create_zone_test.py new file mode 100644 index 000000000000..0e168953b4f9 --- /dev/null +++ b/dns/api/create_zone_test.py @@ -0,0 +1,30 @@ +# Copyright 2025 Google LLC +# +# 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. + +import pytest + +from create_zone import create_zone + + +@pytest.mark.flaky +def test_create_zone( + project_id: str, zone_name: str, zone_dns_name: str, zone_description: str +) -> None: + zone = create_zone( + project_id, zone_name, zone_dns_name, zone_description + ) + + assert zone.name == zone_name + assert zone.dns_name == zone_dns_name + assert zone.description == zone_description diff --git a/dns/api/main.py b/dns/api/main.py index 183bff3ac9fa..3e77ee990e80 100644 --- a/dns/api/main.py +++ b/dns/api/main.py @@ -17,20 +17,7 @@ from google.cloud import dns from google.cloud.exceptions import NotFound - -# [START create_zone] -def create_zone(project_id, name, dns_name, description): - client = dns.Client(project=project_id) - zone = client.zone( - name, # examplezonename - dns_name=dns_name, # example.com. - description=description, - ) - zone.create() - return zone - - -# [END create_zone] +from create_zone import create_zone # [START get_zone] @@ -43,8 +30,6 @@ def get_zone(project_id, name): return zone except NotFound: return None - - # [END get_zone] @@ -53,8 +38,6 @@ def list_zones(project_id): client = dns.Client(project=project_id) zones = client.list_zones() return [zone.name for zone in zones] - - # [END list_zones] @@ -63,8 +46,6 @@ def delete_zone(project_id, name): client = dns.Client(project=project_id) zone = client.zone(name) zone.delete() - - # [END delete_zone] @@ -79,8 +60,6 @@ def list_resource_records(project_id, zone_name): (record.name, record.record_type, record.ttl, record.rrdatas) for record in records ] - - # [END list_resource_records] @@ -92,8 +71,6 @@ def list_changes(project_id, zone_name): changes = zone.list_changes() return [(change.started, change.status) for change in changes] - - # [END changes] diff --git a/dns/api/main_test.py b/dns/api/main_test.py index fbcfe9d4808a..c8643d002226 100644 --- a/dns/api/main_test.py +++ b/dns/api/main_test.py @@ -15,9 +15,6 @@ import time import uuid -from google.cloud import dns -from google.cloud.exceptions import NotFound - import pytest import main @@ -33,37 +30,8 @@ def delay_rerun(*args): return True -@pytest.fixture -def client(): - client = dns.Client(PROJECT) - - yield client - - # Delete anything created during the test. - for zone in client.list_zones(): - try: - zone.delete() - except NotFound: # May have been in process - pass - - -@pytest.fixture -def zone(client): - zone = client.zone(TEST_ZONE_NAME, TEST_ZONE_DNS_NAME) - zone.description = TEST_ZONE_DESCRIPTION - zone.create() - - yield zone - - if zone.exists(): - try: - zone.delete() - except NotFound: # May have been under way - pass - - @pytest.mark.flaky -def test_create_zone(client): +def test_create_zone(): zone = main.create_zone( PROJECT, TEST_ZONE_NAME, TEST_ZONE_DNS_NAME, TEST_ZONE_DESCRIPTION ) @@ -74,7 +42,7 @@ def test_create_zone(client): @pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=delay_rerun) -def test_get_zone(client, zone): +def test_get_zone(): zone = main.get_zone(PROJECT, TEST_ZONE_NAME) assert zone.name == TEST_ZONE_NAME @@ -83,26 +51,26 @@ def test_get_zone(client, zone): @pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=delay_rerun) -def test_list_zones(client, zone): +def test_list_zones(): zones = main.list_zones(PROJECT) assert TEST_ZONE_NAME in zones @pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=delay_rerun) -def test_list_resource_records(client, zone): +def test_list_resource_records(): records = main.list_resource_records(PROJECT, TEST_ZONE_NAME) assert records @pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=delay_rerun) -def test_list_changes(client, zone): +def test_list_changes(): changes = main.list_changes(PROJECT, TEST_ZONE_NAME) assert changes @pytest.mark.flaky(max_runs=3, min_passes=1, rerun_filter=delay_rerun) -def test_delete_zone(client, zone): +def test_delete_zone(): main.delete_zone(PROJECT, TEST_ZONE_NAME) diff --git a/dns/api/noxfile_config.py b/dns/api/noxfile_config.py deleted file mode 100644 index 25d1d4e081c2..000000000000 --- a/dns/api/noxfile_config.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright 2024 Google LLC -# -# 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. - -# Default TEST_CONFIG_OVERRIDE for python repos. - -# You can copy this file into your directory, then it will be imported from -# the noxfile.py. - -# The source of truth: -# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py - -TEST_CONFIG_OVERRIDE = { - # You can opt out from the test for specific Python versions. - # > ℹ️ Test only on Python 3.10. - # > The Python version used is defined by the Dockerfile, so it's redundant - # > to run multiple tests since they would all be running the same Dockerfile. - "ignored_versions": ["2.7", "3.6", "3.7", "3.9", "3.11", "3.12", "3.13"], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - # "enforce_type_hints": True, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - # "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - # "envs": {}, -}