import click
from osmclient.cli_commands import common
import logging
+import yaml
logger = logging.getLogger("osmclient")
"""
logger.debug("")
ctx.obj.cluster.get_credentials(name)
+
+
+@click.command(name="cluster-register", short_help="registers a K8s cluster to OSM")
+@click.argument("name")
+@click.option(
+ "--vim-account",
+ "--vim",
+ prompt=True,
+ help="VIM target or cloud account where the cluster will be created",
+)
+@click.option(
+ "--creds", prompt=True, help="credentials file, i.e. a valid `.kube/config` file"
+)
+@click.option("--description", default="", help="human readable description")
+@click.pass_context
+def cluster_register(
+ ctx,
+ name,
+ vim_account,
+ creds,
+ description,
+):
+ """registers a K8s cluster to OSM
+
+ NAME: name of the K8s cluster
+ """
+ logger.debug("")
+ # kwargs = {k: v for k, v in kwargs.items() if v is not None}
+ cluster = {}
+ cluster["name"] = name
+ with open(creds, "r") as cf:
+ cluster["credentials"] = yaml.safe_load(cf.read())
+ # cluster["k8s_version"] = version
+ cluster["vim_account"] = vim_account
+ # cluster["nets"] = yaml.safe_load(k8s_nets)
+ # if not (init_jujubundle and init_helm3):
+ # cluster["deployment_methods"] = {
+ # "juju-bundle": init_jujubundle,
+ # "helm-chart-v3": init_helm3,
+ # }
+ if description:
+ cluster["description"] = description
+ # if namespace:
+ # cluster["namespace"] = namespace
+ # if cni:
+ # cluster["cni"] = yaml.safe_load(cni)
+ ctx.obj.cluster.register(name, cluster)
cli_osm.add_command(cluster.cluster_list)
cli_osm.add_command(cluster.cluster_show)
cli_osm.add_command(cluster.cluster_update)
+ cli_osm.add_command(cluster.cluster_get_credentials)
+ cli_osm.add_command(cluster.cluster_register)
cli_osm.add_command(netslice_instance.nsi_create1)
cli_osm.add_command(netslice_instance.nsi_create2)
"""
from osmclient.sol005.osm_api_object import GenericOSMAPIObject
-from osmclient.common.exceptions import ClientException
+from osmclient.common.exceptions import ClientException, NotFound
import json
+import yaml
class Cluster(GenericOSMAPIObject):
elif http_code == 204:
print("Updated")
- # def get_credentials(self, cluster_id):
- # # TODO:
+ def get_credentials(self, name):
+ """
+ Gets and shows an individual Generic OSM API Object
+ from the list of all objects of the same kind
+ """
+ self._logger.debug("")
+ item = self.get(name)
+ resp = ""
+ try:
+ _, resp = self._http.get2_cmd(f"{self._apiBase}/{item['_id']}/get_creds")
+ if resp:
+ resp = json.loads(resp)
+ item = self.get(name)
+ print(
+ yaml.safe_dump(
+ item["credentials"], indent=4, default_flow_style=False
+ )
+ )
+ except NotFound:
+ raise NotFound(f"{self._logObjectName} '{name}' not found")
+ except Exception as e:
+ raise ClientException(f"{e}: unexpected response from server - {resp}")
+
+ def register_cluster(self, name, cluster):
+ """
+ Registers a K8s cluster
+ """
+ self._logger.debug("")
+ endpoint = f"{self._apiBase}/register"
+ self.create(name, cluster, None, endpoint=endpoint)
def get_individual(self, name):
"""Gets and shows an individual Generic OSM API Object"""
- # It is redundant, since the method getalready gets the whole item
+ # It is redundant, since the method `get` already gets the whole item
# The only difference is that a different primitive is exercised
self._logger.debug("")
item = self.get(name)
f"failed to create {self._logObjectName}. Http code: {http_code}. Message: {msg}"
)
- def create(self, name, content_dict=None, filename=None):
+ def create(self, name, content_dict=None, filename=None, endpoint=None):
"""Creates a new Generic OSM API Object"""
self._logger.debug(f"Creating Generic OSM API Object {name}")
self._client.get_token()
+ if not endpoint:
+ endpoint = self._apiBase
# If filename is dir, generate a tar.gz file
tempdir = None
if filename:
# Typical case. Only a dict
self._logger.debug("Sending only a dict")
_, resp = self._http.post_cmd(
- endpoint=self._apiBase,
+ endpoint=endpoint,
postfields_dict=content_dict,
skip_query_admin=True,
)
self._logger.debug(f"formfile: {formfile}")
self.set_http_headers(filename, multipart=True)
_, resp = self._http.post_cmd(
- endpoint=self._apiBase, postdata=content_dict, formfile=formfile
+ endpoint=endpoint, postdata=content_dict, formfile=formfile
)
elif not content_dict and filename:
self._logger.debug("Sending only a file")
# Only a file to be sent
self.set_http_headers(filename)
- _, resp = self._http.post_cmd(endpoint=self._apiBase, filename=filename)
+ _, resp = self._http.post_cmd(endpoint=endpoint, filename=filename)
if resp:
resp = json.loads(resp)
self._logger.debug(f"Resp: {resp}")