]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.zone
config hook: prevent two hooks with the same settings
[people/ms/network.git] / src / functions / functions.zone
index d1212252cd5d17914c8c2f6d2fdd972096338212..1eb492f5ad3aa3677e77cddc3ca5dc2177cb376e 100644 (file)
@@ -243,7 +243,7 @@ zone_new() {
 
        # Maybe the zone new hook did not exit correctly.
        # If this is the case we remove the created zone immediately.
-       if [ "${ret}" = "${EXIT_ERROR}" ]; then
+       if [ "${ret}" != "${EXIT_OK}" ]; then
                zone_destroy_now "${zone}"
                return ${EXIT_ERROR}
        fi
@@ -550,28 +550,52 @@ zone_config() {
                        zone_config_new "${zone}" "$@"
                        ;;
                destroy)
-                       local id=${1}
-                       if zone_config_id_is_valid ${zone} ${id}; then
-                               zone_config_destroy "${zone}" "$@"
+                       # usually ${1} is a valid hid
+                       local hid=${1}
+                       shift 1
+
+                       # We convert the hid into an id
+                       local id=$(zone_config_convert_hid_to_id ${zone} ${hid})
+
+                       # If id isset the hid is valid and we can go on with the id
+                       if isset id; then
+                                zone_config_destroy "${zone}" "${id}" "$@"
+
+                       # If we can't get a valid hid we check if we got a valid id
                        else
-                               log ERROR "${id} is not a valid id"
+                               if zone_config_id_is_valid ${zone} ${hid}; then
+                                       zone_config_destroy "${zone}" ${hid} "$@"
+                               else
+                                       log ERROR "${id} is not a valid id or hid"
+                               fi
                        fi
                        ;;
-               edit)
-                       zone_config_edit "${zone}" "$@"
+               list)
+                       zone_config_list "${zone}" "$@"
                        ;;
                *)
-                       # Check is we get a valid id
-                       # TODO This could be also a valid hid
-                       local id=${cmd}
+                       # usually ${1} is a valid hid
+                       local hid=${cmd}
+                       local cmd=${1}
+                       shift 1
 
-                       if zone_config_id_is valid ${zone} ${id} && [[ ${1} == "edit" ]]; then
-                               shift 1
-                               zone_config_edit "${zone}" "${id}""$@"
+                       local id=$(zone_config_convert_hid_to_id ${zone} ${hid})
+
+                       # If id isset the hid is valid and we can go on with the id
+                       if isset id && [[ ${cmd} == "edit" ]]; then
+                                zone_config_edit "${zone}" "${id}" "$@"
+
+                       # If we didn't get a valid hid we check if we got a valid id
                        else
-                               error "Unrecognized argument: ${cmd}"
-                               cli_usage root-zone-config-subcommands
-                               exit ${EXIT_ERROR}
+                               if zone_config_id_is_valid ${zone} ${id} && [[ ${cmd} == "edit" ]]; then
+                                       shift 1
+                                       zone_config_edit "${zone}" "${id}" "$@"
+                               else
+                                       # in ${hid} is saved the command after network zone ${zone} config
+                                       error "Unrecognized argument: ${hid}"
+                                       cli_usage root-zone-config-subcommands
+                                       exit ${EXIT_ERROR}
+                               fi
                        fi
                        ;;
        esac
@@ -613,10 +637,112 @@ zone_config_edit() {
        zone_config_cmd "edit" "$@"
 }
 
+zone_config_list() {
+       # This function list in an nice way all configs of a zone
+       local zone=${1}
+       assert isset zone
+
+       # Print a nice header
+       local format="%-3s %-20s %-20s"
+       print "${format}" "ID" "HOOK" "HID"
+
+       local config
+       local hook
+       local id
+       local hid
+
+       # Print for all config:
+       # id and hook
+       for config in $(zone_configs_list "${zone}"); do
+               id=${config##*.}
+               hook=$(zone_config_get_hook "${zone}" "${config}")
+               hid=$(zone_config_get_hid "${zone}" "${config}")
+               assert isset hook
+               print "${format}" "${id}" "${hook}" "${hid}"
+       done
+}
+
 zone_config_show() {
        zone_config_cmd "show" "$@"
 }
 
+# Returns a list of all used ids for a zone
+zone_config_list_ids() {
+       assert [ $# -eq 1 ]
+
+       local zone=${1}
+       local config
+       local ids
+
+       for config in $(zone_configs_list ${zone}); do
+               list_append ids "$(config_get_id_from_config ${config})"
+       done
+
+       echo ${ids}
+}
+
+# List all hids of a zone
+zone_config_list_hids() {
+       assert [ $# -eq 1 ]
+
+       local zone=${1}
+
+       local config
+       for config in $(zone_configs_list ${zone}); do
+               zone_config_get_hid "${zone}" "${config}"
+       done
+}
+
+# get the hid from a given config
+zone_config_get_hid() {
+       assert [ $# -eq 2 ]
+
+       local zone=${1}
+       local config=${2}
+
+       local hook="$(zone_config_get_hook "${zone}" "${config}")"
+
+       hook_exec "config" "${hook}" "hid" "${zone}" "${config}"
+}
+
+# Checks if a hid is valid for a given zone
+zone_config_hid_is_valid() {
+       assert [ $# -eq 2]
+
+       local zone=${1}
+       local hid=${2}
+
+       local _hid
+       for _hid in $(zone_config_list_hids "${zone}"); do
+               if [[ ${_hid} = ${hid} ]]; then
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
+}
+
+# This function converts a hid to a id
+zone_config_convert_hid_to_id() {
+       assert [ $# -eq 2 ]
+
+       local zone=${1}
+       local hid=${2}
+
+       local config
+       for config in $(zone_configs_list ${zone}); do
+               # Get hook from config
+               local hook="$(zone_config_get_hook "${zone}" "${config}")"
+
+               if [[ "$(hook_exec "config" "${hook}" "hid" "${zone}" "${config}")" == "${hid}" ]]; then
+                       config_get_id_from_config "${config}"
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
+}
+
 zone_show() {
        local zone=${1}
 
@@ -1024,6 +1150,39 @@ zone_config_get_new_id() {
        done
 }
 
+zone_config_check_same_setting() {
+       # This functions checks if a config hook
+       # with the same setting is already configured for this zone.
+       # Returns True when yes and False when no.
+
+       assert [ $# -eq 4 ]
+
+       local zone=${1}
+       local hook=${2}
+       local key=${3}
+       local value=${4}
+
+       # The key should be local for this function
+       local ${key}
+       local config
+
+       for config in $(zone_configs_list ${zone}); do
+               # Check if the config is from the given hook, when not continue
+               if  [[ $(zone_config_get_hook "${zone}" "${config}") != ${hook} ]]; then
+                       continue
+               fi
+               # Get the value of the key for a given function
+               zone_config_settings_read "${zone}" "${config}" \
+                --ignore-superfluous-settings "${key}"
+               # Check if the value of the config and the passed value are eqal
+               if [[ "${value}" == "${!key}" ]]; then
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
+}
+
 zone_config_get_hook() {
        assert [ $# -eq 2 ]
 
@@ -1075,6 +1234,23 @@ zone_config_id_is_valid() {
        [ -f ${zone_path}/configs/*.${id} ];
 }
 
+# This function checks if a given hid is valid for a zone
+# Return True when yes and false when no
+zone_config_hid_is_valid() {
+       assert [ $# -eq 2 ]
+       local zone=${1}
+       local hid=${2}
+
+       local _hid
+       for _hid in $(zone_config_list_hids ${zone}); do
+               if [[ ${_hid} == ${hid} ]]; then
+                       return ${EXIT_TRUE}
+               fi
+       done
+
+       return ${EXIT_FALSE}
+}
+
 zone_config_get_hook_from_id() {
        # Returns the hook for a given id
        assert [ $# -eq 2 ]
@@ -1248,6 +1424,24 @@ zone_config_settings_write() {
        settings_write "${path}" ${args}
 }
 
+zone_config_settings_destroy() {
+       # This function deletes the config file for a given zone and config
+       assert [ $# -ge 2 ]
+       local zone="${1}"
+       local config="${2}"
+
+       local path="$(zone_dir "${zone}")/configs/${config}"
+
+       # Check if path is valid
+       if [ ! -f ${path} ]; then
+               log ERROR "Path: '${path}' is not valid"
+               return ${EXIT_ERROR}
+       fi
+
+       log DEBUG "Deleting config file ${path}"
+       rm -f "${path}"
+
+}
 zone_port_settings_read() {
        assert [ $# -ge 2 ]