Openstack dashboard添加license控制功能

·
Tech openstack django March 21, 2018

openstack_dashboard/settings.py中MIDDLEWARE_CLASSES添加中间件horizon.middleware.LicenseMiddleware。
horizon/middleware/目录中添加license.py,用于检测license状态的中间件,将license状态写入session中。

class LicenseMiddleware(object):

    def process_request(self, request):
        response = check_license()
        request.session['license_info'] = response
        return

horizon/middleware/init.py中添加license中间件

from horizon.middleware import license
LicenseMiddleware = license.LicenseMiddleware

修改table_actions和row_actions的基类horizon.tables.actions.BaseAction添加判断license的方法,对license不敏感的直接返回True。

def _check_license(self, request):
    expire_sensitive_object = ['CreateVolume', 'DeleteVolume', 'StartInstance', 'LaunchLinkNG', 'DeleteInstance',
                               'LaunchImage', 'LaunchImageNG', 'CreateImage', 'CreateVolumeFromImage',
                               'ImportKeyPair', 'CreateKeyPair', 'CreateNetwork', 'CreateSubnet', 'CreateRouter',
                               'CreateGroup', 'CreateRule', 'AllocateIP', 'CreateAggregateAction',
                               'ManageVolumeAction', 'CreateVolumeType', 'CreateQosSpec', 'CreateFlavor',
                               'AdminCreateImage', 'AdminAllocateFloatingIP', 'UpdateDefaultQuotas',
                               'CreateProject', 'CreateUserLink', 'ModifyQuotas']
    if self.__class__.__name__ in expire_sensitive_object:
        self.has_valid_license = request.session.get('license_info')
    else:
        self.has_valid_license = True

在horizon.tables.actions.BaseAction的_allowed中添加_check_license的调用

def _allowed(self, request, datum):
    self._check_license(request)
    policy_check = utils_settings.import_setting("POLICY_CHECK_FUNCTION")

    if policy_check and self.policy_rules:
        target = self.get_policy_target(request, datum)
        return (policy_check(self.policy_rules, request, target) and
                self.allowed(request, datum))
    return self.allowed(request, datum)

在模板horizon/templates/horizon/common/_data_table_action.html添加license的控制

{% load horizon %}
{% load i18n %}
{% minifyspace %}
  {% if action.method != "GET" %}
    <button {{ action.attr_string_nc|safe }}
      {% if not action.has_valid_license %} disabled="disabled" {% endif %}
      class="data-table-action
        {% if is_small %}btn-sm{% endif %}
        {% for class in action.get_default_classes %}
          {{ class }}
        {% endfor %}
        {% if action.action_type %}
          btn-{{ action.action_type }}
        {% endif %} btn"
      name="action"
      {% if action.help_text %}
        help_text="{{ action.help_text }}"
      {% endif %}
      type="submit"
      {% if is_table_action %}
        value="{{ action.get_param_name }}">
        {% if action.icon != None %}
          <span class="fa fa-{{ action.icon }}"></span>
        {% endif %}
        {% if action.handles_multiple %}
            {% if not action.has_valid_license %}
                {{ action.verbose_name_plural }} ({% trans "License Expired" %})
            {% else %}
                {{ action.verbose_name_plural }}
            {% endif %}
        {% else %}
            {% if not action.has_valid_license %}
                {{ action.verbose_name }} ({% trans "License Expired" %})
            {% else %}
                {{ action.verbose_name }}
            {% endif %}
        {% endif %}
      {% else %}
        value="{{ action.table.name }}__{{ action.name }}__{{ row_id }}">
        {% if action.has_valid_license %}
            {{ action.verbose_name }}
        {% else %}
            {{ action.verbose_name }} ({% trans "License Expired" %})
        {% endif %}
      {% endif %}
    </button>
  {% else %}
    <a {{ action.attr_string_nc|safe }}
      class="btn data-table-action {% if is_single %}btn-default {% endif %}{% if is_small %}btn-sm {% endif %}{{ action.get_final_css|safe }}"
      {% if is_table_action %}
        {% if action.has_valid_license %}
          href="{{ action.get_link_url }}"
        {% else %}
          href="#"
          onclick="return false;"
          disabled="true"
          style="color:#ccc;"
        {% endif %}
        title="{{ action.verbose_name }}">
        {% if action.icon != None %}
          <span class="fa fa-{{ action.icon }}"></span>
        {% endif %}
      {% else %}
        {% if action.has_valid_license %}
          href="{{ action.bound_url }}"
        {% else %}
          href="#"
          onclick="return false;"
          disabled="true"
          style="color:#ccc;"
        {% endif %}>
      {% endif %}
      {% if action.has_valid_license %}
          {{ action.verbose_name }}
      {% else %}
          {{ action.verbose_name }} ({% trans "License Expired" %})
      {% endif %}
    </a>
  {% endif %}
{% endminifyspace %}

为了防止修改html元素进行使用,可以在api侧进行控制。
添加装饰器

def has_valid_license(func):
    def wrapper(*args, **kwargs):
        request = args[0]
        license_info = request.session.get('license_info')
        if license_info:
            return func(*args, **kwargs)
        else:
            class LicenseExpiredError(Exception):
                pass
            raise LicenseExpiredError(_('License Expired'))
    return wrapper

在openstack_dashboard.api.xxx xxx对openstack组件名,在需要license控制的方法前添加@has_valid_license装饰器即可。

  • iptables -m physdev
  • ansible-playbook api 调用

已有 22 条评论
  1. do-not-say-mystery

    ミステリと言う勿れ Blu-ray DVD BOX 2枚組 nhk 大河 ドラマ

    do-not-say-mystery July 3rd, 2022 at 07:51 am回复
  2. do-not-say-mystery

    ミステリと言う勿れ Blu-ray DVD BOX 2枚組 ブルーレイ 映画

    do-not-say-mystery July 11th, 2022 at 12:02 am回复
  3. EdwinAveby

    第一借錢網-借錢,小額借款,小額借錢,證件借款,證件借錢,身分證借款,身分證借錢


    https://168cash.com.tw/

    EdwinAveby August 5th, 2022 at 09:17 am回复
  4. Glennbrind

    雙波長亞歷山大除毛雷射 - 八千代


    https://yachiyo.com.tw/alexandrite-laser/

    Glennbrind August 7th, 2022 at 12:35 am回复
  5. Glennbrind

    雙波長亞歷山大除毛雷射 - 八千代


    https://yachiyo.com.tw/alexandrite-laser/

    Glennbrind September 3rd, 2022 at 08:03 pm回复
  6. Doylesmefs

    點子數位科技有限公司

    https://spot-digital.com.tw/

    Doylesmefs September 13th, 2022 at 12:53 am回复
  7. Davidphofe

    水微晶玻尿酸 - 八千代


    https://yachiyo.com.tw/hyadermissmile-injection/

    Davidphofe September 13th, 2022 at 07:52 am回复
  8. TommySup

    點子數位科技有限公司

    https://spot-digital.com.tw/

    TommySup September 16th, 2022 at 10:08 pm回复
  9. Davidbog

    第一借錢網-借錢,小額借款,小額借錢


    https://168cash.com.tw/

    Davidbog October 4th, 2022 at 04:39 am回复
  10. Georgefed

    歐客佬精品咖啡 |OKLAO COFFEE|蝦皮商城|咖啡豆|掛耳|精品咖啡|咖啡禮盒 專賣|精品麵包

    https://first-cafe.com/

    Georgefed October 7th, 2022 at 08:15 pm回复
  11. Jasonhot

    第一借錢網





    https://168cash.com.tw/

    Jasonhot October 10th, 2022 at 07:03 am回复
  12. Glennbrind

    雙波長亞歷山大除毛雷射 - 八千代


    https://yachiyo.com.tw/alexandrite-laser/

    Glennbrind October 10th, 2022 at 09:11 am回复
  13. MichaelDat

    日光共享空間 - Nikko Space


    https://nikkospace.com/

    MichaelDat October 11th, 2022 at 03:16 pm回复
  14. Davidbog

    第一借錢網-借錢,小額借款,小額借錢


    https://168cash.com.tw/

    Davidbog October 25th, 2022 at 05:47 am回复
  15. Davidphofe

    水微晶玻尿酸 - 八千代


    https://yachiyo.com.tw/hyadermissmile-injection/

    Davidphofe October 28th, 2022 at 08:56 am回复
  16. FrancisSof

    詠春拳幣






    https://etherscan.io/token/0xa8930ee8906063d2a5c7ea15b6b4429463d28f58

    FrancisSof October 29th, 2022 at 09:15 am回复
  17. Jasonhot

    第一借錢網





    https://168cash.com.tw/

    Jasonhot October 29th, 2022 at 12:03 pm回复
  18. MichaelDat

    日光共享空間 - Nikko Space


    https://nikkospace.com/

    MichaelDat October 29th, 2022 at 01:20 pm回复
  19. Georgefed

    歐客佬精品咖啡 |OKLAO COFFEE|蝦皮商城|咖啡豆|掛耳|精品咖啡|咖啡禮盒 專賣|精品麵包

    https://first-cafe.com/

    Georgefed November 1st, 2022 at 07:59 am回复
  20. Davidphofe

    水微晶玻尿酸 - 八千代


    https://yachiyo.com.tw/hyadermissmile-injection/

    Davidphofe November 18th, 2022 at 09:28 am回复
  21. PatrickGrala

    第一借錢網-借錢,小額借款,小額借錢


    https://168cash.com.tw/

    PatrickGrala November 18th, 2022 at 12:11 pm回复
  22. Georgefed

    歐客佬精品咖啡 |OKLAO COFFEE|蝦皮商城|咖啡豆|掛耳|精品咖啡|咖啡禮盒 專賣|精品麵包

    https://first-cafe.com/

    Georgefed November 26th, 2022 at 09:58 pm回复
取消回复

说点什么?

© 2023 Coo's Note. Using Typecho & Moricolor.