return None
@staticmethod
- def format_on_operation(content, operation_type, operation_params):
+ def format_on_operation(
+ content, operation_type, operation_params=None, launch_workflow=True
+ ):
if content["current_operation"] is None:
op_id = str(uuid4())
content["current_operation"] = op_id
operation = {}
operation["operationType"] = operation_type
- operation["git_operation_info"] = None
operation["op_id"] = op_id
operation["result"] = None
- operation["workflowState"] = "PROCESSING"
- operation["resourceState"] = "NOT_READY"
operation["creationDate"] = now
operation["endDate"] = None
- operation["operationParams"] = operation_params
+ if launch_workflow:
+ operation["workflowState"] = "PROCESSING"
+ operation["resourceState"] = "NOT_READY"
+ operation["git_operation_info"] = None
+ operation["operationParams"] = operation_params
content["operationHistory"].append(operation)
return op_id
with self.write_lock:
return self.map_topic[topic].move_ksu(session, _id, indata, kwargs)
- def get_cluster_info(self, session, topic, _id, item):
+ def get_cluster_creds_file(self, session, topic, _id, item, op_id):
if topic not in self.map_topic:
raise EngineException(
"Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR
)
- return self.map_topic[topic].get_cluster_info(session, _id, item)
+ return self.map_topic[topic].get_cluster_creds_file(session, _id, item, op_id)
+
+ def get_cluster_creds(self, session, topic, _id, item):
+ if topic not in self.map_topic:
+ raise EngineException(
+ "Unknown topic {}!!!".format(topic), HTTPStatus.INTERNAL_SERVER_ERROR
+ )
+ return self.map_topic[topic].get_cluster_creds(session, _id, item)
def update_cluster(self, session, topic, _id, item, indata):
if topic not in self.map_topic:
f"{item} {profile_id} does'nt exists", HTTPStatus.UNPROCESSABLE_ENTITY
)
- def get_cluster_info(self, session, _id, item):
+ def get_cluster_creds(self, session, _id, item):
if not self.multiproject:
filter_db = {}
else:
filter_db = self._get_project_filter(session)
- # To allow project&user addressing by name AS WELL AS _id
filter_db[BaseTopic.id_field(self.topic, _id)] = _id
- self._send_msg(item, {"_id": _id})
-
+ op_id = str(uuid4())
+ operation_params = {}
data = self.db.get_one(self.topic, filter_db)
- credentials = data["credentials"]
+ data["current_operation"] = op_id
+ self.format_on_operation(data, item, operation_params, launch_workflow=False)
+ self.db.set_one(self.topic, {"_id": data["_id"]}, data)
+ self._send_msg("get_creds", {"cluster_id": _id, "operation_id": op_id})
+ return op_id
- file_pkg = None
- current_path = _id
+ def get_cluster_creds_file(self, session, _id, item, op_id):
+ if not self.multiproject:
+ filter_db = {}
+ else:
+ filter_db = self._get_project_filter(session)
+ filter_db[BaseTopic.id_field(self.topic, _id)] = _id
- self.fs.file_delete(current_path, ignore_non_exist=True)
- self.fs.mkdir(current_path)
- filename = "credentials.yaml"
+ data = self.db.get_one(self.topic, filter_db)
+ creds_flag = None
+ for operations in data["operationHistory"]:
+ if operations["op_id"] == op_id:
+ creds_flag = operations["result"]
+ self.logger.info("Creds Flag: {}".format(creds_flag))
- file_path = (current_path, filename)
- self.logger.info("File path: {}".format(file_path))
- file_pkg = self.fs.file_open(file_path, "a+b")
+ if creds_flag is True:
+ credentials = data["credentials"]
- credentials_yaml = yaml.safe_dump(
- credentials, indent=4, default_flow_style=False
- )
- file_pkg.write(credentials_yaml.encode(encoding="utf-8"))
+ file_pkg = None
+ current_path = _id
- if file_pkg:
- file_pkg.close()
- file_pkg = None
+ self.fs.file_delete(current_path, ignore_non_exist=True)
+ self.fs.mkdir(current_path)
+ filename = "credentials.yaml"
+ file_path = (current_path, filename)
+ self.logger.info("File path: {}".format(file_path))
+ file_pkg = self.fs.file_open(file_path, "a+b")
- self.fs.sync(from_path=current_path)
+ credentials_yaml = yaml.safe_dump(
+ credentials, indent=4, default_flow_style=False
+ )
+ file_pkg.write(credentials_yaml.encode(encoding="utf-8"))
- return (
- self.fs.file_open((current_path, filename), "rb"),
- "text/plain",
- )
+ if file_pkg:
+ file_pkg.close()
+ file_pkg = None
+ self.fs.sync(from_path=current_path)
+
+ return (
+ self.fs.file_open((current_path, filename), "rb"),
+ "text/plain",
+ )
+ else:
+ raise EngineException(
+ "Not possible to get the credentials of the cluster",
+ HTTPStatus.UNPROCESSABLE_ENTITY,
+ )
def update_cluster(self, session, _id, item, indata):
if not self.multiproject:
"METHODS": ("GET",),
"ROLE_PERMISSION": "k8scluster:id:get_creds:",
},
+ "get_creds_file": {
+ "METHODS": ("GET",),
+ "ROLE_PERMISSION": "k8scluster:id:get_creds_file:",
+ "<ID>": {
+ "METHODS": ("GET",),
+ "ROLE_PERMISSION": "k8scluster:id:get_creds_file:id",
+ },
+ },
"scale": {
"METHODS": ("POST",),
"ROLE_PERMISSION": "k8scluster:id:scale:",
filter_q,
api_req=True,
)
- elif topic == "clusters" and item == "get_creds":
- file, _format = self.engine.get_cluster_info(
- engine_session, engine_topic, _id, item
- )
- outdata = file
+ elif (
+ topic == "clusters"
+ and item == "get_creds_file"
+ or item == "get_creds"
+ ):
+ if item == "get_creds_file":
+ op_id = args[0]
+ file, _format = self.engine.get_cluster_creds_file(
+ engine_session, engine_topic, _id, item, op_id
+ )
+ outdata = file
+ if item == "get_creds":
+ op_id = self.engine.get_cluster_creds(
+ engine_session, engine_topic, _id, item
+ )
+ outdata = {"op_id": op_id}
else:
if item == "reports":
# TODO check that project_id (_id in this context) has permissions
nslcmop_desc = {
"lcmOperationType": "terminate",
"nsInstanceId": ns_id,
- "autoremove": indata.get("autoremove")
- if "autoremove" in indata
- else True,
+ "autoremove": (
+ indata.get("autoremove")
+ if "autoremove" in indata
+ else True
+ ),
}
op_id, _, _ = self.engine.new_item(
rollback,