zoukankan      html  css  js  c++  java
  • how to read openstack code: request extension

    We have learned resource extension and action extension. This post we will write a request extension

    First see two API call

    curl -X POST http://liberty-controller01:9696/v2.0/networks.json -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net3", "admin_state_up": true}}'
    
    {"network": {"status": "ACTIVE", "subnets": [], "name": "net3", "provider:physical_network": "physnet1", "admin_state_up": true, "tenant_id": "8c5f13ee6a404759839e48537bdf69ac", "mtu": 0, "router:external": false, "shared": false, "port_security_enabled": true, "provider:network_type": "vlan", "id": "95c955ac-c963-4f53-ab4a-d721fa0cda51", "provider:segmentation_id": 490}}
    

    It run successful. Nothing to say. Then this one

    curl -X POST http://liberty-controller01:9696/v2.0/networks.json -H "Content-Type: application/json" -H "Accept: application/json" -H "X-Auth-Token: $token" -d '{"network": {"name": "net3", "admin_state_up": true, "some_attr":"some_value"}}'
    
    {"NeutronError": {"message": "Unrecognized attribute(s) 'some_attr'", "type": "HTTPBadRequest", "detail": ""}}[root@liberty-controller01 myPluginPKG]# 
    

    This one is bad request. But the only difference is the POST body. Neutron think the some_attr is not a attribute of network and it is right. Because this is a core resource and the attribute map of this resource do not have some_attr

    To solve this we need an request extension which actually update the resource attribute map of network. Below are the code

    from neutron.api import extensions
    
    EXTENDED_ATTRIBUTES_2_0 = {
        'networks': {
            'some_attr': {'allow_post': True,
                     'allow_put': False,
                     'is_visible': True,
                     'default': ''}
        }
    }
    
    
    class Myreq(extensions.ExtensionDescriptor):
        @classmethod
        def get_name(cls):
            return "myreq"
    
        @classmethod
        def get_alias(cls):
            return 'myreq'
    
        @classmethod
        def get_description(cls):
            return "myreq"
    
        @classmethod
        def get_updated(cls):
            return "2017-02-08T10:00:00-00:00"
    
        def get_extended_resources(self, *args, **kwargs):
            return EXTENDED_ATTRIBUTES_2_0
    

    You can see we defined a dict in the extension and return it with method get_extended_resources.

    So to implement an request extension is very easy. Define a method called get_extended_resources and return some attributes that you want to added to the original reosurce

  • 相关阅读:
    单元化架构 定义问题
    STGW 下一代互联网标准传输协议QUIC大规模运营之路 wentaomao 腾讯技术工程 2021-02-01
    string
    进程管理工具 源码分析
    etcd 鉴权体系架构由控制面和数据面组成。
    HTTP/2 是基于二进制而不是文本
    分布式 ID 解决方案
    减少重复开发,GraphQL在低代码平台如何落地? 原创 随刻信息流团队 爱奇艺技术产品团队 2021-01-29
    设计模式混编:观察者模式+中介者模式
    mysql 语法总结
  • 原文地址:https://www.cnblogs.com/kramer/p/6383645.html
Copyright © 2011-2022 走看看