54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
import yaml
|
|
import re
|
|
from flask import Flask, request, jsonify
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Load GPO data
|
|
with open('{{ gpo_yaml_path }}', 'r') as file:
|
|
data = yaml.safe_load(file)
|
|
|
|
@app.route('/gpo', methods=['GET'])
|
|
def get_groups():
|
|
group_name = request.args.get('group')
|
|
print("Group specified:", group_name)
|
|
if not group_name:
|
|
return jsonify({"error": "Group parameter is required"}), 400
|
|
|
|
gpos = []
|
|
for entry in data:
|
|
configured_gpos = entry.get('configured_gpos', {})
|
|
for gpo, details in configured_gpos.items():
|
|
if details is None:
|
|
continue
|
|
for detail in details:
|
|
# Check if 'ad_group' exists and is not None before accessing it
|
|
ad_group = detail.get('ad_group')
|
|
# this allows for partial search
|
|
if isinstance(ad_group, str) and re.search(re.escape(group_name.lower()), ad_group.lower()):
|
|
# i don't like BUILTIN\ so I'm getting rid of it
|
|
local_group = detail.get('local_group')
|
|
if isinstance(local_group, str) and local_group.startswith("BUILTIN\\"):
|
|
local_group = local_group.split("\\", 1)[1]
|
|
gpos.append({
|
|
"gpo": gpo,
|
|
"local_group": local_group,
|
|
"ad_group": detail['ad_group']
|
|
})
|
|
return jsonify(gpos)
|
|
|
|
# test route
|
|
@app.route('/test', methods=['GET'])
|
|
def test():
|
|
try:
|
|
root_ou = data[0]['root_ou']
|
|
except KeyError:
|
|
# Handle the case where 'root_ou' does not exist in the dictionary
|
|
return jsonify({"error": "KeyError: 'root_ou' not found in provided data"}), 400
|
|
except TypeError:
|
|
# Handle cases where data might be of a different type than expected
|
|
return jsonify({"error": "TypeError: Data is not a dictionary"}), 400
|
|
return jsonify({"result": "test", "OU_Root": root_ou})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True, host='{{ api_service_bind_ip }}', port={{ api_service_port }}) |