首先安裝內置青雲SDK的 qlua 命令: go get github.com/chai2010/qingcloud-go/cmd/qlua
注:這個解釋器不是官方的C語言版本Lua解釋器,而且純Go語言實現的 GopherLua 解釋器訂製而來。
創建一個JSON格式的配置文件,裏面含有基本是口令信息 ~/.qingcloud/qcli.json
:
{
"api_server": "https://api.qingcloud.com/iaas/",
"access_key_id": "公鑰",
"secret_access_key": "私鑰",
"zone": "pek3a"
}
然後創建 hello.lua
腳本:
local qc = require("qingcloud.iaas")
if #arg == 1 and arg[1] == '-v' then
print(qc.version)
print(qc.version_info.git_sha1_version)
print(qc.version_info.build_date)
do return end
end
if #arg == 1 and arg[1] == '-h' then
print(qc.copyright)
print("hello, 青雲!")
do return end
end
local config = qc.LoadJSON("~/.qingcloud/qcli.json")
local client = qc.Client:new(config)
local reply, err = client:DescribeInstances {
--owner = "usr-xxxxxxxx",
zone = "pek3a",
limit = 100
}
if err ~= nil then
print("error:", err)
do return end
end
if reply.ret_code ~= 0 then
print(reply.ret_code)
print(reply.message)
do return end
end
for i = 1, #reply.instance_set do
local item = reply.instance_set[i]
print(i,
item.instance_id,
item.instance_type,
item.memory_current..'MB',
item.status,
item.create_time,
item.instance_name
)
end
print('total: ' .. reply.total_count)
可以輸入一下命令執行:
$ qlua hello.lua -h
$ qlua hello.lua -v
$ qlua hello.lua
如果虛機數量巨大,則需要多次調用 client:DescribeInstances
獲取。
參考:
https://github.com/chai2010/qingcloud-go#qucik-guide-gopherlua-version