BMF RESTful API總結(jié)

導(dǎo)讀

  • BMF RESTful API 文檔勘誤
  • interface role 的增刪查
  • lag 的增刪查
  • service node 的增刪查
  • policy 的增刪查
  • Python版本的REST client
BMF RESTful API 文檔勘誤

在《BMF-6.0.0-REST-API-Guide-2017.03.07》文檔的Chapter 1 page16給出了介紹,如何用curl或者Python實(shí)現(xiàn)REST請(qǐng)求。因?yàn)槲臋n有錯(cuò)誤耕挨,所以只糾正這些錯(cuò)誤的地方厢塘。

《BMF-6.0.1-REST-API-Guide-2017.04.19》文檔已經(jīng)修正了這些錯(cuò)誤

1. BMF Controller需要開(kāi)啟REST端口

文檔上是這么寫(xiě)的:

Note: By default, port 8082 is not enabled on the controller firewall. To enable this port, enter the firewall allow port 8082 command from the BMF controller config mode. For details about limiting access to specific source or destination addresses, enter the help firewall command, or refer to the firewall command section in the ...

這里面有兩個(gè)錯(cuò)誤:

  • 8082端口就不是REST端口,而是8443
  • 根本沒(méi)有firewall這個(gè)命令

解決辦法:
在BMF Controller進(jìn)行如下操作

BMF6> enable
BMF6# config
BMF6(config)# controller
BMF6(config-controller)# access-control
BMF6(config-controller-access)# access-list api 
BMF6(config-controller-access-list)# <ACL ID侈询,自己指定> permit from <IP>
2. REST auth URL有錯(cuò)誤

開(kāi)啟了REST端口舌涨,還需要修改auth URL。
文檔里面的是:http://${CONTROLLER_IP}:8082/auth/login
實(shí)際應(yīng)該是:https://${CONTROLLER_IP}:8443/api/v1/auth/login

把上面兩個(gè)問(wèn)題解決后再看文檔可知扔字,在發(fā)送REST請(qǐng)求之前囊嘉,先要登陸拿到sesson_cookie,然后再發(fā)送請(qǐng)求革为。這個(gè)過(guò)程可以通過(guò)curl來(lái)完成(具體見(jiàn)文檔)扭粱,也可以用Python來(lái)完成。我很懶篷角,懶得每次都敲一長(zhǎng)串的curl命令焊刹。所以基于Sample code做了一些修改,用來(lái)做REST client。源碼見(jiàn)最后虐块。

interface role 的增刪查

這里主要列一下創(chuàng)建俩滥、查看以及修改interface role設(shè)計(jì)到的REST URL

創(chuàng)建 interface role
  • uri
PUT https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/interface
  • json
[
{
    "analytics" : true,
    "bigtap-name" : "Filter",
    "breakout" : false,
    "name" : "ethernet1",
    "optics-always-enabled" : false,
    "role" : "filter",
    "shutdown" : false
},
{
    "analytics" : true,
    "bigtap-name" : "Delivery",
    "breakout" : false,
    "name" : "ethernet2",
    "optics-always-enabled" : false,
    "role" : "delivery",
    "shutdown" : false
},
{
    "analytics" : true,
    "bigtap-name" : "Service-ingress",
    "breakout" : false,
    "name" : "ethernet3",
    "optics-always-enabled" : false,
    "role" : "service",
    "shutdown" : false
},
{
    "analytics" : true,
    "bigtap-name" : "Service-egress",
    "breakout" : false,
    "name" : "ethernet4",
    "optics-always-enabled" : false,
    "role" : "service",
    "shutdown" : false
}
]

json的列表長(zhǎng)度、每個(gè)元素的"name""bigtap-name"都可以根據(jù)自己需要定義

  • response
    None
刪除 interface role
  • uri
DELETE https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/interface%5Bname%3D%22${INTERFACE_NAME}%22%5D

這個(gè)uri有必要多說(shuō)一下贺奠,文檔的Chapter 1 Page 14 BigDB REST API YANG Extensions小節(jié)提到霜旧,BMF使用REST API完成查詢操作時(shí)使用了YANG模型儡率,挂据,REST API的請(qǐng)求是由prefixXPATH組成儿普。原文如下:

Note BigDB REST API uses YANG schema language to define resources, which are naturally mapped into an XML structure. BigDB REST API uses XPATH as a query language to query and manipulate resources. XPATH location path and predicates are encoded within URIs of REST API call. In other words, each rest URI contains an XPATH that selects a set of data nodes within the resources tree defined by the YANG schema. For sample XPATH queries, see the sample queries in the REST API section.

示例圖

再來(lái)看上面的uri,它在創(chuàng)建interface role的uri后面拼接了[key=value]部分。我們知道广恢,URL中是不允許出現(xiàn)類似空格呀潭、等號(hào)钉迷、雙引號(hào)等字符的,因此這個(gè)uri需要經(jīng)過(guò)轉(zhuǎn)碼蜗侈。編碼之后便成了這個(gè)樣子:%5Bname%3D%22${INTERFACE_NAME}%22%5D(其中篷牌,雙引號(hào)必不可少)。

  • responese
    None
查詢 interface role
  • uri
# 查詢所有的interface role
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/interface
# 根據(jù)interface name查詢
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/interface%5Bname%3D%22${INTERFACE_NAME}%22%5D
  • response
    創(chuàng)建 interface role PUT的json

lag 的增刪查

創(chuàng)建 lag interface
  • uri
PUT https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/lag-interface%5Bname%3D%22${LAG_NAME}%22%5D
  • json
{
    "hash-type" : "autoconfig",
    "member" : [ { 
        "name" : "ethernet23"
        }, {
        "name" : "ethernet24"
        } ],
    "name" : "LAG-1"
}
  • response
    None
刪除 lag interface
  • uri
DELETE https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/lag-interface%5Bname%3D%22${LAG_NAME}%22%5D
  • response
    None
查看 lag interface
  • uri
# 查看所有的lag interface
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/lag-interface
# 根據(jù)lag interface name查詢
https://${CONTROLLER_IP}:8443/api/v1/data/controller/core/switch-config/lag-interface%5Bname%3D%22${LAG_NAME}%22%5D
  • response
    創(chuàng)建 lag interface PUT的json

service node 的增刪查

創(chuàng)建 service node
  • uri
POST https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/service
  • json
[
{
    "max-from-service-bandwidth-bps" : 1000000000,
    "max-to-service-bandwidth-bps" : 1000000000,
    "name" : "WAF",
    "post-group" : [ { 
        "bigtap-name" : "Service-egress"
    } ],
    "pre-group" : [ { 
        "bigtap-name" : "Service-ingress"
    } ],
    "total-from-service-bps" : 0,
    "total-to-service-bps" : 0 
},
{
    "max-from-service-bandwidth-bps" : 1000000000,
    "max-to-service-bandwidth-bps" : 1000000000,
    "name" : "NF",
    "post-group" : [ { 
        "bigtap-name" : "Service-egress"
    } ],
    "pre-group" : [ { 
        "bigtap-name" : "Service-ingress"
    } ],
    "total-from-service-bps" : 0,
    "total-to-service-bps" : 0 
}
]

json的列表長(zhǎng)度踏幻、每個(gè)元素的"name"枷颊、"post-group""pre-group"根據(jù)自己需要定義

  • response
    None
刪除 service node
  • uri
DELETE https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/service%5Bname%3D%22${SERVICE_NAME}%22%5D
  • response
    None
查看 service node
  • uri
# 查看所有的service node
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/service
# 根據(jù)service node name查詢
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/service%5Bname%3D%22${SERVICE_NAME}%22%5D
  • response
    創(chuàng)建 service node PUT的json

policy 的增刪查

創(chuàng)建 policy
  • uri
POST https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/policy
  • json
[
{
    "action" : "forward",
    "delivery-group" : [ { 
        "bigtap-name" : "Delivery"
    } ],
    "delivery-mode" : "custom",
    "duration" : 0,
    "expired-delivery-count" : false,
    "expired-time" : false,
    "filter-group" : [ { 
        "bigtap-name" : "Filter"
    } ],
    "filter-mode" : "custom",
    "inactive" : false,
    "name" : "SSH",
    "policy-description" : "SSH policy",
    "priority" : 100,
    "rule" : [ { 
        "dst-ip" : "172.16.1.1",
        "dst-ip-mask" : "255.255.255.0",
        "dst-tp-port-max" : 23, 
        "dst-tp-port-min" : 22, 
        "ether-type" : 2048,
        "ip-fragment" : true,
        "ip-proto" : 6,
        "sequence" : 1,
        "src-ip" : "172.16.0.1",
        "src-ip-mask" : "255.255.255.0",
        "src-tp-port-max" : 50025,
        "src-tp-port-min" : 50022,
        "tcp-flags" : 63, 
        "tcp-flags-mask" : 63
    } ],
    "service" : [ { 
        "name" : "WAF",
        "optional" : false,
        "sequence" : 1 
    } ],
    "start-time" : "2017-06-02T14:26:00+08:00"
}
]

json的列表長(zhǎng)度、每個(gè)元素的"name"该面、"rule"夭苗、"service"等字段都可根據(jù)自己需要定義(有一些不必要的字段可以省略,如"start-time"隔缀、"policy-description"等)

  • response
    None
刪除 policy
  • uri
DELETE https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/policy%5Bname%3D%22${POLICY_NAME}%22%5D
  • response
    None
查看 policy
  • uri
# 查看全部policy
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/policy
# 根據(jù)policy name查詢
GET https://${CONTROLLER_IP}:8443/api/v1/data/controller/applications/bigtap/policy%5Bname%3D%22${POLICY_NAME}%22%5D
  • response
    創(chuàng)建 policy PUT的json

Python版本的REST client

import sys
import urllib2
import json
import ssl


# 根據(jù)不同環(huán)境自行修改题造,但PORT是固定的
BIGTAP_CONTROLLER = 
PORT = "8443"
USER = 
PASSWORD = 

def rest_request(url, obj=None, verb="GET", session=None):
    headers = {"Content-type": "application/json"}
    if session:
        headers["Cookie"] = "session_cookie=%s" % session
    request = urllib2.Request(url, obj, headers)
    request.get_method = lambda: verb
    # skip certificate check
    context = ssl._create_unverified_context()
    response = urllib2.urlopen(request, context=context)

    return response.read()

def get_session_cookie():
    url_login = "https://%s:%s/api/v1/auth/login" % (BIGTAP_CONTROLLER, PORT)
    data = {"password": str(PASSWORD), "user": str(USER)}
    output = rest_request(str(url_login), obj=json.dumps(data), verb="POST")
    auth_obj = json.loads(output)
    # print "Login complete %s" % auth_obj["session_cookie"]
    return auth_obj["session_cookie"]

def delete_session(session_cookie=None):
    url_delete_session = "https://%s:%s/api/v1/data/controller/core/aaa/session" % (BIGTAP_CONTROLLER, PORT)
    rest_response = rest_request(url_delete_session, verb="DELETE", session=session_cookie)
    print rest_response

def make_rest_call(rest_uri_path, session_cookie, method, data=None):
    # check whether rest_uri_path need quote (DELETE verb)
    pos = rest_uri_path.find('[')
    if pos != -1:
        rest_uri_path = rest_uri_path[:pos] + urllib2.quote(rest_uri_path[pos:])
    url = "https://%s:%s/api/v1/data/controller/%s" % (BIGTAP_CONTROLLER, PORT, rest_uri_path)
    print "=" * 50
    print "Requesting URI:\n%s %s" % (method, url)
    if not data:
        rest_response = rest_request(url, verb=method, session=session_cookie)
    else:
        rest_response = rest_request(url, verb=method, obj=json.dumps(data), session=session_cookie)
    print "=" * 50
    print "Response:\n%s" % rest_response

def usage():
print  """
python rest_client.py <rest_uri> <method> [json_file]
       <rest_uri>  RESTful API request url
       <method>    POST|GET|PUT|DELETE
                   NOTICE: If method=POST|PUT, you have to specify json data file
       <json_file> json data file path
         
For example: python rest_client.py 'applications/bigtap/policy' GET
             python rest_client.py 'applications/bigtap/policy' POST ssh_policy.json
             python rest_client.py 'applications/bigtap/policy[name="SSH"]' DELETE
"""

def main():
    if len(sys.argv) >= 3:
        rest_uri_path = sys.argv[1]
        method = sys.argv[2]

        session_cookie = get_session_cookie()
        if method == "POST" or method == "PUT":
            if len(sys.argv) != 4:
                print "ERROR usage! You have specify %s json data" % method
                usage()
                sys.exit(1)
            json_data = json.loads(open(sys.argv[3],"r").read())
            make_rest_call(rest_uri_path, session_cookie, method, json_data)
        else:
            make_rest_call(rest_uri_path, session_cookie, method)
        delete_session(session_cookie)
    else:
        usage()


if __name__ == "__main__":
    main()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市猾瘸,隨后出現(xiàn)的幾起案子界赔,更是在濱河造成了極大的恐慌丢习,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淮悼,死亡現(xiàn)場(chǎng)離奇詭異咐低,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)袜腥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門见擦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人羹令,你說(shuō)我怎么就攤上這事鲤屡。” “怎么了福侈?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵酒来,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我癌刽,道長(zhǎng)役首,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任显拜,我火速辦了婚禮,結(jié)果婚禮上爹袁,老公的妹妹穿的比我還像新娘远荠。我一直安慰自己,他們只是感情好失息,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布譬淳。 她就那樣靜靜地躺著,像睡著了一般盹兢。 火紅的嫁衣襯著肌膚如雪邻梆。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,441評(píng)論 1 310
  • 那天绎秒,我揣著相機(jī)與錄音浦妄,去河邊找鬼。 笑死见芹,一個(gè)胖子當(dāng)著我的面吹牛剂娄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播玄呛,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼阅懦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了徘铝?” 一聲冷哼從身側(cè)響起耳胎,我...
    開(kāi)封第一講書(shū)人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤惯吕,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后怕午,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體混埠,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年诗轻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钳宪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡扳炬,死狀恐怖吏颖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恨樟,我是刑警寧澤半醉,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站劝术,受9級(jí)特大地震影響缩多,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜养晋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一衬吆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绳泉,春花似錦逊抡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至四苇,卻和暖如春孝凌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背月腋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工蟀架, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人罗售。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓辜窑,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親寨躁。 傳聞我的和親對(duì)象是個(gè)殘疾皇子穆碎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)职恳,斷路器所禀,智...
    卡卡羅2017閱讀 134,707評(píng)論 18 139
  • springboot配置選項(xiàng)(一) =======================================...
    JanloongDo_0閱讀 4,799評(píng)論 0 3
  • Openstack Mitaka安裝部署教程 一方面、實(shí)驗(yàn)環(huán)境: 系統(tǒng):centos7.2-minimal 網(wǎng)絡(luò):管理...
    指間_流年閱讀 2,206評(píng)論 0 0
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,493評(píng)論 5 6
  • 昨天全世界都在刷月亮,70年一次的超級(jí)月亮之約色徘,讓我想起了很多年前看過(guò)一本幾米的書(shū)叫《月亮忘記了》恭金,里面有這樣一...
    古月心禪茶閱讀 769評(píng)論 0 1