def update_timeperiod(params): """Update a time period""" body = params['body'] name = params['name'] if name == "24X7": raise ProblemException(405, http.client.responses[405], "You cannot change the built-in time period") time_period = load_timeperiod(name) if time_period is None: raise ProblemException(404, http.client.responses[404], f"Time period {name} not found") if "exceptions" in body: time_period = dict( (key, time_period[key]) for key in [*defines.weekday_ids(), "alias", "exclude"] if key in time_period) time_period["exceptions"] = _format_exceptions(body["exceptions"]) if "alias" in body: time_period['alias'] = body['alias'] if "active_time_ranges" in body: time_period.update(_daily_time_ranges(body['active_time_ranges'])) if "exclude" in body: time_period["exclude"] = body["exclude"] save_timeperiod(name, time_period) return Response(status=204)
def update_timeperiod(params): """Update a time period""" body = params["body"] name = params["name"] if name == "24X7": raise ProblemException(405, http.client.responses[405], "You cannot change the built-in time period") try: time_period = load_timeperiod(name) except KeyError: raise ProblemException(404, http.client.responses[404], f"Time period {name} not found") time_period = _to_api_format(time_period, internal_format=True) updated_time_period = _to_checkmk_format( alias=body.get("alias", time_period["alias"]), periods=_daily_time_ranges( body.get("active_time_ranges", time_period["active_time_ranges"])), exceptions=_format_exceptions( body.get("exceptions", time_period["exceptions"])), exclude=body.get("exclude", time_period["exclude"]), ) save_timeperiod(name, updated_time_period) return Response(status=204)
def create_timeperiod(params): """Create a time period""" body = params["body"] name = body["name"] exceptions = _format_exceptions(body.get("exceptions", [])) periods = _daily_time_ranges(body["active_time_ranges"]) time_period = _to_checkmk_format( alias=body["alias"], periods=periods, exceptions=exceptions, exclude=body.get("exclude", []) ) save_timeperiod(name, time_period) return _serve_time_period(_to_api_format(load_timeperiod(name)))
def create_timeperiod(params): """Create a time period""" body = params['body'] name = body['name'] exceptions = _format_exceptions(body.get("exceptions", [])) periods = _daily_time_ranges(body["active_time_ranges"]) time_period = _time_period(alias=body['alias'], periods=periods, exceptions=exceptions, exclude=body.get("exclude", [])) save_timeperiod(name, time_period) return _serve_time_period(_readable_format(load_timeperiod(name)))
def test_openapi_timeperiod_complex_update(wsgi_app, with_automation_user): username, secret = with_automation_user wsgi_app.set_authorization(("Bearer", username + " " + secret)) base = "/NO_SITE/check_mk/api/1.0" _resp = wsgi_app.call_method( "post", base + "/domain-types/time_period/collections/all", params=json.dumps({ "active_time_ranges": [{ "day": "all", "time_ranges": [ { "end": "12:30", "start": "08:00" }, { "end": "17:00", "start": "13:30" }, ], }], "alias": "Test All days 8x5", "exceptions": [{ "date": "2021-04-01", "time_ranges": [{ "end": "15:00", "start": "14:00" }] }], "name": "test_all_8x5", }), status=200, content_type="application/json", ) _resp = wsgi_app.call_method( "put", base + "/objects/time_period/test_all_8x5", params=json.dumps({ "active_time_ranges": [{ "day": "all", "time_ranges": [ { "end": "12:30", "start": "08:00" }, { "end": "17:00", "start": "13:30" }, ], }], "alias": "Test All days 8x5 z", "exceptions": [{ "date": "2021-04-01", "time_ranges": [{ "end": "15:00", "start": "14:00" }] }], }), status=204, content_type="application/json", ) internal_timeperiod = load_timeperiod("test_all_8x5") assert internal_timeperiod == { "alias": "Test All days 8x5 z", "2021-04-01": [("14:00", "15:00")], "monday": [("08:00", "12:30"), ("13:30", "17:00")], "tuesday": [("08:00", "12:30"), ("13:30", "17:00")], "wednesday": [("08:00", "12:30"), ("13:30", "17:00")], "thursday": [("08:00", "12:30"), ("13:30", "17:00")], "friday": [("08:00", "12:30"), ("13:30", "17:00")], "saturday": [("08:00", "12:30"), ("13:30", "17:00")], "sunday": [("08:00", "12:30"), ("13:30", "17:00")], "exclude": [], }
def test_openapi_timeperiod_complex_update(wsgi_app, with_automation_user, suppress_automation_calls): username, secret = with_automation_user wsgi_app.set_authorization(('Bearer', username + " " + secret)) base = '/NO_SITE/check_mk/api/1.0' _resp = wsgi_app.call_method( 'post', base + "/domain-types/time_period/collections/all", params=json.dumps({ 'active_time_ranges': [{ 'day': 'all', 'time_ranges': [{ 'end': '12:30', 'start': '08:00' }, { 'end': '17:00', 'start': '13:30' }] }], 'alias': 'Test All days 8x5', 'exceptions': [{ 'date': '2021-04-01', 'time_ranges': [{ 'end': '15:00', 'start': '14:00' }] }], 'name': 'test_all_8x5' }), status=200, content_type='application/json', ) _resp = wsgi_app.call_method( 'put', base + "/objects/time_period/test_all_8x5", params=json.dumps({ 'active_time_ranges': [{ 'day': 'all', 'time_ranges': [{ 'end': '12:30', 'start': '08:00' }, { 'end': '17:00', 'start': '13:30' }] }], 'alias': 'Test All days 8x5 z', 'exceptions': [{ 'date': '2021-04-01', 'time_ranges': [{ 'end': '15:00', 'start': '14:00' }] }] }), status=204, content_type='application/json', ) internal_timeperiod = load_timeperiod("test_all_8x5") assert internal_timeperiod == { 'alias': 'Test All days 8x5 z', '2021-04-01': [('14:00', '15:00')], 'monday': [('08:00', '12:30'), ('13:30', '17:00')], 'tuesday': [('08:00', '12:30'), ('13:30', '17:00')], 'wednesday': [('08:00', '12:30'), ('13:30', '17:00')], 'thursday': [('08:00', '12:30'), ('13:30', '17:00')], 'friday': [('08:00', '12:30'), ('13:30', '17:00')], 'saturday': [('08:00', '12:30'), ('13:30', '17:00')], 'sunday': [('08:00', '12:30'), ('13:30', '17:00')], 'exclude': [], }