ECS:如何使用 API curl 命令
Summary: 如何結合 GET 和 PUT 範例使用應用程式發展介面 (API) curl 命令。
Instructions
本知識文章說明如何使用 curl 命令來使用 ECS API。
在 ECS 管理指南或資料存取指南等使用者指南中,有使用 curl 命令的 API 使用範例。
這將顯示如何使用 curl 命令在 API 上獲取和放置。在此範例中,每 KB ECS 的 LDAPS 憑證 API:如何在 ECS 上透過 LDAPS 設定和接受所有憑證。
Curl 範例 - LDAPS 憑證 API 範例:
LDAPS 憑證 API 使用管理 IP,不允許 emcmonitor 存取,而是允許 root 使用者。此為使用 ECS API 的範例,LDAPS 憑證 API 位於 "/vdc/truststore"。您的 API 可能不同且位於不同的位置。
若要檢查與 LDAPS 憑證設定相關的 ECS 設定,請先取得要使用的節點管理 IP。如果 UI 管理有網路分隔,則”getrackinfo -n」的值為「mgmt」。下列步驟不需要使用節點 public.mgmt IP 位址、複寫和資料網路分隔:
# getrackinfo -n Named networks ============== Node ID Network Ip Address Netmask Gateway VLAN Interface 1 repl 10.xxx.xxx.11 255.255.254.0 xxx.xxx.xxx.xxx 14 public 1 mgmt 10.xxx.xxx.21 255.255.254.0 xxx.xxx.xxx.xxx 13 public 1 data 10.xxx.xxx.31 255.255.254.0 xxx.xxx.xxx.xxx 15 public 2 repl 10.xxx.xxx.12 255.255.254.0 xxx.xxx.xxx.xxx 14 public 2 mgmt 10.xxx.xxx.22 255.255.254.0 xxx.xxx.xxx.xxx 13 public 2 data 10.xxx.xxx.32 255.255.254.0 xxx.xxx.xxx.xxx 15 public 3 repl 10.xxx.xxx.13 255.255.254.0 xxx.xxx.xxx.xxx 14 public 3 mgmt 10.xxx.xxx.23 255.255.254.0 xxx.xxx.xxx.xxx 13 public 3 data 10.xxx.xxx.33 255.255.254.0 xxx.xxx.xxx.xxx 15 public 4 repl 10.xxx.xxx.14 255.255.254.0 xxx.xxx.xxx.xxx 14 public 4 mgmt 10.xxx.xxx.24 255.255.254.0 xxx.xxx.xxx.xxx 13 public 4 data 10.xxx.xxx.34 255.255.254.0 xxx.xxx.xxx.xxx 15 public
如果 VDC 上沒有網路 public.mgmt 分隔,請改用節點的公共 IP 位址。請注意,如果已設定,則 public.mgmt 網路分隔地址優先。
# getrackinfo Node private Node Public BMC Ip Address Id Status Mac Ip Address Mac Ip Address Node Name =============== ====== ====== ================= ================= ================= ================= ========= 192.1XX.2XX.1 1 MA a4:bf:xx:xx:xx:74 10.xx.xx.1 a4:bf:xx:xx:xx 192.1XX.2XX.101 provo-red 192.1XX.2XX.2 2 SA a4:bf:xx:xx:xx:c8 10.xx.xx.2 a4:bf:xx:xx:xx 192.1XX.2XX.102 sandy-red 192.1XX.2XX.3 3 SA a4:bf:xx:xx:xx:e0 10.xx.xx.3 a4:bf:xx:xx:xx 192.1XX.2XX.103 orem-red 192.1XX.2XX.4 4 SA a4:bf:xx:xx:xx:56 10.xx.xx.4 a4:bf:xx:xx:xx 192.168.219.104 ogden-red
使用取得的管理 IP 位址來取得 root 使用者權杖,root 使用者密碼為必填:
# curl -s -k -v -u root https://<NodeManagementIP>:4443/login 2>&1
範例:
# curl -s -k -v -u root https://10.xxx.xxx.21:4443/login 2>&1 Enter host password for user 'root': ...... < HTTP/1.1 200 OK < Date: Thu, 14 Jan 2021 13:51:24 GMT < Content-Type: application/xml < Content-Length: 93 < Connection: keep-alive < X-SDS-AUTH-TOKEN: BAAcdWhGbnVRVjd1WlpmR0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAgAC0A8= < X-SDS-AUTH-USERNAME: root < X-SDS-AUTH-MAX-AGE: 28800 < * Connection #0 to host 10.xxx.xxx.21 left intact root
如果輸出不是“HTTP/1.1 200 OK”,請檢查密碼和 IP 位址,檢查 UI 管理網路分隔是否存在於命令”getrackinfo -n」。使用 root 使用者權杖建立權杖變數,如果在新的 CLI 工作階段中,則必須更新有效的權杖:
# export TOKEN='X-SDS-AUTH-TOKEN: BAAcdWhGbnVRVjd1WlpmR0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAgAC0A8='
若要測試權杖是否為值,您可以執行 curl 命令:
# curl -k -X GET -H "$TOKEN" https://10.xxx.xxx.21:4443/object/capacity | xmllint --format - % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 176 100 176 0 0 249 0 --:--:-- --:--:-- --:--:-- 250 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <cluster_capacity> <totalFree_gb>100657</totalFree_gb> <totalProvisioned_gb>447005</totalProvisioned_gb> </cluster_capacity>
上述步驟的目的是使用 root 使用者權杖取得 ECS UI 信任存放區的 curl 存取權。如需「新增自訂 LDAP 憑證」的詳細資訊,請參閱 ECS 管理員指南。在此 ECS API 使用範例中,LDAPS 憑證 API 位於 "/vdc/truststore"。您的 API 可能位於不同的位置。
curl GET 範例:
使用 curl GET 命令取得信任存放區設定:
# curl -s -k -X GET -H Content-Type:application/json -H "$TOKEN" -H ACCEPT:application/json https://<NodeManagementIP>:4443/vdc/truststore/settings
範例:
# curl -s -k -X GET -H Content-Type:application/json -H "$TOKEN" -H ACCEPT:application/json https://10.xxx.xxx.21:4443/vdc/truststore/settings
{"accept_all_certificates":false}
在上面的例子中,信任庫 "accept_all_certificates" 設置為 false。這表示只有上傳的 LDAPS 憑證與 AD/LDAP 伺服器憑證相符,才會受到 ECS 信任。使用者可能希望將此設置為 true 或 false,具體取決於他們的需要。若要測試 LDAPS 連線,請將其設為 true 並測試使用者登入,即可進行驗證。
建立 json 檔案,以透過具有 json 裝載的 curl 命令變更信任存放區設定:
# sudo vi truststoresettings.json
# sudo cat truststoresettings.json
{"accept_all_certificates": "true"}
可利用 python 上的 json.tool 模組驗證 json 檔案格式。如果發生錯誤,檔案便可能不是 json 檔案。在此範例中,沒有出現錯誤並輸出檔案,因此這是已格式化的 json 檔案:
# python -m json.tool truststoresettings.json
{
"accept_all_certificates": "true"
}
Curl PUT 範例:
使用 curl PUT 命令,以裝載 json 檔案的狀況執行 curl 命令:
# curl -s -k -X PUT -H Content-Type:application/json -H "$TOKEN" -H ACCEPT:application/json https://:4443/vdc/truststore/settings -d @truststoresettings.json
若要檢查是否已更新,請執行 curl GET 命令:
# curl -s -k -X GET -H Content-Type:application/json -H "$TOKEN" -H ACCEPT:application/json https://10.xxx.xxx.21:4443/vdc/truststore/settings
{"accept_all_certificates":true}