Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • osm/nbi
1 result
Show changes
Commits on Source (5)
......@@ -411,14 +411,8 @@ class BaseTopic:
return None
@staticmethod
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
else:
op_id = content["current_operation"]
def format_on_operation(content, operation_type, operation_params=None):
op_id = str(uuid4())
now = time()
if "operationHistory" not in content:
content["operationHistory"] = []
......@@ -429,16 +423,10 @@ class BaseTopic:
operation["result"] = None
operation["creationDate"] = now
operation["endDate"] = None
if launch_workflow:
operation["workflowState"] = "PROCESSING"
operation["resourceState"] = "NOT_READY"
operation["operationState"] = "IN_PROGRESS"
operation["gitOperationInfo"] = None
operation["operationParams"] = operation_params
else:
operation["workflowState"] = operation["resourceState"] = operation[
"operationState"
] = operation["gitOperationInfo"] = operation["operationParams"] = None
operation["workflowState"] = operation["resourceState"] = operation[
"operationState"
] = operation["gitOperationInfo"] = None
operation["operationParams"] = operation_params
content["operationHistory"].append(operation)
return op_id
......@@ -955,13 +943,15 @@ class BaseTopic:
or self.topic == "k8sinfra_config"
or self.topic == "k8sapp"
or self.topic == "k8sresource"
or self.topic == "clusters"
):
check = self.db.get_one(self.topic, {"_id": _id})
if check["default"] is True:
raise EngineException(
"Cannot edit default profiles",
HTTPStatus.UNPROCESSABLE_ENTITY,
)
if self.topic != "clusters":
if check["default"] is True:
raise EngineException(
"Cannot edit default profiles",
HTTPStatus.UNPROCESSABLE_ENTITY,
)
if "name" in indata:
if check["name"] == indata["name"]:
pass
......@@ -995,6 +985,7 @@ class BaseTopic:
or self.topic == "k8sinfra_config"
or self.topic == "k8sapp"
or self.topic == "k8sresource"
or self.topic == "clusters"
):
pass
else:
......@@ -1031,3 +1022,56 @@ class BaseTopic:
privkey = str(ident)
# return both public and private key
return pubkey, privkey
def add_to_old_collection(self, content, session):
self.logger.info(f"content is : {content}")
item = {}
item["name"] = content["name"]
item["credentials"] = {}
# item["k8s_version"] = content["k8s_version"]
if "k8s_version" in content:
item["k8s_version"] = content["k8s_version"]
else:
item["k8s_version"] = None
vim_account_details = self.db.get_one(
"vim_accounts", {"name": content["vim_account"]}
)
item["vim_account"] = vim_account_details["_id"]
item["nets"] = {"k8s_net1": None}
item["deployment_methods"] = {"juju-bundle": False, "helm-chart-v3": True}
# item["description"] = content["description"]
if "description" in content:
item["description"] = content["description"]
else:
item["description"] = None
item["namespace"] = "kube-system"
item["osm_acm"] = True
item["schema_version"] = "1.11"
self.logger.info(f"item is : {item}")
self.format_on_new(item, session["project_id"], make_public=session["public"])
_id = self.db.create("k8sclusters", item)
self.logger.info(f"_id is : {_id}")
item_1 = self.db.get_one("k8sclusters", {"name": item["name"]})
item_1["_admin"]["operationalState"] = "PROCESSING"
self.logger.info(f"content is : {item_1}")
# Create operation data
now = time()
operation_data = {
"lcmOperationType": "create", # Assuming 'create' operation here
"operationState": "PROCESSING",
"startTime": now,
"statusEnteredTime": now,
"detailed-status": "",
"operationParams": None, # Add parameters as needed
}
# create operation
item_1["_admin"]["operations"] = [operation_data]
item_1["_admin"]["current_operation"] = None
self.logger.info(f"content is : {item_1}")
self.db.set_one("k8sclusters", {"_id": item_1["_id"]}, item_1)
return
......@@ -25,7 +25,10 @@ from osm_nbi.base_topic import BaseTopic, EngineException
from osm_nbi.descriptor_topics import DescriptorTopic
from osm_nbi.validation import (
ValidationError,
validate_input,
clustercreation_new_schema,
cluster_edit_schema,
cluster_update_schema,
infra_controller_profile_create_new_schema,
infra_config_profile_create_new_schema,
app_profile_create_new_schema,
......@@ -187,6 +190,12 @@ class K8sTopic(BaseTopic):
self.resource_topic = ResourceTopic(db, fs, msg, auth)
self.app_topic = AppTopic(db, fs, msg, auth)
@staticmethod
def format_on_new(content, project_id=None, make_public=False):
# logger.info("it is getting into format on new in new fon")
BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public)
content["current_operation"] = None
def new(self, rollback, session, indata=None, kwargs=None, headers=None):
"""
Creates a new k8scluster into database.
......@@ -219,7 +228,6 @@ class K8sTopic(BaseTopic):
self.format_on_new(
cls_create, session["project_id"], make_public=session["public"]
)
cls_create["current_operation"] = None
op_id = self.format_on_operation(
cls_create,
"create",
......@@ -238,6 +246,9 @@ class K8sTopic(BaseTopic):
self.db.set_one("clusters", {"_id": _id}, cls_create)
self._send_msg("create", {"cluster_id": _id, "operation_id": op_id})
# To add the content in old collection "k8sclusters"
self.add_to_old_collection(cls_create, session)
return _id, None
except (
ValidationError,
......@@ -369,15 +380,26 @@ class K8sTopic(BaseTopic):
)
def edit(self, session, _id, item, indata=None, kwargs=None):
indata = self._remove_envelop(indata)
indata = self._validate_input_edit(indata, content=None, force=session["force"])
if indata.get("add_profile"):
self.add_profile(session, _id, item, indata)
elif indata.get("remove_profile"):
self.remove_profile(session, _id, item, indata)
if item not in (
"infra_controller_profiles",
"infra_config_profiles",
"app_profiles",
"resource_profiles",
):
self.schema_edit = cluster_edit_schema
super().edit(session, _id, indata=item, kwargs=kwargs, content=None)
else:
error_msg = "Add / remove operation is only applicable"
raise EngineException(error_msg, HTTPStatus.UNPROCESSABLE_ENTITY)
indata = self._remove_envelop(indata)
indata = self._validate_input_edit(
indata, content=None, force=session["force"]
)
if indata.get("add_profile"):
self.add_profile(session, _id, item, indata)
elif indata.get("remove_profile"):
self.remove_profile(session, _id, item, indata)
else:
error_msg = "Add / remove operation is only applicable"
raise EngineException(error_msg, HTTPStatus.UNPROCESSABLE_ENTITY)
def add_profile(self, session, _id, item, indata=None):
indata = self._remove_envelop(indata)
......@@ -392,7 +414,6 @@ class K8sTopic(BaseTopic):
if profile_id not in profile_list:
content["operatingState"] = "PROCESSING"
content["current_operation"] = None
op_id = self.format_on_operation(
content,
"add",
......@@ -439,8 +460,6 @@ class K8sTopic(BaseTopic):
"Cannot remove default profile", HTTPStatus.UNPROCESSABLE_ENTITY
)
if profile_id in profile_list:
content["operatingState"] = "PROCESSING"
content["current_operation"] = None
op_id = self.format_on_operation(
content,
"remove",
......@@ -467,11 +486,9 @@ class K8sTopic(BaseTopic):
else:
filter_db = self._get_project_filter(session)
filter_db[BaseTopic.id_field(self.topic, _id)] = _id
operation_params = {}
operation_params = None
data = self.db.get_one(self.topic, filter_db)
op_id = self.format_on_operation(
data, item, operation_params, launch_workflow=False
)
op_id = self.format_on_operation(data, item, operation_params)
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
......@@ -530,8 +547,9 @@ class K8sTopic(BaseTopic):
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
operation_params = {}
validate_input(indata, cluster_update_schema)
data = self.db.get_one(self.topic, filter_db)
operation_params = {}
data["operatingState"] = "PROCESSING"
data["resourceState"] = "IN_PROGRESS"
operation_params = indata
......@@ -554,6 +572,11 @@ class K8saddTopic(BaseTopic):
def __init__(self, db, fs, msg, auth):
BaseTopic.__init__(self, db, fs, msg, auth)
@staticmethod
def format_on_new(content, project_id=None, make_public=False):
BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public)
content["current_operation"] = None
def add(self, rollback, session, indata, kwargs=None, headers=None):
step = "checking quotas"
try:
......@@ -575,7 +598,6 @@ class K8saddTopic(BaseTopic):
self.format_on_new(
cls_add_request, session["project_id"], make_public=session["public"]
)
cls_add_request["current_operation"] = None
op_id = self.format_on_operation(
cls_add_request,
"register",
......@@ -593,6 +615,10 @@ class K8saddTopic(BaseTopic):
self.db.set_one(self.topic, {"_id": _id}, cls_add_request)
rollback.append({"topic": self.topic, "_id": _id})
self._send_msg("register", {"cluster_id": _id, "operation_id": op_id})
# To add the content in old collection "k8sclusters"
self.add_to_old_collection(cls_add_request, session)
return _id, None
except (
ValidationError,
......@@ -638,8 +664,6 @@ class K8saddTopic(BaseTopic):
filter_q[self.id_field(self.topic, _id)] = _id
item_content = self.db.get_one(self.topic, filter_q)
item_content["operatingState"] = "PROCESSING"
item_content["current_operation"] = None
op_id = self.format_on_operation(
item_content,
"deregister",
......@@ -713,6 +737,7 @@ class KsusTopic(BaseTopic):
@staticmethod
def format_on_new(content, project_id=None, make_public=False):
BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public)
content["current_operation"] = None
content["state"] = "IN_CREATION"
content["operatingState"] = "PROCESSING"
content["resourceState"] = "IN_PROGRESS"
......@@ -761,7 +786,6 @@ class KsusTopic(BaseTopic):
self.format_on_new(
content, project_id=session["project_id"], make_public=session["public"]
)
content["current_operation"] = None
op_id = self.format_on_operation(
content,
operation_type="create",
......@@ -776,9 +800,6 @@ class KsusTopic(BaseTopic):
_id = self.db.create(self.topic, content)
rollback.append({"topic": self.topic, "_id": _id})
if not op_id:
op_id = content["current_operation"]
_id_list.append(_id)
data = {"ksus_list": _id_list, "operation_id": op_id}
self._send_msg("create", data)
......@@ -789,7 +810,6 @@ class KsusTopic(BaseTopic):
filter_db[BaseTopic.id_field(self.topic, _id)] = _id
data = self.db.get_one(self.topic, filter_db)
data["current_operation"] = None
op_id = self.format_on_operation(
data,
"clone",
......@@ -836,7 +856,6 @@ class KsusTopic(BaseTopic):
# To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name
_id = content.get("_id") or _id
content["current_operation"] = None
op_id = self.format_on_operation(
content,
"move",
......@@ -932,15 +951,18 @@ class KsusTopic(BaseTopic):
for ksus in indata["ksus"]:
content = ksus
_id = content["_id"]
_id_list.append(_id)
content.pop("_id")
op_id = self.delete(session, _id)
op_id, not_send_msg_ksu = self.delete(session, _id)
if not not_send_msg_ksu:
_id_list.append(_id)
else:
_id_list.append(_id)
op_id = self.delete(session, _id)
op_id, not_send_msg_ksu = self.delete(session, _id)
if not not_send_msg_ksu:
_id_list.append(_id)
data = {"ksus_list": _id_list, "operation_id": op_id}
self._send_msg("delete", data)
if _id_list:
data = {"ksus_list": _id_list, "operation_id": op_id}
self._send_msg("delete", data, not_send_msg)
return op_id
def delete(self, session, _id):
......@@ -987,7 +1009,24 @@ class KsusTopic(BaseTopic):
{"_id": oka_id},
{"_admin.usageState": "NOT_IN_USE"},
)
return op_id
# Check if the profile exists. If it doesn't, no message should be sent to Kafka
not_send_msg = None
profile_id = item_content["profile"]["_id"]
profile_type = item_content["profile"]["profile_type"]
profile_collection_map = {
"app_profiles": "k8sapp",
"resource_profiles": "k8sresource",
"infra_controller_profiles": "k8sinfra_controller",
"infra_config_profiles": "k8sinfra_config",
}
profile_collection = profile_collection_map[profile_type]
profile_content = self.db.get_one(
profile_collection, {"_id": profile_id}, fail_on_empty=False
)
if not profile_content:
self.db.del_one(self.topic, filter_q)
not_send_msg = True
return op_id, not_send_msg
class OkaTopic(DescriptorTopic):
......@@ -1005,6 +1044,7 @@ class OkaTopic(DescriptorTopic):
DescriptorTopic.format_on_new(
content, project_id=project_id, make_public=make_public
)
content["current_operation"] = None
content["state"] = "PENDING_CONTENT"
content["operatingState"] = "PROCESSING"
content["resourceState"] = "IN_PROGRESS"
......@@ -1101,7 +1141,6 @@ class OkaTopic(DescriptorTopic):
self.format_on_new(
content, session["project_id"], make_public=session["public"]
)
content["current_operation"] = None
op_id = self.format_on_operation(
content,
operation_type="create",
......@@ -1237,9 +1276,10 @@ class OkaTopic(DescriptorTopic):
kwargs["package"] = filename
if headers["Method"] == "POST":
current_desc["state"] = "IN_CREATION"
op_id = current_desc["current_operation"]
op_id = current_desc.get("operationHistory", [{"op_id": None}])[-1].get(
"op_id"
)
elif headers["Method"] in ("PUT", "PATCH"):
current_desc["current_operation"] = None
op_id = self.format_on_operation(
current_desc,
"update",
......
......@@ -685,7 +685,7 @@ valid_url_methods = {
"METHODS": ("GET", "POST"),
"ROLE_PERMISSION": "k8scluster:",
"<ID>": {
"METHODS": ("GET", "DELETE"),
"METHODS": ("GET", "PATCH", "DELETE"),
"ROLE_PERMISSION": "k8scluster:id:",
"app_profiles": {
"METHODS": ("PATCH", "GET"),
......@@ -719,6 +719,10 @@ valid_url_methods = {
"ROLE_PERMISSION": "k8scluster:id:get_creds_file:id",
},
},
"update": {
"METHODS": ("POST",),
"ROLE_PERMISSION": "k8scluster:id:update:",
},
"scale": {
"METHODS": ("POST",),
"ROLE_PERMISSION": "k8scluster:id:scale:",
......@@ -2040,7 +2044,7 @@ class Server(object):
engine_session, engine_topic, _id, indata, kwargs
)
outdata = {"op_id": op_id}
elif topic == "clusters" and item in ("upgrade", "scale"):
elif topic == "clusters" and item in ("upgrade", "scale", "update"):
op_id = self.engine.update_cluster(
engine_session, engine_topic, _id, item, indata
)
......
......@@ -141,6 +141,16 @@ description_list_schema = {
"items": description_schema,
}
profile_type_schema = {
"type": "string",
"enum": [
"infra_controller_profiles",
"infra_config_profiles",
"app_profiles",
"resource_profiles",
],
}
ns_instantiate_vdu = {
"title": "ns action instantiate input schema for vdu",
"$schema": "http://json-schema.org/draft-04/schema#",
......@@ -1129,6 +1139,29 @@ clusterregistration_new_schema = {
"additionalProperties": False,
}
cluster_edit_schema = {
"title": "cluster edit schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": name_schema,
"description": string_schema,
},
"additionalProperties": False,
}
cluster_update_schema = {
"title": "cluster update schema",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"k8s_version": string_schema,
"node_size": string_schema,
"node_count": integer0_schema,
},
"additionalProperties": True,
}
infra_controller_profile_create_new_schema = {
"title": "infra profile creation operation input schema",
"$schema": "http://json-schema.org/draft-04/schema#",
......@@ -1690,6 +1723,7 @@ oka_schema = {
"properties": {
"name": name_schema,
"description": description_schema,
"profile_type": profile_type_schema,
},
"additionalProperties": False,
}
......@@ -1704,7 +1738,7 @@ ksu_schema = {
"profile": {
"type": "object",
"properties": {
"profile_type": string_schema,
"profile_type": profile_type_schema,
"_id": id_schema,
},
"additionalProperties": False,
......