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')