From 1be08bc133fffd48244440ea9bda509c8f4c1709 Mon Sep 17 00:00:00 2001 From: Michael Haigh Date: Tue, 7 May 2024 11:26:15 -0400 Subject: [PATCH] adding defaultBucketID to getClusters; adding getManagedClusters class Signed-off-by: Michael Haigh --- astraSDK/clusters.py | 88 +++++++++++++++++++++++++++++++++++-- docs/toolkit/list/README.md | 52 +++++++++++----------- 2 files changed, 110 insertions(+), 30 deletions(-) diff --git a/astraSDK/clusters.py b/astraSDK/clusters.py index 07b64ce..3844c93 100644 --- a/astraSDK/clusters.py +++ b/astraSDK/clusters.py @@ -15,6 +15,7 @@ limitations under the License. """ +import copy import yaml import json @@ -35,10 +36,15 @@ def __init__(self, quiet=True, verbose=False, output="json", config=None): self.quiet = quiet self.verbose = verbose self.output = output + self.config = config super().__init__(config=config) self.clouds = getClouds(quiet=True, verbose=verbose, config=config).main() def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None): + if hideUnmanaged: + return getManagedClusters( + quiet=self.quiet, verbose=self.verbose, output=self.output, config=self.config + ).main(nameFilter=nameFilter) clusters = {} clusters["items"] = [] if self.clouds is False: @@ -70,8 +76,6 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None): continue if hideManaged and item.get("managedState") == "managed": continue - if hideUnmanaged and item.get("managedState") == "unmanaged": - continue clusters["items"].append(item) else: if not self.quiet: @@ -91,7 +95,7 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None): "location", "state", "managedState", - "tridentStateAllowed", + "defaultBucketID", ], [ "name", @@ -100,7 +104,7 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None): "location", "state", "managedState", - "tridentManagedStateAllowed", + "defaultBucketID", ], clusters, ) @@ -110,6 +114,82 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None): return dataReturn +class getManagedClusters(SDKCommon): + """Call the managedClusters endpoint to get all managed clusters""" + + def __init__(self, quiet=True, verbose=False, output="json", config=None): + """quiet: Will there be CLI output or just return (datastructure) + verbose: Print all of the ReST call info: URL, Method, Headers, Request Body + output: table: pretty print the data + json: (default) output in JSON + yaml: output in yaml + config: optionally provide a pre-populated common.getConfig().main() object""" + self.quiet = quiet + self.verbose = verbose + self.output = output + super().__init__(config=config) + + def main(self, nameFilter=None): + endpoint = "topology/v1/managedClusters" + url = self.base + endpoint + data = {} + params = {} + + ret = super().apicall( + "get", + url, + data, + self.headers, + params, + quiet=self.quiet, + verbose=self.verbose, + ) + + if ret.ok: + clusters = super().jsonifyResults(ret) + if nameFilter: + filterCopy = copy.deepcopy(clusters) + for counter, r in enumerate(filterCopy.get("items")): + if nameFilter not in self.recursiveGet("name", r): + clusters["items"].remove(filterCopy["items"][counter]) + + if self.output == "json": + dataReturn = clusters + elif self.output == "yaml": + dataReturn = yaml.dump(clusters) + elif self.output == "table": + dataReturn = self.basicTable( + [ + "clusterName", + "clusterID", + "clusterType", + "location", + "state", + "managedState", + "tridentStateAllowed", + ], + [ + "name", + "id", + "clusterType", + "location", + "state", + "managedState", + "tridentManagedStateAllowed", + ], + clusters, + ) + + if not self.quiet: + print(json.dumps(dataReturn) if type(dataReturn) is dict else dataReturn) + return dataReturn + + else: + if not self.quiet: + super().printError(ret) + return False + + class manageCluster(SDKCommon): """This class switches an unmanaged cluster to a managed cluster""" diff --git a/docs/toolkit/list/README.md b/docs/toolkit/list/README.md index ae62b22..542522b 100644 --- a/docs/toolkit/list/README.md +++ b/docs/toolkit/list/README.md @@ -356,44 +356,44 @@ Sample output: ```text $ actoolkit list clusters -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| clusterName | clusterID | clusterType | location | state | managedState | tridentStateAllowed | -+====================+======================================+===============+===============+=========+===============+=====================+ -| prod-cluster | 062728da-ef0c-4dc2-83f9-bedb07c30511 | gke | us-east4-a | running | managed | managed | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| dr-cluster | 001007b8-315a-4b06-be51-4933fc4363fe | gke | us-central1-b | running | managed | unmanaged | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | managed | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ +| clusterName | clusterID | clusterType | location | state | managedState | defaultBucketID | ++====================+======================================+===============+===============+=========+===============+======================================+ +| prod-cluster | 062728da-ef0c-4dc2-83f9-bedb07c30511 | gke | us-east4-a | running | managed | 5758451c-f3ad-4061-8396-0cf96016a11e | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ +| dr-cluster | 001007b8-315a-4b06-be51-4933fc4363fe | gke | us-central1-b | running | managed | | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ +| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ ``` ```text $ actoolkit list clusters --hideManaged -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| clusterName | clusterID | clusterType | location | state | managedState | tridentStateAllowed | -+====================+======================================+===============+===============+=========+===============+=====================+ -| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | managed | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+-------------------+ +| clusterName | clusterID | clusterType | location | state | managedState | defaultBucketID | ++====================+======================================+===============+===============+=========+===============+===================+ +| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+-------------------+ ``` ```text $ actoolkit list clusters --hideUnmanaged -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| clusterName | clusterID | clusterType | location | state | managedState | tridentStateAllowed | -+====================+======================================+===============+===============+=========+===============+=====================+ -| prod-cluster | 062728da-ef0c-4dc2-83f9-bedb07c30511 | gke | us-east4-a | running | managed | managed | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| dr-cluster | 001007b8-315a-4b06-be51-4933fc4363fe | gke | us-central1-b | running | managed | unmanaged | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ +| clusterName | clusterID | clusterType | location | state | managedState | defaultBucketID | ++====================+======================================+===============+===============+=========+===============+======================================+ +| prod-cluster | 062728da-ef0c-4dc2-83f9-bedb07c30511 | gke | us-east4-a | running | managed | 5758451c-f3ad-4061-8396-0cf96016a11e | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ +| dr-cluster | 001007b8-315a-4b06-be51-4933fc4363fe | gke | us-central1-b | running | managed | | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+--------------------------------------+ ``` ```text $ actoolkit list clusters --nameFilter east -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ -| clusterName | clusterID | clusterType | location | state | managedState | tridentStateAllowed | -+====================+======================================+===============+===============+=========+===============+=====================+ -| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | managed | -+--------------------+--------------------------------------+---------------+---------------+---------+---------------+---------------------+ ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+-------------------+ +| clusterName | clusterID | clusterType | location | state | managedState | defaultBucketID | ++====================+======================================+===============+===============+=========+===============+===================+ +| aks-eastus-cluster | 2d326da8-6f87-4f1f-91f9-1efe481854a7 | aks | eastus | running | unmanaged | | ++--------------------+--------------------------------------+---------------+---------------+---------+---------------+-------------------+ ``` ## Credentials