Skip to content

Commit

Permalink
Update to latest version from internal nitrado repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Birkner committed Nov 5, 2016
1 parent 6563d4f commit 298e9bd
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 20 deletions.
11 changes: 10 additions & 1 deletion lib/Nitrapi/Order/Pricing/DimensionPricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ public function getPrice($rentalTime, Service &$service = null) {
}

if (is_array($prices) && isset($prices['price'])) {
return (int)$prices['price'];
$price = (int)$prices['price'];
$advice = $information['advice'];

if ($advice > $price) {
$advice -= (($advice - $price) * (50.0 / 100));
}

$price -= $advice;

return $price;
}

throw new PricingException("No price for selected dimensions not found.");
Expand Down
3 changes: 3 additions & 0 deletions lib/Nitrapi/Order/Pricing/PartPricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function getPrice($rentalTime, Service &$service = null) {
$totalPrice += $bestPrice;
}

//100%
$totalPrice -= $prices['advice'];

return $totalPrice;
}

Expand Down
27 changes: 26 additions & 1 deletion lib/Nitrapi/Order/Pricing/Pricing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ abstract class Pricing implements PricingInterface {
*/
protected $prices = null;

/**
* Currency for price calculation
*
* @var array
*/
protected $currency = null;

/**
* Will be overwritten by parents
*/
Expand All @@ -40,6 +47,15 @@ public function __construct(Nitrapi &$nitrapi, Location $location)
$this->locationId = $location->getId();
}

/**
* This can be used only for pricing information
*
* @var $currency
*/
public function setCurrency($currency = null) {
$this->currency = $currency;
}

/**
* Changes the location id
*
Expand Down Expand Up @@ -94,7 +110,7 @@ public static function getLocation(Nitrapi &$nitrapi, $id) {
* @return mixed
*/
public function getPrices(Service &$service = null) {
$cacheName = $this->locationId;
$cacheName = $this->locationId . "/" . $this->currency;
if ($service instanceof Service) $cacheName .= "/" . $service->getId();
if (isset($this->prices[$cacheName])) {
return $this->prices[$cacheName];
Expand All @@ -108,6 +124,10 @@ public function getPrices(Service &$service = null) {
$query['sale_service'] = $service->getId();
}

if (!empty($this->currency) && (!($service instanceof Service))) {
$query['currency'] = $this->currency;
}

$this->prices[$cacheName] = $this->nitrapi->dataGet("/order/pricing/" . $this->getProduct(), null, [
'query' => $query
])['prices'];
Expand All @@ -122,6 +142,7 @@ public function getPrices(Service &$service = null) {
* @param $rentalTime
*/
public function getExtendPriceForService(Service &$service, $rentalTime) {
$this->setCurrency(null); //use user currency
return $this->prices[$this->locationId] = $this->nitrapi->dataGet("/order/pricing/" . $this->getProduct(), null, [
'query' => [
'method' => 'extend',
Expand All @@ -138,6 +159,7 @@ public function getExtendPriceForService(Service &$service, $rentalTime) {
* @return bool
*/
public function extendService(Service &$service, $rentalTime) {
$this->setCurrency(null); //use user currency
$price = $this->getExtendPriceForService($service, $rentalTime);
$orderArray = [
'price' => $price,
Expand All @@ -158,6 +180,7 @@ public function extendService(Service &$service, $rentalTime) {
* @return bool
*/
public function orderService($rentalTime) {
$this->setCurrency(null); //use user currency
if ($this instanceof PartPricing) {
$this->checkDependencies();
$orderArray = [
Expand Down Expand Up @@ -193,6 +216,7 @@ public function orderService($rentalTime) {
* @return mixed
*/
public function getSwitchPrice(Service &$service, $rentalTime) {
$this->setCurrency(null); //use user currency
return $this->getPrice($rentalTime, $service);
}

Expand All @@ -204,6 +228,7 @@ public function getSwitchPrice(Service &$service, $rentalTime) {
* @return bool
*/
public function switchService(Service &$service, $rentalTime) {
$this->setCurrency(null); //use user currency
if ($this instanceof PartPricing) {
$this->checkDependencies();
$orderArray = [
Expand Down
17 changes: 17 additions & 0 deletions lib/Nitrapi/Order/Pricing/Products/CloudServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Nitrapi\Order\Pricing\Products;

use Nitrapi\Common\Exceptions\NitrapiErrorException;
use Nitrapi\Order\Pricing\PartPricing;
use Nitrapi\Services\CloudServers\Image;

class CloudServer extends PartPricing {

protected static $product = 'cloud_server';

public function setImage(Image $image) {
$this->additionals['image_id'] = $image->getId();
}

}
146 changes: 145 additions & 1 deletion lib/Nitrapi/Services/CloudServers/CloudServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nitrapi\Services\CloudServers;

use Nitrapi\Common\Exceptions\NitrapiException;
use Nitrapi\Nitrapi;
use Nitrapi\Services\Service;

Expand All @@ -25,17 +26,160 @@ public function refresh() {
/**
* Returns informations about the gameserver
*
* @return mixed
* @return CloudServerDetails
*/
public function getDetails() {
return new CloudServerDetails($this->info['cloud_server']);
}

/**
* Returns the password if it's still available.
* After the password has been received it will
* be permanently deleted from the Nitrado database.
*
* @return mixed
*/
public function getInitialPassword() {
$url = "services/" . $this->getId() . "/cloud_servers/password";
$password = $this->getApi()->dataGet($url);

if (isset($password['password'])) {
return $password['password'];
}

return null;
}

/**
* Boots a turned down server.
*
* @return bool
*/
public function doBoot() {
$url = "services/" . $this->getId() . "/cloud_servers/boot";
$this->getApi()->dataPost($url);
return true;
}

/**
* Sends a shutdown command via ACPI.
*
* @return bool
*/
public function doShutdown() {
$url = "services/" . $this->getId() . "/cloud_servers/shutdown";
$this->getApi()->dataPost($url);
return true;
}

/**
* Sends a reboot command via ACPI.
*
* @return bool
*/
public function doReboot() {
$url = "services/" . $this->getId() . "/cloud_servers/reboot";
$this->getApi()->dataPost($url);
return true;
}

/**
* This method resets your server immediately.
* This action might result in data loss.
*
* @return bool
*/
public function doHardReset() {
$url = "services/" . $this->getId() . "/cloud_servers/hard_reset";
$this->getApi()->dataPost($url);
return true;
}

/**
* Returns the noVNC console endpoint.
*
* @return array
*/
public function getConsole() {
$url = "services/" . $this->getId() . "/cloud_servers/console";
return $this->getApi()->dataGet($url);
}

/**
* Changes the PTR record of a specific IPv4 address.
*
* @param string $ip
* @param string $hostname
* @return bool
*/
public function changePTRRecord($ip, $hostname) {
$url = "services/" . $this->getId() . "/cloud_servers/ptr/" . $ip;
$this->getApi()->dataPost($url, [
'hostname' => $hostname
]);
return true;
}

/**
* Changes the hostname of the server.
* If no hostname has been provided, it will be reset to default.
*
* @param string $hostname
* @return bool
*/
public function changeHostname($hostname = null) {
$url = "services/" . $this->getId() . "/cloud_servers/hostname";
$this->getApi()->dataPost($url, [
'hostname' => $hostname
]);
return true;
}

/**
* Returns a full list with all available images.
*
* @param Nitrapi $nitrapi
* @return array
*/
public static function getAvailableImages(Nitrapi &$nitrapi) {
$images = $nitrapi->dataGet('/information/cloud_servers/images');
$imgs = [];
foreach ($images['images'] as $image)
$imgs[] = new Image($image['id'], $image['name'], $image['is_windows']);
return $imgs;
}

/**
* Returns the daily traffic usage of the last 30 days.
*
* @return array
*/
public function getTrafficStatistics() {
$url = "services/" . $this->getId() . "/cloud_servers/traffic";
return $this->getApi()->dataGet($url)['traffic'];
}

/**
* Triggers a reinstallation.
* Optional you can pass a new image.
*
* DANGER! This deletes all your data!
*
* @param Image|null $image
*/
public function doReinstall(Image $image = null) {
$url = "services/" . $this->getId() . "/cloud_servers/reinstall";

$data = [];
if ($image instanceof Image) {
if ($image->isWindows() && !$this->getDetails()->getHardwareInfo()['windows']) {
throw new NitrapiException("You need to rent the windows option to install a windows image.");
}

$data['image_id'] = $image->getId();
}

$this->getApi()->dataPost($url, $data);
return true;
}
}
63 changes: 57 additions & 6 deletions lib/Nitrapi/Services/CloudServers/CloudServerDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,72 @@ public function getHostname() {
}

/**
* Returns the Cloud Server ip
* Returns the main ip address of the server
*
* @return string
*/
public function getIP() {
return (string)$this->data['ip'];
public function getMainIP() {
foreach ($this->getIPs() as $ip) {
if ($ip['main_ip'] && $ip['version'] == 4) {
return $ip['address'];
}
}

return null;
}

/**
* Returns the Cloud Server ips
*
* @return array
*/
public function getIPs() {
return (array)$this->data['ips'];
}

/**
* Returns the Hardware information
*
* @return array
*/
public function getHardwareInfo() {
return (array)$this->data['hardware'];
}

/**
* Return true if the initial password is available
*
* @return boolean
*/
public function isPasswordAvailable() {
return (bool)$this->data['password_available'];
}

/**
* Return true if the bandwdith is currently limited
*
* @return boolean
*/
public function isBandwidthLimited() {
return (bool)$this->data['bandwidth_limited'];
}

/**
* Returns the ID of the currently installed image.
*
* @return int
*/
public function getImageId() {
return $this->data['image']['id'];
}

/**
* Returns the initial admin password
* Returns the name of the currently installed image, as displayed to the user.
*
* @return string
*/
public function getInitialPassword() {
return (string)$this->data['initial_password'];
public function getImageName() {
return $this->data['image']['name'];
}

}
Loading

0 comments on commit 298e9bd

Please sign in to comment.