| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- """
- Provider implementation based on google-api-python-client library
- for GCE.
- """
- from cloudbridge.cloud.base import BaseCloudProvider
- import httplib2
- import json
- import os
- import time
- from googleapiclient import discovery
- import googleapiclient.http
- from oauth2client.client import GoogleCredentials
- from oauth2client.service_account import ServiceAccountCredentials
- from .services import GCEComputeService
- from .services import GCESecurityService
- class GCECloudProvider(BaseCloudProvider):
- PROVIDER_ID = 'gce'
- def __init__(self, config):
- super(GCECloudProvider, self).__init__(config)
- # Initialize cloud connection fields
- self.client_email = self._get_config_value(
- 'gce_client_email', os.environ.get('GCE_CLIENT_EMAIL'))
- self.project_name = self._get_config_value(
- 'gce_project_name', os.environ.get('GCE_PROJECT_NAME'))
- self.credentials_file = self._get_config_value(
- 'gce_service_creds_file', os.environ.get('GCE_SERVICE_CREDS_FILE'))
- self.credentials_dict = self._get_config_value(
- 'gce_service_creds_dict', {})
- # If 'gce_service_creds_dict' is not passed in from config and
- # self.credentials_file is available, read and parse the json file to
- # self.credentials_dict.
- if self.credentials_file and not self.credentials_dict:
- with open(self.credentials_file) as creds_file:
- self.credentials_dict = json.load(creds_file)
- self.default_zone = self._get_config_value(
- 'gce_default_zone', os.environ.get('GCE_DEFAULT_ZONE'))
- self.region_name = self._get_config_value(
- 'gce_region_name', 'us-central1')
- # oauth2client.Credentials to be used for authentication
- self._credentials = None
- # service connections, lazily initialized
- self._gce_compute = None
- # Initialize provider services
- self._compute = GCEComputeService(self)
- self._security = GCESecurityService(self)
- @property
- def compute(self):
- return self._compute
- @property
- def network(self):
- raise NotImplementedError(
- "GCECloudProvider does not implement this service")
- @property
- def security(self):
- return self._security
- @property
- def block_store(self):
- raise NotImplementedError(
- "GCECloudProvider does not implement this service")
- @property
- def object_store(self):
- raise NotImplementedError(
- "GCECloudProvider does not implement this service")
- @property
- def gce_compute(self):
- if not self._gce_compute:
- self._gce_compute = self._connect_gce_compute()
- return self._gce_compute
- def _connect_gce_compute(self):
- if self.credentials_dict:
- credentials = ServiceAccountCredentials.from_json_keyfile_dict(
- self.credentials_dict)
- else:
- credentials = GoogleCredentials.get_application_default()
- self._credentials = credentials
- return discovery.build('compute', 'v1', credentials=credentials)
- def get_gce_resource_data(self, uri):
- """
- Retrieves GCE resoure data given its resource URI.
- """
- http = httplib2.Http()
- http = self._credentials.authorize(http)
- def _postproc(*kwargs):
- if len(kwargs) >= 2:
- # The first argument is request, and the second is response.
- resource_dict = json.loads(kwargs[1])
- return resource_dict
- request = googleapiclient.http.HttpRequest(http=http,
- postproc=_postproc,
- uri=uri)
- # The response is a dict representing the GCE resource data.
- response = request.execute()
- return response
- def wait_for_global_operation(self, operation):
- while True:
- result = self.gce_compute.globalOperations().get(
- project=self.project_name,
- operation=operation['name']).execute()
- if result['status'] == 'DONE':
- if 'error' in result:
- raise Exception(result['error'])
- return result
- time.sleep(0.5)
|