]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.ports
Drop port_get_parents function
[people/ms/network.git] / src / functions / functions.ports
index c6e45d07df4675d6ae4084943963fcab87670f8b..af50ee2a3c2616c79196146c2af3dbba32e2a67c 100644 (file)
@@ -139,11 +139,7 @@ port_is_attached() {
 
        local zone
        for zone in $(zones_get_all); do
-
-               assert isset zone
-               assert zone_exists ${zone}
-
-               if listmatch ${port} $(zone_get_ports ${zone}); then
+               if list_match ${port} $(zone_get_ports ${zone}); then
                        echo "${zone}"
                        return ${EXIT_OK}
                fi
@@ -170,18 +166,20 @@ port_new() {
 
 port_destroy() {
        local port=${1}
-
        assert isset port
 
-       port_exists ${port} || return ${EXIT_OK}
-
-       # Check if the port is attached to any zone and don't delete it.
-       local ok=${EXIT_OK}
+       # Cannot delete a port that does not exist
+       if ! port_exists ${port}; then
+               error "No such port: ${port}"
+               return ${EXIT_ERROR}
+       fi
 
        local attached_zone=$(port_is_attached ${port})
        if [ -n "${attached_zone}" ]; then
-               error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
-               ok=${EXIT_ERROR}
+               if ! zone_port_detach "${attached_zone}" "${port}"; then
+                       error "Could not remove port ${port} from zone ${zone}"
+                       return ${EXIT_ERROR}
+               fi
        fi
 
        # Check if the port is linked to any other port and don't allow the user
@@ -190,22 +188,12 @@ port_destroy() {
        for other_port in $(ports_get); do
                [ "${other_port}" = "${port}" ] && continue
 
-               if listmatch ${port} $(port_get_parents ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
-                       ok=${EXIT_ERROR}
-               fi
-
-               if listmatch ${port} $(port_get_children ${other_port}); then
-                       error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
-                       ok=${EXIT_ERROR}
+               if list_match ${port} $(port_get_children ${other_port}); then
+                       log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
+                       return ${EXIT_ERROR}
                fi
        done
 
-       # If ok says we are not okay --> exit
-       if [ ${ok} -ne ${EXIT_OK} ]; then
-               return ${EXIT_ERROR}
-       fi
-
        port_remove "${port}"
 
        rm -rf $(port_dir ${port})
@@ -219,6 +207,11 @@ port_remove() {
        local port="${1}"
        assert isset port
 
+       if ! port_exists "${port}"; then
+               log ERROR "Port ${port} does not exist"
+               return ${EXIT_ERROR}
+       fi
+
        # If the device is still up, we need to bring it down first.
        if device_is_up "${port}"; then
                port_down "${port}"
@@ -227,6 +220,16 @@ port_remove() {
        port_cmd "remove" "${port}"
 }
 
+# Restarts the port by removing it and then re-creating it
+port_restart() {
+       local port="${1}"
+       assert isset port
+
+       port_remove "${port}"
+
+       port_create "${port}"
+}
+
 port_edit() {
        port_cmd edit $@
 }
@@ -310,16 +313,12 @@ port_get_info() {
        )
 }
 
-port_get_parents() {
-       local port=${1}
-
-       port_get_info ${port} PORT_PARENTS
-}
-
 port_get_children() {
        local port=${1}
 
-       port_get_info ${port} PORT_CHILDREN
+       assert port_exists "${port}"
+
+       port_cmd "children" "${port}"
 }
 
 port_zone() {
@@ -422,3 +421,18 @@ ports_lowest_address() {
 port_identify() {
        device_identify $@
 }
+
+port_get_color() {
+       # This function return the color of a port
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       color_read "port" ${name}
+}
+
+port_get_description_title() {
+       assert [ $# -eq 1 ]
+
+       local name=${1}
+       description_title_read $(description_format_filename "port" "${name}")
+}