Giter VIP home page Giter VIP logo

qingcloud-go's Introduction

QingCloud

青云非官方 SDK Go/GopherLua

Build Status Docker Build Status Go Report Card codebeat badge GoDoc API Reference License

项目特色:

  • Go语言风格的 SDK 封装
  • qcli 命令行工具, 完全等价 SDK 功能
  • 基于 Protobuf 维护规范, 便于升级和维护
  • 最小化外部包依赖: SDK 仅依赖 Protobuf 包
  • 更多的单元闭环测试

在线文档:

接口规范:

qingcloud-go vs Office SDK API

QingCloud Service chai2010/qingcloud-go yunify/qingcloud-sdk-go
Alarm
Cache
Cluster
DNSAlias
EIP
Hadoop
Image
Instance
Job
KeyPair
LoadBalancer
Misc
Mongo
Monitor
Nic
NotificationCenter
RDB
ResourceACL
Router
S2
SecurityGroup
Snapshot
Span
Spark
Subuser
Tag
UserData
Volumes
Vxnet
Zone

qcli 命令行

Docker 运行(配置**区镜像):

  • docker run --rm -it -v $HOME:/root -w /root chai2010/qingcloud-go qcli

从Go源码安装(Go1.11+):

  • go get github.com/chai2010/qingcloud-go/cmd/qcli

输入 qcliqcli -h 查看命令提示:

chai-mba:api chai$ qcli
NAME:
   qcli - QingCloud Command Line Interface

USAGE:
   qcli [global options] command [command options] [arguments...]

VERSION:
   v0.9.0-33-gbf90580

AUTHOR:
   ChaiShushan <[email protected]>

COMMANDS:
     lake, make  build target with lakefile.lua
     help, h     Shows a list of commands or help for one command

   SDK API Style Command:
     alarm               AlarmService
     cache               CacheService
     cluster             ClusterService
     dnsalias            DNSAliasService
     eip                 EIPService
     hadoop              HadoopService
     image               ImageService
     instance            InstanceService
     job                 JobService
     keypair             KeyPairService
     loadbalancer        LoadBalancerService
     misc                MiscService
     mongo               MongoService
     monitor             MonitorService
     nic                 NicService
     notificationcenter  NotificationCenterService
     rdb                 RDBService
     resourceacl         ResourceACLService
     router              RouterService
     s2                  S2Service
     securitygroup       SecurityGroupService
     snapshot            SnapshotService
     span                SpanService
     spark               SparkService
     subuser             SubuserService
     tag                 TagService
     userdata            UserDataService
     volumes             VolumesService
     vxnet               VxnetService
     zone                ZoneService

GLOBAL OPTIONS:
   --config value, -c value             config file (default: "~/.qingcloud/qcli.json") [$QCLI_CONFIG_FILE]
   --api_server value, -s value         api server (default: "https://api.qingcloud.com/iaas/") [$QCLI_API_SERVER]
   --access_key_id value, -i value      access key id [$QCLI_ACCESS_KEY_ID]
   --secret_access_key value, -k value  secret access key [$QCLI_SECRET_ACCESS_KEY]
   --zone value, -z value               zone (pek3a,pek3b,gd1,sh1a,ap1,ap2a,...) (default: "pek3a") [$QCLI_ZONE]
   --debug, -d                          set debug mode [$QCLI_DEBUG]
   --help, -h                           show help
   --version, -v                        print the version
chai-mba:qingcloud-go chai$

为了避免在每次运行时输入密钥, 可以给 qcli 创建一个默认配置文件 (~/.qingcloud/qcli.json):

{
	"api_server": "https://api.qingcloud.com/iaas/",
	"access_key_id": "QYACCESSKEYIDEXAMPLE",
	"secret_access_key": "SECRETACCESSKEY",
	"zone": "pek3a"
}

qcli 内置了一个类似 make 到构建系统, 输入以下命令查看 (lakefile.lua):

$ qcli lake -h
$ qcli lake -t
$ qcli lake list.instance

要查看主机数量, 可以输入以下命令:

$ qcli instance DescribeInstances

加入 -d 选项可以开启调试模式执行:

$ qcli -d instance DescribeInstances

注意: 命令行还在开发中, 欢迎参与完善!

快速入门 (Go语言版本)

以下为 ./hello.go 的内容:

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/golang/protobuf/proto"

	pb "github.com/chai2010/qingcloud-go/pkg/api"
)

var (
	flagId   = flag.String("id", "", "AccessKeyId")
	flagKey  = flag.String("key", "", "SecretAccessKey")
	flagZone = flag.String("zone", "pek3a", "zone")
)

func main() {
	flag.Parse()

	// 返回 NIC 服务, pek3a 为 北京3区-A
	qnic := pb.NewNicService(&pb.ServerInfo{
		AccessKeyId:     proto.String(*flagId),
		SecretAccessKey: proto.String(*flagKey),
		Zone:            proto.String(*flagZone),
	})

	// 列出所有网卡
	reply, err := qnic.DescribeNics(nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(reply)
}

运行例子:

go run hello.go -id=QYACCESSKEYIDEXAMPLE -key=SECRETACCESSKEY

其中 -id-key 分别为 AccessKey 的公钥和私钥.

更完整的例子可以参考 ./pkg/cmd/qcli 的实现.

快速入门(GopherLua 版本)

hello-lua.go:

package main

import (
	"github.com/yuin/gopher-lua"

	qc_iaas "github.com/chai2010/qingcloud-go/pkg/gopher-lua/qingcloud.iaas"
)

func main() {
	L := lua.NewState()
	defer L.Close()

	qc_iaas.Preload(L)

	if err := L.DoFile("hello.lua"); err != nil {
		panic(err)
	}
}

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 命令行解释执行:

$ go install github.com/chai2010/qingcloud-go/cmd/qlua
$ qlua hello.lua -h
$ qlua hello.lua -v
$ qlua hello.lua

快速入门 (qcli lake 版本)

lakefile.lua:

task("default", {"version", "doc"}, function()
	print("done")
end)

task("version", nil, function()
	print("version")
end)

task("doc", {"install"}, function()
	print("doc")
end)

task("install", nil, function(task, destdir)
	print("install:", task.name, destdir)
end)

输入以下命令:

$ qcli lake -h
$ qcli lake -t
$ qcli lake
$ qcli make install[dir=/path/to/dir]

输入以下命令生成 Graphviz 格式的依赖图:

$ qcli make -g > lakefile-graph.dot
$ qcli make -g | dot -Tpng > lakefile-graph.png

lakefile-graph.dot:

digraph G {
	default [label = "default"];
	version [label = "version"];
	doc [label = "doc"];
	install [label = "install"];
	list_instance [label = "list.instance"];

	default -> version;
	default -> doc;
	doc -> install;
}

lakefile-graph.png:

:

版权

The Apache License.

qingcloud-go's People

Contributors

chai2010 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.