|
| 1 | +"""Networking API extension checker for DNS functionality |
| 2 | +
|
| 3 | +This script uses the OpenStack SDK to check conformance to the SCS standard |
| 4 | +concerning DNS API extensions. |
| 5 | +""" |
| 6 | + |
| 7 | +import openstack |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import argparse |
| 11 | + |
| 12 | + |
| 13 | +def connect(cloud_name: str, |
| 14 | + auth_overrides: dict = None) -> openstack.connection.Connection: |
| 15 | + """Create a connection to an OpenStack cloud |
| 16 | +
|
| 17 | + :param string cloud_name: |
| 18 | + The name of the configuration to load from clouds.yaml. |
| 19 | + :param dict auth_overrides: |
| 20 | + A dict that overrides option of the auth section of the cloud |
| 21 | + configuration of the loaded clouds.yaml. Allows to authenticate |
| 22 | + as a different user based on the same general cloud settings. |
| 23 | + Example: |
| 24 | +
|
| 25 | + { |
| 26 | + "username": "claudia", |
| 27 | + "password": "foobar123!%", |
| 28 | + "project_name": "customer1" |
| 29 | + } |
| 30 | +
|
| 31 | + :returns: openstack.connnection.Connection |
| 32 | + """ |
| 33 | + |
| 34 | + if auth_overrides: |
| 35 | + return openstack.connect( |
| 36 | + cloud=cloud_name, |
| 37 | + auth=auth_overrides |
| 38 | + ) |
| 39 | + else: |
| 40 | + return openstack.connect( |
| 41 | + cloud=cloud_name, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def has_extension(conn: openstack.connection.Connection, name: str) -> bool: |
| 46 | + """Checks whether a given Networking API extension is present. |
| 47 | +
|
| 48 | + Connects to the Extension API of the Networking API and checks if the |
| 49 | + extension given by `name` is present and in case it is returns True. |
| 50 | + Otherwise returns False. |
| 51 | + """ |
| 52 | + ext = conn.network.find_extension(name, ignore_missing=True) |
| 53 | + if ext: |
| 54 | + # note that ext.name is a human-readable description, the name as |
| 55 | + # referenced in the standard is the ext.alias |
| 56 | + return ext.alias == name |
| 57 | + else: |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | +def has_dns_api(conn: openstack.connection.Connection) -> bool: |
| 62 | + """Checks whether the OpenStack DNS API is offered by the connected cloud. |
| 63 | +
|
| 64 | + Returns True if the DNS API is offered and False otherwise. |
| 65 | + """ |
| 66 | + # "[...] the dns member of a Connection object [...]" |
| 67 | + # "[...] will only be added if the service is detected." |
| 68 | + # see https://docs.openstack.org/openstacksdk/latest/user/proxies/dns.html |
| 69 | + return hasattr(conn, "dns") |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + parser = argparse.ArgumentParser( |
| 74 | + description="SCS Domain Manager Conformance Checker") |
| 75 | + parser.add_argument( |
| 76 | + "--os-cloud", type=str, |
| 77 | + help="Name of the cloud from clouds.yaml, alternative " |
| 78 | + "to the OS_CLOUD environment variable" |
| 79 | + ) |
| 80 | + parser.add_argument( |
| 81 | + "--debug", action="store_true", |
| 82 | + help="Enable OpenStack SDK debug logging" |
| 83 | + ) |
| 84 | + args = parser.parse_args() |
| 85 | + openstack.enable_logging(debug=args.debug) |
| 86 | + |
| 87 | + # parse cloud name for lookup in clouds.yaml |
| 88 | + cloud = os.environ.get("OS_CLOUD", None) |
| 89 | + if args.os_cloud: |
| 90 | + cloud = args.os_cloud |
| 91 | + assert cloud, ( |
| 92 | + "You need to have the OS_CLOUD environment variable set to your cloud " |
| 93 | + "name or pass it via --os-cloud" |
| 94 | + ) |
| 95 | + conn = connect(cloud) |
| 96 | + |
| 97 | + # bare minimum: dns-integration extension |
| 98 | + if has_extension(conn, "dns-integration"): |
| 99 | + print( |
| 100 | + "Networking API MUST offer 'dns-integration' extension: PASS" |
| 101 | + ) |
| 102 | + else: |
| 103 | + print( |
| 104 | + "Networking API MUST offer 'dns-integration' extension: FAIL" |
| 105 | + ) |
| 106 | + sys.exit(1) |
| 107 | + |
| 108 | + has_designate = has_dns_api(conn) |
| 109 | + print("Is DNS API present (OPTIONAL):", has_designate) |
| 110 | + |
| 111 | + if has_designate: |
| 112 | + if has_extension(conn, "dns-domain-ports"): |
| 113 | + print( |
| 114 | + "When the DNS API is present, the Networking API MUST offer " |
| 115 | + "the 'dns-domain-ports' extension: PASS" |
| 116 | + ) |
| 117 | + else: |
| 118 | + print( |
| 119 | + "When the DNS API is present, the Networking API MUST offer " |
| 120 | + "the 'dns-domain-ports' extension: FAIL" |
| 121 | + ) |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments