Skip to content

Commit

Permalink
Merge pull request #737 from grycap/devel
Browse files Browse the repository at this point in the history
Implements #736
  • Loading branch information
micafer authored Dec 14, 2018
2 parents 1eeaf31 + 8dcbc30 commit baf5224
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 50 deletions.
66 changes: 22 additions & 44 deletions IM/connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ def create_request(self, method, url, auth_data, headers, body=None):
else:
url = "%s://%s%s" % (self.cloud.protocol, self.cloud.server, url)

auths = auth_data.getAuthInfo(self.type, self.cloud.server)
if not auths:
raise Exception("No correct auth data has been specified to OCCI.")
if auth_data:
auths = auth_data.getAuthInfo(self.type, self.cloud.server)
if not auths:
raise Exception("No correct auth data has been specified to OCCI.")
else:
auth = auths[0]
else:
auth = auths[0]
auth = None

return self.create_request_static(method, url, auth, headers, self.verify_ssl, body)

Expand All @@ -118,10 +121,11 @@ def get_auth_header(self, auth_data):
auth = auths[0]

auth_header = None
keystone_uri = KeyStoneAuth.get_keystone_uri(self, auth_data)
keystone_uri, keystone_token = KeyStoneAuth.get_keystone_uri(self)

if keystone_uri:
# TODO: Check validity of token
if keystone_token:
auth_header = {'X-Auth-Token': keystone_token}
elif keystone_uri:
keystone_token = KeyStoneAuth.get_keystone_token(self, keystone_uri, auth)
auth_header = {'X-Auth-Token': keystone_token}
else:
Expand Down Expand Up @@ -881,7 +885,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):

# This error is returned is some sites if the network id is not specified
if resp.status_code == 409:
self.log_warn("Conflict creating the VM. Let's try to add the net id.")
self.log_warn("Conflict creating the VM. Let's try to add the net id: %s" % resp.text)

net_ids = []

Expand Down Expand Up @@ -1323,15 +1327,20 @@ class KeyStoneAuth:
"""

@staticmethod
def get_keystone_uri(occi, auth_data):
def get_keystone_uri(occi):
"""
Contact the OCCI server to check if it needs to contact a keystone server.
It returns the keystone server URI or None.
"""
try:
headers = {'Accept': 'text/plain', 'Connection': 'close'}

resp = occi.create_request('HEAD', occi.cloud.path + "/-/", auth_data, headers)
if occi.keystone_token:
headers = {'Accept': 'text/plain', 'X-Auth-Token': occi.keystone_token, 'Connection': 'close'}

resp = occi.create_request('HEAD', occi.cloud.path + "/-/", None, headers)
if resp.status_code == 200:
return None, occi.keystone_token

www_auth_head = None
if 'Www-Authenticate' in resp.headers:
Expand All @@ -1342,44 +1351,17 @@ def get_keystone_uri(occi, auth_data):
# remove version in some old OpenStack sites
if keystone_uri.endswith("/v2.0"):
keystone_uri = keystone_uri[:-5]
return keystone_uri
return keystone_uri, None
else:
return None
return None, None
except SSLError as ex:
occi.log_exception(
"Error with the credentials when contacting with the OCCI server.")
raise Exception(
"Error with the credentials when contacting with the OCCI server: %s. Check your proxy file." % str(ex))
except:
occi.log_exception("Error contacting with the OCCI server.")
return None

@staticmethod
def check_keystone_token(occi, keystone_uri, version, auth):
"""
Check if old keystone token is stil valid
"""
if occi.keystone_token:
try:
headers = {'Accept': 'application/json', 'Content-Type': 'application/json',
'X-Auth-Token': occi.keystone_token, 'Connection': 'close'}
if version == 2:
url = "%s/v2.0/tenants" % keystone_uri
elif version == 3:
url = "%s/v3/auth/tokens" % keystone_uri
else:
return None
resp = occi.create_request_static('GET', url, auth, headers, occi.verify_ssl)
if resp.status_code == 200:
return occi.keystone_token
else:
occi.log_warn("Keystone token invalid.")
return None
except Exception:
occi.log_exception("Error checking Keystone token")
return None
else:
return None
return None, None

@staticmethod
def get_keystone_token(occi, keystone_uri, auth):
Expand All @@ -1388,10 +1370,6 @@ def get_keystone_token(occi, keystone_uri, auth):
"""
version = KeyStoneAuth.get_keystone_version(occi, keystone_uri, auth)

token = KeyStoneAuth.check_keystone_token(occi, keystone_uri, version, auth)
if token:
return token

if version == 2:
occi.log_info("Getting Keystone v2 token")
occi.keystone_token = KeyStoneAuth.get_keystone_token_v2(occi, keystone_uri, auth)
Expand Down
12 changes: 6 additions & 6 deletions test/unit/connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def test_20_launch(self, save_data, get_keystone_uri, requests):
occi_cloud = self.get_occi_cloud()

requests.side_effect = self.get_response
get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

inf = InfrastructureInfo()
inf.auth = auth
Expand Down Expand Up @@ -304,7 +304,7 @@ def test_30_updateVMInfo(self, get_keystone_uri, requests):

requests.side_effect = self.get_response

get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

success, vm = occi_cloud.updateVMInfo(vm, auth)

Expand All @@ -322,7 +322,7 @@ def test_40_stop(self, get_keystone_uri, requests):

requests.side_effect = self.get_response

get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

success, _ = occi_cloud.stop(vm, auth)

Expand All @@ -340,7 +340,7 @@ def test_50_start(self, get_keystone_uri, requests):

requests.side_effect = self.get_response

get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

success, _ = occi_cloud.start(vm, auth)

Expand Down Expand Up @@ -385,7 +385,7 @@ def test_55_alter(self, get_keystone_uri, requests):

requests.side_effect = self.get_response

get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

success, _ = occi_cloud.alterVM(vm, new_radl, auth)

Expand Down Expand Up @@ -438,7 +438,7 @@ def test_60_finalize(self, get_keystone_uri, requests):

requests.side_effect = self.get_response

get_keystone_uri.return_value = None
get_keystone_uri.return_value = None, None

success, _ = occi_cloud.finalize(vm, True, auth)

Expand Down

0 comments on commit baf5224

Please sign in to comment.