def delete_lease(self, lease_id): lease = self.get_lease(lease_id) if (datetime.datetime.utcnow() < lease['start_date'] or datetime.datetime.utcnow() > lease['end_date']): with trusts.create_ctx_from_trust(lease['trust_id']) as ctx: for reservation in lease['reservations']: plugin = self.plugins[reservation['resource_type']] try: plugin.on_end(reservation['resource_id']) except (db_ex.ClimateDBException, RuntimeError): error_msg = "Failed to delete a reservation for a lease." lease_state = states.LeaseState(id=lease_id, action=states.lease.DELETE, status=states.lease.FAILED, status_reason=error_msg) lease_state.save() LOG.exception(error_msg) raise db_api.lease_destroy(lease_id) self._send_notification(lease, ctx, events=['delete']) else: error_msg = "Already started lease cannot be deleted" lease_state = states.LeaseState(id=lease_id, action=states.lease.DELETE, status=states.lease.FAILED, status_reason=error_msg) lease_state.save() raise common_ex.NotAuthorized(error_msg)
def delete_lease(self, lease_id): lease = self.get_lease(lease_id) if datetime.datetime.utcnow() < lease["start_date"] or datetime.datetime.utcnow() > lease["end_date"]: for reservation in lease["reservations"]: try: self.plugins[reservation["resource_type"]].on_end(reservation["resource_id"]) except RuntimeError: LOG.exception("Failed to delete a reservation") db_api.lease_destroy(lease_id) else: raise exceptions.NotAuthorized("Already started lease cannot be deleted")
def create_lease(self, lease_values): """Create a lease with reservations. Return either the model of created lease or None if any error. """ # Remove and keep reservation values reservations = lease_values.pop("reservations", []) # Create the lease without the reservations start_date = lease_values["start_date"] end_date = lease_values["end_date"] if start_date == "now": start_date = datetime.datetime.utcnow() else: start_date = datetime.datetime.strptime(start_date, "%Y-%m-%d %H:%M") end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d %H:%M") ctx = context.current() lease_values["user_id"] = ctx.user_id lease_values["tenant_id"] = ctx.tenant_id lease_values["start_date"] = start_date lease_values["end_date"] = end_date if not lease_values.get("events"): lease_values["events"] = [] lease_values["events"].append({"event_type": "start_lease", "time": start_date, "status": "UNDONE"}) lease_values["events"].append({"event_type": "end_lease", "time": end_date, "status": "UNDONE"}) # TODO(scroiset): catch DB Exception when they will become alive # see bug #1237293 try: lease = db_api.lease_create(lease_values) lease_id = lease["id"] except RuntimeError: LOG.exception("Cannot create a lease") else: try: for reservation in reservations: reservation["lease_id"] = lease["id"] reservation["start_date"] = lease["start_date"] reservation["end_date"] = lease["end_date"] resource_type = reservation["resource_type"] self.plugins[resource_type].create_reservation(reservation) except RuntimeError: LOG.exception( "Failed to create reservation for a lease. " "Rollback the lease and associated reservations" ) db_api.lease_destroy(lease_id) else: return db_api.lease_get(lease["id"])
def delete_lease(self, lease_id): lease = self.get_lease(lease_id) if (datetime.datetime.now() < lease['start_date'] or datetime.datetime.now() > lease['end_date']): with trusts.create_ctx_from_trust(lease['trust_id']) as ctx: for reservation in lease['reservations']: plugin = self.plugins[reservation['resource_type']] try: plugin.on_end(reservation['resource_id']) except (db_ex.ClimateDBException, RuntimeError): LOG.exception("Failed to delete a reservation " "for a lease.") raise db_api.lease_destroy(lease_id) self._send_notification(lease, ctx, events=['delete']) else: raise common_ex.NotAuthorized( 'Already started lease cannot be deleted')
def delete_lease(self, lease_id): lease = self.get_lease(lease_id) if (datetime.datetime.utcnow() < lease['start_date'] or datetime.datetime.utcnow() > lease['end_date']): with trusts.create_ctx_from_trust(lease['trust_id']) as ctx: for reservation in lease['reservations']: plugin = self.plugins[reservation['resource_type']] try: plugin.on_end(reservation['resource_id']) except (db_ex.ClimateDBException, RuntimeError): LOG.exception("Failed to delete a reservation " "for a lease.") raise db_api.lease_destroy(lease_id) self._send_notification(lease, ctx, events=['delete']) else: raise common_ex.NotAuthorized( 'Already started lease cannot be deleted')
def create_lease(self, lease_values): """Create a lease with reservations. Return either the model of created lease or None if any error. """ try: trust_id = lease_values.pop('trust_id') except KeyError: raise exceptions.MissingTrustId() # Remove and keep reservation values reservations = lease_values.pop("reservations", []) # Create the lease without the reservations start_date = lease_values['start_date'] end_date = lease_values['end_date'] now = datetime.datetime.now() now = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute) if start_date == 'now': start_date = now else: start_date = self._date_from_string(start_date) end_date = self._date_from_string(end_date) if start_date < now: raise common_ex.NotAuthorized( 'Start date must later than current date') with trusts.create_ctx_from_trust(trust_id) as ctx: lease_values['user_id'] = ctx.user_id lease_values['project_id'] = ctx.project_id lease_values['start_date'] = start_date lease_values['end_date'] = end_date if not lease_values.get('events'): lease_values['events'] = [] lease_values['events'].append({'event_type': 'start_lease', 'time': start_date, 'status': 'UNDONE'}) lease_values['events'].append({'event_type': 'end_lease', 'time': end_date, 'status': 'UNDONE'}) before_end_date = lease_values.get('before_end_notification', None) if before_end_date: # incoming param. Validation check try: before_end_date = self._date_from_string( before_end_date) self._check_date_within_lease_limits(before_end_date, lease_values) except common_ex.ClimateException as e: LOG.error("Invalid before_end_date param. %s" % e.message) raise e elif CONF.manager.notify_hours_before_lease_end > 0: delta = datetime.timedelta( hours=CONF.manager.notify_hours_before_lease_end) before_end_date = lease_values['end_date'] - delta if before_end_date: event = {'event_type': 'before_end_lease', 'status': 'UNDONE'} lease_values['events'].append(event) self._update_before_end_event_date(event, before_end_date, lease_values) try: if trust_id: lease_values.update({'trust_id': trust_id}) lease = db_api.lease_create(lease_values) lease_id = lease['id'] except db_ex.ClimateDBDuplicateEntry: LOG.exception('Cannot create a lease - duplicated lease name') raise exceptions.LeaseNameAlreadyExists( name=lease_values['name']) except db_ex.ClimateDBException: LOG.exception('Cannot create a lease') raise else: try: for reservation in reservations: reservation['lease_id'] = lease['id'] reservation['start_date'] = lease['start_date'] reservation['end_date'] = lease['end_date'] resource_type = reservation['resource_type'] if resource_type in self.plugins: self.plugins[resource_type].create_reservation( reservation) else: raise exceptions.UnsupportedResourceType( resource_type) except (exceptions.UnsupportedResourceType, common_ex.ClimateException): LOG.exception("Failed to create reservation for a lease. " "Rollback the lease and associated " "reservations") db_api.lease_destroy(lease_id) raise else: lease = db_api.lease_get(lease['id']) self._send_notification(lease, ctx, events=['create']) return lease
def create_lease(self, lease_values): """Create a lease with reservations. Return either the model of created lease or None if any error. """ try: trust_id = lease_values.pop('trust_id') except KeyError: raise exceptions.MissingTrustId() # Remove and keep reservation values reservations = lease_values.pop("reservations", []) # Create the lease without the reservations start_date = lease_values['start_date'] end_date = lease_values['end_date'] now = datetime.datetime.utcnow() now = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute) if start_date == 'now': start_date = now else: start_date = self._date_from_string(start_date) end_date = self._date_from_string(end_date) if start_date < now: raise common_ex.NotAuthorized( 'Start date must later than current date') with trusts.create_ctx_from_trust(trust_id) as ctx: lease_values['user_id'] = ctx.user_id lease_values['project_id'] = ctx.project_id lease_values['start_date'] = start_date lease_values['end_date'] = end_date if not lease_values.get('events'): lease_values['events'] = [] lease_values['events'].append({'event_type': 'start_lease', 'time': start_date, 'status': 'UNDONE'}) lease_values['events'].append({'event_type': 'end_lease', 'time': end_date, 'status': 'UNDONE'}) before_end_date = lease_values.get('before_end_notification', None) if before_end_date: # incoming param. Validation check try: before_end_date = self._date_from_string( before_end_date) self._check_date_within_lease_limits(before_end_date, lease_values) except common_ex.ClimateException as e: LOG.error("Invalid before_end_date param. %s" % e.message) raise e elif CONF.manager.notify_hours_before_lease_end > 0: delta = datetime.timedelta( hours=CONF.manager.notify_hours_before_lease_end) before_end_date = lease_values['end_date'] - delta if before_end_date: event = {'event_type': 'before_end_lease', 'status': 'UNDONE'} lease_values['events'].append(event) self._update_before_end_event_date(event, before_end_date, lease_values) try: if trust_id: lease_values.update({'trust_id': trust_id}) lease = db_api.lease_create(lease_values) lease_id = lease['id'] except db_ex.ClimateDBDuplicateEntry: LOG.exception('Cannot create a lease - duplicated lease name') raise exceptions.LeaseNameAlreadyExists( name=lease_values['name']) except db_ex.ClimateDBException: LOG.exception('Cannot create a lease') raise else: try: for reservation in reservations: reservation['lease_id'] = lease['id'] reservation['start_date'] = lease['start_date'] reservation['end_date'] = lease['end_date'] resource_type = reservation['resource_type'] if resource_type in self.plugins: self.plugins[resource_type].create_reservation( reservation) else: raise exceptions.UnsupportedResourceType( resource_type) except (exceptions.UnsupportedResourceType, common_ex.ClimateException): LOG.exception("Failed to create reservation for a lease. " "Rollback the lease and associated " "reservations") db_api.lease_destroy(lease_id) raise else: lease_state = states.LeaseState(id=lease['id'], action=states.lease.CREATE, status=states.lease.COMPLETE, status_reason="Successfully created lease") lease_state.save() lease = db_api.lease_get(lease['id']) self._send_notification(lease, ctx, events=['create']) return lease
def delete_lease(self, lease_id): lease = self.get_lease(lease_id) for reservation in lease['reservations']: self.plugins[reservation['resource_type']]\ .on_end(reservation['resource_id']) db_api.lease_destroy(lease_id)