def with_host( request_context, with_admin_login, ): hostnames = ["heute", "example.com"] hosts_and_folders.CREFolder.root_folder().create_hosts([ (hostname, {}, None) for hostname in hostnames ]) yield hostnames hosts_and_folders.CREFolder.root_folder().delete_hosts( hostnames, automation=lambda *args, **kwargs: DeleteHostsResult())
def test_openapi_hosts( monkeypatch: pytest.MonkeyPatch, aut_user_auth_wsgi_app: WebTestAppForCMK, ): base = "/NO_SITE/check_mk/api/1.0" resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/host_config/collections/all", params='{"host_name": "foobar", "folder": "/"}', status=200, headers={"Accept": "application/json"}, content_type="application/json", ) assert isinstance(resp.json["extensions"]["attributes"]["meta_data"]["created_at"], str) assert isinstance(resp.json["extensions"]["attributes"]["meta_data"]["updated_at"], str) resp = aut_user_auth_wsgi_app.follow_link( resp, "self", status=200, headers={"Accept": "application/json"}, ) attributes = { "ipaddress": "127.0.0.1", "snmp_community": { "type": "v1_v2_community", "community": "blah", }, } resp = aut_user_auth_wsgi_app.follow_link( resp, ".../update", status=200, params=json.dumps({"attributes": attributes}), headers={"If-Match": resp.headers["ETag"], "Accept": "application/json"}, content_type="application/json", ) got_attributes = resp.json["extensions"]["attributes"] assert list(attributes.items()) <= list(got_attributes.items()) resp = aut_user_auth_wsgi_app.follow_link( resp, ".../update", status=200, params='{"update_attributes": {"alias": "bar"}}', headers={"If-Match": resp.headers["ETag"], "Accept": "application/json"}, content_type="application/json", ) assert resp.json["extensions"]["attributes"]["alias"] == "bar" resp = aut_user_auth_wsgi_app.follow_link( resp, ".../update", status=200, params='{"remove_attributes": ["alias"]}', headers={"If-Match": resp.headers["ETag"], "Accept": "application/json"}, content_type="application/json", ) assert list(resp.json["extensions"]["attributes"].items()) >= list( {"ipaddress": "127.0.0.1"}.items() ) assert "alias" not in resp.json["extensions"]["attributes"] # make sure changes are written to disk: resp = aut_user_auth_wsgi_app.follow_link( resp, "self", status=200, headers={"Accept": "application/json"}, ) assert list(resp.json["extensions"]["attributes"].items()) >= list( {"ipaddress": "127.0.0.1"}.items() ) # also try to update with wrong attribute aut_user_auth_wsgi_app.follow_link( resp, ".../update", status=400, params='{"attributes": {"foobaz": "bar"}}', headers={"If-Match": resp.headers["ETag"], "Accept": "application/json"}, content_type="application/json", ) monkeypatch.setattr( "cmk.gui.watolib.hosts_and_folders.delete_hosts", lambda *args, **kwargs: DeleteHostsResult(), ) aut_user_auth_wsgi_app.follow_link( resp, ".../delete", status=204, headers={"Accept": "application/json"}, content_type="application/json", )
def test_openapi_activate_changes( monkeypatch: pytest.MonkeyPatch, aut_user_auth_wsgi_app: WebTestAppForCMK, mock_livestatus: MockLiveStatusConnection, ): base = "/NO_SITE/check_mk/api/1.0" # We create a host live = mock_livestatus host_created = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/host_config/collections/all", params='{"host_name": "foobar", "folder": "/"}', headers={"Accept": "application/json"}, status=200, content_type="application/json", ) with live(expect_status_query=True): resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/activation_run/actions/activate-changes/invoke", status=400, params='{"sites": ["asdf"]}', headers={"Accept": "application/json"}, content_type="application/json", ) assert "Unknown site" in repr(resp.json), resp.json resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/activation_run/actions/activate-changes/invoke", status=200, headers={"Accept": "application/json"}, content_type="application/json", ) with live(expect_status_query=True): resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/activation_run/actions/activate-changes/invoke", status=302, params='{"redirect": true}', headers={"Accept": "application/json"}, content_type="application/json", ) for _ in range(10): resp = aut_user_auth_wsgi_app.follow_link( resp, CMK_WAIT_FOR_COMPLETION, ) if resp.status_code == 204: break # We delete the host again monkeypatch.setattr( "cmk.gui.plugins.openapi.endpoints.host_config.delete_hosts", lambda *args, **kwargs: DeleteHostsResult(), ) aut_user_auth_wsgi_app.follow_link( host_created, ".../delete", status=204, headers={ "If-Match": host_created.headers["ETag"], "Accept": "application/json" }, content_type="application/json", ) # And activate the changes with live(expect_status_query=True): resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/activation_run/actions/activate-changes/invoke", headers={"Accept": "application/json"}, content_type="application/json", ) for _ in range(10): resp = aut_user_auth_wsgi_app.follow_link( resp, CMK_WAIT_FOR_COMPLETION, headers={"Accept": "application/json"}, ) if resp.status_code == 204: break
def test_openapi_bulk_hosts( monkeypatch: pytest.MonkeyPatch, aut_user_auth_wsgi_app: WebTestAppForCMK, ): monkeypatch.setattr( "cmk.gui.watolib.hosts_and_folders.delete_hosts", lambda *args, **kwargs: DeleteHostsResult(), ) base = "/NO_SITE/check_mk/api/1.0" resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/host_config/actions/bulk-create/invoke", params=json.dumps( { "entries": [ { "host_name": "foobar", "folder": "/", "attributes": {"ipaddress": "127.0.0.2"}, }, { "host_name": "sample", "folder": "/", "attributes": { "ipaddress": "127.0.0.2", "site": "NO_SITE", }, }, ] } ), status=200, headers={"Accept": "application/json"}, content_type="application/json", ) assert len(resp.json["value"]) == 2 _resp = aut_user_auth_wsgi_app.call_method( "put", base + "/domain-types/host_config/actions/bulk-update/invoke", params=json.dumps( { "entries": [ { "host_name": "foobar", "attributes": { "ipaddress": "192.168.1.1", "tag_address_family": "ip-v4-only", }, } ], } ), status=200, headers={"Accept": "application/json"}, content_type="application/json", ) # verify attribute ipaddress is set corretly resp = aut_user_auth_wsgi_app.call_method( "get", base + "/objects/host_config/foobar", status=200, headers={"Accept": "application/json"}, ) assert resp.json["extensions"]["attributes"]["ipaddress"] == "192.168.1.1" # remove attribute ipaddress via bulk request aut_user_auth_wsgi_app.call_method( "put", base + "/domain-types/host_config/actions/bulk-update/invoke", params=json.dumps( { "entries": [{"host_name": "foobar", "remove_attributes": ["ipaddress"]}], } ), status=200, headers={"Accept": "application/json"}, content_type="application/json", ) # verify attribute ipaddress was removed correctly resp = aut_user_auth_wsgi_app.call_method( "get", base + "/objects/host_config/foobar", status=200, headers={"Accept": "application/json"}, ) assert "ipaddress" not in resp.json["extensions"]["attributes"] # adding invalid attribute should fail _resp = aut_user_auth_wsgi_app.call_method( "put", base + "/domain-types/host_config/actions/bulk-update/invoke", params=json.dumps( { "entries": [{"host_name": "foobar", "attributes": {"foobaz": "bar"}}], } ), status=400, headers={"Accept": "application/json"}, content_type="application/json", ) _resp = aut_user_auth_wsgi_app.call_method( "post", base + "/domain-types/host_config/actions/bulk-delete/invoke", params=json.dumps({"entries": ["foobar", "sample"]}), status=204, headers={"Accept": "application/json"}, content_type="application/json", )
def test_openapi_hosts( monkeypatch: pytest.MonkeyPatch, 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/host_config/collections/all", params='{"host_name": "foobar", "folder": "/"}', status=200, content_type="application/json", ) resp = wsgi_app.follow_link( resp, "self", status=200, ) attributes = { "ipaddress": "127.0.0.1", "snmp_community": { "type": "v1_v2_community", "community": "blah", }, } resp = wsgi_app.follow_link( resp, ".../update", status=200, params=json.dumps({"attributes": attributes}), headers={"If-Match": resp.headers["ETag"]}, content_type="application/json", ) got_attributes = resp.json["extensions"]["attributes"] assert attributes.items() <= got_attributes.items() # pylint: disable=dict-items-not-iterating resp = wsgi_app.follow_link( resp, ".../update", status=200, params='{"update_attributes": {"alias": "bar"}}', headers={"If-Match": resp.headers["ETag"]}, content_type="application/json", ) assert resp.json["extensions"]["attributes"]["alias"] == "bar" resp = wsgi_app.follow_link( resp, ".../update", status=200, params='{"remove_attributes": ["alias"]}', headers={"If-Match": resp.headers["ETag"]}, content_type="application/json", ) assert (resp.json["extensions"]["attributes"].items() >= { "ipaddress": "127.0.0.1" }.items()) # pylint: disable=dict-items-not-iterating # make sure changes are written to disk: resp = wsgi_app.follow_link(resp, "self", status=200) assert (resp.json["extensions"]["attributes"].items() >= { "ipaddress": "127.0.0.1" }.items()) # pylint: disable=dict-items-not-iterating # also try to update with wrong attribute wsgi_app.follow_link( resp, ".../update", status=400, params='{"attributes": {"foobaz": "bar"}}', headers={"If-Match": resp.headers["ETag"]}, content_type="application/json", ) monkeypatch.setattr( "cmk.gui.watolib.hosts_and_folders.delete_hosts", lambda *args, **kwargs: DeleteHostsResult(), ) wsgi_app.follow_link( resp, ".../delete", status=204, content_type="application/json", )