Commit 1fd4d67b authored by Albert's avatar Albert
Browse files

integration fitcloud api

parent 79aa457d
Pipeline #2993 failed with stage
in 36 seconds
......@@ -150,7 +150,6 @@ class Api(object):
if sa_endpoint:
self.ams.append(FitCloud(endpoint, self.sa))
def __getattr__(self, entity):
def method_handler():
if not entity in self._entities:
......@@ -342,6 +341,25 @@ class Api(object):
return result
def execute(self, id, action,params):
exists = self.get(id)
if not isinstance(params, dict):
raise MysParamsTypeError('a dict is expected')
threads = []
result = {}
if not exists:
raise Exception('This object do not exist')
if self._entity in self._am:
for am in self.ams:
threads += [self._thread_handler(am.execute, id, action, params)]
result = self._parallel_request(threads)
if self._entity not in self._am:
raise NotImplementedError('Not implemented')
return result
def merge_dicts(res):
result = {}
......
......@@ -78,7 +78,8 @@ class FitCloud(object):
'slice_urn': urn,
'geni_rspec_version' : {'type': 'GENI', 'version': '3'}
}
result = requests.post(self.server + "/api/fitcloud/describe/",
result = requests.post(self.server + "/api/fitcloud/describe",
headers={str('Content-Type'): 'application/json'},
data=json.dumps(payload))
return [self.parse_slice_data(self.parse_data(result))]
......@@ -138,6 +139,7 @@ class FitCloud(object):
if "resources" in lease:
lease["resources_data"] = lease["resources"]
lease["resources"] = []
lease["parser"] = "fitcloud"
res["leases"] = data["leases"]
if "resources" in data:
......@@ -154,6 +156,7 @@ class FitCloud(object):
if "resources" in lease:
lease["resources_data"] = lease["resources"]
lease["resources"] = []
lease["parser"] = "fitcloud"
return data["leases"]
return []
......@@ -307,14 +310,14 @@ class FitCloud(object):
def renew(self, entity, id, data):
result = []
try:
# Delete only possible for 'slice' entity
# Renew only possible for 'slice' entity
if entity != 'slice':
raise NotImplementedError('Not implemented')
# This will delete the slice on the cloud side and also remove all the leases
# This will renew the slice on the cloud side and also renew all the leases
if not id:
raise Exception('id required for slice update')
raise Exception('id required for slice renewal')
## Payload Format:
......@@ -504,10 +507,10 @@ class FitCloud(object):
def check_allocate_event(self, event):
timeout = self.endpoint.timeout
max_retries = timeout / 10
max_retries = timeout / 5
while max_retries > 0:
res = requests.get(self.server + "/api/fitcloud/allocate/" + event)
result = requests.get(self.server + "/api/fitcloud/allocate/" + event)
try:
result = result.json()
if self.isResultOk(result):
......@@ -517,13 +520,17 @@ class FitCloud(object):
elif 'code' in result and \
'geni_code' in result['code'] and \
result['code']['geni_code']==16:
max_retries -= 1
time.sleep(5)
continue
else:
raise SfaError(result)
except SfaError as e:
raise e
except Exception as e:
max_retries -= 1
time.sleep(10)
time.sleep(5)
continue
return result
......@@ -538,9 +545,10 @@ class FitCloud(object):
final_status = ['SUCCESS', 'WARNING', 'ERROR']
while max_retries > 0:
res = requests.get(self.server + "/api/fitcloud/"+ object_type + "/" + event)
result = requests.get(self.server + "/api/fitcloud/"+ object_type + "/" + event)
try:
result = result.json()
logger.info(result)
if self.isResultOk(result):
result = result['value']
# code 16 == in progress
......@@ -548,9 +556,14 @@ class FitCloud(object):
elif 'code' in result and \
'geni_code' in result['code'] and \
result['code']['geni_code']==16:
max_retries -= 1
time.sleep(10)
continue
else:
raise SfaError(result)
except SfaError as e:
logger.exception(e)
raise
except Exception as e:
max_retries -= 1
......@@ -563,24 +576,65 @@ class FitCloud(object):
def execute(self, id, action, obj_type):
def execute(self, id, action, options={}):
result = []
try:
if not id:
raise Exception('id required for execute')
payload = dict()
payload['slice_id'] = id
#payload["target"]
credentials = self.registry.get_credential(id, raw=True, credentials=[self.registry.user_credential])
payload["credentials"] = credentials
if action.lower() == 'shutdown':
r1 = requests.post(self.server + "/api/fitcloud/shutdown",
headers={str('Content-Type'): 'application/json'},
data=json.dumps(payload))
if r1.status_code != 200:
raise Exception('Request Error : status not 200')
# Check if shutdown call is approved.
# shutdown only apply to whole slice
result = self.check_response(r1.json()['events'][0], "shutdown")
elif action.lower() == 'renew':
if "leases" in options:
payload["leases"] = options["leases"]
if "duration" in options:
payload["duration"] = options["duration"]
r1 = requests.post(self.server + "/api/fitcloud/renew",
headers={str('Content-Type'): 'application/json'},
data=json.dumps(payload))
if r1.status_code != 200:
raise Exception('Request Error : status not 200')
# Check if renew call is approved.
result = self.check_response(r1.json()['events'][0], "renew")
else:
payload["action"] = action
r1 = requests.post(self.server + "/api/fitcloud/performoperationalactions",
if "leases" in options:
payload["sliver_urn"] = options["leases"]
if "resources" in options:
payload["resources"] = options["resources"]
r1 = requests.post(self.server + "/api/fitcloud/perform_operational_action",
headers={str('Content-Type'): 'application/json'},
data=json.dumps(payload))
if r1.status_code != 200:
raise Exception('Request Error : status not 200')
# Check if perform_operational_action call is approved.
result = self.check_response(r1.json()['events'][0], "perform_operational_action")
except Exception as e:
logger.error(self.endpoint.name)
logger.exception(e)
......
......@@ -251,7 +251,7 @@ class SfaAm(SfaApi):
})
return {'data':result,'errors':self.logs}
def execute(self, urn, action, obj_type):
def execute(self, urn, action, options={}):
result = []
try:
if action.lower() == 'shutdown':
......
......@@ -269,6 +269,16 @@ class Entity(object):
'errors': res.get('errors', []),
}
def execute(self, setup=None, action=None, options=None):
if not self.id:
raise Exception("No element specified")
res = self._api(setup).execute(self.id, action, options)
return {
'data': res.get('data', []),
'errors': res.get('errors', []),
}
def delete(self, setup=None):
if not self.id:
......
......@@ -103,7 +103,7 @@ class Slice(Entity):
self.appendAttribute('leases', lease.getAttributes())
self.appendAttribute('addedLeases', lease.getAttributes())
#self.addResources(list(set(lease.getAttribute('resources')) - set(self.getAttribute('resources'))))
if lease.hasAttribute('testbed') and lease.getAttribute('testbed') not in self.getAttribute('testbeds'):
if lease.hasAttribute('testbed') and lease.getAttribute('testbed') and lease.getAttribute('testbed') not in self.getAttribute('testbeds'):
self.appendAttribute('testbeds', lease.getAttribute('testbed'))
self.setAttribute('run_am', True)
return self
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment