X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sla%2Fslaclient%2Frestclient.py;h=4b8614bed88730e66f8ea92754ebbbc95c91bd2e;hb=26b2d32f6132a3e619f47411adeccfa693b71162;hp=8091f4eeaeba9145af646b400e502d7d413007d0;hpb=36a55939d5f370f1ff2b6021796e31edad21d1ff;p=myslice.git diff --git a/sla/slaclient/restclient.py b/sla/slaclient/restclient.py index 8091f4ee..4b8614be 100755 --- a/sla/slaclient/restclient.py +++ b/sla/slaclient/restclient.py @@ -3,6 +3,7 @@ import requests from requests.auth import HTTPBasicAuth +from myslice.settings import logger import xmlconverter import wsag_model @@ -38,17 +39,17 @@ _TEMPLATES_PATH = "templates" _VIOLATIONS_PATH = "violations" _ENFORCEMENTJOBS_PATH = "enforcements" -rooturl = settings.SLA_MANAGER_URL +rooturl = settings.SLA_COLLECTOR_URL class Factory(object): @staticmethod - def agreements(path=_AGREEMENTS_PATH): + def agreements(): """Returns a REST client for Agreements :rtype : Agreements """ - return Agreements(rooturl, path) + return Agreements(rooturl) @staticmethod def providers(): @@ -111,23 +112,25 @@ class Client(object): c = Client("http://localhost:8080/service") c.get("/resource", headers = { "accept": "application/json" }) """ - url = _buildpath_(self.rooturl, path) + url = _buildpath(self.rooturl, path) if "testbed" in kwargs: url = url + "?testbed=" + kwargs["testbed"] if "headers" not in kwargs: kwargs["headers"] = {"accept": "application/xml"} - kwargs["auth"] = HTTPBasicAuth(settings.SLA_MANAGER_USER, - settings.SLA_MANAGER_PASSWORD) + + kwargs["auth"] = HTTPBasicAuth(settings.SLA_COLLECTOR_USER, + settings.SLA_COLLECTOR_PASSWORD) # for key, values in kwargs.iteritems(): # print key, values - result = requests.get(url, **kwargs) - print "GET {} {} {}".format( - result.url, result.status_code, result.text[0:70]) - print result.encoding + result = requests.get(url, verify=False, **kwargs) + logger.debug('SLA GET {} - result: {}'.format(result.url, result.status_code)) + # print "GET {} {} {}".format( + # result.url, result.status_code, result.text[0:70]) + # print result.encoding return result @@ -150,22 +153,23 @@ class Client(object): } ) """ - url = _buildpath_(self.rooturl, path) + url = _buildpath(self.rooturl, path) if "testbed" in kwargs: url = url + "?testbed=" + kwargs["testbed"] + del kwargs["testbed"] if "headers" not in kwargs: kwargs["headers"] = {"accept": "application/xml", "content-type": "application/xml"} - kwargs["auth"] = HTTPBasicAuth(settings.SLA_MANAGER_USER, - settings.SLA_MANAGER_PASSWORD) + kwargs["auth"] = HTTPBasicAuth(settings.SLA_COLLECTOR_USER, + settings.SLA_COLLECTOR_PASSWORD) result = requests.post(url, data, **kwargs) location = result.headers["location"] \ if "location" in result.headers else "" - print "POST {} {} Location: {}".format( + print "POST {} {} - Location: {}".format( result.url, result.status_code, location) return result @@ -228,13 +232,11 @@ class _Resource(object): resource = _Resource._processresult(r, self.converter) return resource, r - def get(self, path, params): + def get(self, path="", params={}): """Generic query over resource: GET /resource?q1=v1&q2=v2... :param dict[str,str] params: values to pass as get parameters """ - if path is None: - path = "" r = self.client.get(path, params=params) resources = self._processresult(r, self.listconverter) @@ -262,9 +264,9 @@ class Agreements(object): The final url to the resource is root_url + "/" + path """ - resourceurl = _buildpath_(root_url, path) - converter = xmlconverter.AgreementConverter() - self.res = _Resource(resourceurl, converter) + self.resourceurl = _buildpath(root_url, path) + self.converter = xmlconverter.AgreementConverter() + self.res = _Resource(self.resourceurl, self.converter) def getall(self): """ @@ -301,12 +303,12 @@ class Agreements(object): :param str agreementid : :rtype : wsag_model.AgreementStatus """ - path = _buildpath_(_AGREEMENTS_PATH, agreementid, "guaranteestatus") + # path = _buildpath(_AGREEMENTS_PATH, agreementid, "guaranteestatus") + path = _buildpath(agreementid, "guaranteestatus") r = self.res.client.get(path, headers={'accept': 'application/json'}, params={'testbed': testbed}) json_obj = r.json() - status = wsag_model.AgreementStatus.json_decode(json_obj) return status, r @@ -316,7 +318,9 @@ class Agreements(object): :rtype : list[wsag_model.Agreement] """ - return self.res.get(slicename, dict()) + self.resourceurl = _buildpath(rooturl, 'slice') + self.res = _Resource(self.resourceurl, self.converter) + return self.res.get(slicename) def create(self, agreement, testbed): """Create a new agreement @@ -335,16 +339,16 @@ class Templates(object): The final url to the resource is root_url + "/" + path """ - resourceurl = _buildpath_(root_url, path) + resourceurl = _buildpath(root_url, path) converter = xmlconverter.AgreementConverter() self.res = _Resource(resourceurl, converter) - def getall(self): + def getall(self, provider_id): """ Get all templates :rtype : list[wsag_model.Template] """ - return self.res.getall() + return self.res.get(path="", params={"testbed": provider_id}) def getbyid(self, provider_id): """Get a template @@ -370,7 +374,7 @@ class Providers(object): The final url to the resource is root_url + "/" + path """ - resourceurl = _buildpath_(root_url, path) + resourceurl = _buildpath(root_url, path) converter = xmlconverter.ProviderConverter() self.res = _Resource(resourceurl, converter) @@ -406,7 +410,7 @@ class Violations(object): The final url to the resource is root_url + "/" + path """ - resourceurl = _buildpath_(root_url, path) + resourceurl = _buildpath(root_url, path) converter = xmlconverter.ViolationConverter() self.res = _Resource(resourceurl, converter) @@ -445,7 +449,7 @@ class Enforcements(object): The final url to the resource is root_url + "/" + path """ - resourceurl = _buildpath_(root_url, path) + resourceurl = _buildpath(root_url, path) converter = xmlconverter.EnforcementConverter() self.res = _Resource(resourceurl, converter) @@ -465,7 +469,7 @@ class Enforcements(object): return self.res.getbyid(agreement_id, params={"testbed": testbed}) -def _buildpath_(*paths): +def _buildpath(*paths): if "" in paths: paths = [path for path in paths if path != ""]