11import logging
2+ import os
3+
24import google .oauth2 .service_account
35import googleapiclient
46import googleapiclient .discovery
5- import logging
7+ import httplib2
8+ import socks
9+ from google_auth_httplib2 import AuthorizedHttp
610from 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
1216class 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
0 commit comments