Skip to content
Snippets Groups Projects
Commit a905f09c authored by garciadeblas's avatar garciadeblas
Browse files

Update cluster-list and show commands to talk both new and old APIs


Change-Id: Icf2f60ae45ae93e8976b475636c86d9fda761506
Signed-off-by: default avatargarciadeblas <gerardo.garciadeblas@telefonica.com>
parent d9bcc2d0
No related branches found
No related tags found
No related merge requests found
Pipeline #17176 passed with stage
in 3 minutes and 26 seconds
......@@ -102,9 +102,33 @@ def cluster_delete(ctx, name, force):
def cluster_list(ctx, filter, output):
"""list all K8s clusters"""
logger.debug("")
extras = {"Created": "created"}
extra_items = [
{
"header": "Created by OSM",
"item": "created",
},
{
"header": "Version",
"item": "k8s_version",
},
{
"header": "VIM account",
"item": "vim_account",
},
{
"header": "Bootstrap",
"item": "bootstrap",
},
{
"header": "Description",
"item": "description",
},
]
common.generic_list(
callback=ctx.obj.cluster.list, filter=filter, format=output, extras=extras
callback=ctx.obj.cluster.list_both,
filter=filter,
format=output,
extras=extra_items,
)
......@@ -121,7 +145,7 @@ def cluster_show(ctx, name, output):
NAME: name or ID of the K8s cluster
"""
logger.debug("")
common.generic_show(callback=ctx.obj.cluster.get, name=name, format=output)
common.generic_show(callback=ctx.obj.cluster.get_both, name=name, format=output)
@click.command(name="cluster-update", short_help="updates a K8s cluster")
......
......@@ -93,13 +93,13 @@ def generic_show(callback, name, format="table"):
def generic_list(callback, filter, format="table", extras=None):
if not extras:
extras = {}
extras = []
logger.debug("")
if filter:
filter = "&".join(filter)
resp = callback(filter)
headers = ["Name", "Id", "State", "Operating State", "Resource State"]
headers.extend(extras.keys())
headers = ["Name", "Id", "Git State", "Resource State", "Operating State"]
headers.extend([item["header"] for item in extras])
rows = []
if format == "table" or format == "csv":
for item in resp:
......@@ -107,10 +107,15 @@ def generic_list(callback, filter, format="table", extras=None):
item["name"],
item["_id"],
item["state"],
item["operatingState"],
item["resourceState"],
item["operatingState"],
]
for v in extras.values():
row_item.append(item.get(v, "-"))
for extra_item in extras:
item1 = item.get(extra_item["item"], "-")
if item1 == "true":
item1 = "YES"
elif item1 == "false":
item1 = "NO"
row_item.append(item1)
rows.append(row_item)
print_output.print_output(format, headers, rows, resp)
......@@ -92,3 +92,50 @@ class Cluster(GenericOSMAPIObject):
self._logger.debug("")
endpoint = f"{self._apiBase}/register"
self.create(name, cluster, None, endpoint=endpoint)
def list_both(self, filter=None):
"""List all clusters from both new and old APIs"""
self._logger.debug("")
cluster_list1 = self.list(filter)
cluster_list2 = self._client.k8scluster.list(filter)
list1_names = {item["name"] for item in cluster_list1}
for item in cluster_list2:
if item["name"] not in list1_names:
# Complete the information for clusters from old API
item["state"] = "N/A"
old_state = item.get("_admin", {}).get("operationalState", "Unknown")
item["bootstrap"] = "NO"
item["operatingState"] = "N/A"
item["resourceState"] = old_state
item["created"] = "NO"
cluster_list1.append(item)
# Complete cluster info with vim_account name and vim_type
vim_list = self._client.vim.list()
for item in cluster_list1:
vim_id = item["vim_account"]
vim_name, vim_type = next(
(
(vim_item["name"], vim_item["vim_type"])
for vim_item in vim_list
if vim_item["_id"] == vim_id or vim_item["name"]
),
None,
)
item["vim_account"] = f"{vim_name} ({vim_type})"
return cluster_list1
def get_both(self, name):
"""
Gets and shows an individual cluster
from both new and old API
"""
self._logger.debug("")
try:
cluster = self.get(name)
return cluster
except NotFound:
try:
cluster2 = self._client.k8scluster.get(name)
return cluster2
except NotFound:
raise NotFound(f"{self._logObjectName} {name} not found")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment