Skip to content

Commit e266b11

Browse files
authored
Merge pull request #17 from raccoon-mh/master
enhance GoogleCloudConnector to support proxy
2 parents eb4f8fe + 42017d3 commit e266b11

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

pkg/pip_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ google-auth
22
google-api-python-client
33
schematics
44
spaceone-api>=1.0.0,<2.0.0
5-
spaceone-core>=1.0.0,<2.0.0
5+
spaceone-core>=1.0.0,<2.0.0
6+
PySocks # for proxy support

src/spaceone/monitoring/connector/google_cloud_connector/__init__.py

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import logging
2+
import os
3+
24
import google.oauth2.service_account
35
import googleapiclient
46
import googleapiclient.discovery
7+
import httplib2
8+
import socks
9+
from google_auth_httplib2 import AuthorizedHttp
10+
511
from spaceone.core.connector import BaseConnector
6-
from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import GoogleCloudMonitoring
12+
from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import (
13+
GoogleCloudMonitoring,
14+
)
715

8-
__all__ = ['GoogleCloudConnector']
16+
__all__ = ["GoogleCloudConnector"]
917
_LOGGER = logging.getLogger(__name__)
1018

1119

@@ -24,13 +32,43 @@ def set_connect(self, schema, options: dict, secret_data: dict):
2432
- ...
2533
"""
2634
try:
27-
self.project_id = secret_data.get('project_id')
28-
credentials = google.oauth2.service_account.Credentials.from_service_account_info(secret_data)
29-
self.client = googleapiclient.discovery.build('monitoring', 'v3', credentials=credentials)
35+
google_client_service = "monitoring"
36+
version = "v3"
3037

38+
self.project_id = secret_data.get("project_id")
39+
credentials = (
40+
google.oauth2.service_account.Credentials.from_service_account_info(
41+
secret_data
42+
)
43+
)
44+
proxy_http = self._create_http_client()
45+
if proxy_http:
46+
_LOGGER.info(
47+
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_http}"
48+
)
49+
self.client = googleapiclient.discovery.build(
50+
google_client_service,
51+
version,
52+
http=AuthorizedHttp(
53+
credentials.with_scopes(
54+
[
55+
"https://www.googleapis.com/auth/cloud-platform"
56+
] # FOR PROXY SCOPE SUPPORT
57+
),
58+
http=proxy_http,
59+
),
60+
)
61+
else:
62+
self.client = googleapiclient.discovery.build(
63+
google_client_service,
64+
version,
65+
credentials=credentials,
66+
)
3167
except Exception as e:
3268
print(e)
33-
raise self.client(message='connection failed. Please check your authentication information.')
69+
raise self.client(
70+
message="connection failed. Please check your authentication information."
71+
)
3472

3573
def list_metrics(self, *args, **kwargs):
3674
monitoring = GoogleCloudMonitoring(self.client, self.project_id)
@@ -39,3 +77,33 @@ def list_metrics(self, *args, **kwargs):
3977
def get_metric_data(self, *args, **kwargs):
4078
monitoring = GoogleCloudMonitoring(self.client, self.project_id)
4179
return monitoring.get_metric_data(*args, **kwargs)
80+
81+
def _create_http_client(self):
82+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
83+
84+
if https_proxy:
85+
_LOGGER.info(
86+
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}"
87+
)
88+
try:
89+
proxy_url = https_proxy.replace("http://", "").replace("https://", "")
90+
if ":" in proxy_url:
91+
proxy_host, proxy_port = proxy_url.split(":", 1)
92+
proxy_port = int(proxy_port)
93+
94+
proxy_info = httplib2.ProxyInfo(
95+
proxy_host=proxy_host,
96+
proxy_port=proxy_port,
97+
proxy_type=socks.PROXY_TYPE_HTTP,
98+
)
99+
100+
return httplib2.Http(
101+
proxy_info=proxy_info, disable_ssl_certificate_validation=True
102+
)
103+
except Exception as e:
104+
_LOGGER.warning(
105+
f"Failed to configure proxy. Using direct connection.: {e}. "
106+
)
107+
return None
108+
else:
109+
return None

0 commit comments

Comments
 (0)