diff options
author | Georg Pfuetzenreuter | 2023-02-05 00:42:41 +0100 |
---|---|---|
committer | Georg Pfuetzenreuter | 2023-02-05 00:42:41 +0100 |
commit | 5a698dfbad584fe28a73dbb35983a57bed671404 (patch) | |
tree | 7fe229e557c13da4baaad62adf0af96ab4b552b9 | |
parent | 6ac3e6c3003f9b44f509165dd3f4f33dc4493898 (diff) | |
download | salt-netbox-roleproxy-5a698dfbad584fe28a73dbb35983a57bed671404.tar.gz salt-netbox-roleproxy-5a698dfbad584fe28a73dbb35983a57bed671404.tar.bz2 salt-netbox-roleproxy-5a698dfbad584fe28a73dbb35983a57bed671404.zip |
New /master endpoint serving the name of the
Salt master for the specified machine.
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
-rwxr-xr-x | roleproxy.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/roleproxy.py b/roleproxy.py index b4f0e1d..4e671b8 100755 --- a/roleproxy.py +++ b/roleproxy.py @@ -43,8 +43,16 @@ def get_roles(netbox, name): if len(vm) == 0: return(404, None) +def get_master(netbox, name): + vm = netbox.virtualization.virtual_machines.filter(name=name) + if len(vm) > 0: + vmmaster = vm[0].config_context.salt_master + return(200, vmmaster) + if len(vm) == 0: + return(404, None) + @app.route('/roles') -def query(): +def query_roles(): name = flask.request.args.get('machine') query = get_roles(connect(host, token), name) response = query[0] @@ -55,6 +63,17 @@ def query(): roledict = {'roles': query[1]} return(flask.jsonify(roledict)) +@app.route('/master') +def query_master(): + name = flask.request.args.get('machine') + query = get_master(connect(host, token), name) + response = query[0] + logger.info(' %s requested master for %s and received %i', flask.request.remote_addr, name, response) + if response == 404: + flask.abort(404) + elif response == 200: + return(query[1]) + if __name__ == '__main__': #app.run(debug=False) logger = logging.getLogger('roleproxy') |