['vcpus' => 2, 'ram' => 4], 'medium' => ['vcpus' => 4, 'ram' => 8], 'big' => ['vcpus' => 8, 'ram' => 16] ]; $vCPUs = isset($sizeMapping[$Size]) ? $sizeMapping[$Size]['vcpus'] : null; $Ram_Value = isset($sizeMapping[$Size]) ? $sizeMapping[$Size]['ram'] * 1024 : null; // Convert GB to MB // Set HDD value to always be 50GB $HDD_Value = 50 * 1024; // Convert GB to MB // Check for required fields if (!$VM_Name || $vCPUs === null || $Ram_Value === null || !$Domain) { echo "Error: Missing or invalid input data."; exit; } // Function to check if a VM name already exists in NetBox function checkIfVmExists($vmName, $netboxApiUrl, $apiToken) { $vmsApiUrl = rtrim($netboxApiUrl, '/') . '/?name=' . urlencode($vmName); // Query NetBox for the VM name $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $vmsApiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Token ' . $apiToken ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) { $vms = json_decode($response, true); if (!empty($vms['results'])) { return true; // VM with the same name exists } } else { echo "Error: Failed to check VM name. HTTP Code: $httpCode
"; echo "Response: $response
"; } return false; // VM does not exist } // Check if the VM name already exists if (checkIfVmExists($VM_Name, $netboxApiUrl, $apiToken)) { echo "Error: A VM with the name '$VM_Name' already exists in NetBox."; exit; } // Function to get or create a tag in NetBox function getOrCreateTag($tagName, $netboxApiUrl, $apiToken) { $tagsApiUrl = 'http://192.168.1.144:80/api/extras/tags/'; // Check if the tag exists $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tagsApiUrl . '?name=' . urlencode($tagName)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Token ' . $apiToken ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) { $tags = json_decode($response, true); if (!empty($tags['results'])) { return $tags['results'][0]['id']; // Return the ID of the existing tag } } // If the tag doesn't exist, create it $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $tagsApiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['name' => $tagName])); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Token ' . $apiToken ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 201) { $tag = json_decode($response, true); return $tag['id']; // Return the ID of the newly created tag } return null; // Return null if the tag couldn't be created } // Get or create the domain tag $domainTagId = getOrCreateTag($Domain, $netboxApiUrl, $apiToken); if (!$domainTagId) { echo "Error: Failed to retrieve or create the domain tag."; exit; } // Function to create a virtual disk in NetBox function createDisk($vmId, $diskName, $diskSize, $netboxApiUrl, $apiToken) { $disksApiUrl = 'http://192.168.1.144:80/api/virtualization/virtual-disks/'; // Prepare disk data $diskData = [ 'virtual_machine' => $vmId, 'name' => $diskName, 'size' => $diskSize // Size in MB ]; // Initialize cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $disksApiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($diskData)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Token ' . $apiToken ]); // Execute cURL request $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 201) { echo "Disk '$diskName' created successfully.
"; } else { echo "Failed to create disk '$diskName'. HTTP Code: $httpCode
"; echo "Response: $response
"; } } // Prepare data for NetBox API to create the VM $data = [ 'name' => $VM_Name, 'vcpus' => $vCPUs, 'memory' => $Ram_Value, 'status' => 'active', // Adjust as needed 'cluster' => 1, // Replace with the appropriate cluster ID 'tags' => [$domainTagId] // Include the domain tag ID ]; // Initialize cURL to create the VM $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $netboxApiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Token ' . $apiToken ]); // Execute cURL request to create the VM $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($response === false) { echo 'cURL Error: ' . curl_error($ch); } else { if ($httpCode === 201) { echo "VM created successfully in NetBox.
"; $vm = json_decode($response, true); $vmId = $vm['id']; // Get the VM ID from the response // Create OS Disk createDisk($vmId, 'osdisk', 60 * 1024, $netboxApiUrl, $apiToken); // Create Data Disk createDisk($vmId, 'datadisk', 50 * 1024, $netboxApiUrl, $apiToken); } else { echo "Failed to create VM in NetBox. HTTP Code: $httpCode
"; echo "Response: $response
"; } } curl_close($ch); ?>

Processing Your VM Configuration...

You will be redirected to the front page in 20 seconds.

Logout