Skip to content

Commit

Permalink
Fix #467
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Oct 26, 2017
1 parent 6d16ead commit 487e2e9
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions IM/connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ def create_request_static(method, url, auth, headers, body=None):

def create_request(self, method, url, auth_data, headers, body=None):
if not url.startswith("http://") and not url.startswith("https://"):
url = "%s://%s:%d%s" % (self.cloud.protocol, self.cloud.server, self.cloud.port, url)
if self.cloud.port > 0:
url = "%s://%s:%d%s" % (self.cloud.protocol, self.cloud.server, self.cloud.port, url)
else:
url = "%s://%s%s" % (self.cloud.protocol, self.cloud.server, url)

auths = auth_data.getAuthInfo(self.type, self.cloud.server)
if not auths:
Expand Down Expand Up @@ -142,7 +145,9 @@ def concreteSystem(self, radl_system, auth_data):
for str_url in image_urls:
url = uriparse(str_url)
protocol = url[0]
cloud_url = self.cloud.protocol + "://" + self.cloud.server + ":" + str(self.cloud.port)
cloud_url = self.cloud.protocol + "://" + self.cloud.server
if self.cloud.port > 0:
cloud_url += ":" + str(self.cloud.port)
site_url = None
if protocol == "appdb":
# The url has this format: appdb://UPV-GRyCAP/egi.docker.ubuntu.16.04?fedcloud.egi.eu
Expand Down Expand Up @@ -220,20 +225,21 @@ def get_net_info(self, occi_res):
lines = occi_res.split("\n")
res = []
link_to_public = False
num_interface = None
ip_address = None
link = None
for l in lines:
if 'Link:' in l and '/network/public' in l:
link_to_public = True
if 'Link:' in l and ('/network/' in l or '/networklink/' in l):
num_interface = None
ip_address = None
link = None
parts = l.split(';')
for part in parts:
kv = part.split('=')
if kv[0].strip() == "occi.networkinterface.address":
ip_address = kv[1].strip('"')
is_private = any([IPAddress(ip_address) in IPNetwork(
mask) for mask in Config.PRIVATE_NET_MASKS])
if kv[1].count('.') == 3 or ip_address is None:
ip_address = kv[1].strip('"')
is_private = any([IPAddress(ip_address) in IPNetwork(
mask) for mask in Config.PRIVATE_NET_MASKS])
elif kv[0].strip() == "occi.networkinterface.interface":
net_interface = kv[1].strip('"')
num_interface = re.findall('\d+', net_interface)[0]
Expand Down Expand Up @@ -688,6 +694,7 @@ def detach_volume(self, volume, auth_data, timeout=180, delay=5):
self.log_debug("Detaching volume: %s" % storage_id)
resp = self.create_request('GET', link, auth_data, headers)
if resp.status_code == 200:
self.log_debug("Volume link %s exists. Try to delete it." % link)
resp = self.create_request('DELETE', link, auth_data, headers)
if resp.status_code in [204, 200]:
self.log_debug("Successfully detached. Wait it to be deleted.")
Expand All @@ -697,6 +704,8 @@ def detach_volume(self, volume, auth_data, timeout=180, delay=5):
# wait until the resource does not exist
self.log_debug("Successfully detached")
return (True, "")
else:
self.log_warn("Error detaching volume: %s" + resp.reason + "\n" + resp.text)
except Exception as ex:
self.log_warn("Error detaching volume " + str(ex))

Expand Down Expand Up @@ -725,19 +734,27 @@ def delete_volume(self, storage_id, auth_data, timeout=180, delay=5):
while wait < timeout:
self.log_debug("Delete storage: %s" % storage_id)
try:
resp = self.create_request('DELETE', storage_id, auth_data, headers)

if resp.status_code == 404:
resp = self.create_request('GET', storage_id, auth_data, headers)
if resp.status_code == 200:
self.log_debug("Storage %s exists. Try to delete it." % storage_id)
resp = self.create_request('DELETE', storage_id, auth_data, headers)

if resp.status_code == 404:
self.log_debug("It does not exist.")
return (True, "")
elif resp.status_code == 409:
self.log_debug("Error deleting the Volume. It seems that it is still "
"attached to a VM: %s" % resp.text)
elif resp.status_code != 200 and resp.status_code != 204:
self.log_warn("Error deleting the Volume: " + resp.reason + "\n" + resp.text)
else:
self.log_debug("Successfully deleted")
return (True, "")
elif resp.status_code == 404:
self.log_debug("It does not exist.")
return (True, "")
elif resp.status_code == 409:
self.log_debug("Error deleting the Volume. It seems that it is still "
"attached to a VM: %s" % resp.text)
elif resp.status_code != 200 and resp.status_code != 204:
self.log_warn("Error deleting the Volume: " + resp.reason + "\n" + resp.text)
else:
self.log_debug("Successfully deleted")
return (True, "")
self.log_warn("Error deleting storage: %s" + resp.reason + "\n" + resp.text)
time.sleep(delay)
wait += delay
except Exception:
Expand Down Expand Up @@ -1422,7 +1439,7 @@ def get_keystone_token_v2(occi, keystone_uri, auth):
'X-Auth-Token': token_id, 'Connection': 'close'}
url = "%s/v2.0/tokens" % keystone_uri
resp = occi.create_request_static('POST', url, auth, headers, body)
if resp.status_code == 200:
if resp.status_code in [200, 202]:
# format: -> "{\"access\": {\"token\": {\"issued_at\":
# \"2014-12-29T17:10:49.609894\", \"expires\":
# \"2014-12-30T17:10:49Z\", \"id\":
Expand Down

0 comments on commit 487e2e9

Please sign in to comment.