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
Expand Up @@ -2,4 +2,5 @@ google-auth
google-api-python-client
schematics
spaceone-api>=1.0.0,<2.0.0
spaceone-core>=1.0.0,<2.0.0
spaceone-core>=1.0.0,<2.0.0
PySocks # for proxy support
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import logging
import os

import google.oauth2.service_account
import googleapiclient
import googleapiclient.discovery
import httplib2
import socks
from google_auth_httplib2 import AuthorizedHttp

from spaceone.core.connector import BaseConnector
from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import GoogleCloudMonitoring
from spaceone.monitoring.connector.google_cloud_connector.google_cloud_monitoring import (
GoogleCloudMonitoring,
)

__all__ = ['GoogleCloudConnector']
__all__ = ["GoogleCloudConnector"]
_LOGGER = logging.getLogger(__name__)


Expand All @@ -24,13 +32,43 @@ def set_connect(self, schema, options: dict, secret_data: dict):
- ...
"""
try:
self.project_id = secret_data.get('project_id')
credentials = google.oauth2.service_account.Credentials.from_service_account_info(secret_data)
self.client = googleapiclient.discovery.build('monitoring', 'v3', credentials=credentials)
google_client_service = "monitoring"
version = "v3"

self.project_id = secret_data.get("project_id")
credentials = (
google.oauth2.service_account.Credentials.from_service_account_info(
secret_data
)
)
proxy_http = self._create_http_client()
if proxy_http:
_LOGGER.info(
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_http}"
)
self.client = googleapiclient.discovery.build(
google_client_service,
version,
http=AuthorizedHttp(
credentials.with_scopes(
[
"https://www.googleapis.com/auth/cloud-platform"
] # FOR PROXY SCOPE SUPPORT
),
http=proxy_http,
),
)
else:
self.client = googleapiclient.discovery.build(
google_client_service,
version,
credentials=credentials,
)
except Exception as e:
print(e)
raise self.client(message='connection failed. Please check your authentication information.')
raise self.client(
message="connection failed. Please check your authentication information."
)

def list_metrics(self, *args, **kwargs):
monitoring = GoogleCloudMonitoring(self.client, self.project_id)
Expand All @@ -39,3 +77,33 @@ def list_metrics(self, *args, **kwargs):
def get_metric_data(self, *args, **kwargs):
monitoring = GoogleCloudMonitoring(self.client, self.project_id)
return monitoring.get_metric_data(*args, **kwargs)

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}"
)
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
Loading