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 @@
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
85 changes: 71 additions & 14 deletions src/cloudforet/monitoring/libs/google_cloud_connector.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
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'
DEFAULT_SCHEMA = "google_oauth_client_id"
_LOGGER = logging.getLogger(__name__)


class GoogleCloudConnector(BaseConnector):
google_client_service = 'compute'
version = 'v1'
google_client_service = "compute"
version = "v1"

def __init__(self, *args, **kwargs):
"""
Expand All @@ -28,24 +32,77 @@ def __init__(self, *args, **kwargs):
"""

super().__init__(*args, **kwargs)
secret_data = kwargs.get('secret_data')
self.project_id = secret_data.get('project_id')
self.credentials = google.oauth2.service_account.Credentials.from_service_account_info(secret_data)
self.client = googleapiclient.discovery.build(self.google_client_service,
self.version,
credentials=self.credentials)
secret_data = kwargs.get("secret_data")
self.project_id = secret_data.get("project_id")
self.credentials = (
google.oauth2.service_account.Credentials.from_service_account_info(
secret_data
)
)
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 verify(self, **kwargs):
if self.client is None:
self.set_connect(**kwargs)

def generate_query(self, **query):
query.update({
'project': self.project_id,
})
query.update(
{
"project": self.project_id,
}
)
return query

def list_zones(self, **query):
query = self.generate_query(**query)
result = self.client.zones().list(**query).execute()
return result.get('items', [])
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
16 changes: 13 additions & 3 deletions src/cloudforet/monitoring/service/monitoring_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import os

from spaceone.core.service import *

from cloudforet.monitoring.manager.monitoring_manager import MonitoringManager

_LOGGER = logging.getLogger(__name__)
Expand All @@ -13,10 +16,10 @@ def __init__(self, metadata):
super().__init__(metadata)

@transaction
@check_required(['options', 'secret_data', 'query', 'start', 'end'])
@change_timestamp_value(['start', 'end'], timestamp_format='iso8601')
@check_required(["options", "secret_data", "query", "start", "end"])
@change_timestamp_value(["start", "end"], timestamp_format="iso8601")
def list_logs(self, params):
""" Get quick list of resources
"""Get quick list of resources

Args:
params (dict) {
Expand All @@ -33,6 +36,13 @@ def list_logs(self, params):

Returns: list of resources
"""

proxy_env = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
if proxy_env:
_LOGGER.info(
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_env}"
) # src/cloudforet/monitoring/libs/google_cloud_connector.py _create_http_client

mon_manager = self.locator.get_manager(MonitoringManager)
for logs in mon_manager.list_logs(params):
yield logs
Loading