1+ import logging
2+ import os
3+
14import google .oauth2 .service_account
25import googleapiclient
36import googleapiclient .discovery
4- import logging
5-
7+ import httplib2
8+ import socks
9+ from google_auth_httplib2 import AuthorizedHttp
610from spaceone .core .connector import BaseConnector
711
812DEFAULT_SCHEMA = "google_oauth_client_id"
@@ -35,11 +39,26 @@ def __init__(self, *args, **kwargs):
3539 secret_data
3640 )
3741 )
38- self .client = googleapiclient .discovery .build (
39- self .google_client_service ,
40- self .version ,
41- credentials = self .credentials ,
42- )
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+ )
4362
4463 def generate_query (self , ** query ):
4564 query .update (
@@ -53,3 +72,33 @@ def list_zones(self, **query):
5372 query = self .generate_query (** query )
5473 result = self .client .zones ().list (** query ).execute ()
5574 return result .get ("items" , [])
75+
76+ def _create_http_client (self ):
77+ https_proxy = os .environ .get ("HTTPS_PROXY" ) or os .environ .get ("https_proxy" )
78+
79+ if https_proxy :
80+ # _LOGGER.info(
81+ # f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}"
82+ # ) # TOO MANY LOGGING
83+ try :
84+ proxy_url = https_proxy .replace ("http://" , "" ).replace ("https://" , "" )
85+ if ":" in proxy_url :
86+ proxy_host , proxy_port = proxy_url .split (":" , 1 )
87+ proxy_port = int (proxy_port )
88+
89+ proxy_info = httplib2 .ProxyInfo (
90+ proxy_host = proxy_host ,
91+ proxy_port = proxy_port ,
92+ proxy_type = socks .PROXY_TYPE_HTTP ,
93+ )
94+
95+ return httplib2 .Http (
96+ proxy_info = proxy_info , disable_ssl_certificate_validation = True
97+ )
98+ except Exception as e :
99+ _LOGGER .warning (
100+ f"Failed to configure proxy. Using direct connection.: { e } . "
101+ )
102+ return None
103+ else :
104+ return None
0 commit comments