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
;;
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
+
+ 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 zone_config_id_is_valid ${zone} ${id} && [[ ${1} == "edit" ]]; then
- shift 1
- 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
assert isset zone
# Print a nice header
- local format="%-3s %-20s"
- print "${format}" "ID" "HOOK"
+ 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
- # TODO: Add hids here
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}"
+ print "${format}" "${id}" "${hook}" "${hid}"
done
}
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}
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 ]
[ -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 ]
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 ]