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,
)
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")
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:
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)
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")