def update_policy_target_attributes(request, pt): url = "horizon:project:application_policy:policy_rule_set_details" provided = pt.provided_policy_rule_sets consumed = pt.consumed_policy_rule_sets provided = [client.policy_rule_set_get(request, item) for item in provided] consumed = [client.policy_rule_set_get(request, item) for item in consumed] p = ["<ul>"] li = lambda x: "<li><a href='" + \ reverse(url, kwargs={'policy_rule_set_id': x.id}) + \ "'>" + x.name + "</a></li>" for item in provided: p.append(li(item)) p.append("</ul>") p = "".join(p) c = ["<ul>"] for item in consumed: c.append(li(item)) c.append("</ul>") c = "".join(c) consumed = [item.name for item in consumed] setattr(pt, 'provided_policy_rule_sets', mark_safe(p)) setattr(pt, 'consumed_policy_rule_sets', mark_safe(c)) l2url = "horizon:project:network_policy:l2policy_details" if pt.l2_policy_id is not None: policy = client.l2policy_get(request, pt.l2_policy_id) u = reverse(l2url, kwargs={'l2policy_id': policy.id}) atag = mark_safe( "<a href='" + u + "'>" + policy.name + "</a>") setattr(pt, 'l2_policy_id', atag) return pt
def __init__(self, request, *args, **kwargs): super(UpdatePolicyRuleSetForm, self).__init__(request, *args, **kwargs) rules = [] try: items = client.policyrule_list(request) rules = [(p.id, p.name) for p in items] policy_rule_set = client.policy_rule_set_get(request, self.initial["policy_rule_set_id"]) if policy_rule_set: self.fields["name"].initial = policy_rule_set.name self.fields["description"].initial = policy_rule_set.description self.fields["shared"].initial = policy_rule_set.shared existing = [item for item in policy_rule_set.policy_rules] self.fields["rules"].initial = existing except Exception: exceptions.handle(request, _("Unable to retrieve policy rules")) self.fields["rules"].choices = rules
def get_provided_policy_rule_sets_data(self): try: policy_targetid = self.tab_group.kwargs["policy_target_id"] policy_target = client.policy_target_get(self.request, policy_targetid) provided_policy_rule_set_ids = policy_target.get("provided_policy_rule_sets") provided_policy_rule_sets = [] for _id in provided_policy_rule_set_ids: provided_policy_rule_sets.append(client.policy_rule_set_get(self.request, _id)) provided_policy_rule_sets = [ gfilters.update_pruleset_attributes(self.request, item) for item in provided_policy_rule_sets ] return provided_policy_rule_sets except Exception: error_message = _("Unable to get provided rule sets") exceptions.handle(self.request, error_message) return []
def __init__(self, request, *args, **kwargs): super(UpdatePolicyRuleSetForm, self).__init__(request, *args, **kwargs) rules = [] try: items = client.policyrule_list(request) rules = [(p.id, p.name) for p in items] policy_rule_set = client.policy_rule_set_get( request, self.initial['policy_rule_set_id']) if policy_rule_set: self.fields['name'].initial = policy_rule_set.name self.fields[ 'description'].initial = policy_rule_set.description existing = [item for item in policy_rule_set.policy_rules] self.fields['rules'].initial = existing except Exception: exceptions.handle(request, _('Unable to retrieve policy rules')) self.fields['rules'].choices = rules
def get_context_data(self, request): cid = self.tab_group.kwargs['policy_rule_set_id'] try: policy_rule_set = client.policy_rule_set_get(request, cid) rules = client.policyrule_list( request, tenant_id=request.user.tenant_id, policy_rule_set_id=policy_rule_set.id) rules = [ item for item in rules if item.id in policy_rule_set.policy_rules ] rules_with_details = [] for rule in rules: r = {} r['name'] = rule.name r['id'] = rule.id action_list = [] for aid in rule.policy_actions: action = client.policyaction_get(request, aid) a = {'id': action.id} if action.action_value: if action.action_type == 'redirect': scspec = client.get_servicechain_spec( request, action.action_value) a['name'] = "Redirect:%s" % scspec.name else: values = (str(action.action_type), str(action.action_value)) name = "%s:%s" % values a['name'] = name else: a['name'] = str(action.action_type) action_list.append(a) r['actions'] = action_list r['classifier'] = client.policyclassifier_get( request, rule.policy_classifier_id) rules_with_details.append(r) except Exception as e: msg = _('Unable to retrieve policy_rule_set details.') % (str(e)) exceptions.handle(request, msg, redirect=self.failure_url) return { 'policy_rule_set': policy_rule_set, 'rules_with_details': rules_with_details }
def get_provided_policy_rule_sets_data(self): try: policy_targetid = self.tab_group.kwargs['policy_target_id'] policy_target = client.policy_target_get( self.request, policy_targetid) provided_policy_rule_set_ids = policy_target.get( 'provided_policy_rule_sets') provided_policy_rule_sets = [] for _id in provided_policy_rule_set_ids: provided_policy_rule_sets.append( client.policy_rule_set_get(self.request, _id)) provided_policy_rule_sets = [gfilters.update_pruleset_attributes( self.request, item) for item in provided_policy_rule_sets] return provided_policy_rule_sets except Exception: error_message = _('Unable to get provided rule sets') exceptions.handle(self.request, error_message) return []
def get_consumed_policy_rule_sets_data(self): try: policy_targetid = self.tab_group.kwargs['policy_target_id'] policy_target = client.policy_target_get( self.request, policy_targetid) consumed_policy_rule_set_ids = policy_target.get( 'consumed_policy_rule_sets') consumed_policy_rule_sets = [] for _id in consumed_policy_rule_set_ids: consumed_policy_rule_sets.append( client.policy_rule_set_get(self.request, _id)) consumed_policy_rule_sets = [gfilters.update_pruleset_attributes( self.request, item) for item in consumed_policy_rule_sets] return consumed_policy_rule_sets except Exception: error_message = _('Unable to get consumed rule sets') exceptions.handle(self.request, error_message) return []
def __init__(self, request, *args, **kwargs): super(UpdatePolicyRuleSetForm, self).__init__(request, *args, **kwargs) rules = [] try: items = client.policyrule_list(request, tenant_id=request.user.tenant_id) rules = [(p.id, p.name) for p in items] policy_rule_set = client.policy_rule_set_get( request, self.initial['policy_rule_set_id']) if policy_rule_set: self.fields['name'].initial = policy_rule_set.name self.fields[ 'description'].initial = policy_rule_set.description self.fields['shared'].initial = policy_rule_set.shared existing = [item for item in policy_rule_set.policy_rules] self.fields['policy_rules'].initial = existing except Exception: exceptions.handle(request, _('Unable to retrieve policy rules')) self.fields['policy_rules'].choices = rules
def get_consumed_policy_rule_sets_data(self): try: ext_policy_targetid = self.tab_group.kwargs['ext_policy_target_id'] ext_policy_target = client.ext_policy_target_get( self.request, ext_policy_targetid) consumed_policy_rule_set_ids = ext_policy_target.get( 'consumed_policy_rule_sets') consumed_policy_rule_sets = [] for _id in consumed_policy_rule_set_ids: consumed_policy_rule_sets.append( client.policy_rule_set_get(self.request, _id)) consumed_policy_rule_sets = [ gfilters.update_pruleset_attributes(self.request, item) for item in consumed_policy_rule_sets ] return consumed_policy_rule_sets except Exception: error_message = _('Unable to get consumed rule sets') exceptions.handle(self.request, error_message) return []
def get_context_data(self, request): cid = self.tab_group.kwargs['policy_rule_set_id'] try: policy_rule_set = client.policy_rule_set_get(request, cid) rules = client.policyrule_list( request, tenant_id=request.user.tenant_id, policy_rule_set_id=policy_rule_set.id) rules = [ item for item in rules if item.id in policy_rule_set.policy_rules] rules_with_details = [] for rule in rules: r = {} r['name'] = rule.name r['id'] = rule.id action_list = [] for aid in rule.policy_actions: action = client.policyaction_get(request, aid) a = {'id': action.id} if action.action_value: if action.action_type == 'redirect': scspec = client.get_servicechain_spec(request, action.action_value) a['name'] = "Redirect:%s" % scspec.name else: values = (str(action.action_type), str(action.action_value)) name = "%s:%s" % values a['name'] = name else: a['name'] = str(action.action_type) action_list.append(a) r['actions'] = action_list r['classifier'] = client.policyclassifier_get( request, rule.policy_classifier_id) rules_with_details.append(r) except Exception as e: msg = _('Unable to retrieve policy_rule_set details.') % (str(e)) exceptions.handle(request, msg, redirect=self.failure_url) return {'policy_rule_set': policy_rule_set, 'rules_with_details': rules_with_details}