def target_wrapper(*args, **kwargs): try: return method(*args, **kwargs) except TargetdError as te: raise LsmError(ErrorNumber.PLUGIN_BUG, "Got error %d from targetd: %s" % (te.errno, te.reason)) except LsmError: raise except Exception as e: common_urllib2_error_handler(e)
def target_wrapper(*args, **kwargs): try: return method(*args, **kwargs) except TargetdError as te: raise LsmError( ErrorNumber.PLUGIN_BUG, "Got error %d from targetd: %s" % (te.errno, te.reason)) except LsmError: raise except Exception as e: common_urllib2_error_handler(e)
def _ns_request(self, path, data): response = None parms = json.dumps(data) url = '%s://%s:%s/%s' % \ (self._scheme, self.uparse.hostname, self._port, path) request = Request(url, parms.encode('utf-8')) username = self.uparse.username or 'admin' user_name_pass = '******' % (username, self.password) auth = base64.b64encode(user_name_pass.encode('utf-8')).decode('utf-8') request.add_header('Authorization', 'Basic %s' % auth) request.add_header('Content-Type', 'application/json') try: response = urlopen(request, timeout=int_div(self.timeout, 1000)) except Exception as e: try: common_urllib2_error_handler(e) except LsmError as lsm_e: exc_info = sys.exc_info() if lsm_e.code == ErrorNumber.NETWORK_CONNREFUSED: if not self.uparse.port and \ self._port == NexentaStor._V3_PORT: self._port = NexentaStor._V4_PORT return self._ns_request(path, data) six.reraise(*exc_info) resp_json = response.read().decode('utf-8') resp = json.loads(resp_json) if resp['error']: if 'message' in resp['error']: msg = resp['error']['message'] # Check to see if there is a better way to do this... if 'dataset already exists' in msg: raise LsmError(ErrorNumber.NAME_CONFLICT, msg) if 'Unable to destroy hostgroup' in msg: raise LsmError(ErrorNumber.IS_MASKED, msg) raise LsmError(ErrorNumber.PLUGIN_BUG, resp['error']) return resp['result']