From 4032b144ec290fc34d4c9dd3c39aff06c7d34fee Mon Sep 17 00:00:00 2001 From: raccoon-mh Date: Wed, 26 Nov 2025 16:30:25 +0900 Subject: [PATCH 1/2] feat: enhance GoogleCloudConnector to support proxy configuration and update pip requirements --- pkg/pip_requirements.txt | 3 +- .../google_cloud_connector/__init__.py | 80 +++++++++++++++++-- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/pkg/pip_requirements.txt b/pkg/pip_requirements.txt index c3a2e5f..089282f 100644 --- a/pkg/pip_requirements.txt +++ b/pkg/pip_requirements.txt @@ -2,4 +2,5 @@ google-auth google-api-python-client schematics spaceone-api>=1.0.0,<2.0.0 -spaceone-core>=1.0.0,<2.0.0 \ No newline at end of file +spaceone-core>=1.0.0,<2.0.0 +PySocks # for proxy support \ No newline at end of file diff --git a/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py b/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py index b336ab0..cfaec3b 100644 --- a/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py +++ b/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py @@ -1,11 +1,19 @@ import logging +import os + import google.oauth2.service_account import googleapiclient import googleapiclient.discovery +import httplib2 +import socks +from google_auth_httplib2 import AuthorizedHttp + from spaceone.core.connector import BaseConnector -from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import GoogleCloudMonitoring +from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import ( + GoogleCloudMonitoring, +) -__all__ = ['GoogleCloudConnector'] +__all__ = ["GoogleCloudConnector"] _LOGGER = logging.getLogger(__name__) @@ -24,13 +32,43 @@ def set_connect(self, schema, options: dict, secret_data: dict): - ... """ try: - self.project_id = secret_data.get('project_id') - credentials = google.oauth2.service_account.Credentials.from_service_account_info(secret_data) - self.client = googleapiclient.discovery.build('monitoring', 'v3', credentials=credentials) + google_client_service = "monitoring" + version = "v3" + self.project_id = secret_data.get("project_id") + credentials = ( + google.oauth2.service_account.Credentials.from_service_account_info( + secret_data + ) + ) + proxy_http = self._create_http_client() + if proxy_http: + _LOGGER.info( + f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_http}" + ) + self.client = googleapiclient.discovery.build( + google_client_service, + version, + http=AuthorizedHttp( + credentials.with_scopes( + [ + "https://www.googleapis.com/auth/cloud-platform" + ] # FOR PROXY SCOPE SUPPORT + ), + http=proxy_http, + ), + ) + else: + self.client = googleapiclient.discovery.build( + google_client_service, + version, + credentials=credentials, + ) except Exception as e: print(e) - raise self.client(message='connection failed. Please check your authentication information.') + raise self.client( + message="connection failed. Please check your authentication information." + ) def list_metrics(self, *args, **kwargs): monitoring = GoogleCloudMonitoring(self.client, self.project_id) @@ -39,3 +77,33 @@ def list_metrics(self, *args, **kwargs): def get_metric_data(self, *args, **kwargs): monitoring = GoogleCloudMonitoring(self.client, self.project_id) return monitoring.get_metric_data(*args, **kwargs) + + def _create_http_client(self): + https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy") + + if https_proxy: + # _LOGGER.info( + # f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}" + # ) # TOO MANY LOGGING + try: + proxy_url = https_proxy.replace("http://", "").replace("https://", "") + if ":" in proxy_url: + proxy_host, proxy_port = proxy_url.split(":", 1) + proxy_port = int(proxy_port) + + proxy_info = httplib2.ProxyInfo( + proxy_host=proxy_host, + proxy_port=proxy_port, + proxy_type=socks.PROXY_TYPE_HTTP, + ) + + return httplib2.Http( + proxy_info=proxy_info, disable_ssl_certificate_validation=True + ) + except Exception as e: + _LOGGER.warning( + f"Failed to configure proxy. Using direct connection.: {e}. " + ) + return None + else: + return None From 42017d337446dbbb3b2c62b3504c64f4501616cb Mon Sep 17 00:00:00 2001 From: raccoon-mh Date: Wed, 26 Nov 2025 16:31:39 +0900 Subject: [PATCH 2/2] fix: enable logging for proxy configuration in GoogleCloudConnector --- .../monitoring/connector/google_cloud_connector/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py b/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py index cfaec3b..2d166f4 100644 --- a/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py +++ b/src/spaceone/monitoring/connector/google_cloud_connector/__init__.py @@ -82,9 +82,9 @@ def _create_http_client(self): https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy") if https_proxy: - # _LOGGER.info( - # f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}" - # ) # TOO MANY LOGGING + _LOGGER.info( + f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}" + ) try: proxy_url = https_proxy.replace("http://", "").replace("https://", "") if ":" in proxy_url: