def test_refresh_from_db_add_and_delete(self): zm = zone_manager.ZoneManager() zone_state = zone_manager.ZoneState() zone_state.update_credentials( FakeZone(id=1, api_url='http://foo.com', username='******', password='******', name='child', weight_offset=2.0, weight_scale=3.0)) zm.zone_states[1] = zone_state self.mox.StubOutWithMock(db, 'zone_get_all') db.zone_get_all(mox.IgnoreArg()).AndReturn([ FakeZone(id=2, api_url='http://foo.com', username='******', password='******', name='child', weight_offset=2.0, weight_scale=3.0), ]) self.assertEquals(len(zm.zone_states), 1) self.mox.ReplayAll() zm._refresh_from_db(None) self.mox.VerifyAll() self.assertEquals(len(zm.zone_states), 1) self.assertEquals(zm.zone_states[2].username, 'user2')
def get_zone_list(context): """Return a list of zones associated with this zone.""" items = _call_scheduler('get_zone_list', context) for item in items: item['api_url'] = item['api_url'].replace('\\/', '/') if not items: items = db.zone_get_all(context.elevated()) return items
def get_zone_list(context): """Return a list of zones associated with this zone.""" items = _call_scheduler("get_zone_list", context) for item in items: item["api_url"] = item["api_url"].replace("\\/", "/") if not items: items = db.zone_get_all(context.elevated()) return items
def test_refresh_from_db_new(self): zm = zone_manager.ZoneManager() self.mox.StubOutWithMock(db, 'zone_get_all') db.zone_get_all(mox.IgnoreArg()).AndReturn([ FakeZone(id=1, api_url='http://foo.com', username='******', password='******', name='child', weight_offset=0.0, weight_scale=1.0), ]) self.assertEquals(len(zm.zone_states), 0) self.mox.ReplayAll() zm._refresh_from_db(None) self.mox.VerifyAll() self.assertEquals(len(zm.zone_states), 1) self.assertEquals(zm.zone_states[1].username, 'user1')
def test_refresh_from_db_add_and_delete(self): zm = zone_manager.ZoneManager() zone_state = zone_manager.ZoneState() zone_state.update_credentials(FakeZone(id=1, api_url='http://foo.com', username='******', password='******', name='child', weight_offset=2.0, weight_scale=3.0)) zm.zone_states[1] = zone_state self.mox.StubOutWithMock(db, 'zone_get_all') db.zone_get_all(mox.IgnoreArg()).AndReturn([ FakeZone(id=2, api_url='http://foo.com', username='******', password='******', name='child', weight_offset=2.0, weight_scale=3.0), ]) self.assertEquals(len(zm.zone_states), 1) self.mox.ReplayAll() zm._refresh_from_db(None) self.mox.VerifyAll() self.assertEquals(len(zm.zone_states), 1) self.assertEquals(zm.zone_states[2].username, 'user2')
def call_zone_method(context, method_name, errors_to_ignore=None, engineclient_collection_name='zones', zones=None, *args, **kwargs): """Returns a list of (zone, call_result) objects.""" if not isinstance(errors_to_ignore, (list, tuple)): # This will also handle the default None errors_to_ignore = [errors_to_ignore] pool = greenpool.GreenPool() results = [] if zones is None: zones = db.zone_get_all(context.elevated()) for zone in zones: try: # Do this on behalf of the user ... engine = engineclient.Client(zone.username, zone.password, None, zone.api_url, region_name=zone.name, token=context.auth_token) engine.authenticate() except engineclient_exceptions.BadRequest, e: url = zone.api_url name = zone.name LOG.warn( _("Authentication failed to zone " "'%(name)s' URL=%(url)s: %(e)s") % locals()) #TODO (dabo) - add logic for failure counts per zone, # with escalation after a given number of failures. continue engineclient_collection = getattr(engine, engineclient_collection_name) collection_method = getattr(engineclient_collection, method_name) def _error_trap(*args, **kwargs): try: return collection_method(*args, **kwargs) except Exception as e: if type(e) in errors_to_ignore: return None raise res = pool.spawn(_error_trap, *args, **kwargs) results.append((zone, res))
def _refresh_from_db(self, context): """Make our zone state map match the db.""" # Add/update existing zones ... zones = db.zone_get_all(context) existing = self.zone_states.keys() db_keys = [] for zone in zones: db_keys.append(zone.id) if zone.id not in existing: self.zone_states[zone.id] = ZoneState() self.zone_states[zone.id].update_credentials(zone) # Cleanup zones removed from db ... keys = self.zone_states.keys() # since we're deleting for zone_id in keys: if zone_id not in db_keys: del self.zone_states[zone_id]
def _route_to_child_zones(self, context, collection, item_uuid): if not FLAGS.enable_zone_routing: raise exception.InstanceNotFound(instance_id=item_uuid) self.item_uuid = item_uuid zones = db.zone_get_all(context) if not zones: raise exception.InstanceNotFound(instance_id=item_uuid) # Ask the children to provide an answer ... LOG.debug(_("Asking child zones ...")) result = self._call_child_zones( context, zones, wrap_engineclient_function(_issue_engineclient_command, collection, self.method_name, item_uuid)) # Scrub the results and raise another exception # so the API layers can bail out gracefully ... raise RedirectResult(self.unmarshall_result(result))
def _route_to_child_zones(self, context, collection, item_uuid): if not FLAGS.enable_zone_routing: raise exception.InstanceNotFound(instance_id=item_uuid) self.item_uuid = item_uuid zones = db.zone_get_all(context) if not zones: raise exception.InstanceNotFound(instance_id=item_uuid) # Ask the children to provide an answer ... LOG.debug(_("Asking child zones ...")) result = self._call_child_zones( context, zones, wrap_engineclient_function(_issue_engineclient_command, collection, self.method_name, item_uuid), ) # Scrub the results and raise another exception # so the API layers can bail out gracefully ... raise RedirectResult(self.unmarshall_result(result))
def call_zone_method( context, method_name, errors_to_ignore=None, engineclient_collection_name="zones", zones=None, *args, **kwargs ): """Returns a list of (zone, call_result) objects.""" if not isinstance(errors_to_ignore, (list, tuple)): # This will also handle the default None errors_to_ignore = [errors_to_ignore] pool = greenpool.GreenPool() results = [] if zones is None: zones = db.zone_get_all(context.elevated()) for zone in zones: try: # Do this on behalf of the user ... engine = engineclient.Client( zone.username, zone.password, None, zone.api_url, region_name=zone.name, token=context.auth_token ) engine.authenticate() except engineclient_exceptions.BadRequest, e: url = zone.api_url name = zone.name LOG.warn(_("Authentication failed to zone " "'%(name)s' URL=%(url)s: %(e)s") % locals()) # TODO (dabo) - add logic for failure counts per zone, # with escalation after a given number of failures. continue engineclient_collection = getattr(engine, engineclient_collection_name) collection_method = getattr(engineclient_collection, method_name) def _error_trap(*args, **kwargs): try: return collection_method(*args, **kwargs) except Exception as e: if type(e) in errors_to_ignore: return None raise res = pool.spawn(_error_trap, *args, **kwargs) results.append((zone, res))
def _zone_get_all(self, context): """Broken out for testing.""" return db.zone_get_all(context)