From a888c8c953d6ad35b6441ed4844a425151eae733 Mon Sep 17 00:00:00 2001 From: kyeongnapark Date: Thu, 5 Sep 2024 21:39:28 +0900 Subject: [PATCH] Add create, delete, find, list and UPDATE example connect code - create zone, recordset - list zone - delete zone, recordset - example connect Change-Id: I1859170b06311330c954beec7919ade0e76b6c48 --- doc/source/user/guides/dns.rst | 93 +++++++++++++++++++++++++++++++++- examples/dns/create.py | 69 +++++++++++++++++++++++++ examples/dns/delete.py | 47 +++++++++++++++++ examples/dns/find.py | 62 +++++++++++++++++++++++ examples/dns/list.py | 15 ++++++ 5 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 examples/dns/create.py create mode 100644 examples/dns/delete.py create mode 100644 examples/dns/find.py diff --git a/doc/source/user/guides/dns.rst b/doc/source/user/guides/dns.rst index c2d268083..88caba15e 100644 --- a/doc/source/user/guides/dns.rst +++ b/doc/source/user/guides/dns.rst @@ -5,14 +5,105 @@ Before working with the DNS service, you'll need to create a connection to your OpenStack cloud by following the :doc:`connect` user guide. This will provide you with the ``conn`` variable used in the examples below. -.. TODO(gtema): Implement this guide +.. contents:: Table of Contents + :local: + +The primary resource of the DNS service is the server. List Zones ---------- +**Zone** is a logical grouping of DNS records for a domain, allowing for the +centralized management of DNS resources, including domain names, +nameservers, and DNS queries. + .. literalinclude:: ../examples/dns/list.py :pyobject: list_zones Full example: `dns resource list`_ +List Recordsets +--------------- + +**Recordsets** allow for the centralized management of various DNS records +within a Zone, helping to define how a domain responds to different types +of DNS queries. + +.. literalinclude:: ../examples/dns/list.py + :pyobject: list_recordsets + +Full example: `dns resource list`_ + +Create Zone +----------- + +Create a zone. +It allows users to define and manage the DNS namespace for a particular domain. + +.. literalinclude:: ../examples/dns/create.py + :pyobject: create_zone + +Full example: `dns resource list`_ + +Create Recordset +---------------- + +Create a recordset. It accepts several parameters that define the DNS +record's properties and sends an API request to OpenStack to create the +recordset within a specified DNS zone. + +.. literalinclude:: ../examples/dns/create.py + :pyobject: create_recordset + +Full example: `dns resource list`_ + +Delete Zone +----------- + +Delete a zone. +It allows users to completely delete the DNS management for a specified domain. + +.. literalinclude:: ../examples/dns/delete.py + :pyobject: delete_zone + +Full example: `dns resource list`_ + +Delete Recordset +---------------- + +Delete a recordset. + +.. literalinclude:: ../examples/dns/delete.py + :pyobject: delete_recordset + +Full example: `dns resource list`_ + +Find Zone +--------- + +The find_zone function searches for and returns a DNS zone by its name +using a given connection object. + +.. literalinclude:: ../examples/dns/find.py + :pyobject: find_zone + +Full example: `dns resource list`_ + +Find Recordset +-------------- + +The find_recordset function searches for a DNS recordset +with a specific name and type +within a given zone. If multiple recordsets +with the same name exist, +the record type can be specified to find the exact match. + +.. literalinclude:: ../examples/dns/find.py + :pyobject: find_recordset + +Full example: `dns resource list`_ + .. _dns resource list: https://opendev.org/openstack/openstacksdk/src/branch/master/examples/dns/list.py +.. _dns resource create: https://opendev.org/openstack/openstacksdk/src/branch/master/examples/dns/create.py +.. _dns resource delete: https://opendev.org/openstack/openstacksdk/src/branch/master/examples/dns/delete.py +.. _dns resource find: https://opendev.org/openstack/openstacksdk/src/branch/master/examples/dns/find.py diff --git a/examples/dns/create.py b/examples/dns/create.py new file mode 100644 index 000000000..94cd81f08 --- /dev/null +++ b/examples/dns/create.py @@ -0,0 +1,69 @@ +# 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. + +""" +Create resources from the DNS service. + +For a full guide see +https://docs.openstack.org/openstacksdk/latest/user/guides/dns.html +""" + + +def create_zone( + conn, + name, + email, + ttl=3600, + description="Default description", + zone_type="PRIMARY", +): + print("Create Zone: ") + + zone = { + "name": name, + "email": email, + "ttl": ttl, + "description": description, + "type": zone_type, + } + + print(conn.dns.create_zone(**zone)) + + +def create_recordset( + conn, + name_or_id, + recordset_name, + recordset_type="A", + records=["192.168.1.1"], + ttl=3600, + description="Default description", +): + print("Create Recordset: ") + + zone = conn.dns.find_zone(name_or_id) + + if not zone: + print("Zone not found.") + return None + + zone_id = zone.id + + recordset_data = { + "name": recordset_name, + "type": recordset_type, + "records": records, + "ttl": ttl, + "description": description, + } + + print(conn.dns.create_recordset(zone_id, **recordset_data)) diff --git a/examples/dns/delete.py b/examples/dns/delete.py new file mode 100644 index 000000000..9ad8f80f1 --- /dev/null +++ b/examples/dns/delete.py @@ -0,0 +1,47 @@ +# 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. + +""" +Delete resources from the DNS service. + +For a full guide see +https://docs.openstack.org/openstacksdk/latest/user/guides/dns.html +""" + + +def delete_zone(conn, name_or_id): + print(f"Delete Zone: {name_or_id}") + + zone = conn.dns.find_zone(name_or_id) + + if zone: + conn.dns.delete_zone(zone.id) + else: + return None + + +def delete_recordset(conn, name_or_id, recordset_name): + print(f"Deleting Recordset: {recordset_name} in Zone: {name_or_id}") + + zone = conn.dns.find_zone(name_or_id) + + if zone: + try: + recordset = conn.dns.find_recordset(zone.id, recordset_name) + if recordset: + conn.dns.delete_recordset(recordset, zone.id) + else: + print("Recordset not found") + except Exception as e: + print(f"{e}") + else: + return None diff --git a/examples/dns/find.py b/examples/dns/find.py new file mode 100644 index 000000000..b5f0c26d8 --- /dev/null +++ b/examples/dns/find.py @@ -0,0 +1,62 @@ +# 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. + +""" +Find resources from the DNS service. + +For a full guide see +https://docs.openstack.org/openstacksdk/latest/user/guides/dns.html +""" + + +def find_zone(conn, name_or_id): + print(f"Find Zone: {name_or_id}") + + zone = conn.dns.find_zone(name_or_id) + + if zone: + print(zone) + return zone + else: + print("Zone not found.") + return None + + +def find_recordset(conn, name_or_id, recordset_name, recordset_type=None): + print(f"Find Recordset: {recordset_name} in Zone: {name_or_id}") + + zone = conn.dns.find_zone(name_or_id) + + if not zone: + print("Zone not found.") + return None + + zone_id = zone.id + + try: + if recordset_type: + recordset = conn.dns.find_recordset( + zone_id, recordset_name, type=recordset_type + ) + else: + recordset = conn.dns.find_recordset(zone_id, recordset_name) + + if recordset: + print(recordset) + return recordset + else: + print("Recordset not found in Zone.") + return None + + except Exception as e: + print(f"{e}") + return None diff --git a/examples/dns/list.py b/examples/dns/list.py index 50a5e8170..782334c4b 100644 --- a/examples/dns/list.py +++ b/examples/dns/list.py @@ -23,3 +23,18 @@ def list_zones(conn): for zone in conn.dns.zones(): print(zone) + + +def list_recordsets(conn, name_or_id): + print("List Recordsets for Zone") + + zone = conn.dns.find_zone(name_or_id) + + if zone: + zone_id = zone.id + recordsets = conn.dns.recordsets(zone_id) + + for recordset in recordsets: + print(recordset) + else: + print("Zone not found.")