1+ import logging
2+ import os
3+
14import google .oauth2 .service_account
25import googleapiclient
36import googleapiclient .discovery
4- import logging
7+ import httplib2
8+ import socks
9+ from google_auth_httplib2 import AuthorizedHttp
510
611from spaceone .core .connector import BaseConnector
712
@@ -35,9 +40,26 @@ def __init__(self, *args, **kwargs):
3540 secret_data
3641 )
3742 )
38- self .client = googleapiclient .discovery .build (
39- self .google_client_service , self .version , credentials = self .credentials
40- )
43+ proxy_http = self ._create_http_client ()
44+ if proxy_http :
45+ self .client = googleapiclient .discovery .build (
46+ self .google_client_service ,
47+ self .version ,
48+ http = AuthorizedHttp (
49+ self .credentials .with_scopes (
50+ [
51+ "https://www.googleapis.com/auth/cloud-platform"
52+ ] # FOR PROXY SCOPE SUPPORT
53+ ),
54+ http = proxy_http ,
55+ ),
56+ )
57+ else :
58+ self .client = googleapiclient .discovery .build (
59+ self .google_client_service ,
60+ self .version ,
61+ credentials = self .credentials ,
62+ )
4163
4264 def verify (self , ** kwargs ):
4365 if self .client is None :
@@ -55,3 +77,33 @@ def list_zones(self, **query):
5577 query = self .generate_query (** query )
5678 result = self .client .zones ().list (** query ).execute ()
5779 return result .get ("items" , [])
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+ # ) # TOO MANY LOGGING
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