Skip to content

Commit b510c41

Browse files
authored
Merge pull request #13 from raccoon-mh/master
feat: enhance GoogleCloudConnector with proxy support and update requ…
2 parents d4cca4b + 7cdc1a5 commit b510c41

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

pkg/pip_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
google-api-python-client
22
schematics
33
spaceone-api>=1.0.0,<2.0.0
4-
spaceone-core>=1.0.0,<2.0.0
4+
spaceone-core>=1.0.0,<2.0.0
5+
PySocks # for proxy support
Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import logging
2+
import os
3+
24
import google.oauth2.service_account
35
import googleapiclient
46
import googleapiclient.discovery
5-
import logging
7+
import httplib2
8+
import socks
9+
from google_auth_httplib2 import AuthorizedHttp
610
from spaceone.core.connector import BaseConnector
711

8-
DEFAULT_SCHEMA = 'google_oauth_client_id'
12+
DEFAULT_SCHEMA = "google_oauth_client_id"
913
_LOGGER = logging.getLogger(__name__)
1014

1115

1216
class GoogleCloudConnector(BaseConnector):
13-
google_client_service = 'compute'
14-
version = 'v1'
17+
google_client_service = "compute"
18+
version = "v1"
1519

1620
def __init__(self, *args, **kwargs):
1721
"""
@@ -28,24 +32,77 @@ def __init__(self, *args, **kwargs):
2832
"""
2933

3034
super().__init__(*args, **kwargs)
31-
secret_data = kwargs.get('secret_data')
32-
self.project_id = secret_data.get('project_id')
33-
self.credentials = google.oauth2.service_account.Credentials.from_service_account_info(secret_data)
34-
self.client = googleapiclient.discovery.build(self.google_client_service,
35-
self.version,
36-
credentials=self.credentials)
35+
secret_data = kwargs.get("secret_data")
36+
self.project_id = secret_data.get("project_id")
37+
self.credentials = (
38+
google.oauth2.service_account.Credentials.from_service_account_info(
39+
secret_data
40+
)
41+
)
42+
proxy_http = self._create_http_client()
43+
if proxy_http:
44+
self.client = googleapiclient.discovery.build(
45+
self.google_client_service,
46+
self.version,
47+
http=AuthorizedHttp(
48+
self.credentials.with_scopes(
49+
[
50+
"https://www.googleapis.com/auth/cloud-platform"
51+
] # FOR PROXY SCOPE SUPPORT
52+
),
53+
http=proxy_http,
54+
),
55+
)
56+
else:
57+
self.client = googleapiclient.discovery.build(
58+
self.google_client_service,
59+
self.version,
60+
credentials=self.credentials,
61+
)
3762

3863
def verify(self, **kwargs):
3964
if self.client is None:
4065
self.set_connect(**kwargs)
4166

4267
def generate_query(self, **query):
43-
query.update({
44-
'project': self.project_id,
45-
})
68+
query.update(
69+
{
70+
"project": self.project_id,
71+
}
72+
)
4673
return query
4774

4875
def list_zones(self, **query):
4976
query = self.generate_query(**query)
5077
result = self.client.zones().list(**query).execute()
51-
return result.get('items', [])
78+
return result.get("items", [])
79+
80+
def _create_http_client(self):
81+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
82+
83+
if https_proxy:
84+
# _LOGGER.info(
85+
# f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}"
86+
# ) # TOO MANY LOGGING
87+
try:
88+
proxy_url = https_proxy.replace("http://", "").replace("https://", "")
89+
if ":" in proxy_url:
90+
proxy_host, proxy_port = proxy_url.split(":", 1)
91+
proxy_port = int(proxy_port)
92+
93+
proxy_info = httplib2.ProxyInfo(
94+
proxy_host=proxy_host,
95+
proxy_port=proxy_port,
96+
proxy_type=socks.PROXY_TYPE_HTTP,
97+
)
98+
99+
return httplib2.Http(
100+
proxy_info=proxy_info, disable_ssl_certificate_validation=True
101+
)
102+
except Exception as e:
103+
_LOGGER.warning(
104+
f"Failed to configure proxy. Using direct connection.: {e}. "
105+
)
106+
return None
107+
else:
108+
return None

src/cloudforet/monitoring/service/monitoring_service.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import logging
2+
import os
3+
24
from spaceone.core.service import *
5+
36
from cloudforet.monitoring.manager.monitoring_manager import MonitoringManager
47

58
_LOGGER = logging.getLogger(__name__)
@@ -13,10 +16,10 @@ def __init__(self, metadata):
1316
super().__init__(metadata)
1417

1518
@transaction
16-
@check_required(['options', 'secret_data', 'query', 'start', 'end'])
17-
@change_timestamp_value(['start', 'end'], timestamp_format='iso8601')
19+
@check_required(["options", "secret_data", "query", "start", "end"])
20+
@change_timestamp_value(["start", "end"], timestamp_format="iso8601")
1821
def list_logs(self, params):
19-
""" Get quick list of resources
22+
"""Get quick list of resources
2023
2124
Args:
2225
params (dict) {
@@ -33,6 +36,13 @@ def list_logs(self, params):
3336
3437
Returns: list of resources
3538
"""
39+
40+
proxy_env = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
41+
if proxy_env:
42+
_LOGGER.info(
43+
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_env}"
44+
) # src/cloudforet/monitoring/libs/google_cloud_connector.py _create_http_client
45+
3646
mon_manager = self.locator.get_manager(MonitoringManager)
3747
for logs in mon_manager.list_logs(params):
3848
yield logs

0 commit comments

Comments
 (0)