def template_constraints(request, template_id): t = get_template(template_id) te_list = [] if t: try: for index, te in enumerate(t["terms"]["allTerms"]["guaranteeTerms"]): cns = json.loads(te["serviceLevelObjetive"]["kpitarget"]["customServiceLevel"])["constraint"].split(" ") cns_name = t["terms"]["allTerms"]["serviceProperties"][0]["variableSet"]["variables"][index]["name"] kpiName = te["serviceLevelObjetive"]["kpitarget"]["kpiName"] te_item = { "t_name": t["name"], "t_time": t["context"]["expirationTime"], "service": t["context"]["service"], "provider": t["context"]["agreementInitiator"], "cns": cns[1], "cns_val": cns[2], "cns_name": guiformatter.humanReadableMetric(cns_name), "kpiName": guiformatter.humanReadableMetric(kpiName, True) } if "policy" in te["serviceLevelObjetive"]["kpitarget"]["customServiceLevel"]: policyValue = json.loads(te["serviceLevelObjetive"]["kpitarget"]["customServiceLevel"])["policy"]; te_item["policy"] = guiformatter.getIntervalFromPolicy(policyValue) else: te_item["policy"] = "" te_list.append(te_item) print json.dumps(te_list) except: return HttpResponse(json.dumps([]), content_type="application/json") return HttpResponse(json.dumps(te_list), content_type="application/json")
def create_agreement(request): rp = request.POST#request params guarantee_terms = [] s_properties = [] variables = [] template = get_template(rp.get('tempid')) agreement_name = rp.get('aname') if rp.get('grRowNum'): for x in range(1, int(rp.get('grRowNum')) + 1): sname = guiformatter.machineReadableMetric(rp.get('sname' + str(x))) sp = { "name": sname, "metric": UtilHelper._metric_type.get(sname), "location": UtilHelper._metric_location.get(sname) } variables.append(sp) variable_set = {"variables": variables} inners_properties = { "variableSet": variable_set, "name": "ServiceProperties", "serviceName": str(template["context"]["service"]) } s_properties.append(inners_properties) const = {} for y in range(1, int(rp.get('grRowNum')) + 1): gname = guiformatter.machineReadableMetric(rp.get("gname" + str(y))) const["constraint"] = (gname + " " + rp.get("cons" + str(y)) + " " + rp.get("consval" + str(y))) policy_obj = rp.get("polval" + str(y)) if u"Real time" not in policy_obj: const["policy"] = "(1 breach, " + guiformatter.getIntervalFromPolicy(policy_obj) + " hours)" gr = { "name": gname, "serviceScope": { "serviceName": str(template["context"]["service"]), "value": str(template["context"]["service"]), }, "serviceLevelObjetive": { "kpitarget": { "kpiName": gname, "customServiceLevel": json.dumps(const) } } } guarantee_terms.append(gr) doto = { "context": { "agreementInitiator": request.session.get("user_email"), "agreementResponder": str(template["context"]["agreementInitiator"]), "serviceProvider": "AgreementResponder", "service": str(template["context"]["service"]), "expirationTime": str(template["context"]["expirationTime"]), "templateId": str(rp.get('tempid')) }, "name": agreement_name, "terms": { "allTerms": { "serviceDescriptionTerm": { "name": None, "serviceName": None }, "serviceProperties": s_properties, "guaranteeTerms": guarantee_terms } } } sla_url = settings.SLA_MANAGER_URL + '/agreements' headers = {settings.ACCEPT_HEADER: settings.JSON_HEADER, 'content-type': settings.JSON_HEADER} r = requests.post(sla_url, data=json.dumps(doto), headers=headers, auth=(settings.SLA_MANAGER_USER, settings.SLA_MANAGER_PASSWORD)) context = "" resultinfo = r._content data = json.loads(resultinfo) if not r.ok: message = data.get("message") raise Exception(message) else: agreementId = data.get("elementId") try: success = request.GET.get("success") __start_agreement(request, agreementId) except: success = "" context = {"success": success } return render(request, 'slagui/agreements.html', context)