Skip to content

Commit d6a3c68

Browse files
committed
Add PHP8 compatibility, Add new service:engagement action
1 parent 0f50570 commit d6a3c68

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ A simple [OVH API](https://api.ovh.com/console/) client for managing OVH infrast
2222
- Perform IPMI reset
2323
- Request hardware reboot
2424
- Manage service renewal
25+
- Manage service engagement
2526

2627
**vRack**
2728
- List associated servers
@@ -44,7 +45,7 @@ A simple [OVH API](https://api.ovh.com/console/) client for managing OVH infrast
4445

4546
## Requirements
4647

47-
* PHP >= 7.3
48+
* PHP >= 8
4849
* PHP gmp extension (used by [rlanvin/php-ip](https://github.com/rlanvin/php-ip))
4950
* [Composer](https://getcomposer.org)
5051

@@ -56,11 +57,6 @@ $ cd php-ovh-cli
5657
$ composer update
5758
```
5859

59-
Also works on PHP8, but with some workaround:
60-
```
61-
$ composer update --ignore-platform-reqs
62-
```
63-
6460
If you prefer to have a standalone executable, then create a PHAR package:
6561
```
6662
$ ./create-phar.php

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"require": {
33
"ovh/ovh": "^2.0",
4-
"rlanvin/php-ip": "^2.0",
5-
"ulrichsg/getopt-php": "^3.2",
6-
"phpfastcache/phpfastcache": "^7.1"
4+
"rlanvin/php-ip": "^3.0",
5+
"ulrichsg/getopt-php": "^4.0",
6+
"phpfastcache/phpfastcache": "^8.0"
77
},
88
"autoload": {
99
"psr-4": {

src/Command/Service/Engagement.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace OvhCli\Command\Service;
4+
5+
use GetOpt\GetOpt;
6+
use GetOpt\Operand;
7+
use GetOpt\Option;
8+
use OvhCli\Cli;
9+
10+
class Engagement extends \OvhCli\Command
11+
{
12+
public $shortDescription = 'Manages dedicated servers billing engagement';
13+
14+
public function __construct()
15+
{
16+
parent::__construct($this->getName(), [$this, 'handle']);
17+
18+
$this->addOperands([
19+
Operand::create('server', Operand::MULTIPLE)
20+
->setDescription('Server name'),
21+
]);
22+
23+
$this->addOptions([
24+
Option::create('a', 'all', GetOpt::NO_ARGUMENT)
25+
->setDescription('Run against all servers'),
26+
Option::create(null, 'on', GetOpt::NO_ARGUMENT)
27+
->setDescription('Enable service billing engagement'),
28+
Option::create(null, 'off', GetOpt::NO_ARGUMENT)
29+
->setDescription('Disable service billing engagement'),
30+
Option::create(null, 'enabled-only', GetOpt::NO_ARGUMENT)
31+
->setDescription('Shows enabled services only'),
32+
]);
33+
}
34+
35+
public function handle(GetOpt $getopt)
36+
{
37+
$servers = $getopt->getOption('all') ?
38+
$this->ovh()->getServers() :
39+
$getopt->getOperand('server');
40+
41+
$on = (bool) $getopt->getOption('on');
42+
$off = (bool) $getopt->getOption('off');
43+
$show_enabled_only = (bool) $getopt->getOption('enabled-only');
44+
45+
if ($on && $off) {
46+
return $this->missingArgument($getopt, 'ERROR: --on and --off options should be enabled exclusively');
47+
}
48+
49+
if (empty($servers)) {
50+
echo $getopt->getHelpText();
51+
exit();
52+
}
53+
54+
$data = [];
55+
foreach ($servers as $server) {
56+
$server = $this->resolve($server);
57+
$details = $this->ovh()->getServerDetails($server);
58+
$service = $this->ovh()->getServerServiceInfo($server);
59+
$service_id = $service['serviceId'];
60+
try {
61+
$commitment = $this->ovh()->getServiceBillingEngagement($service_id);
62+
$strategy = $commitment['endRule']['strategy'];
63+
$engagement_enabled = ($strategy == 'REACTIVATE_ENGAGEMENT');
64+
} catch (\Exception $e) {
65+
// Server doesn't support billing engagement
66+
continue;
67+
}
68+
69+
if ($engagement_enabled && $off) {
70+
Cli::out('Disabling server engagement on %s ...', $details['reverse']);
71+
$this->ovh()->setServiceBillingEngagement($service_id, false);
72+
} elseif (!$engagement_enabled && $on) {
73+
Cli::out('Enabling server engagement on %s ...', $details['reverse']);
74+
$this->ovh()->setServiceBillingEngagement($service_id, true);
75+
} elseif (!$on && !$off) {
76+
if (!$engagement_enabled && $show_enabled_only) {
77+
continue;
78+
}
79+
$data[$server] = [
80+
'reverse' => $details['reverse'],
81+
'enabled' => $engagement_enabled,
82+
'period' => $commitment['currentPeriod'],
83+
];
84+
}
85+
}
86+
87+
if (!empty($data)) {
88+
Cli::format($data);
89+
}
90+
}
91+
}

src/Command/Service/Info.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function handle(GetOpt $getopt)
3838
} catch (\Exception $e) {
3939
Cli::error($e);
4040
}
41+
ksort($service);
4142
Cli::format($service, [
4243
'maxSize' => 40,
4344
'grep' => (bool) $getopt->getOption('grep'),

src/Ovh.php

+15
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,21 @@ public function replyToSupportTicket($id, $body)
566566
]);
567567
}
568568

569+
public function getServiceBillingEngagement($id)
570+
{
571+
return $this->get("/services/{$id}/billing/engagement");
572+
}
573+
574+
public function setServiceBillingEngagement($id, bool $value)
575+
{
576+
$res = $this->put("/services/{$id}/billing/engagement/endRule", [
577+
'strategy' => ($value === true) ?
578+
'REACTIVATE_ENGAGEMENT' :
579+
'STOP_ENGAGEMENT_FALLBACK_DEFAULT_PRICE',
580+
]);
581+
return $res;
582+
}
583+
569584
protected function shouldCache($url)
570585
{
571586
if (self::$disableCache) {

0 commit comments

Comments
 (0)