Create GCE Service outside Neutron service start path

Creating gce client inside Neutron service start causes neutron-server
crash if service_key is not there at mentioned path. Neutron service
should not crash at start if service key is not there.

Change-Id: I8e0f8b150c60d55d71846face217d5213b5df9b7
Closes-Bug: #1707872
This commit is contained in:
Sanket 2017-08-01 17:57:58 +05:30
parent 6c6d849778
commit 8d58fd2f16
4 changed files with 203 additions and 180 deletions

View File

@ -11,6 +11,7 @@ License for the specific language governing permissions and limitations
under the License.
"""
import os
import time
import uuid
@ -75,6 +76,10 @@ class GceResourceNotFound(e.NotFound):
message = _("GCE Resource %(name)s %(identifier)s was not found")
class GceServiceKeyNotFound(e.NotFound):
message = _("GCE service key was not found at %(path)s location")
def list_instances(compute, project, zone):
"""Returns list of GCE instance resources for specified project
@ -151,6 +156,9 @@ def get_gce_service(service_key):
:return: :class:`Resource <Resource>` object
:rtype: googleapiclient.discovery.Resource
"""
if not os.path.exists(service_key):
raise GceServiceKeyNotFound(path=service_key)
credentials = GoogleCredentials.from_stream(service_key)
service = build('compute', 'v1', credentials=credentials)
return service

View File

@ -48,13 +48,19 @@ class GceMechanismDriver(api.MechanismDriver):
self.gce_region = gceconf.region
self.gce_project = gceconf.project_id
self.gce_svc_key = gceconf.service_key_path
self._gce_svc = None
def initialize(self):
self.gce_svc = gceutils.get_gce_service(self.gce_svc_key)
LOG.info("GCE Mechanism driver init with %s project, %s region" %
(self.gce_project, self.gce_region))
self._subscribe_events()
@property
def gce_svc(self):
if self._gce_svc is None:
self._gce_svc = gceutils.get_gce_service(self.gce_svc_key)
return self._gce_svc
def _subscribe_events(self):
registry.subscribe(self.secgroup_callback, resources.SECURITY_GROUP,
events.BEFORE_DELETE)

View File

@ -75,10 +75,16 @@ class GceRouterPlugin(
self.gce_region = gceconf.region
self.gce_project = gceconf.project_id
self.gce_svc_key = gceconf.service_key_path
self.gce_svc = gceutils.get_gce_service(self.gce_svc_key)
self._gce_svc = None
LOG.info("GCE Router plugin init with %s project, %s region" %
(self.gce_project, self.gce_region))
@property
def gce_svc(self):
if self._gce_svc is None:
self._gce_svc = gceutils.get_gce_service(self.gce_svc_key)
return self._gce_svc
def get_plugin_type(self):
return plugin_type

View File

@ -35,10 +35,13 @@ else:
class GCENeutronTestCase(test_sg.SecurityGroupsTestCase, base.BaseTestCase):
@mock.patch('neutron.common.gceutils.get_gce_service')
def setUp(self, mock_service):
mock_service.side_effect = gce_mock.get_gce_service
def setUp(self):
super(GCENeutronTestCase, self).setUp()
self.service_patcher = mock.patch(
'neutron.common.gceutils.get_gce_service').start()
mock_service = self.service_patcher.start()
mock_service.side_effect = gce_mock.get_gce_service
self.addCleanup(self.service_patcher.stop)
self._driver = GceMechanismDriver()
self._driver.gce_zone = 'us-central1-c'
self._driver.gce_region = 'us-central1'