def delete_cluster(self, cluster_name): """Delete the cluster with a given name in PKS environment. System administrator can delete the given cluster regardless of who is the owner of the cluster. Other users can only delete the cluster they own. :param str cluster_name: Name of the cluster """ if self.tenant_client.is_sysadmin() \ or is_org_admin(self.client_session): cluster_info = self.get_cluster_info(cluster_name) qualified_cluster_name = cluster_info['pks_cluster_name'] else: qualified_cluster_name = self._append_user_id(cluster_name) result = self._delete_cluster(qualified_cluster_name) # remove cluster network isolation LOGGER.debug(f"Removing network isolation of cluster {cluster_name}.") try: cluster_network_isolater = ClusterNetworkIsolater(self.nsxt_client) cluster_network_isolater.remove_cluster_isolation( qualified_cluster_name) except Exception as err: # NSX-T oprations are idempotent so they should not cause erros # if say NSGroup is missing. But for any other exception, simply # catch them and ignore. LOGGER.debug(f"Error {err} occured while deleting cluster " f"isolation rules for cluster {cluster_name}") self._restore_original_name(result) self._filter_pks_properties(result) return result
def delete_cluster(self, data): """Delete the cluster with a given name in PKS environment. System administrator can delete the given cluster regardless of who is the owner of the cluster. Other users can only delete the cluster they own. :param str cluster_name: Name of the cluster """ cluster_name = data[RequestKey.CLUSTER_NAME] if self.tenant_client.is_sysadmin() \ or is_org_admin(self.client_session): cluster_info = self._get_cluster_info(data) qualified_cluster_name = cluster_info['pks_cluster_name'] else: qualified_cluster_name = self._append_user_id(cluster_name) result = {} cluster_api = ClusterApi(api_client=self.client) try: LOGGER.debug( f"Sending request to PKS: {self.pks_host_uri} to delete " f"the cluster with name: {qualified_cluster_name}") cluster_api.delete_cluster(cluster_name=qualified_cluster_name) LOGGER.debug( f"PKS: {self.pks_host_uri} accepted the request to delete" f" the cluster: {qualified_cluster_name}") except ApiException as err: LOGGER.debug(f"Deleting cluster {qualified_cluster_name} failed" f" with error:\n {err}") raise PksServerError(err.status, err.body) result['name'] = qualified_cluster_name result['task_status'] = 'in progress' # remove cluster network isolation LOGGER.debug("Removing network isolation of cluster " f"{qualified_cluster_name}.") try: cluster_network_isolater = ClusterNetworkIsolater(self.nsxt_client) cluster_network_isolater.remove_cluster_isolation( qualified_cluster_name) except Exception as err: # NSX-T oprations are idempotent so they should not cause erros # if say NSGroup is missing. But for any other exception, simply # catch them and ignore. LOGGER.debug(f"Error {err} occured while deleting cluster " "isolation rules for cluster " f"{qualified_cluster_name}") return result
def _delete_cluster(self, cluster_name): """Delete the cluster with a given name in PKS environment. Also deletes associated NSX-T Distributed Firewall rules that kept the cluster network isolated from other clusters. :param str cluster_name: Name of the cluster """ result = {} cluster_api = ClusterApiV1(api_client=self.client_v1) LOGGER.debug(f"Sending request to PKS: {self.pks_host_uri} to delete " f"the cluster with name: {cluster_name}") try: cluster_api.delete_cluster(cluster_name=cluster_name) except v1Exception as err: LOGGER.debug(f"Deleting cluster {cluster_name} failed with " f"error:\n {err}") raise PksServerError(err.status, err.body) LOGGER.debug(f"PKS: {self.pks_host_uri} accepted the request to delete" f" the cluster: {cluster_name}") # remove cluster network isolation try: LOGGER.debug("Removing network isolation of cluster " f"{cluster_name}.") cluster_network_isolater = ClusterNetworkIsolater(self.nsxt_client) cluster_network_isolater.remove_cluster_isolation(cluster_name) except Exception: # NSX-T oprations are idempotent so they should not cause erros # if say NSGroup is missing. But for any other exception, simply # them and ignore. pass result['cluster_name'] = cluster_name result['task_status'] = 'in progress' return result