X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetstack%2Fcore%2Fxoslib%2Fstatic%2Fjs%2Fxoslib%2Fxos-util.js;h=2fa38a68fc74a3771619bc2cf2d0ee909ed37fcb;hb=df5a86a9734879dee4eebb6d5b372a407eb85682;hp=59fb06ec5fb186c14c8096654ae75ece84dff340;hpb=85743c1a683903301d2d69b4a552b56ab6effcba;p=plstackapi.git diff --git a/planetstack/core/xoslib/static/js/xoslib/xos-util.js b/planetstack/core/xoslib/static/js/xoslib/xos-util.js index 59fb06e..2fa38a6 100644 --- a/planetstack/core/xoslib/static/js/xoslib/xos-util.js +++ b/planetstack/core/xoslib/static/js/xoslib/xos-util.js @@ -105,6 +105,12 @@ function array_subtract(a1, a2) return result; } +function array_same_elements(arr1, arr2) +{ + // return true if arrays have same elements, even if order is different + return ($(arr1).not(arr2).length == 0 && $(arr2).not(arr1).length == 0); +} + function array_pair_lookup(x, names, values) { for (index in values) { @@ -130,3 +136,62 @@ function make_same_width(containerSelector, itemSelector) { console.log(maxWidth); $(containerSelector).find(itemSelector).each( function(index) { $(this).width(maxWidth); }); } + +function parse_portlist(ports) { + /* Support a list of ports in the format "protocol:port, protocol:port, ..." + examples: + tcp 123 + tcp 123:133 + tcp 123, tcp 124, tcp 125, udp 201, udp 202 + + User can put either a "/" or a " " between protocol and ports + Port ranges can be specified with "-" or ":" + + This is a straightforward port of the code in core/models/network.py + */ + + var nats = []; + if (ports) { + parts = ports.split(",") + $.each(parts, function(index, part) { + part = $.trim(part); + if (part.indexOf("/")>=0) { + parts2 = part.split("/",2); + protocol=parts2[0]; + ports=parts2[1]; + } else if (part.indexOf(" ")>=0) { + parts2 = part.split(" +",2); + protocol=parts2[0]; + ports=parts2[1]; + } else { + throw 'malformed port specifier ' + part + ', format example: "tcp 123, tcp 201:206, udp 333"'; + } + + protocol = $.trim(protocol); + ports = $.trim(ports); + + console.log(ports); + + if (protocol!="tcp" && protocol!="udp") { + throw 'unknown protocol ' + protocol; + } + + if (ports.indexOf("-")>=0) { + parts2 = ports.split("-"); + first = parseInt($.trim(parts2[0])); + last = parseInt($.trim(parts2[1])); + portStr = first + ":" + last; + } else if (ports.indexOf(":")>=0) { + parts2 = ports.split(":"); + first = parseInt($.trim(parts2[0])); + last = parseInt($.trim(parts2[1])); + portStr = first + ":" + last; + } else { + portStr = parseInt(ports).toString(); + } + + nats.push( {l4_protocol: protocol, l4_port: portStr} ); + }); /* end $.each(ports) */ + } + return nats +}