Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/pip_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
spaceone-api
google-api-python-client
requests
beautifulsoup4
beautifulsoup4
PySocks # for proxy support
63 changes: 56 additions & 7 deletions src/cloudforet/plugin/connector/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import logging
import os

import google.oauth2.service_account
import googleapiclient
import googleapiclient.discovery
import logging

import httplib2
import socks
from google_auth_httplib2 import AuthorizedHttp
from spaceone.core.connector import BaseConnector

DEFAULT_SCHEMA = "google_oauth_client_id"
Expand Down Expand Up @@ -35,11 +39,26 @@ def __init__(self, *args, **kwargs):
secret_data
)
)
self.client = googleapiclient.discovery.build(
self.google_client_service,
self.version,
credentials=self.credentials,
)
proxy_http = self._create_http_client()
if proxy_http:
self.client = googleapiclient.discovery.build(
self.google_client_service,
self.version,
http=AuthorizedHttp(
self.credentials.with_scopes(
[
"https://www.googleapis.com/auth/cloud-platform"
] # FOR PROXY SCOPE SUPPORT
),
http=proxy_http,
),
)
else:
self.client = googleapiclient.discovery.build(
self.google_client_service,
self.version,
credentials=self.credentials,
)

def generate_query(self, **query):
query.update(
Expand All @@ -53,3 +72,33 @@ def list_zones(self, **query):
query = self.generate_query(**query)
result = self.client.zones().list(**query).execute()
return result.get("items", [])

def _create_http_client(self):
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")

if https_proxy:
# _LOGGER.info(
# f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}"
# ) # TOO MANY LOGGING
try:
proxy_url = https_proxy.replace("http://", "").replace("https://", "")
if ":" in proxy_url:
proxy_host, proxy_port = proxy_url.split(":", 1)
proxy_port = int(proxy_port)

proxy_info = httplib2.ProxyInfo(
proxy_host=proxy_host,
proxy_port=proxy_port,
proxy_type=socks.PROXY_TYPE_HTTP,
)

return httplib2.Http(
proxy_info=proxy_info, disable_ssl_certificate_validation=True
)
except Exception as e:
_LOGGER.warning(
f"Failed to configure proxy. Using direct connection.: {e}. "
)
return None
else:
return None
11 changes: 9 additions & 2 deletions src/cloudforet/plugin/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import logging
import os
import time

from spaceone.inventory.plugin.collector.lib.server import CollectorPluginServer

Expand Down Expand Up @@ -32,7 +33,13 @@ def collector_collect(params: dict) -> dict:
options = params["options"]
secret_data = params["secret_data"]
schema = params.get("schema")


proxy_env = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
if proxy_env:
_LOGGER.debug(
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_env}"
) # src/cloudforet/plugin/connector/base.py _create_http_client

resource_mgrs = ResourceManager.list_managers()
for manager in resource_mgrs:
start_time = time.time()
Expand Down
Loading