1<?php 2 3/** 4 * phpIPAM API class to work with vrfs 5 * 6 * 7 */ 8 9class Vrfs_controller extends Common_api_functions { 10 11 12 /** 13 * _params [provided 14 * 15 * @var mixed 16 * @access public 17 */ 18 public $_params; 19 20 /** 21 * Custom address fields 22 * 23 * @var mixed 24 * @access public 25 */ 26 public $custom_fields; 27 28 /** 29 * Database object 30 * 31 * @var mixed 32 * @access protected 33 */ 34 protected $Database; 35 36 /** 37 * Master Sections object 38 * 39 * @var mixed 40 * @access protected 41 */ 42 protected $Sections; 43 44 /** 45 * Master Subnets object 46 * 47 * @var mixed 48 * @access protected 49 */ 50 protected $Subnets; 51 52 /** 53 * Master Tools object 54 * 55 * @var mixed 56 * @access protected 57 */ 58 protected $Tools; 59 60 /** 61 * Master Admin object 62 * 63 * @var mixed 64 * @access protected 65 */ 66 protected $Admin; 67 68 69 /** 70 * __construct function 71 * 72 * @access public 73 * @param class $Database 74 * @param class $Tools 75 * @param mixed $params // post/get values 76 * @param class $Response 77 */ 78 public function __construct($Database, $Tools, $params, $Response) { 79 $this->Database = $Database; 80 $this->Tools = $Tools; 81 $this->_params = $params; 82 $this->Response = $Response; 83 // init required objects 84 $this->init_object ("Admin", $Database); 85 $this->init_object ("Subnets", $Database); 86 // set valid keys 87 $this->set_valid_keys ("vrf"); 88 } 89 90 91 92 93 94 95 /** 96 * Returns json encoded options 97 * 98 * @access public 99 * @return void 100 */ 101 public function OPTIONS () { 102 // validate 103 $this->validate_options_request (); 104 105 // methods 106 $result['methods'] = array( 107 array("href"=>"/api/".$this->_params->app_id."/vrfs/", "methods"=>array(array("rel"=>"options", "method"=>"OPTIONS"))), 108 array("href"=>"/api/".$this->_params->app_id."/vrfs/{id}/", "methods"=>array(array("rel"=>"read", "method"=>"GET"), 109 array("rel"=>"create", "method"=>"POST"), 110 array("rel"=>"update", "method"=>"PATCH"), 111 array("rel"=>"delete", "method"=>"DELETE"))), 112 ); 113 # result 114 return array("code"=>200, "data"=>$result); 115 } 116 117 118 119 120 121 122 /** 123 * Read vrf 124 * 125 * identifiers: 126 * - / // returns all VRFs 127 * - /custom_fields/ // returns all VRF custom fields 128 * - /{id}/ // returns VRF by id 129 * - /{id}/subnets/ // subnets inside vrf 130 * - /all/ // returns all VRFs 131 * 132 * 133 * @access public 134 * @return void 135 */ 136 public function GET () { 137 // all 138 if (!isset($this->_params->id) || $this->_params->id == "all") { 139 $result = $this->Tools->fetch_all_objects ("vrf", 'vrfId'); 140 // check result 141 if($result===false) { $this->Response->throw_exception(200, 'No vrfs configured'); } 142 else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); } 143 } 144 // custom fields 145 if($this->_params->id=="custom_fields") { 146 // check result 147 if(sizeof($this->custom_fields)==0) { $this->Response->throw_exception(200, 'No custom fields defined'); } 148 else { return array("code"=>200, "data"=>$this->custom_fields); } 149 } 150 // subnets 151 elseif (isset($this->_params->id2)) { 152 // subnets 153 if ($this->_params->id2 == "subnets") { 154 // validate 155 $this->validate_vrf (); 156 // fetch 157 $result = $this->Tools->fetch_multiple_objects ("subnets", "vrfId", $this->_params->id, 'subnet', true); 158 // add gateway if present 159 if($result!=false) { 160 foreach ($result as $k=>$r) { 161 $gateway = $this->read_subnet_gateway ($r->id); 162 if ( $gateway!== false) { 163 $result[$k]->gatewayId = $gateway->id; 164 } 165 } 166 } 167 168 // check result 169 if($result===false) { $this->Response->throw_exception(200, 'No subnets belonging to this vrf'); } 170 else { 171 $this->custom_fields = $this->Tools->fetch_custom_fields('subnets'); 172 return array("code"=>200, "data"=>$this->prepare_result ($result, "subnets", true, true)); 173 } 174 } 175 // error 176 else { 177 { $this->Response->throw_exception(400, "Invalid identifier"); } 178 } 179 } 180 // by id 181 else { 182 // validate 183 $this->validate_vrf (); 184 // fetch 185 $result = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id); 186 // check result 187 if($result===false) { $this->Response->throw_exception(404, "VRF not found"); } 188 else { return array("code"=>200, "data"=>$this->prepare_result ($result, null, true, true)); } 189 } 190 } 191 192 193 194 195 196 /** 197 * HEAD, no response 198 * 199 * @access public 200 * @return void 201 */ 202 public function HEAD () { 203 return $this->GET (); 204 } 205 206 207 208 209 210 /** 211 * Creates new VRF 212 * 213 * @access public 214 * @return void 215 */ 216 public function POST () { 217 # check for valid keys 218 $values = $this->validate_keys (); 219 220 # validate input 221 $this->validate_vrf_edit (); 222 223 # execute update 224 if(!$this->Admin->object_modify ("vrf", "add", "vrfId", $values)) 225 { $this->Response->throw_exception(500, "VRF creation failed"); } 226 else { 227 //set result 228 return array("code"=>201, "message"=>"VRF created", "id"=>$this->Admin->lastId, "location"=>"/api/".$this->_params->app_id."/vrfs/".$this->Admin->lastId."/"); 229 } 230 } 231 232 233 234 235 236 /** 237 * Updates existing vrf 238 * 239 * @access public 240 * @return void 241 */ 242 public function PATCH () { 243 # verify 244 $this->validate_vrf (); 245 # check that it exists 246 $this->validate_vrf_edit (); 247 248 # rewrite id 249 $this->_params->vrfId = $this->_params->id; 250 unset($this->_params->id); 251 252 # validate and prepare keys 253 $values = $this->validate_keys (); 254 255 # execute update 256 if(!$this->Admin->object_modify ("vrf", "edit", "vrfId", $values)) 257 { $this->Response->throw_exception(500, "Vrf edit failed"); } 258 else { 259 //set result 260 return array("code"=>200, "message"=>"VRF updated"); 261 } 262 } 263 264 265 266 267 268 269 /** 270 * Deletes existing vrf 271 * 272 * @access public 273 * @return void 274 */ 275 public function DELETE () { 276 # check that vrf exists 277 $this->validate_vrf (); 278 279 # set variables for update 280 $values = array(); 281 $values["vrfId"] = $this->_params->id; 282 283 # execute delete 284 if(!$this->Admin->object_modify ("vrf", "delete", "vrfId", $values)) 285 { $this->Response->throw_exception(500, "Vrf delete failed"); } 286 else { 287 // delete all references 288 $this->Admin->remove_object_references ("subnets", "vrfId", $this->_params->id); 289 290 // set result 291 return array("code"=>200, "message"=>"VRF deleted"); 292 } 293 } 294 295 296 297 298 299 300 301 302 303 304 /* @validations ---------- */ 305 306 307 308 /** 309 * Validates VRF - checks if it exists 310 * 311 * @access private 312 * @return void 313 */ 314 private function validate_vrf () { 315 // validate id 316 if(!isset($this->_params->id)) { $this->Response->throw_exception(400, "Vrf Id is required"); } 317 // validate number 318 if(!is_numeric($this->_params->id)) { $this->Response->throw_exception(400, "Vrf Id must be numeric"); } 319 // check that it exists 320 if($this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id) === false ) { $this->Response->throw_exception(400, "Invalid VRF id"); } 321 } 322 323 324 /** 325 * Validates VRF on add and edit 326 * 327 * @access private 328 * @return void 329 */ 330 private function validate_vrf_edit () { 331 // check for POST method 332 if($_SERVER['REQUEST_METHOD']=="POST") { 333 // check name 334 if(strlen($this->_params->name)==0) { $this->Response->throw_exception(400, "VRF name is required"); } 335 // check that it exists 336 if($this->Tools->fetch_object ("vrf", "name", $this->_params->name) !== false ) { $this->Response->throw_exception(409, "VRF with that name already exists"); } 337 } 338 // update check 339 else { 340 // old values 341 $vrf_old = $this->Tools->fetch_object ("vrf", "vrfId", $this->_params->id); 342 343 if(isset($this->_params->name)) { 344 if ($this->_params->name != $vrf_old->name) { 345 if($this->Tools->fetch_object ("vrf", "name", $this->_params->name)) { $this->Response->throw_exception(409, "VRF with that name already exists"); } 346 } 347 } 348 } 349 } 350 351 /** 352 * Returns id of subnet gateay 353 * 354 * @access private 355 * @params mixed $subnetId 356 * @return void 357 */ 358 private function read_subnet_gateway ($subnetId) { 359 return $this->Subnets->find_gateway ($subnetId); 360 } 361 362}