Skip to content

Commit

Permalink
Merge pull request #130 from chiniforooshan/18-07-05
Browse files Browse the repository at this point in the history
GCP: Fixing tests and applying API changes
  • Loading branch information
nuwang authored Sep 4, 2018
2 parents 0536951 + 939520e commit fe7d702
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 306 deletions.
16 changes: 10 additions & 6 deletions cloudbridge/cloud/base/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,16 @@ class BasePageableObjectMixin(PageableObjectMixin):
"""

def __iter__(self):
result_list = self.list()
for result in self.iter():
yield result

def iter(self, **kwargs):
result_list = self.list(**kwargs)
if result_list.supports_server_paging:
for result in result_list:
yield result
while result_list.is_truncated:
result_list = self.list(marker=result_list.marker)
result_list = self.list(marker=result_list.marker, **kwargs)
for result in result_list:
yield result
else:
Expand Down Expand Up @@ -863,8 +867,8 @@ def __eq__(self, other):
self.name == other.name)

def __repr__(self):
return "<CB-{0}: {1}>".format(self.__class__.__name__,
self.name)
return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
self.name, self.id)


class BaseBucket(BaseCloudResource, Bucket):
Expand Down Expand Up @@ -902,8 +906,8 @@ def __eq__(self, other):
self.name == other.name)

def __repr__(self):
return "<CB-{0}: {1}>".format(self.__class__.__name__,
self.name)
return "<CB-{0}: {1} ({2})>".format(self.__class__.__name__,
self.name, self.id)


class BaseBucketContainer(BasePageableObjectMixin, BucketContainer):
Expand Down
57 changes: 46 additions & 11 deletions cloudbridge/cloud/providers/gce/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import cloudbridge as cb
from cloudbridge.cloud.base import BaseCloudProvider
from cloudbridge.cloud.interfaces.exceptions import ProviderConnectionException

import googleapiclient
from googleapiclient import discovery
Expand Down Expand Up @@ -229,19 +230,16 @@ def __init__(self, config):
# service connections, lazily initialized
self._gce_compute = None
self._gcs_storage = None
self._credentials_cache = None
self._compute_resources_cache = None
self._storage_resources_cache = None

# Initialize provider services
self._compute = GCEComputeService(self)
self._security = GCESecurityService(self)
self._networking = GCENetworkingService(self)
self._storage = GCPStorageService(self)

self._compute_resources = GCPResources(self.gce_compute,
project=self.project_name,
region=self.region_name,
zone=self.default_zone)
self._storage_resources = GCPResources(self.gcs_storage)

@property
def compute(self):
return self._compute
Expand Down Expand Up @@ -270,13 +268,40 @@ def gcs_storage(self):
self._gcs_storage = self._connect_gcs_storage()
return self._gcs_storage

@property
def _compute_resources(self):
if not self._compute_resources_cache:
self._compute_resources_cache = GCPResources(
self.gce_compute,
project=self.project_name,
region=self.region_name,
zone=self.default_zone)
return self._compute_resources_cache

@property
def _storage_resources(self):
if not self._storage_resources_cache:
self._storage_resources_cache = GCPResources(self.gcs_storage)
return self._storage_resources_cache

@property
def _credentials(self):
if self.credentials_dict:
return ServiceAccountCredentials.from_json_keyfile_dict(
self.credentials_dict)
else:
return GoogleCredentials.get_application_default()
if not self._credentials_cache:
if self.credentials_dict:
self._credentials_cache = (
ServiceAccountCredentials.from_json_keyfile_dict(
self.credentials_dict))
else:
self._credentials_cache = (
GoogleCredentials.get_application_default())
return self._credentials_cache

def sign_blob(self, string_to_sign):
return self._credentials.sign_blob(string_to_sign)[1]

@property
def client_id(self):
return self._credentials.service_account_email

def _connect_gcs_storage(self):
return discovery.build('storage', 'v1', credentials=self._credentials,
Expand Down Expand Up @@ -311,6 +336,8 @@ def parse_url(self, url):
return out if out else self._storage_resources.parse_url(url)

def get_resource(self, resource, url_or_name, **kwargs):
if not url_or_name:
return None
resource_url = (
self._compute_resources.get_resource_url_with_default(
resource, url_or_name, **kwargs) or
Expand All @@ -324,3 +351,11 @@ def get_resource(self, resource, url_or_name, **kwargs):
cb.log.warning(
"googleapiclient.errors.HttpError: {0}".format(http_error))
return None

def authenticate(self):
try:
self.gce_compute
return True
except Exception as e:
raise ProviderConnectionException(
'Authentication with Google cloud provider failed: %s', e)
Loading

0 comments on commit fe7d702

Please sign in to comment.