From e355a51606d2f79cdd253d568614a366decfd0f9 Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Thu, 11 Jan 2024 13:54:10 +0000 Subject: [PATCH] Add extended parameter for cluster-status Closes-Bug: #2051568 Change-Id: I53a6a1e4b54e3632ce90e4350a14bb45fba125c6 --- src/actions.yaml | 6 ++++++ src/actions/actions.py | 11 ++++++++++- src/lib/charm/openstack/mysql_innodb_cluster.py | 17 +++++++++++++---- ..._lib_charm_openstack_mysql_innodb_cluster.py | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/actions.yaml b/src/actions.yaml index 645da18..c824ecf 100644 --- a/src/actions.yaml +++ b/src/actions.yaml @@ -25,6 +25,12 @@ cluster-status: description: | JSON dump of the cluster schema and status. This action can be used to determine the RW and RO instances. + params: + extended: + type: integer + default: 0 + description: | + Level of information to report back, valid values between 0 and 3 inclusive reboot-cluster-from-complete-outage: description: | In the case of a complete outage, reboot the cluster from this instance's diff --git a/src/actions/actions.py b/src/actions/actions.py index 75a6131..eeb8dd2 100755 --- a/src/actions/actions.py +++ b/src/actions/actions.py @@ -130,7 +130,9 @@ def cluster_status(args): """ try: with charm.provide_charm_instance() as instance: - _status = json.dumps(instance.get_cluster_status()) + _extended = ch_core.hookenv.action_get("extended") + _status = json.dumps( + instance.get_cluster_status(extended=_extended)) ch_core.hookenv.action_set({"cluster-status": _status}) except subprocess.CalledProcessError as e: ch_core.hookenv.action_set({ @@ -138,6 +140,13 @@ def cluster_status(args): "return-code": e.returncode, "traceback": traceback.format_exc()}) ch_core.hookenv.action_fail("Cluster status failed") + except ValueError as e: + ch_core.hookenv.action_set({ + "error": str(e), + "traceback": traceback.format_exc() + }) + ch_core.hookenv.action_fail( + "Cluster status failed, invalid values for extended parameter") def reboot_cluster_from_complete_outage(args): diff --git a/src/lib/charm/openstack/mysql_innodb_cluster.py b/src/lib/charm/openstack/mysql_innodb_cluster.py index 0dfc8a8..dbec459 100644 --- a/src/lib/charm/openstack/mysql_innodb_cluster.py +++ b/src/lib/charm/openstack/mysql_innodb_cluster.py @@ -1214,7 +1214,7 @@ class MySQLInnoDBClusterCharm( self.configure_instance(address) self.add_instance_to_cluster(address) - def get_cluster_status(self, nocache=False): + def get_cluster_status(self, nocache=False, extended=0): """Get cluster status Return cluster.status() as a dictionary. If cached data exists and is @@ -1223,6 +1223,8 @@ class MySQLInnoDBClusterCharm( :param nocache: Do not return cached data :type nocache: Boolean + :param extended: Extended output for cluster-status + :type extended: integer :side effect: Calls self.check_mysql_connection :returns: Dictionary cluster status output :rtype: Union[None, dict] @@ -1251,12 +1253,18 @@ class MySQLInnoDBClusterCharm( .format(self._error_str(e)), "ERROR") return + if extended < 0 or extended > 3: + raise ValueError( + 'The value of extended parameter needs to be between ' + '0 and 3 inclusive') + _script = ( "shell.connect('{}:{}@{}')\n" "cluster = dba.get_cluster('{}')\n" - "print(cluster.status())" + "print(cluster.status({{'extended': {}}}))" .format(self.cluster_user, self.cluster_password, - self.cluster_address, self.cluster_name)) + self.cluster_address, self.cluster_name, + extended)) try: output = self.run_mysqlsh_script(_script) except subprocess.CalledProcessError as e: @@ -1264,7 +1272,8 @@ class MySQLInnoDBClusterCharm( "Failed checking cluster status: {}" .format(self._error_str(e)), "ERROR") return - self._cached_cluster_status = json.loads(output.decode("UTF-8")) + output = ''.join(str(output.decode('UTF-8')).split('\n')) + self._cached_cluster_status = json.loads(output) return self._cached_cluster_status @staticmethod diff --git a/unit_tests/test_lib_charm_openstack_mysql_innodb_cluster.py b/unit_tests/test_lib_charm_openstack_mysql_innodb_cluster.py index 2a0e8b5..1d5634a 100644 --- a/unit_tests/test_lib_charm_openstack_mysql_innodb_cluster.py +++ b/unit_tests/test_lib_charm_openstack_mysql_innodb_cluster.py @@ -1313,7 +1313,7 @@ class TestMySQLInnoDBClusterCharm(test_utils.PatchHelper): _script = ( "shell.connect('{}:{}@{}')\n" "cluster = dba.get_cluster('{}')\n" - "print(cluster.status())" + "print(cluster.status({{'extended': 0}}))" .format( midbc.cluster_user, midbc.cluster_password, midbc.cluster_address, midbc.cluster_name))