def put(capsule_id, user): capsule = _get_capsule(capsule_id, user) webapp = capsule.webapp webapp_data = request.get_json() # PUT become POST if there is no webapp if webapp is None: return post(capsule_id=capsule_id, webapp_data=webapp_data) data = webapp_schema.load(webapp_data) webapp.env = None if "env" in webapp_data: webapp.env = webapp_data["env"] # Ensure new runtime_id has same familly new_runtime_id = str(data["runtime_id"]) try: new_runtime = Runtime.query.get(new_runtime_id) except StatementError: raise BadRequest(description=f"'{new_runtime_id}' is not a valid id.") if new_runtime is None: raise BadRequest(description=f"The runtime_id '{new_runtime_id}' " "does not exist.") new_fam = str(new_runtime.fam) old_fam = str(webapp.runtime.fam) if new_fam != old_fam: raise BadRequest(f"Changing runtime familly from '{old_fam}' " f"to '{new_fam}' is not possible") webapp.runtime_id = data["runtime_id"] webapp.opts = [] if "opts" in data: opts = Option.create(data["opts"], data["runtime_id"], user.role) webapp.opts = opts if "volume_size" in data: if (user.role is RoleEnum.user) and (not user.parts_manager): raise Forbidden(description='You cannot set webapp volume size.') remaining_size = getWebappsVolumeUsage(str(webapp.id)) target_size = data['volume_size'] + remaining_size if target_size > current_app.config['VOLUMES_GLOBAL_SIZE']: msg = 'Please set a lower volume size for this webapp or prepare '\ 'some Bitcoins... :-)' raise PaymentRequired(description=msg) webapp.volume_size = data['volume_size'] capsule.webapp = webapp db.session.commit() now = datetime.datetime.now() if now > (capsule.no_update + datetime.timedelta(hours=24)): nats.publish_webapp_present(capsule) return get(capsule_id)
def post(capsule_id, user, addon_data=None): capsule = _get_capsule(capsule_id, user) if addon_data is None: addon_data = request.get_json() data = addon_schema.load(addon_data) runtime_id = data["runtime_id"] runtime = Runtime.query.get(runtime_id) if runtime is None: raise BadRequest(description=f"The runtime_id '{runtime_id}' " "does not exist.") if runtime.runtime_type is not RuntimeTypeEnum.addon: raise BadRequest(description=f"The runtime_id '{runtime.id}' " "has not type 'addon'.") if "opts" in data: opts = Option.create(data["opts"], runtime_id, user.role) data.pop("opts") addon = AddOn(**data, opts=opts) else: addon = AddOn(**data) (addon.uri, addon.name) = runtime.generate_uri_dbname(capsule) # HACK: if addon.name is not setted, we will use addon.id after commit if addon.name is None: addon.name = "changeme" capsule.addons.append(addon) db.session.add(addon) db.session.commit() if addon.name == "changeme": addon.name = str(addon.id) db.session.commit() if addon.description is None: addon.description = addon.name db.session.commit() now = datetime.datetime.now() if now > (capsule.no_update + datetime.timedelta(hours=24)): nats.publish_addon_present(addon, capsule.name) result_json = addon_schema.dump(addon) return result_json, 201, { 'Location': f'{request.base_url}/capsules/{capsule_id}/addons/{addon.id}', }
def put(capsule_id, addon_id, user): capsule = _get_capsule(capsule_id, user) addon_data = request.get_json() data = addon_schema.load(addon_data) try: addon = AddOn.query.get(addon_id) except StatementError: raise BadRequest(description=f"'{addon_id}' is not a valid id.") if not addon: raise NotFound(description=f"The requested addon '{addon_id}' " "has not been found.") if str(addon.capsule.id) != capsule_id: raise Forbidden(description="bad capsule id") addon.description = data["description"] if data["runtime_id"] != str(addon.runtime_id): raise BadRequest(description="The runtime_id cannot be changed.") addon.runtime_id = data["runtime_id"] addon.env = data["env"] if "opts" in data: opts = Option.create(data["opts"], data['runtime_id'], user.role) addon.opts = opts else: addon.opts = [] if addon.description is None: addon.description = addon.name db.session.commit() now = datetime.datetime.now() if now > (capsule.no_update + datetime.timedelta(hours=24)): nats.publish_addon_present(addon, capsule.name) return get(capsule_id, addon_id)
def post(capsule_id, user, webapp_data=None): capsule = _get_capsule(capsule_id, user) if len(capsule.fqdns) == 0: raise Conflict(description="A webapp need at least one FQDN.") webapp = capsule.webapp # Only one webapp per capsule if webapp is not None: raise Conflict(description="This capsule already has a webapp.") # Datas could come from PUT if webapp is None: webapp_data = request.get_json() data = webapp_schema.load(webapp_data) runtime_id = data["runtime_id"] try: runtime = Runtime.query.get(runtime_id) except StatementError: raise BadRequest(description=f"'{runtime_id}' is not a valid id.") if runtime is None: raise BadRequest(description=f"The runtime_id '{runtime_id}' " "does not exist.") if runtime.runtime_type is not RuntimeTypeEnum.webapp: raise BadRequest(description=f"The runtime_id '{runtime.id}' " "has not type 'webapp'.") newArgs = dict() if "opts" in data: opts = Option.create(data["opts"], runtime_id, user.role) data.pop("opts") newArgs["opts"] = opts if "volume_size" not in data: data['volume_size'] = current_app.config['VOLUMES_DEFAULT_SIZE'] else: if (user.role is RoleEnum.user) and (not user.parts_manager): raise Forbidden(description='You cannot set webapp volume size.') remaining_size = getWebappsVolumeUsage() target_size = data['volume_size'] + remaining_size if target_size > current_app.config['VOLUMES_GLOBAL_SIZE']: msg = 'Please set a lower volume size for this webapp or prepare '\ 'some Bitcoins... :-)' raise PaymentRequired(description=msg) webapp = WebApp(**data, **newArgs) capsule.webapp = webapp db.session.add(webapp) db.session.commit() result = WebApp.query.get(capsule.webapp_id) now = datetime.datetime.now() if now > (capsule.no_update + datetime.timedelta(hours=24)): nats.publish_webapp_present(capsule) # Api response result_json = webapp_schema.dump(result) return result_json, 201, { 'Location': f'{request.base_url}/{capsule.id}/webapp', }