The Aliyun ECS API Client for Ruby 是调用 阿里云 ECS服务 的 Ruby客户端类库.
Add this line to your application's Gemfile:
gem 'aliyun-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install aliyun-api
首先,需要在代码中引入类库:
require 'aliyun'
options = {
:access_key_id => "xxxxxx",
:access_key_secret => "yyyyyy"
}
Aliyun.config options
ecs = Aliyun::ECS.new
options = {
:access_key_id => "xxxxxx",
:access_key_secret => "yyyyyy"
}
ecs = Aliyun::ECS.new options
如果环境变量里ALIYUN_ACCESS_KEY_ID
和ALIYUN_ACCESS_KEY_SECRET
初始化了密钥,那么可以直接初始化ecs:
ecs = Aliyun::ECS.new
这样, 你就可以根据 阿里云弹性计算服务API参考手册初始化业务参数(除Action参数之外)为一个hash对象,并且将其作为参数传给Action方法(Action参数), action方法需要将阿里云手册中的Action名按ruby方式命名, 如:阿里云手册中的Action名StartInstance
对应到这里的方法名为start_instance
。
(1) 例如查询可用地域列表,其Action参数为DescribeRegions,而没有其他参数,代码如下
ecs.describe_regions {}
# 输出如下:
{"Regions"=>{"Region"=>[{"LocalName"=>"深圳", "RegionId"=>"cn-shenzhen"}, {"LocalName"=>"青岛", "RegionId"=>"cn-qingdao"}, {"LocalName"=>"北京", "RegionId"=>"cn-beijing"}, {"LocalName"=>"香港", "RegionId"=>"cn-hongkong"}, {"LocalName"=>"杭州", "RegionId"=>"cn-hangzhou"}]}, "RequestId"=>"abcdefg"}
(2) 再比如查询可用镜像,代码如下
parameters = {:RegionId => "cn-beijing", :PageNumber => 2, :PageSize => 20}
service.describe_images parameters
# or use parameters in rubyway
parameters = {:region_id => "cn-beijing", :page_number => 2, :page_size => 20}
(3) 任意API都可以传递一个block,该block可以用来调试实际生成的请求参数
ecs.describe_instances region_id: 'cn-hangzhou' do |params|
puts params
end
# 输出
{:Format=>"JSON", :Version=>"2014-05-26", :SignatureMethod=>"HMAC-SHA1", :SignatureVersion=>"1.0", :RegionId=>"cn-hangzhou", :AccessKeyId=>"xxxx", :Action=>"DescribeInstances", :SignatureNonce=>"aaaa-5ade-4391-a032-013a53e692db", :TimeStamp=>"2015-12-28T08:14:46Z", :Signature=>"+Spi/ee="}
参数列表
- region_id,地域,required
- zone_id,子区域,optional
- image_id,镜像,required
- instance_type, required
- security_group_id, required
- instance_name, optional
- description, optional
- 略
例:
ecs.create_instance :region_id=>'cn-hangzhou',:image_id=>'img_identifier',:instance_type=>'t2.small',:security_group_id=>'sg_id'
参数列表
- instance_id,实例id,required
例:
ecs.start_instance :instance_id=>'AYxxx'
参数列表
- instance_id,实例id,required
- force_stop,重启机器时是否强制关机,默认为false, optional
例:
ecs.stop_instance :instance_id=>'AYxxx'
参数列表
- instance_id,实例id,required
- force_stop,重启机器时是否强制关机,默认为false, optional
例:
ecs.reboot_instance :instance_id=>'AYxxx'
参数列表
- instance_id,实例id,required
- instance_name,optional
- description, optional
- password, optional
- host_name, optional
例:
ecs.modify_instance_attribute :instance_id=>'AYxxx',:instance_name=>'new-name'
参数列表
- region_id,地域id,required
- zone_id,子区域,optional
- page_number, optional
- page_size, optional
例:
ecs.describe_instance_status :region_id=>'cn-hangzhou'
参数列表
- instance_id,实例id,required
例:
ecs.describe_instance_attribute :instance_id=>'AYxxx'
参数列表
- instance_id,实例id,required
例:
ecs.delete_instance :instance_id=>'AYxxx'
参数列表
- instance_id,实例id,required
- security_group_id, required
例:
ecs.join_security_group :instance_id=>'AYxxx',:security_group_id=>'sg_id'
参数列表
- instance_id,实例id,required
- security_group_id, required
例:
ecs.leave_security_group :instance_id=>'AYxxx',:security_group_id=>'sg_id'
参数列表
- region_id,地域id,required
- zone_id,子区域,optional
- disk_name, optional
- description, optional
- size,磁盘大小GB, optional
- snapshot_id,磁盘快照, optional
- client_token, optional
例:
ecs.create_disk :region_id=>'cn-hangzhou',:size=>100
ecs.create_disk :region_id=>'cn-hangzhou',:snapshot_id=>'snap-id'
参数列表
- region_id,地域id,required
- zone_id,子区域,optional
- disk_ids, optional
- instance_id, optional
- disk_type, all|system|data, optional,默认all
- category, all|cloud|ephemeral,默认all, optional
- status, In_use|Available|Attaching|Detaching|Creating|ReIniting|All, optional
- snapshot_id, optional
- portable, optional
- delete_with_instance, 是否随实例释放, optional
- delete_auto_snapshot, 删除磁盘时是否删除快照, optional
- page_number, optional
- page_size, optional
例:
ecs.describe_disks :disk_ids=>["d1","d2"],:region_id=>'cn-hangzhou'
(其他API类似,不再这里赘述,否则成了阿里的翻译机了^_^)
- Fork it ( https://github.com/qjpcpu/aliyun-api/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request