Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit 4ae4bad

Browse files
author
Ricardo Kirkner
committed
Filter autoscaling service list to only show supported services
Improve the interactive autoscaling configuration UX by pre-filtering the service list to only show services that support horizontal scaling. This prevents users from selecting services that will fail validation, providing a cleaner and more intuitive experience.
1 parent f7f6357 commit 4ae4bad

2 files changed

Lines changed: 122 additions & 10 deletions

File tree

src/Command/Autoscaling/AutoscalingSettingsGetCommand.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Platformsh\Cli\Command\CommandBase;
66
use Platformsh\Cli\Service\Table;
77
use Platformsh\Client\Exception\EnvironmentStateException;
8+
use Platformsh\Client\Model\Deployment\Service;
89
use Symfony\Component\Console\Input\ArgvInput;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
@@ -68,6 +69,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
6869
}
6970

7071
if (!empty($autoscalingSettings['services'])) {
72+
// Filter autoscaling settings to only show services that are allowed to be configured
73+
$filteredSettings = $this->filterAutoscalingSettings($autoscalingSettings['services'], $services);
74+
75+
if (empty($filteredSettings)) {
76+
$this->stdErr->writeln(sprintf('No autoscaling configuration found for the project %s, environment %s.', $this->api()->getProjectLabel($this->getSelectedProject()), $this->api()->getEnvironmentLabel($environment)));
77+
$isOriginalCommand = $input instanceof ArgvInput;
78+
if ($isOriginalCommand) {
79+
$this->stdErr->writeln('');
80+
$this->stdErr->writeln(sprintf('You can configure autoscaling by running: <info>%s autoscaling:set</info>', $this->config()->get('application.executable')));
81+
}
82+
return 0;
83+
}
84+
7185
/** @var Table $table */
7286
$table = $this->getService('table');
7387

@@ -81,7 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8195
$empty = $table->formatIsMachineReadable() ? '' : '<comment>not set</comment>';
8296

8397
$rows = [];
84-
foreach ($autoscalingSettings['services'] as $service => $settings) {
98+
foreach ($filteredSettings as $service => $settings) {
8599
$row = [
86100
'service' => $service,
87101
'metric' => $empty,
@@ -131,4 +145,53 @@ protected function execute(InputInterface $input, OutputInterface $output)
131145

132146
return 0;
133147
}
148+
149+
/**
150+
* Filters autoscaling settings to only include services that are allowed to be configured.
151+
*
152+
* @param array $autoscalingSettings Autoscaling settings from the API
153+
* @param array $services Array of Service|WebApp|Worker objects from deployment
154+
*
155+
* @return array Filtered autoscaling settings
156+
*/
157+
protected function filterAutoscalingSettings(array $autoscalingSettings, array $services)
158+
{
159+
$project = $this->getSelectedProject();
160+
// Force refresh to ensure we have the latest capabilities
161+
$capabilities = $this->api()->getProjectCapabilities($project, true);
162+
$servicesCapabilityEnabled = !empty($capabilities->autoscaling['supports_horizontal_scaling_services']);
163+
164+
$filtered = [];
165+
foreach ($autoscalingSettings as $serviceName => $settings) {
166+
// Skip if service doesn't exist in deployment
167+
if (!isset($services[$serviceName])) {
168+
continue;
169+
}
170+
171+
$service = $services[$serviceName];
172+
$properties = $service->getProperties();
173+
174+
// For apps and workers: check supports_horizontal_scaling if present, otherwise allow
175+
if (!($service instanceof Service)) {
176+
if (isset($properties['supports_horizontal_scaling']) && !$properties['supports_horizontal_scaling']) {
177+
continue;
178+
}
179+
$filtered[$serviceName] = $settings;
180+
continue;
181+
}
182+
183+
// For services: check both the deployment property and the project capability
184+
if (!isset($properties['supports_horizontal_scaling']) || !$properties['supports_horizontal_scaling']) {
185+
continue;
186+
}
187+
188+
if (!$servicesCapabilityEnabled) {
189+
continue;
190+
}
191+
192+
$filtered[$serviceName] = $settings;
193+
}
194+
195+
return $filtered;
196+
}
134197
}

src/Command/Autoscaling/AutoscalingSettingsSetCommand.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
179179

180180
if ($showInteractiveForm) {
181181
// Interactive mode: let user select services and configure them
182-
$serviceNames = array_keys($services);
182+
// Filter to only show services that support autoscaling
183+
$supportedServices = $this->filterServicesWithAutoscalingSupport($services);
184+
185+
if (empty($supportedServices)) {
186+
$this->stdErr->writeln('No services that support autoscaling were found.');
187+
return 1;
188+
}
189+
190+
$serviceNames = array_keys($supportedServices);
183191

184192
if ($service === null) {
185193
// Ask user to select services to configure
@@ -189,9 +197,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
189197
$service = $serviceNames[$selectedService];
190198
}
191199

192-
// Validate that the selected service supports autoscaling
193-
$this->validateServiceSupportsAutoscaling($service, $services[$service]);
194-
195200
// Get autoscaling current values for selected service
196201
$currentServiceSettings = $autoscalingSettings['services'][$service];
197202

@@ -658,9 +663,9 @@ protected function validateServiceSupportsAutoscaling($serviceName, $service)
658663
{
659664
$properties = $service->getProperties();
660665

661-
// If supports_horizontal_scaling is explicitly set, use that value
662-
if (isset($properties['supports_horizontal_scaling'])) {
663-
if (!$properties['supports_horizontal_scaling']) {
666+
// For apps and workers: check supports_horizontal_scaling if present, otherwise allow
667+
if (!($service instanceof Service)) {
668+
if (isset($properties['supports_horizontal_scaling']) && !$properties['supports_horizontal_scaling']) {
664669
throw new InvalidArgumentException(sprintf(
665670
'The %s <error>%s</error> does not support autoscaling.',
666671
$this->typeName($service),
@@ -670,13 +675,57 @@ protected function validateServiceSupportsAutoscaling($serviceName, $service)
670675
return;
671676
}
672677

673-
// Fall back to current behavior: only apps and workers support autoscaling
674-
if ($service instanceof Service) {
678+
// For services: check both the deployment property and the project capability
679+
if (!isset($properties['supports_horizontal_scaling']) || !$properties['supports_horizontal_scaling']) {
675680
throw new InvalidArgumentException(sprintf(
676681
'The service <error>%s</error> does not support autoscaling.',
677682
$serviceName
678683
));
679684
}
685+
686+
$project = $this->getSelectedProject();
687+
// Force refresh to ensure we have the latest capabilities
688+
$capabilities = $this->api()->getProjectCapabilities($project, true);
689+
if (empty($capabilities->autoscaling['supports_horizontal_scaling_services'])) {
690+
throw new InvalidArgumentException(sprintf(
691+
'The service <error>%s</error> does not support autoscaling because the project does not have horizontal scaling enabled for services.',
692+
$serviceName
693+
));
694+
}
695+
}
696+
697+
/**
698+
* Filters services to only those that support autoscaling.
699+
*
700+
* @param array $services Array of Service|WebApp|Worker objects
701+
*
702+
* @return array Filtered array of services that support autoscaling
703+
*/
704+
protected function filterServicesWithAutoscalingSupport(array $services)
705+
{
706+
$project = $this->getSelectedProject();
707+
// Force refresh to ensure we have the latest capabilities
708+
$capabilities = $this->api()->getProjectCapabilities($project, true);
709+
$servicesCapabilityEnabled = !empty($capabilities->autoscaling['supports_horizontal_scaling_services']);
710+
711+
return array_filter($services, function ($service) use ($servicesCapabilityEnabled) {
712+
$properties = $service->getProperties();
713+
714+
// For apps and workers: check supports_horizontal_scaling if present, otherwise allow
715+
if (!($service instanceof Service)) {
716+
if (isset($properties['supports_horizontal_scaling'])) {
717+
return $properties['supports_horizontal_scaling'];
718+
}
719+
return true;
720+
}
721+
722+
// For services: check both the deployment property and the project capability
723+
if (!isset($properties['supports_horizontal_scaling']) || !$properties['supports_horizontal_scaling']) {
724+
return false;
725+
}
726+
727+
return $servicesCapabilityEnabled;
728+
});
680729
}
681730

682731
/**

0 commit comments

Comments
 (0)