1 # -*- coding: utf-8 -*-
5 from requests.auth import HTTPBasicAuth
6 from myslice.settings import logger
11 from django.conf import settings
14 """REST client to SLA Manager.
16 Contains a generic rest client and wrappers over this generic client
19 Each resource client implements business-like() functions, but
20 returns a tuple (output, requests.Response)
22 The resource clients are initialized with the rooturl and a path, which
23 are combined to build the resource url. The path is defaulted to the known
24 resource path. So, for example, to create a agreements client:
26 c = Agreements("http://localhost/slagui-service")
28 A Factory facility is provided to create resource client instances. The
29 Factory uses "rooturl" module variable to use as rooturl parameter.
31 restclient.rooturl = "http://localhost/slagui-service"
32 c = restclient.Factory.agreements()
36 _PROVIDERS_PATH = "providers"
37 _AGREEMENTS_PATH = "agreements"
38 _TEMPLATES_PATH = "templates"
39 _VIOLATIONS_PATH = "violations"
40 _ENFORCEMENTJOBS_PATH = "enforcements"
42 rooturl = settings.SLA_COLLECTOR_URL
45 class Factory(object):
48 """Returns a REST client for Agreements
52 return Agreements(rooturl)
56 """Returns a REST client for Providers
60 return Providers(rooturl)
64 """Returns a REST client for Violations
68 return Violations(rooturl)
72 """Returns a REST client for Violations
76 return Templates(rooturl)
80 """Returns a REST client for Enforcements jobs
84 return Enforcements(rooturl)
89 def __init__(self, root_url):
91 """Generic rest client using requests library
93 Each operation mimics the corresponding "requests" operation (arguments
96 :param str root_url: this url is used as prefix in all subsequent
99 self.rooturl = root_url
101 def get(self, path, **kwargs):
102 """Just a wrapper over request.get, just in case.
104 Returns a requests.Response
106 :rtype : request.Response
107 :param str path: remaining path from root url;
108 empty if desired path equal to rooturl.
109 :param kwargs: arguments to requests.get
112 c = Client("http://localhost:8080/service")
113 c.get("/resource", headers = { "accept": "application/json" })
115 url = _buildpath(self.rooturl, path)
116 if "testbed" in kwargs:
117 url = url + "?testbed=" + kwargs["testbed"]
119 if "headers" not in kwargs:
120 kwargs["headers"] = {"accept": "application/xml"}
123 kwargs["auth"] = HTTPBasicAuth(settings.SLA_COLLECTOR_USER,
124 settings.SLA_COLLECTOR_PASSWORD)
126 # for key, values in kwargs.iteritems():
129 result = requests.get(url, **kwargs)
130 logger.debug('SLA GET {} - result: {}'.format(result.url, result.status_code))
131 # print "GET {} {} {}".format(
132 # result.url, result.status_code, result.text[0:70])
133 # print result.encoding
137 def post(self, path, data=None, **kwargs):
138 """Just a wrapper over request.post, just in case
140 :rtype : request.Response
141 :param str path: remaining path from root url;
142 empty if desired path equal to rooturl.
143 :param dict[str, str] kwargs: arguments to requests.post
146 c = Client("http://localhost:8080/service")
149 '{ "id": "1", "name": "provider-a" }',
151 "content-type": "application/json",
152 "accept": "application/xml"
156 url = _buildpath(self.rooturl, path)
158 if "testbed" in kwargs:
159 url = url + "?testbed=" + kwargs["testbed"]
160 del kwargs["testbed"]
162 if "headers" not in kwargs:
163 kwargs["headers"] = {"accept": "application/xml",
164 "content-type": "application/xml"}
166 kwargs["auth"] = HTTPBasicAuth(settings.SLA_COLLECTOR_USER,
167 settings.SLA_COLLECTOR_PASSWORD)
169 result = requests.post(url, data, **kwargs)
170 location = result.headers["location"] \
171 if "location" in result.headers else "<null>"
172 print "POST {} {} - Location: {}".format(
173 result.url, result.status_code, location)
177 class _Resource(object):
179 def __init__(self, url, converter):
180 """Provides some common operations over resources.
182 The operations return a structured representation of the resource.
184 :param str url: url to the resource
185 :param Converter converter: resouce xml converter
187 Some attributes are initialized to be used from the owner if needed:
188 * client: Client instance
189 * converter: resource xml converter
190 * listconverter: list of resources xml converter
192 self.client = Client(url)
193 self.converter = converter
194 self.listconverter = xmlconverter.ListConverter(self.converter)
197 def _processresult(r, converter):
199 """Generic processing of the REST call.
201 If no errors, tries to convert the result to a destination entity.
204 :param converter Converter:
206 if r.status_code == 404:
209 content_type = r.headers.get('content-type', '')
211 #print("content-type = " + content_type)
212 if content_type == 'application/json':
214 elif content_type == 'application/xml':
216 result = xmlconverter.convertstring(converter, xml)
225 r = self.client.get("")
226 resources = self._processresult(r, self.listconverter)
229 def getbyid(self, id, params):
230 """Get resource 'id'"""
231 r = self.client.get(id, params=params)
232 resource = _Resource._processresult(r, self.converter)
235 def get(self, path="", params={}):
236 """Generic query over resource: GET /resource?q1=v1&q2=v2...
238 :param dict[str,str] params: values to pass as get parameters
241 r = self.client.get(path, params=params)
242 resources = self._processresult(r, self.listconverter)
245 def create(self, body, **kwargs):
246 """Creates (POST method) a resource.
248 It should be convenient to set content-type header.
251 resource.create(body, headers={'content-type': 'application/xml'})
253 r = self.client.post("", body, **kwargs)
258 class Agreements(object):
260 def __init__(self, root_url, path=_AGREEMENTS_PATH):
261 """Business methods for Agreement resource
262 :param str root_url: url to the root of resources
263 :param str path: path to resource from root_url
265 The final url to the resource is root_url + "/" + path
267 self.resourceurl = _buildpath(root_url, path)
268 self.converter = xmlconverter.AgreementConverter()
269 self.res = _Resource(self.resourceurl, self.converter)
275 :rtype : list[wsag_model.Agreement]
277 return self.res.getall()
279 def getbyid(self, agreementid):
282 :rtype : wsag_model.Agreement
284 return self.res.getbyid(agreementid)
286 def getbyconsumer(self, consumerid):
287 """Get a consumer's agreements
289 :rtype : list[wsag_model.Agreement]
291 return self.res.get(dict(consumerId=consumerid))
293 def getbyprovider(self, providerid):
294 """Get the agreements served by a provider
296 :rtype : list[wsag_model.Agreement]
298 return self.res.get(dict(providerId=providerid))
300 def getstatus(self, agreementid, testbed):
301 """Get guarantee status of an agreement
303 :param str agreementid :
304 :rtype : wsag_model.AgreementStatus
306 # path = _buildpath(_AGREEMENTS_PATH, agreementid, "guaranteestatus")
307 path = _buildpath(agreementid, "guaranteestatus")
308 r = self.res.client.get(path, headers={'accept': 'application/json'},
309 params={'testbed': testbed})
312 status = wsag_model.AgreementStatus.json_decode(json_obj)
316 def getbyslice(self, slicename):
317 """Get the agreements corresponding to a slice
319 :rtype : list[wsag_model.Agreement]
321 self.resourceurl = _buildpath(rooturl, 'slice')
322 self.res = _Resource(self.resourceurl, self.converter)
323 return self.res.get(slicename)
325 def create(self, agreement, testbed):
326 """Create a new agreement
328 :param str agreement: sla template in ws-agreement format.
330 return self.res.create(agreement, params={'testbed': testbed})
333 class Templates(object):
335 def __init__(self, root_url, path=_TEMPLATES_PATH):
336 """Business methods for Templates resource
337 :param str root_url: url to the root of resources
338 :param str path: path to resource from root_url
340 The final url to the resource is root_url + "/" + path
342 resourceurl = _buildpath(root_url, path)
343 converter = xmlconverter.AgreementConverter()
344 self.res = _Resource(resourceurl, converter)
347 """ Get all templates
349 :rtype : list[wsag_model.Template]
351 return self.res.getall()
353 def getbyid(self, provider_id):
356 :rtype: wsag_model.Template
358 return self.res.getbyid(provider_id, {"testbed": provider_id})
360 def create(self, template):
361 """Create a new template
363 :param str template: sla template in ws-agreement format.
365 self.res.create(template)
368 class Providers(object):
370 def __init__(self, root_url, path=_PROVIDERS_PATH):
371 """Business methods for Providers resource
372 :param str root_url: url to the root of resources
373 :param str path: path to resource from root_url
375 The final url to the resource is root_url + "/" + path
377 resourceurl = _buildpath(root_url, path)
378 converter = xmlconverter.ProviderConverter()
379 self.res = _Resource(resourceurl, converter)
382 """ Get all providers
384 :rtype : list[wsag_model.Provider]
386 return self.res.getall()
388 def getbyid(self, provider_id):
391 :rtype: wsag_model.Provider
393 return self.res.getbyid(provider_id)
395 def create(self, provider):
396 """Create a new provider
398 :type provider: wsag_model.Provider
400 body = provider.to_xml()
401 return self.res.create(body)
404 class Violations(object):
406 def __init__(self, root_url, path=_VIOLATIONS_PATH):
407 """Business methods for Violation resource
408 :param str root_url: url to the root of resources
409 :param str path: path to resource from root_url
411 The final url to the resource is root_url + "/" + path
413 resourceurl = _buildpath(root_url, path)
414 converter = xmlconverter.ViolationConverter()
415 self.res = _Resource(resourceurl, converter)
418 """ Get all violations
419 :rtype : list[wsag_model.Violation]
421 return self.res.getall()
423 def getbyid(self, violationid):
426 :rtype : wsag_model.Violation
428 return self.res.getbyid(violationid)
430 def getbyagreement(self, agreement_id, testbed, term=None):
431 """Get the violations of an agreement.
433 :param str agreement_id:
434 :param str term: optional GuaranteeTerm name. If not specified,
435 violations from all terms will be returned
436 :rtype: list[wsag_model.Violation]
438 return self.res.get("", params={"agreementId": agreement_id,
439 "guaranteeTerm": term,
443 class Enforcements(object):
445 def __init__(self, root_url, path=_ENFORCEMENTJOBS_PATH):
446 """Business methods for Violation resource
447 :param str root_url: url to the root of resources
448 :param str path: path to resource from root_url
450 The final url to the resource is root_url + "/" + path
452 resourceurl = _buildpath(root_url, path)
453 converter = xmlconverter.EnforcementConverter()
454 self.res = _Resource(resourceurl, converter)
457 """ Get all Enforcements
458 :rtype : list[wsag_model.Violation]
460 return self.res.getall()
462 def getbyagreement(self, agreement_id, testbed):
463 """Get the enforcement of an agreement.
465 :param str agreement_id:
467 :rtype: list[wsag_model.Enforcement]
469 return self.res.getbyid(agreement_id, params={"testbed": testbed})
472 def _buildpath(*paths):
474 paths = [path for path in paths if path != ""]
476 return "/".join(paths)
484 rooturl = "http://127.0.0.1:8080/sla-service"
486 c = Factory.templates()
488 #r = c.getbyid("noexiste")
489 #r = c.getstatus("agreement03")
492 #r = c.getbyconsumer('RandomClient')
493 r = c.getbyid("template02")
498 if __name__ == "__main__":