def apply(self, cluster_config, cluster_id=None, **kwargs): """Apply the configuration either to create or update the cluster. :param dict cluster_config: cluster configuration information :return: dictionary containing the apply operation task :rtype: dict """ entity_svc = def_entity_svc.DefEntityService(self._cloudapi_client) cluster_spec = rde_1_0_0.NativeEntity(**cluster_config) cluster_name = cluster_spec.metadata.cluster_name if cluster_id: # If cluster id doesn't exist, an exception will be raised def_entity = entity_svc.get_entity(cluster_id) else: def_entity = entity_svc.get_native_rde_by_name_and_rde_version( cluster_name, self._server_rde_version) if not def_entity: cluster_entity = self._native_cluster_api.create_cluster( cluster_spec) # noqa: E501 else: cluster_id = def_entity.id cluster_entity = \ self._native_cluster_api.update_cluster_by_cluster_id(cluster_id, cluster_spec) # noqa: E501 return client_utils.construct_task_console_message( cluster_entity.entity.status.task_href) # noqa: E501
def apply(self, cluster_apply_spec: dict, cluster_id: str = None, **kwargs): # noqa: E501 """Apply the configuration either to create or update the cluster. :param dict cluster_config: cluster configuration information :return: dictionary containing the apply operation task :rtype: dict """ cluster_name = \ self._get_cluster_name_from_cluster_apply_specification(cluster_apply_spec) # noqa: E501 # cluster name should not be missing from the apply specification if not cluster_name: raise Exception('Cluster name missing in the cluster apply specification') # noqa: E501 entity_svc = def_entity_svc.DefEntityService(self._cloudapi_client) if cluster_id: # If cluster id doesn't exist, an exception will be raised def_entity = entity_svc.get_entity(cluster_id) else: def_entity = entity_svc.get_native_rde_by_name_and_rde_version( cluster_name, self._server_rde_version) if not def_entity: cluster_def_entity = self._native_cluster_api.create_cluster( cluster_apply_spec) else: cluster_id = def_entity.id cluster_def_entity = \ self._native_cluster_api.update_cluster_by_cluster_id( cluster_id, cluster_apply_spec) task_href = cluster_def_entity.entity.status.task_href return client_utils.construct_task_console_message(task_href)
def delete_cluster_by_id(self, cluster_id, **kwargs): """Delete the existing Kubernetes cluster by id. :param str cluster_id: native cluster entity id :return: string containing the task for delete operation :rtype: str """ cluster_entity = \ self._native_cluster_api.delete_cluster_by_cluster_id(cluster_id) return client_utils.construct_task_console_message( cluster_entity.entity.status.task_href) # noqa: E501
def delete_nfs_by_cluster_id(self, cluster_id, node_name): """Delete the nfs-node by name from the given cluster id. :param str cluster_id: native cluster entity id :param str node_name: nfs-node name :return: string containing the task for delete operation :rtype: str """ cluster_entity = \ self._native_cluster_api.delete_nfs_node_by_node_name(cluster_id, node_name) return client_utils.construct_task_console_message( cluster_entity.entity.status.task_href) # noqa: E501
def upgrade_cluster_by_cluster_id(self, cluster_id, cluster_def_entity, **kwargs): # noqa: E501 """Get the upgrade plan for give cluster id. :param str cluster_id: unique id of the cluster :param dict cluster_def_entity: defined entity :return: string containing upgrade cluster task href :rtype: str """ # TODO: check if we really need to decode-encode-decode-encode cluster_upgrade_definition = common_models.DefEntity(**cluster_def_entity) # noqa: E501 cluster_def_entity = \ self._native_cluster_api.upgrade_cluster_by_cluster_id( cluster_id, cluster_upgrade_definition) task_href = cluster_def_entity.entity.status.task_href return client_utils.construct_task_console_message(task_href)
def apply(self, cluster_config: dict, cluster_id=None, org=None, **kwargs): """Apply the configuration either to create or update the cluster. :param dict cluster_config: cluster configuration information :param str cluster_id: :param str org: :return: string containing the task href for the operation :rtype: str """ try: self.set_tenant_org_context(org_name=org) cluster_name = cluster_config.get('metadata', {}).get('name') vdc_name = cluster_config.get('metadata', {}).get( 'virtualDataCenterName') # noqa: E501 try: if cluster_id: tkg_entity, tkg_def_entity = self.get_tkg_s_cluster( cluster_id) # noqa: E501 else: tkg_entities, tkg_def_entities = \ self.get_tkg_s_clusters_by_name(cluster_name, vdc=vdc_name) # noqa: E501 tkg_entity = tkg_entities[0] tkg_def_entity = tkg_def_entities[0] cluster_id = tkg_def_entity.get('id') cluster_config['metadata'][ 'resourceVersion'] = tkg_entity.metadata.resource_version # noqa: E501 response = \ self._tkg_s_client_api.update_tkg_cluster_with_http_info( tkg_cluster_id=cluster_id, tkg_cluster=cluster_config) except cse_exceptions.ClusterNotFoundError: response = \ self._tkg_s_client_api.create_tkg_cluster_with_http_info(tkg_cluster=cluster_config) # noqa: E501 # Get the task href from the header headers = response[2] return client_utils.construct_task_console_message( headers.get('Location')) # noqa: E501 except tkg_s_rest.ApiException as e: server_message = json.loads(e.body).get('message') or e.reason msg = cli_constants.TKG_RESPONSE_MESSAGES_BY_STATUS_CODE.get( e.status, f"{server_message}") # noqa: E501 logger.CLIENT_LOGGER.error(msg) raise Exception(msg) except Exception as e: logger.CLIENT_LOGGER.error( f"Error while applying cluster spec: {e}") # noqa: E501 raise
def delete_cluster_by_id(self, cluster_id, org=None, **kwargs): """Delete a cluster using the cluster id. :param str cluster_id: :return: string containing the task href of delete cluster operation """ try: self.set_tenant_org_context(org_name=org) response = \ self._tkg_client_api.delete_tkg_cluster_with_http_info(tkg_cluster_id=cluster_id) # noqa: E501 headers = response[2] return client_utils.construct_task_console_message(headers.get('Location')) # noqa: E501 except tkg_rest.ApiException as e: msg = cli_constants.TKG_RESPONSE_MESSAGES_BY_STATUS_CODE.get(e.status, f"{e.reason}") # noqa: E501 logger.CLIENT_LOGGER.error(msg) raise Exception(msg) except Exception as e: logger.CLIENT_LOGGER.error(f"Error deleting cluster: {e}") raise