diff --git a/modules/cloud_service_providers/aws_cloud/aws_cloud.install b/modules/cloud_service_providers/aws_cloud/aws_cloud.install index 35553ea..b79ebd8 100644 --- a/modules/cloud_service_providers/aws_cloud/aws_cloud.install +++ b/modules/cloud_service_providers/aws_cloud/aws_cloud.install @@ -10,6 +10,8 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\Core\Field\BaseFieldDefinition; +use Drupal\aws_cloud\Service\Pricing\PricingService; + /** * Implements hook_uninstall(). */ @@ -1573,6 +1575,16 @@ function aws_cloud_update_8188() { } /** + * Add EC2 pricing endpoint configuration item. + */ +function aws_cloud_update_8189() { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('aws_cloud.settings'); + $config->set('aws_cloud_ec2_pricing_endpoint', PricingService::DEFAULT_ENDPOINT); + $config->save(); +} + +/** * Helper function to add fields to the entity type. * * @param string $entity_type diff --git a/modules/cloud_service_providers/aws_cloud/config/install/aws_cloud.settings.yml b/modules/cloud_service_providers/aws_cloud/config/install/aws_cloud.settings.yml index 6455c81..5ecd91e 100644 --- a/modules/cloud_service_providers/aws_cloud/config/install/aws_cloud.settings.yml +++ b/modules/cloud_service_providers/aws_cloud/config/install/aws_cloud.settings.yml @@ -39,4 +39,5 @@ aws_cloud_instance_type_cost_list: true aws_cloud_instance_list_cost_column: true google_credential_file_path: 'private://aws_cloud/.gapps/client_secrets.json' google_credential_signature: '' +aws_cloud_ec2_pricing_endpoint: 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current' aws_cloud_price_rate_ec2: 100 diff --git a/modules/cloud_service_providers/aws_cloud/config/schema/aws_cloud.settings.schema.yml b/modules/cloud_service_providers/aws_cloud/config/schema/aws_cloud.settings.schema.yml index 6e53eb6..855f2cb 100644 --- a/modules/cloud_service_providers/aws_cloud/config/schema/aws_cloud.settings.schema.yml +++ b/modules/cloud_service_providers/aws_cloud/config/schema/aws_cloud.settings.schema.yml @@ -105,5 +105,7 @@ aws_cloud.settings: type: string google_credential_signature: type: string + aws_cloud_ec2_pricing_endpoint: + type: string aws_cloud_price_rate_ec2: type: integer diff --git a/modules/cloud_service_providers/aws_cloud/src/Form/Config/AwsCloudAdminSettings.php b/modules/cloud_service_providers/aws_cloud/src/Form/Config/AwsCloudAdminSettings.php index 5b98174..448fe35 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Form/Config/AwsCloudAdminSettings.php +++ b/modules/cloud_service_providers/aws_cloud/src/Form/Config/AwsCloudAdminSettings.php @@ -8,6 +8,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\aws_cloud\Service\Pricing\GoogleSpreadsheetService; +use Drupal\aws_cloud\Service\Pricing\PricingService; use Drupal\cloud\Plugin\cloud\config\CloudConfigPluginManagerInterface; use Drupal\Core\Config\Config; @@ -589,6 +590,16 @@ class AwsCloudAdminSettings extends ConfigFormBase { '#default_value' => $config->get('aws_cloud_instance_list_cost_column'), ]; + $form['cost_management']['aws_cloud_ec2_pricing_endpoint'] = [ + '#type' => 'textfield', + '#title' => $this->t('EC2 Pricing Endpoint'), + '#description' => $this->t('The endpoint of EC2 pricing service.'), + '#size' => 80, + '#default_value' => + $config->get('aws_cloud_ec2_pricing_endpoint') + ?: PricingService::DEFAULT_ENDPOINT, + ]; + $form['price_rate'] = [ '#type' => 'details', '#title' => $this->t('Price Rate'), diff --git a/modules/cloud_service_providers/aws_cloud/src/Service/Pricing/PricingService.php b/modules/cloud_service_providers/aws_cloud/src/Service/Pricing/PricingService.php index 043fd3e..e514055 100644 --- a/modules/cloud_service_providers/aws_cloud/src/Service/Pricing/PricingService.php +++ b/modules/cloud_service_providers/aws_cloud/src/Service/Pricing/PricingService.php @@ -18,6 +18,8 @@ class PricingService implements PricingServiceInterface { use StringTranslationTrait; + const DEFAULT_ENDPOINT = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current'; + /** * The Messenger service. * @@ -73,13 +75,6 @@ class PricingService implements PricingServiceInterface { private $dryRun; /** - * The base url for the pricing endpoint. - * - * @var string - */ - private $ec2PricingEndpoint = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current'; - - /** * The cloud service provider (CloudConfig) entity. * * @var \Drupal\cloud\Entity\CloudConfig @@ -147,7 +142,15 @@ class PricingService implements PricingServiceInterface { private function getPricingEndpoint() { $endpoint = FALSE; if ($this->cloudConfigEntity != FALSE) { - $endpoint = $this->ec2PricingEndpoint . "/{$this->cloudConfigEntity->get('field_region')->value}/index.json"; + $pricing_endpoint = $this->configFactory + ->get('aws_cloud.settings') + ->get('aws_cloud_ec2_pricing_endpoint'); + + if (empty($pricing_endpoint)) { + $pricing_endpoint = self::DEFAULT_ENDPOINT; + } + + $endpoint = "$pricing_endpoint/{$this->cloudConfigEntity->get('field_region')->value}/index.json"; } return $endpoint; }