def delete_config_policy(id: str): """Deletes config compliance policy based on its respective id""" config_policies = yaml_function("./APIs/config_policies.yml", "load") for policy in config_policies: if policy.get("id") == id: config_policies.remove(policy) yaml_function("./APIs/config_policies.yml", "dump", data=config_policies) return jsonify({}), 200 return jsonify({"error": f"Config policy ID {id} not found!"}), 404
def get_device(device: str): """Gets metadata for a specific device""" if request.method == "GET": devices = yaml_function("./APIs/lab_devices.yml", "load") for a_device in devices: if a_device.get("deviceName") == device: return jsonify({"data": a_device}), 200 return jsonify({"error": f"Device {device} not found!"}), 404
def get_devices(): """Gets metadata for a specific device""" if request.method == "GET": devices = yaml_function("./APIs/lab_devices.yml", "load") return jsonify({"data": devices}), 200 elif request.method == "POST": invalid_schema = validate_schema(request.json, "lab_devices") if invalid_schema: return jsonify(json.loads(invalid_schema)), 400 if len(request.json) != 9: return jsonify({"error": "The number of parameters you specified is invalid!"}), 400 data = yaml_function("./APIs/lab_devices.yml", "load") for entry in data: if entry["deviceName"].lower() == request.json["deviceName"].lower(): return jsonify({"error": "Device already exists!"}), 200 new_data = request.json data.append(new_data) yaml_function("./lab_devices.yml", "dump", data=data) return jsonify({"data": request.json}), 201
def validate_schema(data, validator): for num, mapping in enumerate(mapping_table): if mapping.get(validator): schema_file = yaml_function( f"./Schemas/{mapping_table[num][validator]['file']}", "load") for field, myvalidator in mapping_table[num][validator][ "customValidators"].items(): schema_file[field]["validator"] = myvalidator v = CustomValidator() v.validate(data, schema_file) if v.errors: return json.dumps(v.errors)
def get_config_policies(): """Gets all config policies and creates new config policies""" schema = { "name": "Telnet Disable Cisco", "description": "Disables telnet on Cisco devices per security requirement 1234", "platform": "IOS", "device_types": ["router", "switch"], "config": "no transport input telnet", "parent": "line vty 0 4", } if request.method == "GET": config_policies = yaml_function("./APIs/config_policies.yml", "load") return jsonify({"data": config_policies}), 200 elif request.method == "POST": invalid_schema = validate_schema(request.json, "config_policy") if invalid_schema: return jsonify(json.loads(invalid_schema)), 400 if len(request.json) != 6: return ( jsonify( { "The number of parameters you specified is invalid! Valid fields example": schema } ), 400, ) data = yaml_function("./config_policies.yml", "load") for entry in data: if ( entry["name"].lower() == request.json["name"].lower() and entry["platform"].lower() == request.json["platform"].lower() ): return jsonify({"error": "Configuration policy already exists!"}), 200 new_data = request.json new_data["id"] = token_hex(16) data.append(new_data) yaml_function("./config_policies.yml", "dump", data=data) return jsonify({"data": request.json}), 201
def validate_device_types(field, value, error): """ Validates that the device type is within the schema of the Cerberus definition :param field: The device_types field :type field: str :param value: The set of device types :type value: list :param error: The error given if the data validation fails :type error: dict :returns: dict -- Only if the device type/types were not valid """ schema_file = yaml_function("./Schemas/config_policy_validators.yml", "load") for device_type in value: if device_type not in schema_file[field]["allowed"]: error( field, f"Valid values: {[x for x in schema_file[field]['allowed']]}")
from render_templates import render_template from shared.resources import yaml_function devices = yaml_function("APIs/lab_devices.yml", "load") for device in devices: render_template( device.get("deviceName"), "interface", device.get("Model"), int_prefix="GigabitEthernet0/", vlan=15, ) # breakpoint() - builtin function to replace pdb
def validate_vendor(field, value, error): schema_file = yaml_function("./Schemas/lab_devices_validators.yml", "load") if value not in schema_file[field]["allowed"]: error(field, f"Valid values: {[x for x in schema_file[field]['allowed']]}")
def validate_platform(field, value, error): schema_file = yaml_function("./Schemas/config_policy_validators.yml", "load") if value not in schema_file[field]["allowed"]: error(field, f"Valid values: {[x for x in schema_file[field]['allowed']]}")