Merge pull request #16 from hpcloud-mon/feature/fix-url

Fixing Keystone URL to be base URL only and some cleanup of the Keystone code.
This commit is contained in:
Gary Hessler 2014-06-26 15:28:29 -06:00
commit bacee52c9c
3 changed files with 32 additions and 31 deletions

View File

@ -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'])

View File

@ -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

View File

@ -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)