@@ -23,19 +23,20 @@ class APIInterfaceVLANCreate extends APIModel {
2323 parent ::__construct ();
2424 $ this ->privileges = ["page-all " , "page-interfaces-vlan-edit " ];
2525 $ this ->change_note = "Added VLAN interface via API " ;
26-
2726 }
2827
2928 public function action () {
30- $ this ->config [ " vlans " ][ " vlan " ] = [] ;
31- $ this ->config ["vlans " ]["vlan " ][] = $ this ->validated_data ; // Write our configuration change
32- interface_vlan_configure ($ this ->validated_data ); // Configure our VLAN on the backend
29+ $ this ->__init_config () ;
30+ $ this ->config ["vlans " ]["vlan " ][] = $ this ->validated_data ;
31+ interface_vlan_configure ($ this ->validated_data );
3332 $ this ->write_config ();
3433 return APIResponse \get (0 , $ this ->validated_data );
3534 }
3635
37- public function validate_payload () {
36+ private function __validate_if () {
37+ # Require clients to specify an if value to indicate which interface this VLAN will apply to
3838 if (isset ($ this ->initial_data ['if ' ])) {
39+ # Return an error if the requested parent interface does not exist
3940 if (!does_interface_exist ($ this ->initial_data ['if ' ])) {
4041 $ this ->errors [] = APIResponse \get (3051 );
4142 } else {
@@ -44,19 +45,32 @@ class APIInterfaceVLANCreate extends APIModel {
4445 } else {
4546 $ this ->errors [] = APIResponse \get (3055 );
4647 }
48+ }
4749
50+ private function __validate_tag () {
51+ # Require clients to specify a VLAN tag value
4852 if (isset ($ this ->initial_data ['tag ' ])) {
53+ # Return an error if VLAN tag is not numeric, or is not between 1 and 4096
4954 if (!is_numeric ($ this ->initial_data ['tag ' ]) or (1 > intval ($ this ->initial_data ['tag ' ]) or intval ($ this ->initial_data ['tag ' ]) > 4096 )) {
5055 $ this ->errors [] = APIResponse \get (3052 );
51- } else {
52- $ this ->validated_data ["tag " ] = intval (trim ($ this ->initial_data ['tag ' ]));
53- $ str_tag = strval ($ this ->validated_data ["tag " ]);
56+ }
57+ # Return an error if this VLAN already exists on this interface
58+ elseif ($ this ->__is_vlan ($ this ->validated_data ["if " ], $ this ->initial_data ["tag " ])) {
59+ $ this ->errors [] = APIResponse \get (3054 );
60+ }
61+ # Otherwise, store our tag value as validated
62+ else {
63+ $ this ->validated_data ["tag " ] = $ this ->initial_data ['tag ' ];
5464 }
5565 } else {
5666 $ this ->errors [] = APIResponse \get (3048 );
5767 }
68+ }
5869
70+ private function __validate_pcp () {
71+ # Optionally allow client to specify a VLAN PCP value. Default if not set.
5972 if (isset ($ this ->initial_data ['pcp ' ])) {
73+ # Return error if PCP value is not between 0 and 7
6074 if (0 > $ this ->initial_data ['pcp ' ] or $ this ->initial_data ['pcp ' ] > 7 ) {
6175 $ this ->errors [] = APIResponse \get (3053 );
6276 } else {
@@ -65,25 +79,43 @@ class APIInterfaceVLANCreate extends APIModel {
6579 } else {
6680 $ this ->validated_data ["pcp " ] = "" ;
6781 }
82+ }
6883
84+ private function __validate__descr () {
85+ # Optionally allow client to specify a VLAN description. Default if not set.
6986 if (isset ($ this ->initial_data ['descr ' ])) {
7087 $ this ->validated_data ["descr " ] = $ this ->initial_data ['descr ' ];
7188 } else {
7289 $ this ->validated_data ["descr " ] = "" ;
7390 }
91+ }
92+
93+ public function validate_payload () {
94+ $ this ->__validate_if ();
95+ $ this ->__validate_tag ();
96+ $ this ->__validate_pcp ();
97+ $ this ->__validate__descr ();
98+ $ this ->validated_data ["vlanif " ] = $ this ->validated_data ["if " ].". " .$ this ->initial_data ['tag ' ];
99+ }
74100
75- // Check if our VLAN is already in use
76- if (is_array ($ this ->config ["vlans " ]["vlan " ])) {
77- foreach ($ this ->config ["vlans " ]["vlan " ] as $ vle ) {
78- if ($ this ->validated_data ["if " ] === $ vle ["if " ] and $ str_tag === $ vle ["tag " ]) {
79- $ this ->errors [] = APIResponse \get (3054 );
80- }
101+ private function __is_vlan ($ if , $ tag ) {
102+ # Check if our VLAN already exists
103+ foreach ($ this ->config ["vlans " ]["vlan " ] as $ vle ) {
104+ if ($ if === $ vle ["if " ] and strval ($ tag ) === $ vle ["tag " ]) {
105+ return true ;
81106 }
82- } else {
83- $ this ->config ["vlans " ] = [];
84- $ this ->config ["vlans " ]["vlan " ] = [];
85107 }
86- $ this ->validated_data ["vlanif " ] = $ this ->validated_data ["if " ].". " .$ this ->initial_data ['tag ' ];
108+ return false ;
109+ }
87110
111+ private function __init_config () {
112+ # Ensure our VLANs config section is an array
113+ if (!is_array ($ this ->config ["vlans " ])) {
114+ $ this ->config ["vlans " ] = [];
115+ }
116+ # Ensure our VLAN config section is an array
117+ if (!is_array ($ this ->config ["vlans " ]["vlan " ])) {
118+ $ this ->config ["vlan " ] = [];
119+ }
88120 }
89- }
121+ }
0 commit comments