Add master query endpoint

New /master endpoint serving the name of the
Salt master for the specified machine.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2023-02-05 00:42:41 +01:00
parent 6ac3e6c300
commit 5a698dfbad
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57

View File

@ -43,8 +43,16 @@ def get_roles(netbox, name):
if len(vm) == 0: if len(vm) == 0:
return(404, None) 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') @app.route('/roles')
def query(): def query_roles():
name = flask.request.args.get('machine') name = flask.request.args.get('machine')
query = get_roles(connect(host, token), name) query = get_roles(connect(host, token), name)
response = query[0] response = query[0]
@ -55,6 +63,17 @@ def query():
roledict = {'roles': query[1]} roledict = {'roles': query[1]}
return(flask.jsonify(roledict)) 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__': if __name__ == '__main__':
#app.run(debug=False) #app.run(debug=False)
logger = logging.getLogger('roleproxy') logger = logging.getLogger('roleproxy')