Skip to main content

ПРИМЕР RACKCORP REST API

PHP:

Простое создание сервера:

<?php
function rackcorpAPI($action, $request) {
$request["APIUUID"] = "";
$request["APISECRET"] = "";
$request["cmd"] = $action;

$curl = curl_init("https://api.rackcorp.net/api/rest/v2.7/json.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
if ( !$response ) {
return Array("code" => "FAULT", "message" => "API Error");
}
return json_decode($response, true);
}

$customerID = 1000; // Change this to your customer ID (available in portal under ADMINISTRATION -> MY DETAILS)
// locations are defined here: https://wiki.rackcorp.com/books/help-and-support-en/page/rackcorp-datacenter-locations-and-codes

$neworder = Array(
"productCode" => "SERVER_VIRTUAL_PERFORMANCE_AU",
"customerId" => $customerID,
"quantity" => 1,
"productDetails" => Array(
"hostname" => "testrouter01",
"cpu" => 4,
"memoryGB" => 4,
"trafficGB" => 100,
"nics" => Array(
Array(
"name" => "public",
"speed" -> 1000,
"ipv4" => 1,
)
),
"storage" => Array(
Array(
"name" => "MAIN",
"sizeGB" => 30,
"type" => "SSD",
"order" => 1
)
),
"credentials" => Array(
Array(
"username" => "root",
"password" => "DUGHsUIYGUI312TEST"
)
),
"location" => "RC-AU-GLOBESW1",
"timezone" => "UTC",
"install" => Array (
"operatingSystem" => "UBUNTU20.04_64",
"template" => "standard",
"postInstallScript" => ""
)
)
);
// Lodge the order (this just locks pricing in for up to 72 hours but doesnt actually create any resources)
$response = rackcorpAPI("order.create", $neworder);
var_dump($response);

// You can look up the order if you want:
$neworder = Array ("orderId" => $response["orderId"]);
$response = rackcorpAPI("order.getall", $neworder);
var_dump($response);

// Then confirm the order to start provisioning:
$neworder = Array ("orderId" => $response["orderId"]);
$response = rackcorpAPI("order.confirm", $neworder);
var_dump($response);

?>

Запуск сервера с помощью cloud-init:

После создания сервера вы также можете запустить его с помощью cloud-init со своим собственным кодом:

$cloudInitStartupData = Array(
"cloudInit" => Array(
"volumeName" => "config-2",
"userData" => "#cloud-config
ssh_pwauth: True
users:
- default
- name: user1
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
plain_text_passwd: testtest888
lock_passwd: false
",
"metaData" => "instance-id: ServerTest9999
local-hostname: MyServerHostname9999
"
)
);
$serverIDToStart = 9999;
$tx = Array ("objId"=>$serverIDToStart, "objType"=>"DEVICE", "type"=>"STARTUP", "data"=>json_encode($cloudInitStartupData));

// See earlier example for rackcorpAPI function
$response = rackcorpAPI("rctransaction.create", $tx);
var_dump($response);
?>