X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=o2ims%2Fviews%2Focloud_view.py;h=56f970d23ce9d5430f11f97942d00a6193c1a78c;hb=refs%2Fchanges%2F26%2F8826%2F2;hp=7213fc41f1387e7c9bbe06b95ec1c458f00d1c53;hpb=1a9dcb5b8f598ee348f23eafecc2d92ef09c8122;p=pti%2Fo2.git diff --git a/o2ims/views/ocloud_view.py b/o2ims/views/ocloud_view.py index 7213fc4..56f970d 100644 --- a/o2ims/views/ocloud_view.py +++ b/o2ims/views/ocloud_view.py @@ -12,13 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import filecmp +import os.path import uuid import yaml -import random -import string from datetime import datetime +import shutil from o2common.service import unit_of_work +from o2ims.domain import ocloud from o2ims.views.ocloud_dto import SubscriptionDTO from o2ims.domain.subscription_obj import Subscription @@ -105,81 +107,74 @@ def deployment_managers(uow: unit_of_work.AbstractUnitOfWork): def deployment_manager_one(deploymentManagerId: str, uow: unit_of_work.AbstractUnitOfWork, - profile: str = 'params'): + profile: str = 'default'): + profile = profile.lower() with uow: first = uow.deployment_managers.get(deploymentManagerId) if first is None: return first result = first.serialize() + if result is None: + return None - if "params" == profile: + profile_data = result.pop("profile", None) + result['profileName'] = profile + + if ocloud.DeploymentManagerProfileDefault == profile: pass - elif "file" == profile and result.hasattr("profile"): - p = result.pop("profile", None) - result["profile"] = _gen_kube_config(deploymentManagerId, p) + elif ocloud.DeploymentManagerProfileSOL018 == profile: + result['deploymentManagementServiceEndpoint'] = \ + profile_data['cluster_api_endpoint'] + result['profileData'] = profile_data + elif ocloud.DeploymentManagerProfileSOL018HelmCLI == profile: + result['deploymentManagementServiceEndpoint'] = \ + profile_data['cluster_api_endpoint'] + + helmcli_profile = dict() + helmcli_profile["helmcli_host_with_port"], helmcli_profile[ + "helmcli_username"], helmcli_profile["helmcli_password"] = \ + config.get_helmcli_access() + helmcli_profile["helmcli_kubeconfig"] = _gen_kube_config( + deploymentManagerId, profile_data) + result['profileData'] = helmcli_profile else: - result.pop("profile", None) + return None return result def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict: - # KUBECONFIG environment variable - # reference: - # https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/ - data = { - 'apiVersion': 'v1', - 'clusters': [ - { - 'cluster': { - 'server': - kubeconfig.pop('cluster_api_endpoint', None), - 'certificate-authority-data': - kubeconfig.pop('cluster_ca_cert', None), - }, - 'name': 'inf-cluster' - }], - 'contexts': [ - { - 'context': { - 'cluster': 'inf-cluster', - 'user': 'kubernetes-admin' - }, - 'name': 'kubernetes-admin@inf-cluster' - } - ], - 'current-context': 'kubernetes-admin@inf-cluster', - 'kind': 'Config', - 'preferences': {}, - 'users': [ - { - 'name': kubeconfig.pop('admin_user', None), - 'user': { - 'client-certificate-data': - kubeconfig.pop('admin_client_cert', None), - 'client-key-data': - kubeconfig.pop('admin_client_key', None), - } - }] - } + data = config.gen_k8s_config_dict( + kubeconfig.pop('cluster_api_endpoint', None), + kubeconfig.pop('cluster_ca_cert', None), + kubeconfig.pop('admin_user', None), + kubeconfig.pop('admin_client_cert', None), + kubeconfig.pop('admin_client_key', None), + ) # Generate a random key for tmp kube config file - letters = string.ascii_uppercase - random_key = ''.join(random.choice(letters) for i in range(10)) + # letters = string.ascii_uppercase + # random_key = ''.join(random.choice(letters) for i in range(10)) + name_key = dmId[:8] # Get datetime of now as tag of the tmp file current_time = datetime.now().strftime("%Y%m%d%H%M%S") - tmp_file_name = random_key + "_" + current_time + tmp_file_name = 'kubeconfig_' + name_key + "_" + current_time + kube_config_name = 'kubeconfig_' + name_key + '.config' # write down the yaml file of kubectl into tmp folder - with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file: + with open('/tmp/' + tmp_file_name, 'w') as file: yaml.dump(data, file) - kubeconfig["kube_config_file"] = config.get_api_url() + \ - config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name + # generate the kube config file if not exist or update the file if it + # changes + if not os.path.exists('/configs/' + kube_config_name) or not \ + filecmp.cmp('/tmp/'+tmp_file_name, '/configs/'+kube_config_name): + shutil.move(os.path.join('/tmp', tmp_file_name), + os.path.join('/configs', kube_config_name)) - return kubeconfig + return '/configs/'+kube_config_name def subscriptions(uow: unit_of_work.AbstractUnitOfWork):