| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 16 | import logging |
| 17 | import yaml |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 18 | import shutil |
| 19 | import os |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 20 | from http import HTTPStatus |
| 21 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 22 | from time import time |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 23 | from osm_nbi.base_topic import BaseTopic, EngineException |
| 24 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 25 | from osm_nbi.descriptor_topics import DescriptorTopic |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 26 | from osm_nbi.validation import ( |
| 27 | ValidationError, |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 28 | validate_input, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 29 | clustercreation_new_schema, |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 30 | cluster_edit_schema, |
| 31 | cluster_update_schema, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 32 | infra_controller_profile_create_new_schema, |
| 33 | infra_config_profile_create_new_schema, |
| 34 | app_profile_create_new_schema, |
| 35 | resource_profile_create_new_schema, |
| 36 | infra_controller_profile_create_edit_schema, |
| 37 | infra_config_profile_create_edit_schema, |
| 38 | app_profile_create_edit_schema, |
| 39 | resource_profile_create_edit_schema, |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 40 | # k8scluster_new_schema, |
| 41 | clusterregistration_new_schema, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 42 | attach_dettach_profile_schema, |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 43 | ksu_schema, |
| 44 | oka_schema, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 45 | ) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 46 | from osm_common.dbbase import deep_update_rfc7396, DbException |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 47 | from osm_common.msgbase import MsgException |
| 48 | from osm_common.fsbase import FsException |
| 49 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 50 | __author__ = ( |
| 51 | "Shrinithi R <shrinithi.r@tataelxsi.co.in>", |
| 52 | "Shahithya Y <shahithya.y@tataelxsi.co.in>", |
| 53 | ) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | class InfraContTopic(BaseTopic): |
| 57 | topic = "k8sinfra_controller" |
| 58 | topic_msg = "k8s_infra_controller" |
| 59 | schema_new = infra_controller_profile_create_new_schema |
| 60 | schema_edit = infra_controller_profile_create_edit_schema |
| 61 | |
| 62 | def __init__(self, db, fs, msg, auth): |
| 63 | BaseTopic.__init__(self, db, fs, msg, auth) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 64 | |
| 65 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 66 | # To create the new infra controller profile |
| 67 | return self.new_profile(rollback, session, indata, kwargs, headers) |
| 68 | |
| 69 | def default(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 70 | # To create the default infra controller profile while creating the cluster |
| 71 | return self.default_profile(rollback, session, indata, kwargs, headers) |
| 72 | |
| 73 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 74 | item_content = self.db.get_one(self.topic, {"_id": _id}) |
| 75 | if item_content.get("default", False): |
| 76 | raise EngineException( |
| 77 | "Cannot delete item because it is marked as default", |
| 78 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 79 | ) |
| 80 | # Before deleting, detach the profile from the associated clusters. |
| 81 | self.detach(session, _id, profile_type="infra_controller_profiles") |
| 82 | # To delete the infra controller profile |
| 83 | super().delete(session, _id, not_send_msg=not_send_msg) |
| yshah | ffcac5f | 2024-08-19 12:49:07 +0000 | [diff] [blame] | 84 | return _id |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 85 | |
| 86 | |
| 87 | class InfraConfTopic(BaseTopic): |
| 88 | topic = "k8sinfra_config" |
| 89 | topic_msg = "k8s_infra_config" |
| 90 | schema_new = infra_config_profile_create_new_schema |
| 91 | schema_edit = infra_config_profile_create_edit_schema |
| 92 | |
| 93 | def __init__(self, db, fs, msg, auth): |
| 94 | BaseTopic.__init__(self, db, fs, msg, auth) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 95 | |
| 96 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 97 | # To create the new infra config profile |
| 98 | return self.new_profile(rollback, session, indata, kwargs, headers) |
| 99 | |
| 100 | def default(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 101 | # To create the default infra config profile while creating the cluster |
| 102 | return self.default_profile(rollback, session, indata, kwargs, headers) |
| 103 | |
| 104 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 105 | item_content = self.db.get_one(self.topic, {"_id": _id}) |
| 106 | if item_content.get("default", False): |
| 107 | raise EngineException( |
| 108 | "Cannot delete item because it is marked as default", |
| 109 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 110 | ) |
| 111 | # Before deleting, detach the profile from the associated clusters. |
| 112 | self.detach(session, _id, profile_type="infra_config_profiles") |
| 113 | # To delete the infra config profile |
| 114 | super().delete(session, _id, not_send_msg=not_send_msg) |
| yshah | ffcac5f | 2024-08-19 12:49:07 +0000 | [diff] [blame] | 115 | return _id |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class AppTopic(BaseTopic): |
| 119 | topic = "k8sapp" |
| 120 | topic_msg = "k8s_app" |
| 121 | schema_new = app_profile_create_new_schema |
| 122 | schema_edit = app_profile_create_edit_schema |
| 123 | |
| 124 | def __init__(self, db, fs, msg, auth): |
| 125 | BaseTopic.__init__(self, db, fs, msg, auth) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 126 | |
| 127 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 128 | # To create the new app profile |
| 129 | return self.new_profile(rollback, session, indata, kwargs, headers) |
| 130 | |
| 131 | def default(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 132 | # To create the default app profile while creating the cluster |
| 133 | return self.default_profile(rollback, session, indata, kwargs, headers) |
| 134 | |
| 135 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 136 | item_content = self.db.get_one(self.topic, {"_id": _id}) |
| 137 | if item_content.get("default", False): |
| 138 | raise EngineException( |
| 139 | "Cannot delete item because it is marked as default", |
| 140 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 141 | ) |
| 142 | # Before deleting, detach the profile from the associated clusters. |
| 143 | self.detach(session, _id, profile_type="app_profiles") |
| 144 | # To delete the app profile |
| 145 | super().delete(session, _id, not_send_msg=not_send_msg) |
| yshah | ffcac5f | 2024-08-19 12:49:07 +0000 | [diff] [blame] | 146 | return _id |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | class ResourceTopic(BaseTopic): |
| 150 | topic = "k8sresource" |
| 151 | topic_msg = "k8s_resource" |
| 152 | schema_new = resource_profile_create_new_schema |
| 153 | schema_edit = resource_profile_create_edit_schema |
| 154 | |
| 155 | def __init__(self, db, fs, msg, auth): |
| 156 | BaseTopic.__init__(self, db, fs, msg, auth) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 157 | |
| 158 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 159 | # To create the new resource profile |
| 160 | return self.new_profile(rollback, session, indata, kwargs, headers) |
| 161 | |
| 162 | def default(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 163 | # To create the default resource profile while creating the cluster |
| 164 | return self.default_profile(rollback, session, indata, kwargs, headers) |
| 165 | |
| 166 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 167 | item_content = self.db.get_one(self.topic, {"_id": _id}) |
| 168 | if item_content.get("default", False): |
| 169 | raise EngineException( |
| 170 | "Cannot delete item because it is marked as default", |
| 171 | http_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 172 | ) |
| 173 | # Before deleting, detach the profile from the associated clusters. |
| 174 | self.detach(session, _id, profile_type="resource_profiles") |
| 175 | # To delete the resource profile |
| 176 | super().delete(session, _id, not_send_msg=not_send_msg) |
| yshah | ffcac5f | 2024-08-19 12:49:07 +0000 | [diff] [blame] | 177 | return _id |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | class K8sTopic(BaseTopic): |
| 181 | topic = "clusters" |
| 182 | topic_msg = "cluster" |
| 183 | schema_new = clustercreation_new_schema |
| 184 | schema_edit = attach_dettach_profile_schema |
| 185 | |
| 186 | def __init__(self, db, fs, msg, auth): |
| 187 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 188 | self.infra_contr_topic = InfraContTopic(db, fs, msg, auth) |
| 189 | self.infra_conf_topic = InfraConfTopic(db, fs, msg, auth) |
| 190 | self.resource_topic = ResourceTopic(db, fs, msg, auth) |
| 191 | self.app_topic = AppTopic(db, fs, msg, auth) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 192 | |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 193 | @staticmethod |
| 194 | def format_on_new(content, project_id=None, make_public=False): |
| rshri | 50e34dc | 2024-12-02 03:10:39 +0000 | [diff] [blame] | 195 | # logger.info("it is getting into format on new in new fon") |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 196 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| 197 | content["current_operation"] = None |
| 198 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 199 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 200 | """ |
| 201 | Creates a new k8scluster into database. |
| 202 | :param rollback: list to append the created items at database in case a rollback must be done |
| 203 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 204 | :param indata: params to be used for the k8cluster |
| 205 | :param kwargs: used to override the indata |
| 206 | :param headers: http request headers |
| 207 | :return: the _id of k8scluster created at database. Or an exception of type |
| 208 | EngineException, ValidationError, DbException, FsException, MsgException. |
| 209 | Note: Exceptions are not captured on purpose. They should be captured at called |
| 210 | """ |
| 211 | step = "checking quotas" # first step must be defined outside try |
| 212 | try: |
| 213 | self.check_quota(session) |
| 214 | step = "name unique check" |
| 215 | self.check_unique_name(session, indata["name"]) |
| 216 | step = "validating input parameters" |
| 217 | cls_request = self._remove_envelop(indata) |
| 218 | self._update_input_with_kwargs(cls_request, kwargs) |
| 219 | cls_request = self._validate_input_new(cls_request, session["force"]) |
| 220 | operation_params = cls_request |
| 221 | |
| 222 | step = "filling cluster details from input data" |
| 223 | cls_create = self._create_cluster( |
| 224 | cls_request, rollback, session, indata, kwargs, headers |
| 225 | ) |
| 226 | |
| 227 | step = "creating cluster at database" |
| 228 | self.format_on_new( |
| 229 | cls_create, session["project_id"], make_public=session["public"] |
| 230 | ) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 231 | op_id = self.format_on_operation( |
| 232 | cls_create, |
| 233 | "create", |
| 234 | operation_params, |
| 235 | ) |
| 236 | _id = self.db.create(self.topic, cls_create) |
| garciadeblas | 6e88d9c | 2024-08-15 10:55:04 +0200 | [diff] [blame] | 237 | pubkey, privkey = self._generate_age_key() |
| 238 | cls_create["age_pubkey"] = self.db.encrypt( |
| 239 | pubkey, schema_version="1.11", salt=_id |
| 240 | ) |
| 241 | cls_create["age_privkey"] = self.db.encrypt( |
| 242 | privkey, schema_version="1.11", salt=_id |
| 243 | ) |
| 244 | # TODO: set age_pubkey and age_privkey in the default profiles |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 245 | rollback.append({"topic": self.topic, "_id": _id}) |
| 246 | self.db.set_one("clusters", {"_id": _id}, cls_create) |
| 247 | self._send_msg("create", {"cluster_id": _id, "operation_id": op_id}) |
| 248 | |
| rshri | 50e34dc | 2024-12-02 03:10:39 +0000 | [diff] [blame] | 249 | # To add the content in old collection "k8sclusters" |
| 250 | self.add_to_old_collection(cls_create, session) |
| 251 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 252 | return _id, None |
| 253 | except ( |
| 254 | ValidationError, |
| 255 | EngineException, |
| 256 | DbException, |
| 257 | MsgException, |
| 258 | FsException, |
| 259 | ) as e: |
| 260 | raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code) |
| 261 | |
| 262 | def _create_cluster(self, cls_request, rollback, session, indata, kwargs, headers): |
| 263 | # Check whether the region name and resource group have been given |
| garciadeblas | c3a6c49 | 2024-08-15 10:00:42 +0200 | [diff] [blame] | 264 | region_given = "region_name" in indata |
| 265 | resource_group_given = "resource_group" in indata |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 266 | |
| 267 | # Get the vim_account details |
| 268 | vim_account_details = self.db.get_one( |
| 269 | "vim_accounts", {"name": cls_request["vim_account"]} |
| 270 | ) |
| 271 | |
| garciadeblas | c3a6c49 | 2024-08-15 10:00:42 +0200 | [diff] [blame] | 272 | # Check whether the region name and resource group have been given |
| 273 | if not region_given and not resource_group_given: |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 274 | region_name = vim_account_details["config"]["region_name"] |
| 275 | resource_group = vim_account_details["config"]["resource_group"] |
| garciadeblas | c3a6c49 | 2024-08-15 10:00:42 +0200 | [diff] [blame] | 276 | elif region_given and not resource_group_given: |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 277 | region_name = cls_request["region_name"] |
| 278 | resource_group = vim_account_details["config"]["resource_group"] |
| garciadeblas | c3a6c49 | 2024-08-15 10:00:42 +0200 | [diff] [blame] | 279 | elif not region_given and resource_group_given: |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 280 | region_name = vim_account_details["config"]["region_name"] |
| 281 | resource_group = cls_request["resource_group"] |
| 282 | else: |
| 283 | region_name = cls_request["region_name"] |
| 284 | resource_group = cls_request["resource_group"] |
| 285 | |
| 286 | cls_desc = { |
| 287 | "name": cls_request["name"], |
| 288 | "vim_account": self.check_vim(session, cls_request["vim_account"]), |
| 289 | "k8s_version": cls_request["k8s_version"], |
| 290 | "node_size": cls_request["node_size"], |
| 291 | "node_count": cls_request["node_count"], |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 292 | "bootstrap": cls_request["bootstrap"], |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 293 | "region_name": region_name, |
| 294 | "resource_group": resource_group, |
| 295 | "infra_controller_profiles": [ |
| 296 | self._create_default_profiles( |
| 297 | rollback, session, indata, kwargs, headers, self.infra_contr_topic |
| 298 | ) |
| 299 | ], |
| 300 | "infra_config_profiles": [ |
| 301 | self._create_default_profiles( |
| 302 | rollback, session, indata, kwargs, headers, self.infra_conf_topic |
| 303 | ) |
| 304 | ], |
| 305 | "resource_profiles": [ |
| 306 | self._create_default_profiles( |
| 307 | rollback, session, indata, kwargs, headers, self.resource_topic |
| 308 | ) |
| 309 | ], |
| 310 | "app_profiles": [ |
| 311 | self._create_default_profiles( |
| 312 | rollback, session, indata, kwargs, headers, self.app_topic |
| 313 | ) |
| 314 | ], |
| 315 | "created": "true", |
| 316 | "state": "IN_CREATION", |
| 317 | "operatingState": "PROCESSING", |
| 318 | "git_name": self.create_gitname(cls_request, session), |
| 319 | "resourceState": "IN_PROGRESS.REQUEST_RECEIVED", |
| 320 | } |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 321 | # Add optional fields if they exist in the request |
| 322 | if "description" in cls_request: |
| 323 | cls_desc["description"] = cls_request["description"] |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 324 | return cls_desc |
| 325 | |
| 326 | def check_vim(self, session, name): |
| 327 | try: |
| 328 | vim_account_details = self.db.get_one("vim_accounts", {"name": name}) |
| 329 | if vim_account_details is not None: |
| 330 | return name |
| 331 | except ValidationError as e: |
| 332 | raise EngineException( |
| 333 | e, |
| 334 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 335 | ) |
| 336 | |
| 337 | def _create_default_profiles( |
| 338 | self, rollback, session, indata, kwargs, headers, topic |
| 339 | ): |
| 340 | topic = self.to_select_topic(topic) |
| 341 | default_profiles = topic.default(rollback, session, indata, kwargs, headers) |
| 342 | return default_profiles |
| 343 | |
| 344 | def to_select_topic(self, topic): |
| 345 | if topic == "infra_controller_profiles": |
| 346 | topic = self.infra_contr_topic |
| 347 | elif topic == "infra_config_profiles": |
| 348 | topic = self.infra_conf_topic |
| 349 | elif topic == "resource_profiles": |
| 350 | topic = self.resource_topic |
| 351 | elif topic == "app_profiles": |
| 352 | topic = self.app_topic |
| 353 | return topic |
| 354 | |
| 355 | def show_one(self, session, _id, profile, filter_q=None, api_req=False): |
| 356 | try: |
| 357 | filter_q = self._get_project_filter(session) |
| 358 | filter_q[self.id_field(self.topic, _id)] = _id |
| 359 | content = self.db.get_one(self.topic, filter_q) |
| 360 | existing_profiles = [] |
| 361 | topic = None |
| 362 | topic = self.to_select_topic(profile) |
| 363 | for profile_id in content[profile]: |
| 364 | data = topic.show(session, profile_id, filter_q, api_req) |
| 365 | existing_profiles.append(data) |
| 366 | return existing_profiles |
| 367 | except ValidationError as e: |
| 368 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 369 | |
| 370 | def state_check(self, profile_id, session, topic): |
| 371 | topic = self.to_select_topic(topic) |
| 372 | content = topic.show(session, profile_id, filter_q=None, api_req=False) |
| 373 | state = content["state"] |
| 374 | if state == "CREATED": |
| 375 | return |
| 376 | else: |
| 377 | raise EngineException( |
| 378 | f" {profile_id} is not in created state", |
| 379 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 380 | ) |
| 381 | |
| 382 | def edit(self, session, _id, item, indata=None, kwargs=None): |
| rshri | 50e34dc | 2024-12-02 03:10:39 +0000 | [diff] [blame] | 383 | if item not in ( |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 384 | "infra_controller_profiles", |
| 385 | "infra_config_profiles", |
| 386 | "app_profiles", |
| 387 | "resource_profiles", |
| 388 | ): |
| 389 | self.schema_edit = cluster_edit_schema |
| 390 | super().edit(session, _id, indata=item, kwargs=kwargs, content=None) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 391 | else: |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 392 | indata = self._remove_envelop(indata) |
| 393 | indata = self._validate_input_edit( |
| 394 | indata, content=None, force=session["force"] |
| 395 | ) |
| 396 | if indata.get("add_profile"): |
| 397 | self.add_profile(session, _id, item, indata) |
| 398 | elif indata.get("remove_profile"): |
| 399 | self.remove_profile(session, _id, item, indata) |
| 400 | else: |
| 401 | error_msg = "Add / remove operation is only applicable" |
| 402 | raise EngineException(error_msg, HTTPStatus.UNPROCESSABLE_ENTITY) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 403 | |
| 404 | def add_profile(self, session, _id, item, indata=None): |
| 405 | indata = self._remove_envelop(indata) |
| 406 | operation_params = indata |
| 407 | profile_id = indata["add_profile"][0]["id"] |
| 408 | # check state |
| 409 | self.state_check(profile_id, session, item) |
| 410 | filter_q = self._get_project_filter(session) |
| 411 | filter_q[self.id_field(self.topic, _id)] = _id |
| 412 | content = self.db.get_one(self.topic, filter_q) |
| 413 | profile_list = content[item] |
| 414 | |
| 415 | if profile_id not in profile_list: |
| 416 | content["operatingState"] = "PROCESSING" |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 417 | op_id = self.format_on_operation( |
| 418 | content, |
| 419 | "add", |
| 420 | operation_params, |
| 421 | ) |
| 422 | self.db.set_one("clusters", {"_id": content["_id"]}, content) |
| 423 | self._send_msg( |
| 424 | "add", |
| 425 | { |
| 426 | "cluster_id": _id, |
| 427 | "profile_id": profile_id, |
| 428 | "profile_type": item, |
| 429 | "operation_id": op_id, |
| 430 | }, |
| 431 | ) |
| 432 | else: |
| 433 | raise EngineException( |
| 434 | f"{item} {profile_id} already exists", HTTPStatus.UNPROCESSABLE_ENTITY |
| 435 | ) |
| 436 | |
| 437 | def _get_default_profiles(self, session, topic): |
| 438 | topic = self.to_select_topic(topic) |
| 439 | existing_profiles = topic.list(session, filter_q=None, api_req=False) |
| 440 | default_profiles = [ |
| 441 | profile["_id"] |
| 442 | for profile in existing_profiles |
| 443 | if profile.get("default", False) |
| 444 | ] |
| 445 | return default_profiles |
| 446 | |
| 447 | def remove_profile(self, session, _id, item, indata): |
| 448 | indata = self._remove_envelop(indata) |
| 449 | operation_params = indata |
| 450 | profile_id = indata["remove_profile"][0]["id"] |
| 451 | filter_q = self._get_project_filter(session) |
| 452 | filter_q[self.id_field(self.topic, _id)] = _id |
| 453 | content = self.db.get_one(self.topic, filter_q) |
| 454 | profile_list = content[item] |
| 455 | |
| 456 | default_profiles = self._get_default_profiles(session, item) |
| 457 | |
| 458 | if profile_id in default_profiles: |
| 459 | raise EngineException( |
| 460 | "Cannot remove default profile", HTTPStatus.UNPROCESSABLE_ENTITY |
| 461 | ) |
| 462 | if profile_id in profile_list: |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 463 | op_id = self.format_on_operation( |
| 464 | content, |
| 465 | "remove", |
| 466 | operation_params, |
| 467 | ) |
| 468 | self.db.set_one("clusters", {"_id": content["_id"]}, content) |
| 469 | self._send_msg( |
| 470 | "remove", |
| 471 | { |
| 472 | "cluster_id": _id, |
| 473 | "profile_id": profile_id, |
| 474 | "profile_type": item, |
| 475 | "operation_id": op_id, |
| 476 | }, |
| 477 | ) |
| 478 | else: |
| 479 | raise EngineException( |
| 480 | f"{item} {profile_id} does'nt exists", HTTPStatus.UNPROCESSABLE_ENTITY |
| 481 | ) |
| 482 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 483 | def get_cluster_creds(self, session, _id, item): |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 484 | if not self.multiproject: |
| 485 | filter_db = {} |
| 486 | else: |
| 487 | filter_db = self._get_project_filter(session) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 488 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 489 | operation_params = None |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 490 | data = self.db.get_one(self.topic, filter_db) |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 491 | op_id = self.format_on_operation(data, item, operation_params) |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 492 | self.db.set_one(self.topic, {"_id": data["_id"]}, data) |
| 493 | self._send_msg("get_creds", {"cluster_id": _id, "operation_id": op_id}) |
| 494 | return op_id |
| 495 | |
| 496 | def get_cluster_creds_file(self, session, _id, item, op_id): |
| 497 | if not self.multiproject: |
| 498 | filter_db = {} |
| 499 | else: |
| 500 | filter_db = self._get_project_filter(session) |
| 501 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 502 | |
| 503 | data = self.db.get_one(self.topic, filter_db) |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 504 | creds_flag = None |
| 505 | for operations in data["operationHistory"]: |
| 506 | if operations["op_id"] == op_id: |
| 507 | creds_flag = operations["result"] |
| 508 | self.logger.info("Creds Flag: {}".format(creds_flag)) |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 509 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 510 | if creds_flag is True: |
| 511 | credentials = data["credentials"] |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 512 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 513 | file_pkg = None |
| 514 | current_path = _id |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 515 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 516 | self.fs.file_delete(current_path, ignore_non_exist=True) |
| 517 | self.fs.mkdir(current_path) |
| 518 | filename = "credentials.yaml" |
| 519 | file_path = (current_path, filename) |
| 520 | self.logger.info("File path: {}".format(file_path)) |
| 521 | file_pkg = self.fs.file_open(file_path, "a+b") |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 522 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 523 | credentials_yaml = yaml.safe_dump( |
| 524 | credentials, indent=4, default_flow_style=False |
| 525 | ) |
| 526 | file_pkg.write(credentials_yaml.encode(encoding="utf-8")) |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 527 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 528 | if file_pkg: |
| 529 | file_pkg.close() |
| 530 | file_pkg = None |
| 531 | self.fs.sync(from_path=current_path) |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 532 | |
| shahithya | b9eb414 | 2024-10-17 05:51:39 +0000 | [diff] [blame] | 533 | return ( |
| 534 | self.fs.file_open((current_path, filename), "rb"), |
| 535 | "text/plain", |
| 536 | ) |
| 537 | else: |
| 538 | raise EngineException( |
| 539 | "Not possible to get the credentials of the cluster", |
| 540 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 541 | ) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 542 | |
| 543 | def update_cluster(self, session, _id, item, indata): |
| 544 | if not self.multiproject: |
| 545 | filter_db = {} |
| 546 | else: |
| 547 | filter_db = self._get_project_filter(session) |
| 548 | # To allow project&user addressing by name AS WELL AS _id |
| 549 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 550 | validate_input(indata, cluster_update_schema) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 551 | data = self.db.get_one(self.topic, filter_db) |
| yshah | 99122b8 | 2024-11-18 07:05:29 +0000 | [diff] [blame] | 552 | operation_params = {} |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 553 | data["operatingState"] = "PROCESSING" |
| 554 | data["resourceState"] = "IN_PROGRESS" |
| 555 | operation_params = indata |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 556 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 557 | data, |
| 558 | item, |
| 559 | operation_params, |
| 560 | ) |
| 561 | self.db.set_one(self.topic, {"_id": _id}, data) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 562 | data = {"cluster_id": _id, "operation_id": op_id} |
| 563 | self._send_msg(item, data) |
| 564 | return op_id |
| 565 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 566 | |
| 567 | class K8saddTopic(BaseTopic): |
| 568 | topic = "clusters" |
| 569 | topic_msg = "cluster" |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 570 | schema_new = clusterregistration_new_schema |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 571 | |
| 572 | def __init__(self, db, fs, msg, auth): |
| 573 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 574 | |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 575 | @staticmethod |
| 576 | def format_on_new(content, project_id=None, make_public=False): |
| 577 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| 578 | content["current_operation"] = None |
| 579 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 580 | def add(self, rollback, session, indata, kwargs=None, headers=None): |
| 581 | step = "checking quotas" |
| 582 | try: |
| 583 | self.check_quota(session) |
| 584 | step = "name unique check" |
| 585 | self.check_unique_name(session, indata["name"]) |
| 586 | step = "validating input parameters" |
| 587 | cls_add_request = self._remove_envelop(indata) |
| 588 | self._update_input_with_kwargs(cls_add_request, kwargs) |
| 589 | cls_add_request = self._validate_input_new( |
| 590 | cls_add_request, session["force"] |
| 591 | ) |
| 592 | operation_params = cls_add_request |
| 593 | |
| 594 | step = "filling cluster details from input data" |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 595 | cls_add_request = self._add_cluster(cls_add_request, session) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 596 | |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 597 | step = "registering the cluster at database" |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 598 | self.format_on_new( |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 599 | cls_add_request, session["project_id"], make_public=session["public"] |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 600 | ) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 601 | op_id = self.format_on_operation( |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 602 | cls_add_request, |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 603 | "register", |
| 604 | operation_params, |
| 605 | ) |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 606 | _id = self.db.create(self.topic, cls_add_request) |
| garciadeblas | 9d9d926 | 2024-09-25 11:25:33 +0200 | [diff] [blame] | 607 | pubkey, privkey = self._generate_age_key() |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 608 | cls_add_request["age_pubkey"] = self.db.encrypt( |
| garciadeblas | 9d9d926 | 2024-09-25 11:25:33 +0200 | [diff] [blame] | 609 | pubkey, schema_version="1.11", salt=_id |
| 610 | ) |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 611 | cls_add_request["age_privkey"] = self.db.encrypt( |
| garciadeblas | 9d9d926 | 2024-09-25 11:25:33 +0200 | [diff] [blame] | 612 | privkey, schema_version="1.11", salt=_id |
| 613 | ) |
| 614 | # TODO: set age_pubkey and age_privkey in the default profiles |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 615 | self.db.set_one(self.topic, {"_id": _id}, cls_add_request) |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 616 | rollback.append({"topic": self.topic, "_id": _id}) |
| 617 | self._send_msg("register", {"cluster_id": _id, "operation_id": op_id}) |
| rshri | 50e34dc | 2024-12-02 03:10:39 +0000 | [diff] [blame] | 618 | |
| 619 | # To add the content in old collection "k8sclusters" |
| 620 | self.add_to_old_collection(cls_add_request, session) |
| 621 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 622 | return _id, None |
| 623 | except ( |
| 624 | ValidationError, |
| 625 | EngineException, |
| 626 | DbException, |
| 627 | MsgException, |
| 628 | FsException, |
| 629 | ) as e: |
| 630 | raise type(e)("{} while '{}'".format(e, step), http_code=e.http_code) |
| 631 | |
| 632 | def _add_cluster(self, cls_add_request, session): |
| 633 | cls_add = { |
| 634 | "name": cls_add_request["name"], |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 635 | "credentials": cls_add_request["credentials"], |
| 636 | "vim_account": cls_add_request["vim_account"], |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 637 | "bootstrap": cls_add_request["bootstrap"], |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 638 | "created": "false", |
| 639 | "state": "IN_CREATION", |
| 640 | "operatingState": "PROCESSING", |
| 641 | "git_name": self.create_gitname(cls_add_request, session), |
| 642 | "resourceState": "IN_PROGRESS.REQUEST_RECEIVED", |
| 643 | } |
| rshri | 17b09ec | 2024-11-07 05:48:12 +0000 | [diff] [blame] | 644 | # Add optional fields if they exist in the request |
| 645 | if "description" in cls_add_request: |
| 646 | cls_add["description"] = cls_add_request["description"] |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 647 | return cls_add |
| 648 | |
| 649 | def remove(self, session, _id, dry_run=False, not_send_msg=None): |
| 650 | """ |
| 651 | Delete item by its internal _id |
| 652 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 653 | :param _id: server internal id |
| 654 | :param dry_run: make checking but do not delete |
| 655 | :param not_send_msg: To not send message (False) or store content (list) instead |
| 656 | :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ... |
| 657 | """ |
| 658 | |
| 659 | # To allow addressing projects and users by name AS WELL AS by _id |
| 660 | if not self.multiproject: |
| 661 | filter_q = {} |
| 662 | else: |
| 663 | filter_q = self._get_project_filter(session) |
| 664 | filter_q[self.id_field(self.topic, _id)] = _id |
| 665 | item_content = self.db.get_one(self.topic, filter_q) |
| 666 | |
| rshri | 2d386cb | 2024-07-05 14:35:51 +0000 | [diff] [blame] | 667 | op_id = self.format_on_operation( |
| 668 | item_content, |
| 669 | "deregister", |
| 670 | None, |
| 671 | ) |
| 672 | self.db.set_one(self.topic, {"_id": _id}, item_content) |
| 673 | |
| 674 | self.check_conflict_on_del(session, _id, item_content) |
| 675 | if dry_run: |
| 676 | return None |
| 677 | |
| 678 | if self.multiproject and session["project_id"]: |
| 679 | # remove reference from project_read if there are more projects referencing it. If it last one, |
| 680 | # do not remove reference, but delete |
| 681 | other_projects_referencing = next( |
| 682 | ( |
| 683 | p |
| 684 | for p in item_content["_admin"]["projects_read"] |
| 685 | if p not in session["project_id"] and p != "ANY" |
| 686 | ), |
| 687 | None, |
| 688 | ) |
| 689 | |
| 690 | # check if there are projects referencing it (apart from ANY, that means, public).... |
| 691 | if other_projects_referencing: |
| 692 | # remove references but not delete |
| 693 | update_dict_pull = { |
| 694 | "_admin.projects_read": session["project_id"], |
| 695 | "_admin.projects_write": session["project_id"], |
| 696 | } |
| 697 | self.db.set_one( |
| 698 | self.topic, filter_q, update_dict=None, pull_list=update_dict_pull |
| 699 | ) |
| 700 | return None |
| 701 | else: |
| 702 | can_write = next( |
| 703 | ( |
| 704 | p |
| 705 | for p in item_content["_admin"]["projects_write"] |
| 706 | if p == "ANY" or p in session["project_id"] |
| 707 | ), |
| 708 | None, |
| 709 | ) |
| 710 | if not can_write: |
| 711 | raise EngineException( |
| 712 | "You have not write permission to delete it", |
| 713 | http_code=HTTPStatus.UNAUTHORIZED, |
| 714 | ) |
| 715 | |
| 716 | # delete |
| 717 | self._send_msg( |
| 718 | "deregister", |
| 719 | {"cluster_id": _id, "operation_id": op_id}, |
| 720 | not_send_msg=not_send_msg, |
| 721 | ) |
| 722 | return None |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 723 | |
| 724 | |
| 725 | class KsusTopic(BaseTopic): |
| 726 | topic = "ksus" |
| 727 | okapkg_topic = "okas" |
| 728 | infra_topic = "k8sinfra" |
| 729 | topic_msg = "ksu" |
| 730 | schema_new = ksu_schema |
| 731 | schema_edit = ksu_schema |
| 732 | |
| 733 | def __init__(self, db, fs, msg, auth): |
| 734 | BaseTopic.__init__(self, db, fs, msg, auth) |
| 735 | self.logger = logging.getLogger("nbi.ksus") |
| 736 | |
| 737 | @staticmethod |
| 738 | def format_on_new(content, project_id=None, make_public=False): |
| 739 | BaseTopic.format_on_new(content, project_id=project_id, make_public=make_public) |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 740 | content["current_operation"] = None |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 741 | content["state"] = "IN_CREATION" |
| 742 | content["operatingState"] = "PROCESSING" |
| 743 | content["resourceState"] = "IN_PROGRESS" |
| 744 | |
| 745 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 746 | _id_list = [] |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 747 | for ksus in indata["ksus"]: |
| 748 | content = ksus |
| 749 | oka = content["oka"][0] |
| 750 | oka_flag = "" |
| 751 | if oka["_id"]: |
| 752 | oka_flag = "_id" |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 753 | oka["sw_catalog_path"] = "" |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 754 | elif oka["sw_catalog_path"]: |
| 755 | oka_flag = "sw_catalog_path" |
| 756 | |
| 757 | for okas in content["oka"]: |
| 758 | if okas["_id"] and okas["sw_catalog_path"]: |
| 759 | raise EngineException( |
| 760 | "Cannot create ksu with both OKA and SW catalog path", |
| 761 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 762 | ) |
| 763 | if not okas["sw_catalog_path"]: |
| 764 | okas.pop("sw_catalog_path") |
| 765 | elif not okas["_id"]: |
| 766 | okas.pop("_id") |
| 767 | if oka_flag not in okas.keys(): |
| 768 | raise EngineException( |
| 769 | "Cannot create ksu. Give either OKA or SW catalog path for all oka in a KSU", |
| 770 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 771 | ) |
| 772 | |
| 773 | # Override descriptor with query string kwargs |
| 774 | content = self._remove_envelop(content) |
| 775 | self._update_input_with_kwargs(content, kwargs) |
| 776 | content = self._validate_input_new(input=content, force=session["force"]) |
| 777 | |
| 778 | # Check for unique name |
| 779 | self.check_unique_name(session, content["name"]) |
| 780 | |
| 781 | self.check_conflict_on_new(session, content) |
| 782 | |
| 783 | operation_params = {} |
| 784 | for content_key, content_value in content.items(): |
| 785 | operation_params[content_key] = content_value |
| 786 | self.format_on_new( |
| 787 | content, project_id=session["project_id"], make_public=session["public"] |
| 788 | ) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 789 | op_id = self.format_on_operation( |
| 790 | content, |
| 791 | operation_type="create", |
| 792 | operation_params=operation_params, |
| 793 | ) |
| 794 | content["git_name"] = self.create_gitname(content, session) |
| 795 | |
| 796 | # Update Oka_package usage state |
| 797 | for okas in content["oka"]: |
| 798 | if "_id" in okas.keys(): |
| 799 | self.update_usage_state(session, okas) |
| 800 | |
| 801 | _id = self.db.create(self.topic, content) |
| 802 | rollback.append({"topic": self.topic, "_id": _id}) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 803 | _id_list.append(_id) |
| 804 | data = {"ksus_list": _id_list, "operation_id": op_id} |
| 805 | self._send_msg("create", data) |
| 806 | return _id_list, op_id |
| 807 | |
| 808 | def clone(self, rollback, session, _id, indata, kwargs, headers): |
| 809 | filter_db = self._get_project_filter(session) |
| 810 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| 811 | data = self.db.get_one(self.topic, filter_db) |
| 812 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 813 | op_id = self.format_on_operation( |
| 814 | data, |
| 815 | "clone", |
| 816 | indata, |
| 817 | ) |
| 818 | self.db.set_one(self.topic, {"_id": data["_id"]}, data) |
| 819 | self._send_msg("clone", {"ksus_list": [data["_id"]], "operation_id": op_id}) |
| 820 | return op_id |
| 821 | |
| 822 | def update_usage_state(self, session, oka_content): |
| 823 | _id = oka_content["_id"] |
| 824 | filter_db = self._get_project_filter(session) |
| 825 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| 826 | |
| 827 | data = self.db.get_one(self.okapkg_topic, filter_db) |
| 828 | if data["_admin"]["usageState"] == "NOT_IN_USE": |
| 829 | usage_state_update = { |
| 830 | "_admin.usageState": "IN_USE", |
| 831 | } |
| 832 | self.db.set_one( |
| 833 | self.okapkg_topic, {"_id": _id}, update_dict=usage_state_update |
| 834 | ) |
| 835 | |
| 836 | def move_ksu(self, session, _id, indata=None, kwargs=None, content=None): |
| 837 | indata = self._remove_envelop(indata) |
| 838 | |
| 839 | # Override descriptor with query string kwargs |
| 840 | if kwargs: |
| 841 | self._update_input_with_kwargs(indata, kwargs) |
| 842 | try: |
| 843 | if indata and session.get("set_project"): |
| 844 | raise EngineException( |
| 845 | "Cannot edit content and set to project (query string SET_PROJECT) at same time", |
| 846 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 847 | ) |
| 848 | # TODO self._check_edition(session, indata, _id, force) |
| 849 | if not content: |
| 850 | content = self.show(session, _id) |
| 851 | indata = self._validate_input_edit( |
| 852 | input=indata, content=content, force=session["force"] |
| 853 | ) |
| 854 | operation_params = indata |
| 855 | deep_update_rfc7396(content, indata) |
| 856 | |
| 857 | # To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name |
| 858 | _id = content.get("_id") or _id |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 859 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 860 | content, |
| 861 | "move", |
| 862 | operation_params, |
| 863 | ) |
| 864 | if content.get("_admin"): |
| 865 | now = time() |
| 866 | content["_admin"]["modified"] = now |
| 867 | content["operatingState"] = "PROCESSING" |
| 868 | content["resourceState"] = "IN_PROGRESS" |
| 869 | |
| 870 | self.db.replace(self.topic, _id, content) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 871 | data = {"ksus_list": [content["_id"]], "operation_id": op_id} |
| 872 | self._send_msg("move", data) |
| 873 | return op_id |
| 874 | except ValidationError as e: |
| 875 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 876 | |
| 877 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| 878 | if final_content["name"] != edit_content["name"]: |
| 879 | self.check_unique_name(session, edit_content["name"]) |
| 880 | return final_content |
| 881 | |
| 882 | @staticmethod |
| 883 | def format_on_edit(final_content, edit_content): |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 884 | op_id = BaseTopic.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 885 | final_content, |
| 886 | "update", |
| 887 | edit_content, |
| 888 | ) |
| 889 | final_content["operatingState"] = "PROCESSING" |
| 890 | final_content["resourceState"] = "IN_PROGRESS" |
| 891 | if final_content.get("_admin"): |
| 892 | now = time() |
| 893 | final_content["_admin"]["modified"] = now |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 894 | return op_id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 895 | |
| 896 | def edit(self, session, _id, indata, kwargs): |
| 897 | _id_list = [] |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 898 | if _id == "update": |
| 899 | for ksus in indata["ksus"]: |
| 900 | content = ksus |
| 901 | _id = content["_id"] |
| 902 | _id_list.append(_id) |
| 903 | content.pop("_id") |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 904 | op_id = self.edit_ksu(session, _id, content, kwargs) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 905 | else: |
| 906 | content = indata |
| 907 | _id_list.append(_id) |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 908 | op_id = self.edit_ksu(session, _id, content, kwargs) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 909 | |
| 910 | data = {"ksus_list": _id_list, "operation_id": op_id} |
| 911 | self._send_msg("edit", data) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 912 | |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 913 | def edit_ksu(self, session, _id, indata, kwargs): |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 914 | content = None |
| 915 | indata = self._remove_envelop(indata) |
| 916 | |
| 917 | # Override descriptor with query string kwargs |
| 918 | if kwargs: |
| 919 | self._update_input_with_kwargs(indata, kwargs) |
| 920 | try: |
| 921 | if indata and session.get("set_project"): |
| 922 | raise EngineException( |
| 923 | "Cannot edit content and set to project (query string SET_PROJECT) at same time", |
| 924 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 925 | ) |
| 926 | # TODO self._check_edition(session, indata, _id, force) |
| 927 | if not content: |
| 928 | content = self.show(session, _id) |
| 929 | |
| 930 | for okas in indata["oka"]: |
| 931 | if not okas["_id"]: |
| 932 | okas.pop("_id") |
| 933 | if not okas["sw_catalog_path"]: |
| 934 | okas.pop("sw_catalog_path") |
| 935 | |
| 936 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| 937 | |
| 938 | # To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name |
| 939 | _id = content.get("_id") or _id |
| 940 | |
| 941 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 942 | op_id = self.format_on_edit(content, indata) |
| 943 | self.db.replace(self.topic, _id, content) |
| 944 | return op_id |
| 945 | except ValidationError as e: |
| 946 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 947 | |
| 948 | def delete_ksu(self, session, _id, indata, dry_run=False, not_send_msg=None): |
| 949 | _id_list = [] |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 950 | if _id == "delete": |
| 951 | for ksus in indata["ksus"]: |
| 952 | content = ksus |
| 953 | _id = content["_id"] |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 954 | content.pop("_id") |
| garciadeblas | ac28587 | 2024-12-05 12:21:09 +0100 | [diff] [blame] | 955 | op_id, not_send_msg_ksu = self.delete(session, _id) |
| 956 | if not not_send_msg_ksu: |
| 957 | _id_list.append(_id) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 958 | else: |
| garciadeblas | ac28587 | 2024-12-05 12:21:09 +0100 | [diff] [blame] | 959 | op_id, not_send_msg_ksu = self.delete(session, _id) |
| 960 | if not not_send_msg_ksu: |
| 961 | _id_list.append(_id) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 962 | |
| garciadeblas | ac28587 | 2024-12-05 12:21:09 +0100 | [diff] [blame] | 963 | if _id_list: |
| 964 | data = {"ksus_list": _id_list, "operation_id": op_id} |
| 965 | self._send_msg("delete", data, not_send_msg) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 966 | return op_id |
| 967 | |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 968 | def delete(self, session, _id): |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 969 | if not self.multiproject: |
| 970 | filter_q = {} |
| 971 | else: |
| 972 | filter_q = self._get_project_filter(session) |
| 973 | filter_q[self.id_field(self.topic, _id)] = _id |
| 974 | item_content = self.db.get_one(self.topic, filter_q) |
| 975 | item_content["state"] = "IN_DELETION" |
| 976 | item_content["operatingState"] = "PROCESSING" |
| 977 | item_content["resourceState"] = "IN_PROGRESS" |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 978 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 979 | item_content, |
| 980 | "delete", |
| 981 | None, |
| 982 | ) |
| 983 | self.db.set_one(self.topic, {"_id": item_content["_id"]}, item_content) |
| 984 | |
| 985 | if item_content["oka"][0].get("_id"): |
| 986 | used_oka = {} |
| 987 | existing_oka = [] |
| 988 | for okas in item_content["oka"]: |
| 989 | used_oka["_id"] = okas["_id"] |
| 990 | |
| 991 | filter = self._get_project_filter(session) |
| 992 | data = self.db.get_list(self.topic, filter) |
| 993 | |
| 994 | if data: |
| 995 | for ksus in data: |
| 996 | if ksus["_id"] != _id: |
| 997 | for okas in ksus["oka"]: |
| shahithya | 8bded11 | 2024-10-15 08:01:44 +0000 | [diff] [blame] | 998 | self.logger.info("OKA: {}".format(okas)) |
| 999 | if okas.get("sw_catalog_path", ""): |
| 1000 | continue |
| 1001 | elif okas["_id"] not in existing_oka: |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1002 | existing_oka.append(okas["_id"]) |
| 1003 | |
| 1004 | if used_oka: |
| 1005 | for oka, oka_id in used_oka.items(): |
| 1006 | if oka_id not in existing_oka: |
| 1007 | self.db.set_one( |
| 1008 | self.okapkg_topic, |
| 1009 | {"_id": oka_id}, |
| 1010 | {"_admin.usageState": "NOT_IN_USE"}, |
| 1011 | ) |
| garciadeblas | ac28587 | 2024-12-05 12:21:09 +0100 | [diff] [blame] | 1012 | # Check if the profile exists. If it doesn't, no message should be sent to Kafka |
| 1013 | not_send_msg = None |
| 1014 | profile_id = item_content["profile"]["_id"] |
| 1015 | profile_type = item_content["profile"]["profile_type"] |
| 1016 | profile_collection_map = { |
| 1017 | "app_profiles": "k8sapp", |
| 1018 | "resource_profiles": "k8sresource", |
| 1019 | "infra_controller_profiles": "k8sinfra_controller", |
| 1020 | "infra_config_profiles": "k8sinfra_config", |
| 1021 | } |
| 1022 | profile_collection = profile_collection_map[profile_type] |
| 1023 | profile_content = self.db.get_one( |
| 1024 | profile_collection, {"_id": profile_id}, fail_on_empty=False |
| 1025 | ) |
| 1026 | if not profile_content: |
| 1027 | self.db.del_one(self.topic, filter_q) |
| 1028 | not_send_msg = True |
| 1029 | return op_id, not_send_msg |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1030 | |
| 1031 | |
| 1032 | class OkaTopic(DescriptorTopic): |
| 1033 | topic = "okas" |
| 1034 | topic_msg = "oka" |
| 1035 | schema_new = oka_schema |
| 1036 | schema_edit = oka_schema |
| 1037 | |
| 1038 | def __init__(self, db, fs, msg, auth): |
| 1039 | super().__init__(db, fs, msg, auth) |
| 1040 | self.logger = logging.getLogger("nbi.oka") |
| 1041 | |
| 1042 | @staticmethod |
| 1043 | def format_on_new(content, project_id=None, make_public=False): |
| 1044 | DescriptorTopic.format_on_new( |
| 1045 | content, project_id=project_id, make_public=make_public |
| 1046 | ) |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 1047 | content["current_operation"] = None |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1048 | content["state"] = "PENDING_CONTENT" |
| 1049 | content["operatingState"] = "PROCESSING" |
| 1050 | content["resourceState"] = "IN_PROGRESS" |
| 1051 | |
| 1052 | def check_conflict_on_del(self, session, _id, db_content): |
| 1053 | usage_state = db_content["_admin"]["usageState"] |
| 1054 | if usage_state == "IN_USE": |
| 1055 | raise EngineException( |
| 1056 | "There is a KSU using this package", |
| 1057 | http_code=HTTPStatus.CONFLICT, |
| 1058 | ) |
| 1059 | |
| 1060 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| 1061 | if ( |
| 1062 | final_content["name"] == edit_content["name"] |
| 1063 | and final_content["description"] == edit_content["description"] |
| 1064 | ): |
| 1065 | raise EngineException( |
| 1066 | "No update", |
| 1067 | http_code=HTTPStatus.CONFLICT, |
| 1068 | ) |
| 1069 | if final_content["name"] != edit_content["name"]: |
| 1070 | self.check_unique_name(session, edit_content["name"]) |
| 1071 | return final_content |
| 1072 | |
| 1073 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| 1074 | indata = self._remove_envelop(indata) |
| 1075 | |
| 1076 | # Override descriptor with query string kwargs |
| 1077 | if kwargs: |
| 1078 | self._update_input_with_kwargs(indata, kwargs) |
| 1079 | try: |
| 1080 | if indata and session.get("set_project"): |
| 1081 | raise EngineException( |
| 1082 | "Cannot edit content and set to project (query string SET_PROJECT) at same time", |
| 1083 | HTTPStatus.UNPROCESSABLE_ENTITY, |
| 1084 | ) |
| 1085 | # TODO self._check_edition(session, indata, _id, force) |
| 1086 | if not content: |
| 1087 | content = self.show(session, _id) |
| 1088 | |
| 1089 | indata = self._validate_input_edit(indata, content, force=session["force"]) |
| 1090 | |
| 1091 | # To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name |
| 1092 | _id = content.get("_id") or _id |
| 1093 | |
| 1094 | content = self.check_conflict_on_edit(session, content, indata, _id=_id) |
| 1095 | op_id = self.format_on_edit(content, indata) |
| 1096 | deep_update_rfc7396(content, indata) |
| 1097 | |
| 1098 | self.db.replace(self.topic, _id, content) |
| 1099 | return op_id |
| 1100 | except ValidationError as e: |
| 1101 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 1102 | |
| 1103 | def delete(self, session, _id, dry_run=False, not_send_msg=None): |
| 1104 | if not self.multiproject: |
| 1105 | filter_q = {} |
| 1106 | else: |
| 1107 | filter_q = self._get_project_filter(session) |
| 1108 | filter_q[self.id_field(self.topic, _id)] = _id |
| 1109 | item_content = self.db.get_one(self.topic, filter_q) |
| 1110 | item_content["state"] = "IN_DELETION" |
| 1111 | item_content["operatingState"] = "PROCESSING" |
| 1112 | self.check_conflict_on_del(session, _id, item_content) |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 1113 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1114 | item_content, |
| 1115 | "delete", |
| 1116 | None, |
| 1117 | ) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1118 | self.db.set_one(self.topic, {"_id": item_content["_id"]}, item_content) |
| 1119 | self._send_msg( |
| 1120 | "delete", {"oka_id": _id, "operation_id": op_id}, not_send_msg=not_send_msg |
| 1121 | ) |
| yshah | ffcac5f | 2024-08-19 12:49:07 +0000 | [diff] [blame] | 1122 | return op_id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1123 | |
| 1124 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| 1125 | # _remove_envelop |
| 1126 | if indata: |
| 1127 | if "userDefinedData" in indata: |
| 1128 | indata = indata["userDefinedData"] |
| 1129 | |
| 1130 | content = {"_admin": {"userDefinedData": indata, "revision": 0}} |
| 1131 | |
| 1132 | self._update_input_with_kwargs(content, kwargs) |
| 1133 | content = BaseTopic._validate_input_new( |
| 1134 | self, input=kwargs, force=session["force"] |
| 1135 | ) |
| 1136 | |
| 1137 | self.check_unique_name(session, content["name"]) |
| 1138 | operation_params = {} |
| 1139 | for content_key, content_value in content.items(): |
| 1140 | operation_params[content_key] = content_value |
| 1141 | self.format_on_new( |
| 1142 | content, session["project_id"], make_public=session["public"] |
| 1143 | ) |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 1144 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1145 | content, |
| 1146 | operation_type="create", |
| 1147 | operation_params=operation_params, |
| 1148 | ) |
| 1149 | content["git_name"] = self.create_gitname(content, session) |
| 1150 | _id = self.db.create(self.topic, content) |
| 1151 | rollback.append({"topic": self.topic, "_id": _id}) |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 1152 | return _id, op_id |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1153 | |
| 1154 | def upload_content(self, session, _id, indata, kwargs, headers): |
| 1155 | current_desc = self.show(session, _id) |
| 1156 | |
| 1157 | compressed = None |
| 1158 | content_type = headers.get("Content-Type") |
| 1159 | if ( |
| 1160 | content_type |
| 1161 | and "application/gzip" in content_type |
| 1162 | or "application/x-gzip" in content_type |
| 1163 | ): |
| 1164 | compressed = "gzip" |
| 1165 | if content_type and "application/zip" in content_type: |
| 1166 | compressed = "zip" |
| 1167 | filename = headers.get("Content-Filename") |
| 1168 | if not filename and compressed: |
| 1169 | filename = "package.tar.gz" if compressed == "gzip" else "package.zip" |
| 1170 | elif not filename: |
| 1171 | filename = "package" |
| 1172 | |
| 1173 | revision = 1 |
| 1174 | if "revision" in current_desc["_admin"]: |
| 1175 | revision = current_desc["_admin"]["revision"] + 1 |
| 1176 | |
| 1177 | file_pkg = None |
| 1178 | fs_rollback = [] |
| 1179 | |
| 1180 | try: |
| 1181 | start = 0 |
| 1182 | # Rather than using a temp folder, we will store the package in a folder based on |
| 1183 | # the current revision. |
| 1184 | proposed_revision_path = _id + ":" + str(revision) |
| 1185 | # all the content is upload here and if ok, it is rename from id_ to is folder |
| 1186 | |
| 1187 | if start: |
| 1188 | if not self.fs.file_exists(proposed_revision_path, "dir"): |
| 1189 | raise EngineException( |
| 1190 | "invalid Transaction-Id header", HTTPStatus.NOT_FOUND |
| 1191 | ) |
| 1192 | else: |
| 1193 | self.fs.file_delete(proposed_revision_path, ignore_non_exist=True) |
| 1194 | self.fs.mkdir(proposed_revision_path) |
| 1195 | fs_rollback.append(proposed_revision_path) |
| 1196 | |
| 1197 | storage = self.fs.get_params() |
| 1198 | storage["folder"] = proposed_revision_path |
| yshah | 2c932bd | 2024-09-24 18:16:07 +0000 | [diff] [blame] | 1199 | storage["zipfile"] = filename |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1200 | |
| 1201 | file_path = (proposed_revision_path, filename) |
| 1202 | file_pkg = self.fs.file_open(file_path, "a+b") |
| 1203 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1204 | if isinstance(indata, dict): |
| 1205 | indata_text = yaml.safe_dump(indata, indent=4, default_flow_style=False) |
| 1206 | file_pkg.write(indata_text.encode(encoding="utf-8")) |
| 1207 | else: |
| 1208 | indata_len = 0 |
| 1209 | indata = indata.file |
| 1210 | while True: |
| 1211 | indata_text = indata.read(4096) |
| 1212 | indata_len += len(indata_text) |
| 1213 | if not indata_text: |
| 1214 | break |
| 1215 | file_pkg.write(indata_text) |
| 1216 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1217 | # Need to close the file package here so it can be copied from the |
| 1218 | # revision to the current, unrevisioned record |
| 1219 | if file_pkg: |
| 1220 | file_pkg.close() |
| 1221 | file_pkg = None |
| 1222 | |
| 1223 | # Fetch both the incoming, proposed revision and the original revision so we |
| 1224 | # can call a validate method to compare them |
| 1225 | current_revision_path = _id + "/" |
| 1226 | self.fs.sync(from_path=current_revision_path) |
| 1227 | self.fs.sync(from_path=proposed_revision_path) |
| 1228 | |
| garciadeblas | 807b8bf | 2024-09-23 13:03:00 +0200 | [diff] [blame] | 1229 | # Is this required? |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1230 | if revision > 1: |
| 1231 | try: |
| 1232 | self._validate_descriptor_changes( |
| 1233 | _id, |
| 1234 | filename, |
| 1235 | current_revision_path, |
| 1236 | proposed_revision_path, |
| 1237 | ) |
| 1238 | except Exception as e: |
| 1239 | shutil.rmtree( |
| 1240 | self.fs.path + current_revision_path, ignore_errors=True |
| 1241 | ) |
| 1242 | shutil.rmtree( |
| 1243 | self.fs.path + proposed_revision_path, ignore_errors=True |
| 1244 | ) |
| 1245 | # Only delete the new revision. We need to keep the original version in place |
| 1246 | # as it has not been changed. |
| 1247 | self.fs.file_delete(proposed_revision_path, ignore_non_exist=True) |
| 1248 | raise e |
| 1249 | |
| 1250 | indata = self._remove_envelop(indata) |
| 1251 | |
| 1252 | # Override descriptor with query string kwargs |
| 1253 | if kwargs: |
| 1254 | self._update_input_with_kwargs(indata, kwargs) |
| 1255 | |
| 1256 | current_desc["_admin"]["storage"] = storage |
| 1257 | current_desc["_admin"]["onboardingState"] = "ONBOARDED" |
| 1258 | current_desc["_admin"]["operationalState"] = "ENABLED" |
| 1259 | current_desc["_admin"]["modified"] = time() |
| 1260 | current_desc["_admin"]["revision"] = revision |
| 1261 | |
| 1262 | deep_update_rfc7396(current_desc, indata) |
| 1263 | |
| 1264 | # Copy the revision to the active package name by its original id |
| 1265 | shutil.rmtree(self.fs.path + current_revision_path, ignore_errors=True) |
| 1266 | os.rename( |
| 1267 | self.fs.path + proposed_revision_path, |
| 1268 | self.fs.path + current_revision_path, |
| 1269 | ) |
| 1270 | self.fs.file_delete(current_revision_path, ignore_non_exist=True) |
| 1271 | self.fs.mkdir(current_revision_path) |
| 1272 | self.fs.reverse_sync(from_path=current_revision_path) |
| 1273 | |
| 1274 | shutil.rmtree(self.fs.path + _id) |
| 1275 | kwargs = {} |
| 1276 | kwargs["package"] = filename |
| 1277 | if headers["Method"] == "POST": |
| 1278 | current_desc["state"] = "IN_CREATION" |
| garciadeblas | becc705 | 2024-11-20 12:04:53 +0100 | [diff] [blame] | 1279 | op_id = current_desc.get("operationHistory", [{"op_id": None}])[-1].get( |
| 1280 | "op_id" |
| 1281 | ) |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1282 | elif headers["Method"] in ("PUT", "PATCH"): |
| yshah | d0c876f | 2024-11-11 09:24:48 +0000 | [diff] [blame] | 1283 | op_id = self.format_on_operation( |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1284 | current_desc, |
| 1285 | "update", |
| 1286 | kwargs, |
| 1287 | ) |
| 1288 | current_desc["operatingState"] = "PROCESSING" |
| 1289 | current_desc["resourceState"] = "IN_PROGRESS" |
| 1290 | |
| 1291 | self.db.replace(self.topic, _id, current_desc) |
| 1292 | |
| 1293 | # Store a copy of the package as a point in time revision |
| 1294 | revision_desc = dict(current_desc) |
| 1295 | revision_desc["_id"] = _id + ":" + str(revision_desc["_admin"]["revision"]) |
| 1296 | self.db.create(self.topic + "_revisions", revision_desc) |
| 1297 | fs_rollback = [] |
| 1298 | |
| yshah | 53cc9eb | 2024-07-05 13:06:31 +0000 | [diff] [blame] | 1299 | if headers["Method"] == "POST": |
| 1300 | self._send_msg("create", {"oka_id": _id, "operation_id": op_id}) |
| 1301 | elif headers["Method"] == "PUT" or "PATCH": |
| 1302 | self._send_msg("edit", {"oka_id": _id, "operation_id": op_id}) |
| 1303 | |
| 1304 | return True |
| 1305 | |
| 1306 | except EngineException: |
| 1307 | raise |
| 1308 | finally: |
| 1309 | if file_pkg: |
| 1310 | file_pkg.close() |
| 1311 | for file in fs_rollback: |
| 1312 | self.fs.file_delete(file, ignore_non_exist=True) |