diff --git a/monagent/collector/checks_d/http_check.py b/monagent/collector/checks_d/http_check.py index 9ab19cb5..9e210d7d 100644 --- a/monagent/collector/checks_d/http_check.py +++ b/monagent/collector/checks_d/http_check.py @@ -37,7 +37,7 @@ class HTTPCheck(ServicesCheck): config = get_config() api_config = config['Api'] if use_keystone: - keystone = Keystone(api_config['keystone_url'] + '/tokens', + keystone = Keystone(api_config['keystone_url'], api_config['username'], api_config['password'], api_config['project_name']) diff --git a/monagent/common/keystone.py b/monagent/common/keystone.py index ef62d86c..ff9a168c 100644 --- a/monagent/common/keystone.py +++ b/monagent/common/keystone.py @@ -57,7 +57,7 @@ class Keystone(object): self.password_auth['auth']['scope']['project']['name'] = self.project_name data = json.dumps(self.password_auth) headers = {'Content-Type': 'application/json'} - response = requests.post(self.endpoint, data=data, headers=headers) + response = requests.post(self.endpoint.rstrip('/') + '/auth/tokens', data=data, headers=headers) response.raise_for_status() self.token = response.headers['X-Subject-Token'] return self.token diff --git a/monagent/forwarder/api/__init__.py b/monagent/forwarder/api/__init__.py index e8a7beef..a6d06b68 100644 --- a/monagent/forwarder/api/__init__.py +++ b/monagent/forwarder/api/__init__.py @@ -28,29 +28,17 @@ class MonAPI(object): if not 'hostname' in self.default_dimensions: self.default_dimensions['hostname'] = get_hostname() - try: - log.debug("Getting token from Keystone") - self.keystone_url = config['keystone_url'] + '/tokens' - self.username = config['username'] - self.password = config['password'] - self.project_name = config['project_name'] - - self.keystone = Keystone(self.keystone_url, - self.username, - self.password, - self.project_name) - self.token = self.keystone.get_token() - - except Exception as ex: - log.error("Error getting token from Keystone: {0}". - format(str(ex.message))) - raise ex - - # construct the mon client - self.kwargs = { - 'token': self.token - } - self.mon_client = client.Client(self.api_version, self.url, **self.kwargs) + log.debug("Getting token from Keystone") + self.keystone_url = config['keystone_url'] + self.username = config['username'] + self.password = config['password'] + self.project_name = config['project_name'] + + self.keystone = Keystone(self.keystone_url, + self.username, + self.password, + self.project_name) + self.mon_client = None def _post(self, measurements): """Does the actual http post @@ -61,6 +49,10 @@ class MonAPI(object): 'jsonbody': data } try: + if not self.mon_client: + # construct the mon client + self.mon_client = self.get_client() + done = False while not done: response = self.mon_client.metrics.create(**kwargs) @@ -72,12 +64,8 @@ class MonAPI(object): # Good status from web service but some type of issue # with the data if response.status_code == 401: - # Get a new token and retry - self.token = self.keystone.refresh_token() - # Re-create the client. This is temporary until - # the client is updated to be able to reset the - # token. - self.mon_client = client.Client(self.api_version, self.url, **self.kwargs) + # Get a new token/client and retry + self.mon_client = self.get_client() continue else: error_msg = "Successful web service call but there" + \ @@ -105,3 +93,16 @@ class MonAPI(object): measurement.dimensions.update({dimension: self.default_dimensions[dimension]}) self._post(measurements) + + def get_client(self): + """get_client + get a new mon-client object + """ + token = self.keystone.refresh_token() + # Re-create the client. This is temporary until + # the client is updated to be able to reset the + # token. + kwargs = { + 'token': token + } + return client.Client(self.api_version, self.url, **kwargs)