Skip to content

Commit

Permalink
adding defaultBucketID to getClusters; adding getManagedClusters class
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Haigh <[email protected]>
  • Loading branch information
MichaelHaigh committed May 7, 2024
1 parent eb6c742 commit 1be08bc
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 30 deletions.
88 changes: 84 additions & 4 deletions astraSDK/clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
limitations under the License.
"""

import copy
import yaml
import json

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -91,7 +95,7 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None):
"location",
"state",
"managedState",
"tridentStateAllowed",
"defaultBucketID",
],
[
"name",
Expand All @@ -100,7 +104,7 @@ def main(self, hideManaged=False, hideUnmanaged=False, nameFilter=None):
"location",
"state",
"managedState",
"tridentManagedStateAllowed",
"defaultBucketID",
],
clusters,
)
Expand All @@ -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"""

Expand Down
52 changes: 26 additions & 26 deletions docs/toolkit/list/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1be08bc

Please sign in to comment.